Skip to content

Commit

Permalink
fix: navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Feb 28, 2024
1 parent d783dd9 commit b5ea964
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
release
fix: navigation
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.k2fsa.sherpa.onnx.tts.engine.utils.longToast
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream

open class ImplYamlConfig<T>(val filePath: String, val factory: () -> T) {
Expand Down Expand Up @@ -52,11 +53,17 @@ open class ImplYamlConfig<T>(val filePath: String, val factory: () -> T) {
file.inputStream().use {
return decode(it)
}

} catch (e: Exception) {
Log.e(ConfigManager.TAG, "readConfig: ", e)
errorHandler(e)

val obj = factory()
// write(config = obj)

if (e is FileNotFoundException)
write(config = obj)
else
errorHandler(e)

return obj
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ object SampleTextConfig :
this[code] = list
})
}

fun remove(code: String) {
updateConfig(config.toMutableMap().apply {
this.remove(code)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.k2fsa.sherpa.onnx.tts.engine.ui

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DeleteForever
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import com.k2fsa.sherpa.onnx.tts.engine.R

@Composable
fun ConfirmDeleteDialog(onDismissRequest: () -> Unit, name: String, onConfirm: () -> Unit) {
AlertDialog(
onDismissRequest = onDismissRequest,
title = { Text(stringResource(id = R.string.is_confirm_delete)) },
text = {
Text(
name,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
},
confirmButton = {
TextButton(onClick = {
onConfirm()
}) {
Text(
stringResource(id = R.string.delete),
color = MaterialTheme.colorScheme.error,
fontWeight = FontWeight.Bold
)
}
},
dismissButton = {
TextButton(onClick = onDismissRequest) {
Text(stringResource(id = android.R.string.cancel))
}
},
icon = {
Icon(
Icons.Default.DeleteForever,
contentDescription = null,
tint = MaterialTheme.colorScheme.error
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.k2fsa.sherpa.onnx.tts.engine.R
import com.k2fsa.sherpa.onnx.tts.engine.ui.models.ModelManagerScreen
import com.k2fsa.sherpa.onnx.tts.engine.ui.settings.SettingsScreen
import com.k2fsa.sherpa.onnx.tts.engine.ui.theme.SherpaOnnxTtsEngineTheme
import com.k2fsa.sherpa.onnx.tts.engine.utils.navigateSingleTop
import com.k2fsa.sherpa.onnx.tts.engine.utils.toast

const val TAG = "sherpa-onnx-tts-engine"
Expand Down Expand Up @@ -93,8 +94,7 @@ class MainActivity : ComponentActivity() {
alwaysShowLabel = false,
selected = containsRoute(id),
onClick = {

navController.navigate(id)
navController.navigateSingleTop(id, popUpToMain = true)
},
icon = { Icon(icon, contentDescription = null) },
label = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.k2fsa.sherpa.onnx.tts.engine.ui.sampletext

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.DeleteForever
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand All @@ -26,6 +28,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.k2fsa.sherpa.onnx.tts.engine.R
import com.k2fsa.sherpa.onnx.tts.engine.ui.ConfirmDeleteDialog
import com.k2fsa.sherpa.onnx.tts.engine.ui.LanguageSelectionDialog
import com.k2fsa.sherpa.onnx.tts.engine.utils.newLocaleFromCode

Expand Down Expand Up @@ -75,6 +78,18 @@ fun SampleTextManagerContent(modifier: Modifier, vm: SampleTextMangerViewModel =
)
}

var showDeleteDialog by remember { mutableStateOf<String?>(null) }
if (showDeleteDialog != null) {
val code = showDeleteDialog!!
ConfirmDeleteDialog(
onDismissRequest = { showDeleteDialog = null },
name = code
) {
vm.removeLanguage(code)
showDeleteDialog = null
}
}

LazyColumn(modifier) {
items(vm.languages) {
val locale = remember(it) { newLocaleFromCode(it) }
Expand All @@ -86,21 +101,32 @@ fun SampleTextManagerContent(modifier: Modifier, vm: SampleTextMangerViewModel =
.padding(4.dp),
name = displayName,
code = it,
onClick = {
showEditDialog = it
}
onClick = { showEditDialog = it },
onDelete = { showDeleteDialog = it }
)
}
}
}


@Composable
fun LanguageItem(modifier: Modifier, name: String, code: String, onClick: () -> Unit) {
fun LanguageItem(
modifier: Modifier,
name: String,
code: String,
onClick: () -> Unit,
onDelete: () -> Unit
) {
ElevatedCard(modifier = modifier, onClick = onClick) {
Column(Modifier.padding(4.dp)) {
Text(text = name, style = MaterialTheme.typography.titleMedium)
Text(text = code, style = MaterialTheme.typography.bodyMedium)
Row(Modifier.padding(4.dp)) {
Column(Modifier.weight(1f)) {
Text(text = name, style = MaterialTheme.typography.titleMedium)
Text(text = code, style = MaterialTheme.typography.bodyMedium)
}

IconButton(onClick = onDelete) {
Icon(Icons.Default.DeleteForever, stringResource(id = R.string.delete))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ class SampleTextMangerViewModel : ViewModel() {
fun addLanguage(code: String) {
SampleTextConfig[code] = emptyList()
}

fun removeLanguage(code: String) {
SampleTextConfig.remove(code)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.k2fsa.sherpa.onnx.tts.engine.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Notification
import android.app.Service
Expand All @@ -14,6 +15,7 @@ import android.graphics.Rect
import android.graphics.Typeface
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.text.Spanned
import android.text.style.ForegroundColorSpan
Expand Down Expand Up @@ -52,27 +54,79 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.NavController
import androidx.navigation.NavDeepLinkRequest
import androidx.navigation.NavDestination
import androidx.navigation.NavHostController
import androidx.navigation.NavOptions
import androidx.navigation.Navigator
import java.util.Locale

fun newLocaleFromCode(code: String): Locale {
val parts = code.split("-")

return when (parts.size) {
1 -> Locale(parts[0])
2 -> Locale(parts[0], parts[1])
else -> Locale(code)
@SuppressLint("RestrictedApi")
fun NavController.navigate(
route: String,
argsBuilder: Bundle.() -> Unit = {},
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
) {
navigate(route, Bundle().apply(argsBuilder), navOptions, navigatorExtras)
}

/*
* 可传递 Bundle 到 Navigation
* */
@SuppressLint("RestrictedApi")
fun NavController.navigate(
route: String,
args: Bundle,
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
) {
val routeLink = NavDeepLinkRequest
.Builder
.fromUri(NavDestination.createRoute(route).toUri())
.build()

val deepLinkMatch = graph.matchDeepLink(routeLink)
if (deepLinkMatch != null) {
val destination = deepLinkMatch.destination
val id = destination.id
navigate(id, args, navOptions, navigatorExtras)
} else {
navigate(route, navOptions, navigatorExtras)
}
}

fun Locale.toCode(): String {
return try {
"$language-$country"
} catch (e: Exception) {
language
}.trimEnd('-')
/**
* 单例并清空其他栈
*/
fun NavHostController.navigateSingleTop(
route: String,
args: Bundle? = null,
popUpToMain: Boolean = false
) {
val navController = this
val navOptions = NavOptions.Builder()
.setLaunchSingleTop(true)
.apply {
if (popUpToMain) setPopUpTo(
navController.graph.startDestinationId,
inclusive = false,
saveState = true
)
}
.setRestoreState(true)
.build()
if (args == null)
navController.navigate(route, navOptions)
else
navController.navigate(route, args, navOptions)
}


fun FloatArray.toByteArray(): ByteArray {
// byteArray is actually a ShortArray
val byteArray = ByteArray(this.size * 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ fun String.toLocale(): Locale {
2 -> Locale(parts[0], parts[1])
else -> Locale(this)
}
}
}

fun newLocaleFromCode(code: String): Locale = code.toLocale()

fun Locale.toCode(): String {
return try {
"$language-$country"
} catch (e: Exception) {
language
}.trimEnd('-')
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
<string name="minute_format">%1$s min</string>
<string name="timeout_destruction_summary">The model is automatically destroyed after it has not been used for a specified period of time to save running memory.</string>
<string name="press_back_again_to_exit">Press back again to exit</string>
<string name="is_confirm_delete">Do you want to delete it?</string>
</resources>

0 comments on commit b5ea964

Please sign in to comment.