@@ -22,7 +22,6 @@ import androidx.compose.material3.TextFieldDefaults
2222import androidx.compose.material3.TopAppBar
2323import androidx.compose.runtime.Composable
2424import androidx.compose.runtime.CompositionLocalProvider
25- import androidx.compose.runtime.DisposableEffect
2625import androidx.compose.runtime.collectAsState
2726import androidx.compose.runtime.getValue
2827import androidx.compose.ui.Alignment
@@ -33,26 +32,23 @@ import androidx.compose.ui.graphics.RectangleShape
3332import androidx.compose.ui.unit.dp
3433import androidx.lifecycle.viewModelScope
3534import androidx.lifecycle.viewmodel.compose.viewModel
36- import com.blankj.utilcode.util.KeyboardUtils
3735import com.ramcosta.composedestinations.annotation.Destination
3836import com.ramcosta.composedestinations.annotation.RootGraph
3937import com.ramcosta.composedestinations.generated.destinations.SubsAppGroupListPageDestination
4038import com.ramcosta.composedestinations.generated.destinations.SubsGlobalGroupListPageDestination
4139import com.ramcosta.composedestinations.generated.destinations.UpsertRuleGroupPageDestination
4240import kotlinx.coroutines.Dispatchers
41+ import kotlinx.coroutines.launch
4342import kotlinx.coroutines.withContext
4443import li.songe.gkd.MainActivity
4544import li.songe.gkd.ui.component.autoFocus
4645import li.songe.gkd.ui.component.waitResult
4746import li.songe.gkd.ui.local.LocalDarkTheme
4847import li.songe.gkd.ui.local.LocalMainViewModel
49- import li.songe.gkd.ui.local.LocalNavController
5048import li.songe.gkd.ui.style.ProfileTransitions
51- import li.songe.gkd.ui.style.clearJson5TransformationCache
5249import li.songe.gkd.ui.style.getJson5Transformation
5350import li.songe.gkd.ui.style.scaffoldPadding
5451import li.songe.gkd.util.launchAsFn
55- import li.songe.gkd.util.launchTry
5652import li.songe.gkd.util.throttle
5753
5854@Suppress(" unused" )
@@ -66,71 +62,56 @@ fun UpsertRuleGroupPage(
6662) {
6763 val mainVm = LocalMainViewModel .current
6864 val context = LocalActivity .current as MainActivity
69- val navController = LocalNavController .current
7065 val vm = viewModel<UpsertRuleGroupVm >()
7166 val text by vm.textFlow.collectAsState()
72- fun checkIfSaveText () = mainVm.viewModelScope.launchTry(Dispatchers .Default ) {
73- if (vm.textChanged) {
67+
68+ val checkIfSaveText = throttle(mainVm.viewModelScope.launchAsFn(Dispatchers .Default ) {
69+ if (vm.hasTextChanged()) {
70+ vm.viewModelScope.launch {
71+ context.hideSoftInput()
72+ }
7473 mainVm.dialogFlow.waitResult(
7574 title = " 放弃编辑" ,
7675 text = " 当前内容未保存,是否放弃编辑?" ,
7776 )
77+ } else {
78+ context.hideSoftInput()
7879 }
79- withContext( Dispatchers . Main ) { mainVm.navController. popBackStack() }
80- }. let { }
80+ mainVm.popBackStack()
81+ })
8182
82- val onClickSave = throttle(vm.viewModelScope.launchAsFn(Dispatchers .Default ) {
83- vm.saveRule()
84- if (KeyboardUtils .isSoftInputVisible(context)) {
85- KeyboardUtils .hideSoftInput(context)
86- }
87- withContext(Dispatchers .Main ) {
88- if (forward) {
89- if (appId == null ) {
90- navController.navigate(SubsGlobalGroupListPageDestination (subsItemId = subsId).route) {
91- popUpTo(UpsertRuleGroupPageDestination .route) {
92- inclusive = true
93- }
94- }
95- } else {
96- navController.navigate(
97- SubsAppGroupListPageDestination (
98- subsItemId = subsId,
99- vm.addAppId ? : appId
100- ).route
101- ) {
102- popUpTo(UpsertRuleGroupPageDestination .route) {
103- inclusive = true
104- }
83+ val onClickSave = throttle(vm.viewModelScope.launchAsFn(Dispatchers .Main ) {
84+ withContext(Dispatchers .Default ) { vm.saveRule() }
85+ context.hideSoftInput()
86+ if (forward) {
87+ if (appId == null ) {
88+ mainVm.navigatePage(SubsGlobalGroupListPageDestination (subsItemId = subsId)) {
89+ popUpTo(UpsertRuleGroupPageDestination .route) {
90+ inclusive = true
10591 }
10692 }
10793 } else {
108- navController.popBackStack()
94+ mainVm.navigatePage(
95+ SubsAppGroupListPageDestination (
96+ subsItemId = subsId,
97+ vm.addAppId ? : appId
98+ )
99+ ) {
100+ popUpTo(UpsertRuleGroupPageDestination .route) {
101+ inclusive = true
102+ }
103+ }
109104 }
105+ } else {
106+ mainVm.popBackStack()
110107 }
111108 })
112- BackHandler (true ) {
113- if (KeyboardUtils .isSoftInputVisible(context)) {
114- KeyboardUtils .hideSoftInput(context)
115- return @BackHandler
116- }
117- checkIfSaveText()
118- }
119- DisposableEffect (null ) {
120- onDispose {
121- clearJson5TransformationCache()
122- }
123- }
109+ BackHandler (true , checkIfSaveText)
124110 Scaffold (modifier = Modifier , topBar = {
125111 TopAppBar (
126112 modifier = Modifier .fillMaxWidth(),
127113 navigationIcon = {
128- IconButton (onClick = throttle {
129- if (KeyboardUtils .isSoftInputVisible(context)) {
130- KeyboardUtils .hideSoftInput(context)
131- }
132- checkIfSaveText()
133- }) {
114+ IconButton (onClick = checkIfSaveText) {
134115 Icon (
135116 imageVector = Icons .AutoMirrored .Filled .ArrowBack ,
136117 contentDescription = null ,
@@ -159,14 +140,21 @@ fun UpsertRuleGroupPage(
159140 .fillMaxSize(),
160141 ) {
161142 CompositionLocalProvider (LocalTextStyle provides MaterialTheme .typography.bodyLarge) {
162- // need compose 1.9.0
143+ val imeShowing by context.imeShowingFlow.collectAsState()
144+ val modifier = Modifier
145+ .autoFocus()
146+ .fillMaxSize()
147+ .run {
148+ if (imeShowing) {
149+ this
150+ } else {
151+ imePadding()
152+ }
153+ }
163154 TextField (
164155 value = text,
165156 onValueChange = { vm.textFlow.value = it },
166- modifier = Modifier
167- .autoFocus()
168- .fillMaxSize()
169- .imePadding(),
157+ modifier = modifier,
170158 shape = RectangleShape ,
171159 colors = textColors,
172160 visualTransformation = getJson5Transformation(LocalDarkTheme .current),
0 commit comments