Skip to content

Commit

Permalink
Properly restore drafts for new notes when restarting app after proce…
Browse files Browse the repository at this point in the history
…ss death
  • Loading branch information
farmerbb committed Sep 22, 2024
1 parent 675f60f commit d1daffc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class NotepadActivity: ComponentActivity(), FSAFActivityCallbacks {

vm.migrateData {
setContent {
NotepadComposeAppRoute()
NotepadComposeAppRoute(
restoredFromState = savedInstanceState != null,
)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/farmerbb/notepad/model/NavState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ val navStateSaver = Saver<MutableState<NavState>, Pair<String, Long?>>(
else -> "" to null
}
},
restore = {
restore = { (key, id) ->
mutableStateOf(
when(it.first) {
VIEW -> NavState.View(it.second ?: 0)
EDIT -> NavState.Edit(it.second)
when(key) {
VIEW -> NavState.View(id ?: 0)
EDIT -> NavState.Edit(id)
else -> NavState.Empty
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ import com.zachklipp.richtext.ui.printing.rememberPrintableController
import org.koin.androidx.compose.koinViewModel

@Composable
fun NotepadComposeAppRoute() {
fun NotepadComposeAppRoute(
restoredFromState: Boolean,
) {
val vm: NotepadViewModel = koinViewModel()
val configuration = LocalConfiguration.current

Expand All @@ -117,6 +119,7 @@ fun NotepadComposeAppRoute() {
NotepadTheme(isLightTheme, backgroundColorRes, rtlLayout) {
NotepadComposeApp(
vm = vm,
restoredFromState = restoredFromState,
isMultiPane = configuration.screenWidthDp >= 600,
initState = when (draftId) {
-1L -> Empty
Expand All @@ -129,6 +132,7 @@ fun NotepadComposeAppRoute() {
@Composable
private fun NotepadComposeApp(
vm: NotepadViewModel = koinViewModel(),
restoredFromState: Boolean,
isMultiPane: Boolean = false,
initState: NavState = Empty
) {
Expand Down Expand Up @@ -158,6 +162,7 @@ private fun NotepadComposeApp(
val showDoubleTapMessage by vm.prefs.showDoubleTapMessage.collectAsState()

var navState by rememberSaveable(saver = navStateSaver) { mutableStateOf(initState) }
var shouldRestoreDraftId by remember { mutableStateOf(restoredFromState) }
var isPrinting by remember { mutableStateOf(false) }
var isSaveButton by rememberSaveable { mutableStateOf(false) }
var onSaveComplete by remember { mutableStateOf({ _: Long -> }) }
Expand Down Expand Up @@ -367,6 +372,18 @@ private fun NotepadComposeApp(
}
}

LaunchedEffect(shouldRestoreDraftId, navState, initState) {
if (shouldRestoreDraftId
&& navState is Edit
&& initState is Edit
&& (navState as Edit).id == null) {
// Overwrite the navState restored from savedInstanceState
// in the case of a saved draft of a new note
navState = initState
shouldRestoreDraftId = false
}
}

BackHandler(
enabled = multiSelectEnabled || navState != Empty || searchNotesEnabled,
onBack = onBack
Expand Down

0 comments on commit d1daffc

Please sign in to comment.