Skip to content

Feat: Add auto-save functionality on screen stop event #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ import androidx.compose.ui.util.fastJoinToString
import androidx.core.content.FileProvider
import androidx.core.net.toUri
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.yangdai.opennote.MainActivity
import com.yangdai.opennote.R
Expand Down Expand Up @@ -163,6 +167,8 @@ fun NoteScreen(
val noteTextDetails by viewModel.textState.collectAsStateWithLifecycle()
val dataAction by viewModel.dataActionStateFlow.collectAsStateWithLifecycle()
val appSettings by viewModel.settingsStateFlow.collectAsStateWithLifecycle()
val lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current


// 确保屏幕旋转等配置变更时,不会重复加载笔记
var lastLoadedNoteId by rememberSaveable { mutableStateOf<Long?>(null) }
Expand All @@ -178,6 +184,18 @@ fun NoteScreen(
}
}

DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_STOP) {
viewModel.onNoteEvent(NoteEvent.SaveOrUpdate)
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}

val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
val pagerState = rememberPagerState(pageCount = { 2 })
Expand Down