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 f511ddd commit f1786df
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.jing332.tts_server_android.compose.systts

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Checkbox
import androidx.compose.material3.MaterialTheme
Expand All @@ -12,11 +14,13 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
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.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.FontWeight
Expand All @@ -25,27 +29,35 @@ import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.widgets.AppDialog
import com.github.jing332.tts_server_android.compose.widgets.LoadingContent
import com.github.jing332.tts_server_android.utils.clickableRipple
import com.github.jing332.tts_server_android.utils.toast
import kotlinx.coroutines.launch
import kotlin.system.measureTimeMillis

@Composable
fun ListSortSettingsDialog(
onDismissRequest: () -> Unit,
entries: List<String>,
sorting: Boolean,
onConfirm: (index: Int, descending: Boolean) -> Unit
onConfirm: suspend (index: Int, descending: Boolean) -> Unit
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()

var descending by remember { mutableStateOf(false) }
var current by remember { mutableIntStateOf(0) }
AppDialog(
onDismissRequest = onDismissRequest,
title = { Text(stringResource(id = R.string.list_soft_settings)) },
title = { Text(stringResource(id = R.string.sort)) },
content = {
LoadingContent(isLoading = sorting) {
LazyColumn {
LazyColumn(Modifier.padding(vertical = 8.dp)) {
items(entries.size) { index ->
val selected = index == current
Row(
Modifier
.height(48.dp)
.fillMaxWidth()
.padding(horizontal = 16.dp)
.clip(MaterialTheme.shapes.medium)
.clickableRipple {
current = index
Expand All @@ -56,7 +68,8 @@ fun ListSortSettingsDialog(
entries[index],
maxLines = 1,
fontWeight = if (selected) FontWeight.Bold else FontWeight.Normal,
color = if (selected) MaterialTheme.colorScheme.primary else Color.Unspecified
color = if (selected) MaterialTheme.colorScheme.primary else Color.Unspecified,
modifier = Modifier.padding(horizontal = 8.dp)
)
}
}
Expand All @@ -66,22 +79,31 @@ fun ListSortSettingsDialog(
buttons = {
Row(
Modifier
.align(Alignment.CenterStart)
.height(48.dp)
.padding(end = 16.dp)
.clip(MaterialTheme.shapes.medium)
.clickableRipple(role = Role.Checkbox) { descending = !descending }
.clickableRipple(role = Role.Checkbox) { descending = !descending },
verticalAlignment = Alignment.CenterVertically,
) {
Checkbox(checked = descending, onCheckedChange = { descending = it })
Text(stringResource(id = R.string.descending))
Text(
stringResource(id = R.string.descending),
modifier = Modifier.padding(end = 8.dp)
)
}

Row {
TextButton(onClick = onDismissRequest) {
Text(stringResource(id = R.string.cancel))
}

TextButton(onClick = { onConfirm(current, descending) }) {
Text(stringResource(id = R.string.confirm))
TextButton(onClick = {
scope.launch {
val cost = measureTimeMillis { onConfirm(current, descending) }
context.toast(R.string.sorting_complete_msg, cost)
}
}) {
Text(stringResource(id = R.string.start))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.jing332.tts_server_android.compose.systts.list

import androidx.annotation.StringRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.DriveFileRenameOutline
import androidx.compose.material.icons.filled.Sort
import androidx.compose.material.icons.filled.Speed
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
Expand All @@ -17,9 +19,9 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.state.ToggleableState
import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.systts.GroupItem
import com.github.jing332.tts_server_android.compose.systts.ListSortSettingsDialog
import com.github.jing332.tts_server_android.compose.widgets.TextFieldDialog


@Composable
fun Group(
modifier: Modifier,
Expand All @@ -33,6 +35,7 @@ fun Group(
onRename: (newName: String) -> Unit,
onCopy: (newName: String) -> Unit,
onEditAudioParams: () -> Unit,
onSort: () -> Unit,
) {
var showRenameDialog by remember { mutableStateOf(false) }
if (showRenameDialog) {
Expand Down Expand Up @@ -99,6 +102,16 @@ fun Group(
Icon(Icons.Default.Speed, null)
}
)

DropdownMenuItem(text = { Text(stringResource(id = R.string.sort)) },
onClick = {
dismiss()
onSort()
},
leadingIcon = {
Icon(Icons.Default.Sort, null)
}
)
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import org.burnoutcrew.reorderable.detectReorderAfterLongPress
import org.burnoutcrew.reorderable.rememberReorderableLazyListState
import org.burnoutcrew.reorderable.reorderable


@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
internal fun ListManagerScreen(vm: ListManagerViewModel = viewModel()) {
Expand All @@ -76,6 +77,12 @@ internal fun ListManagerScreen(vm: ListManagerViewModel = viewModel()) {
val context = LocalContext.current
val drawerState = LocalDrawerState.current

var showSortDialog by remember { mutableStateOf<List<SystemTts>?>(null) }
if (showSortDialog != null) SortDialog(
onDismissRequest = { showSortDialog = null },
list = showSortDialog!!
)

var showQuickEdit by remember { mutableStateOf<SystemTts?>(null) }
if (showQuickEdit != null) {
QuickEditBottomSheet(onDismissRequest = {
Expand Down Expand Up @@ -104,7 +111,9 @@ internal fun ListManagerScreen(vm: ListManagerViewModel = viewModel()) {
val model = systts.copy()
if (model.speechRule.target == SpeechTarget.BGM) return

if (model.speechRule.target == SpeechTarget.CUSTOM_TAG) appDb.speechRuleDao.getByRuleId(model.speechRule.tagRuleId)
if (model.speechRule.target == SpeechTarget.CUSTOM_TAG) appDb.speechRuleDao.getByRuleId(
model.speechRule.tagRuleId
)
?.let {
val keys = it.tags.keys.toList()
val idx = keys.indexOf(model.speechRule.tag)
Expand Down Expand Up @@ -400,6 +409,9 @@ internal fun ListManagerScreen(vm: ListManagerViewModel = viewModel()) {
},
onExport = {
showExportSheet = listOf(groupWithSystemTts)
},
onSort = {
showSortDialog = groupWithSystemTts.list
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.jing332.tts_server_android.compose.systts.list

import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.drake.net.utils.withDefault
import com.drake.net.utils.withIO
import com.drake.net.utils.withMain
import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.systts.ListSortSettingsDialog
import com.github.jing332.tts_server_android.data.appDb
import com.github.jing332.tts_server_android.data.entities.systts.SystemTts
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

internal enum class SortFields(@StringRes val strResId: Int) {
NAME(R.string.name),
TAG_NAME(R.string.tag),
ENABLE(R.string.enabled),
ID(R.string.created_time_id)
}

@Composable
internal fun SortDialog(onDismissRequest: () -> Unit, list: List<SystemTts>) {
var sorting by remember { mutableStateOf(false) }
ListSortSettingsDialog(
onDismissRequest = onDismissRequest,
entries = SortFields.values().map { stringResource(id = it.strResId) },
sorting = sorting,
onConfirm = { index, descending ->
sorting = true
withIO {
val sortedList = when (SortFields.values()[index]) {
SortFields.NAME -> list.sortedBy { it.displayName }
SortFields.TAG_NAME -> list.sortedBy { it.speechRule.tagName }
SortFields.ENABLE -> list.sortedBy { it.isEnabled }
SortFields.ID -> list.sortedBy { it.id }
}.run {
if (descending) this.reversed() else this
}
sortedList.forEachIndexed { i, systemTts ->
appDb.systemTtsDao.updateTts(systemTts.copy(order = i))
}
}

sorting = false
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import android.widget.Toast
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment

fun Context.toast(@StringRes message: Int) {
fun Context.toast(@StringRes message: Int, vararg args: Any) {
runOnUI {
kotlin.runCatching {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
Toast.makeText(this, getString(message, *args), Toast.LENGTH_SHORT).show()
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@
<string name="limit_tag_length_summary">The excess tag is ignored</string>
<string name="limit_name_length">Limit the display name length</string>
<string name="limit_name_length_summary">The excess of the name is ignored</string>
<string name="list_soft_settings">排序设置</string>
<string name="descending">降序</string>
<string name="descending">Descending</string>
<string name="sort">Sort</string>
<string name="created_time_id">Created time (ID)</string>
<string name="sorting_complete_msg">Sorting complete, taking %1$sms</string>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@
<string name="limit_tag_length_summary">超出的部分忽略</string>
<string name="limit_name_length">限制显示名称长度</string>
<string name="limit_name_length_summary">名称超出的部分忽略</string>
<string name="list_soft_settings">排序设置</string>
<string name="descending">降序</string>
<string name="sort">排序</string>
<string name="created_time_id">创建时间(ID)</string>
<string name="sorting_complete_msg">排序完成,耗时%1$sms</string>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@
<string name="limit_tag_length_summary">标签超出的部分忽略</string>
<string name="limit_name_length">限制显示名称长度</string>
<string name="limit_name_length_summary">名称超出的部分忽略</string>
<string name="list_soft_settings">排序设置</string>
<string name="descending">降序</string>
<string name="sort">排序</string>
<string name="created_time_id">创建时间(ID)</string>
<string name="sorting_complete_msg">排序完成,耗时%1$sms</string>
</resources>

0 comments on commit f1786df

Please sign in to comment.