manager: refresh template screen if needed.

This commit is contained in:
weishu
2023-10-21 17:13:23 +08:00
parent 571f89fac3
commit d4e19bb8fc
2 changed files with 23 additions and 17 deletions

View File

@@ -1,16 +1,12 @@
package me.weishu.kernelsu.ui.screen package me.weishu.kernelsu.ui.screen
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.ExperimentalMaterialApi
@@ -21,7 +17,6 @@ import androidx.compose.material.icons.filled.Sync
import androidx.compose.material.pullrefresh.PullRefreshIndicator import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton import androidx.compose.material3.IconButton
@@ -35,20 +30,16 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.ramcosta.composedestinations.annotation.Destination import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.ResultRecipient
import com.ramcosta.composedestinations.result.getOr
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.weishu.kernelsu.R import me.weishu.kernelsu.R
import me.weishu.kernelsu.ui.screen.destinations.TemplateEditorScreenDestination import me.weishu.kernelsu.ui.screen.destinations.TemplateEditorScreenDestination
import me.weishu.kernelsu.ui.viewmodel.TemplateViewModel import me.weishu.kernelsu.ui.viewmodel.TemplateViewModel
import java.util.Locale
/** /**
* @author weishu * @author weishu
@@ -58,7 +49,10 @@ import java.util.Locale
@OptIn(ExperimentalMaterialApi::class) @OptIn(ExperimentalMaterialApi::class)
@Destination @Destination
@Composable @Composable
fun AppProfileTemplateScreen(navigator: DestinationsNavigator) { fun AppProfileTemplateScreen(
navigator: DestinationsNavigator,
resultRecipient: ResultRecipient<TemplateEditorScreenDestination, Boolean>
) {
val viewModel = viewModel<TemplateViewModel>() val viewModel = viewModel<TemplateViewModel>()
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
@@ -68,6 +62,13 @@ fun AppProfileTemplateScreen(navigator: DestinationsNavigator) {
} }
} }
// handle result from TemplateEditorScreen, refresh if needed
resultRecipient.onNavResult { result ->
if (result.getOr { false }) {
scope.launch { viewModel.fetchTemplates() }
}
}
Scaffold( Scaffold(
topBar = { topBar = {
TopBar(onBack = { navigator.popBackStack() }, TopBar(onBack = { navigator.popBackStack() },

View File

@@ -1,5 +1,6 @@
package me.weishu.kernelsu.ui.screen package me.weishu.kernelsu.ui.screen
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@@ -34,7 +35,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import com.ramcosta.composedestinations.annotation.Destination import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator import com.ramcosta.composedestinations.result.ResultBackNavigator
import me.weishu.kernelsu.Natives import me.weishu.kernelsu.Natives
import me.weishu.kernelsu.R import me.weishu.kernelsu.R
import me.weishu.kernelsu.profile.Capabilities import me.weishu.kernelsu.profile.Capabilities
@@ -55,7 +56,7 @@ import org.json.JSONObject
@Destination @Destination
@Composable @Composable
fun TemplateEditorScreen( fun TemplateEditorScreen(
navigator: DestinationsNavigator, navigator: ResultBackNavigator<Boolean>,
initialTemplate: TemplateViewModel.TemplateInfo, initialTemplate: TemplateViewModel.TemplateInfo,
readOnly: Boolean = true, readOnly: Boolean = true,
) { ) {
@@ -67,6 +68,10 @@ fun TemplateEditorScreen(
mutableStateOf(initialTemplate) mutableStateOf(initialTemplate)
} }
BackHandler {
navigator.navigateBack(result = !readOnly)
}
Scaffold( Scaffold(
topBar = { topBar = {
val author = val author =
@@ -88,15 +93,15 @@ fun TemplateEditorScreen(
}, },
readOnly = readOnly, readOnly = readOnly,
summary = titleSummary, summary = titleSummary,
onBack = { navigator.popBackStack() }, onBack = { navigator.navigateBack(result = !readOnly) },
onDelete = { onDelete = {
if (deleteAppProfileTemplate(template.id)) { if (deleteAppProfileTemplate(template.id)) {
navigator.popBackStack() navigator.navigateBack(result = true)
} }
}, },
onSave = { onSave = {
if (saveTemplate(template, isCreation)) { if (saveTemplate(template, isCreation)) {
navigator.popBackStack() navigator.navigateBack(result = true)
} }
}) })
}, },