Skip to content

Commit

Permalink
Use base64 for search query encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
z-huang committed Aug 5, 2024
1 parent ab4ef13 commit 5eaba5c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/zionhuang/music/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.Base64
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -106,7 +107,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.net.URLEncoder
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -240,7 +240,7 @@ class MainActivity : ComponentActivity() {
val onSearch: (String) -> Unit = {
if (it.isNotEmpty()) {
onActiveChange(false)
navController.navigate("search/${URLEncoder.encode(it, "UTF-8")}")
navController.navigate("search/${Base64.encodeToString(it.toByteArray(), Base64.DEFAULT)}")
if (dataStore[PauseSearchHistoryKey] != true) {
database.query {
insert(SearchHistory(query = it))
Expand Down Expand Up @@ -292,7 +292,7 @@ class MainActivity : ComponentActivity() {
LaunchedEffect(navBackStackEntry) {
if (navBackStackEntry?.destination?.route?.startsWith("search/") == true) {
val searchQuery = withContext(Dispatchers.IO) {
navBackStackEntry?.arguments?.getString("query")!!
String(Base64.decode(navBackStackEntry?.arguments?.getString("query")!!, Base64.DEFAULT))
}
onQueryChange(TextFieldValue(searchQuery, TextRange(searchQuery.length)))
} else if (navigationItems.fastAny { it.route == navBackStackEntry?.destination?.route }) {
Expand Down Expand Up @@ -701,7 +701,7 @@ class MainActivity : ComponentActivity() {
onQueryChange = onQueryChange,
navController = navController,
onSearch = {
navController.navigate("search/${URLEncoder.encode(it, "UTF-8")}")
navController.navigate("search/${Base64.encodeToString(it.toByteArray(), Base64.DEFAULT)}")
if (dataStore[PauseSearchHistoryKey] != true) {
database.query {
insert(SearchHistory(query = it))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zionhuang.music.viewmodels

import android.util.Base64
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
Expand All @@ -20,7 +21,7 @@ import javax.inject.Inject
class OnlineSearchViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
) : ViewModel() {
val query = savedStateHandle.get<String>("query")!!
val query = String(Base64.decode(savedStateHandle.get<String>("query")!!, Base64.DEFAULT))
val filter = MutableStateFlow<YouTube.SearchFilter?>(null)
var summaryPage by mutableStateOf<SearchSummaryPage?>(null)
val viewStateMap = mutableStateMapOf<String, ItemsPage?>()
Expand Down

0 comments on commit 5eaba5c

Please sign in to comment.