Skip to content

Commit

Permalink
feat: 替换管理搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Aug 29, 2023
1 parent a0df849 commit 8d49273
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.NavigationDrawerItemDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
Expand All @@ -57,6 +58,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
Expand Down Expand Up @@ -237,18 +239,34 @@ fun NavDrawerContent(
}

@Composable
fun drawerItem(
fun DrawerItem(
selected: Boolean = false,
icon: @Composable () -> Unit,
label: @Composable () -> Unit,
onClick: () -> Unit
) {
NavigationDrawerItem(
modifier = Modifier.padding(vertical = 2.dp),
icon = icon,
label = label,
selected = selected,
onClick = onClick,
colors = NavigationDrawerItemDefaults.colors(unselectedContainerColor = Color.Transparent)
)
}


@Composable
fun NavDrawerItem(
icon: @Composable () -> Unit,
targetScreen: NavRoutes,
onClick: () -> Unit = {
scope.launch {
drawerState.close()
}
scope.launch { drawerState.close() }
navController.navigateSingleTop(targetScreen.id, popUpToMain = true)
}
) {
val isSelected = navController.currentDestination?.route == targetScreen.id
NavigationDrawerItem(
DrawerItem(
icon = icon,
label = { Text(text = stringResource(id = targetScreen.strId)) },
selected = isSelected,
Expand Down Expand Up @@ -313,8 +331,7 @@ fun NavDrawerContent(
)

for (route in NavRoutes.routes) {
drawerItem(icon = route.icon, targetScreen = route)
Spacer(modifier = Modifier.height(4.dp))
NavDrawerItem(icon = route.icon, targetScreen = route)
}

HorizontalDivider(
Expand All @@ -323,7 +340,14 @@ fun NavDrawerContent(
.padding(vertical = 16.dp, horizontal = 4.dp)
)

NavigationDrawerItem(
DrawerItem(
icon = { Icon(Icons.Default.BatteryFull, null) },
label = { Text(stringResource(id = R.string.battery_optimization_whitelist)) },
selected = false,
onClick = { context.killBattery() }
)

DrawerItem(
icon = { Icon(Icons.Default.ArrowCircleUp, null) },
label = { Text(stringResource(id = R.string.check_update)) },
selected = false,
Expand All @@ -332,35 +356,25 @@ fun NavDrawerContent(
drawerState.close()
updateCheckTrigger = true
}
}
},
)

Spacer(modifier = Modifier.height(4.dp))
NavigationDrawerItem(
DrawerItem(
icon = { Icon(Icons.Default.HelpOutline, null) },
label = { Text(stringResource(id = R.string.app_help_document)) },
selected = false,
onClick = {
context.startActivity(Intent(context, AppHelpDocumentActivity::class.java).apply {
action = Intent.ACTION_VIEW
})
}
},
)

Spacer(modifier = Modifier.height(4.dp))
NavigationDrawerItem(
icon = { Icon(Icons.Default.BatteryFull, null) },
label = { Text(stringResource(id = R.string.battery_optimization_whitelist)) },
selected = false,
onClick = { context.killBattery() }
)

Spacer(modifier = Modifier.height(4.dp))
var showAboutDialog by remember { mutableStateOf(false) }
if (showAboutDialog)
AboutDialog { showAboutDialog = false }

NavigationDrawerItem(
DrawerItem(
icon = { Icon(Icons.Default.Info, null) },
label = { Text(stringResource(id = R.string.about)) },
selected = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.jing332.tts_server_android.data.appDb
import com.github.jing332.tts_server_android.data.entities.replace.GroupWithReplaceRule
import com.github.jing332.tts_server_android.data.entities.replace.ReplaceRule
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.launch

internal class ManagerViewModel : ViewModel() {
var listItemIndex = 0
var listItemOffset = 0

private val _list = MutableStateFlow<List<GroupWithReplaceRule>>(emptyList())
val list: MutableStateFlow<List<GroupWithReplaceRule>>
get() = _list
Expand All @@ -26,4 +24,43 @@ internal class ManagerViewModel : ViewModel() {
}
}
}

fun updateSearchResult(text: String, type: SearchType) {
if (list.value.isEmpty() || text.isBlank()) {
_list.value = appDb.replaceRuleDao.allGroupWithReplaceRules()
return
}

val resultList = mutableListOf<GroupWithReplaceRule>()
list.value.forEach {
val subList = mutableListOf<ReplaceRule>()
val groupWithRules = GroupWithReplaceRule(it.group, subList)
resultList.add(groupWithRules)

it.list.forEach { rule ->
when (type) {
SearchType.GROUP_NAME -> {
if (it.group.name.contains(text)) subList.add(rule)
}

SearchType.NAME -> {
if (rule.name.contains(text)) subList.add(rule)
}

SearchType.PATTERN -> {
if (rule.pattern.contains(text)) subList.add(rule)
}

SearchType.REPLACEMENT -> {
if (rule.replacement.contains(text)) subList.add(rule)
}
}
}

if (subList.isEmpty())
resultList.remove(groupWithRules)
}

_list.value = resultList
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -95,12 +97,26 @@ internal fun ManagerScreen(vm: ManagerViewModel = viewModel(), finish: () -> Uni
)
}



val models by vm.list.collectAsStateWithLifecycle()
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
TopAppBar(
title = { Text(stringResource(id = R.string.replace_rule_manager)) },
title = {
var type by rememberSaveable { mutableStateOf(SearchType.NAME) }
var text by rememberSaveable { mutableStateOf("") }
LaunchedEffect(text, type) {
vm.updateSearchResult(text, type)
}
SearchTextField(
value = text,
onValueChange = { text = it },
searchType = type,
onSearchTypeChange = { type = it }
)
},
navigationIcon = {
IconButton(onClick = finish) {
Icon(Icons.Default.ArrowBack, stringResource(id = R.string.nav_back))
Expand Down Expand Up @@ -251,7 +267,7 @@ internal fun ManagerScreen(vm: ManagerViewModel = viewModel(), finish: () -> Uni
},
onClick = { },
onEdit = { navigateToEdit(rule) },
onDelete = { appDb.replaceRuleDao.delete(rule) },
onDelete = { appDb.replaceRuleDao.delete(rule) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,124 @@
package com.github.jing332.tts_server_android.compose.systts.replace

import android.os.Parcelable
import androidx.annotation.StringRes
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountTree
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.theme.AppTheme
import kotlinx.parcelize.Parcelize

@Parcelize
enum class SearchType(@StringRes val strId: Int) : Parcelable {
GROUP_NAME(R.string.group_name),
NAME(R.string.display_name),
PATTERN(R.string.replace_rule),
REPLACEMENT(R.string.systts_replace_as),
}

@Composable
fun SearchTextField() {
fun SearchTextField(
value: String,
onValueChange: (String) -> Unit,
searchType: SearchType,
onSearchTypeChange: (SearchType) -> Unit
) {
TextField(
value = value,
onValueChange = onValueChange,
colors = TextFieldDefaults.colors(
focusedContainerColor = Color.Transparent,
unfocusedContainerColor = Color.Transparent,
),
singleLine = true,
leadingIcon = {
var showTypeOptions by remember { mutableStateOf(false) }
IconButton(onClick = { showTypeOptions = true }) {
Icon(
Icons.Default.AccountTree, stringResource(id = R.string.type)
)

DropdownMenu(
expanded = showTypeOptions,
onDismissRequest = { showTypeOptions = false }) {

@Composable
fun RadioMenuItem(
isSelected: Boolean,
title: @Composable () -> Unit,
onClick: () -> Unit
) {
DropdownMenuItem(
modifier = Modifier.semantics {
role = Role.RadioButton
},
text = title,
onClick = {
showTypeOptions = false
onClick()
},
leadingIcon = {
RadioButton(
modifier = Modifier.focusable(false),
selected = isSelected,
onClick = null
)
}
)
}

SearchType.values().forEach {
RadioMenuItem(
isSelected = it == searchType,
title = { Text(stringResource(id = it.strId)) },
onClick = { onSearchTypeChange(it) }
)
}

}
}
}
)
}

@Preview
@Composable
fun PreviewRuleSearchTextField() {
AppTheme {
Column {
Spacer(modifier = Modifier.height(64.dp))
var value by remember { mutableStateOf("") }
var searchType by remember { mutableStateOf(SearchType.NAME) }
SearchTextField(
value = value,
onValueChange = { value = it },
searchType = searchType,
onSearchTypeChange = { searchType = it })
}
}
}

0 comments on commit 8d49273

Please sign in to comment.