Files
Incido/composeApp/src/main/kotlin/org/incido/compose/App.kt
T
Incido Developer a39c52cf89
dev / Build debug APK (push) Successful in 1m7s
release / Build & attach release APK (push) Failing after 28s
feat(trash): v0.3.7.0 — Trash / soft delete + release docs backfill
- Deleting a note now soft-deletes it (trashed flag on NoteEntity,
  additive schema, auto-migrated); trashed notes are hidden from Home,
  Favorites, Untagged, tag views, and ordinary search.
- New Trash drawer entry lists soft-deleted notes; each Trash card's
  overflow menu exposes Restore and Delete permanently.
- NoteFilter gains a Trash case; repository gains moveToTrash /
  restoreFromTrash / deletePermanently; backups round-trip the
  trashed state.
- Version bump (lockstep): versionCode 30->31, versionName
  0.3.6.3->0.3.7.0, SettingsViewModel.APP_VERSION, README intro +
  changelog, ReleaseNotes.kt 5-most-recent window.
- Development Plan §10: fix the merged 0.3.4.0/0.3.3.8 table row and
  backfill the missing 0.3.6.2/0.3.6.3 rows; add the 0.3.7.0 row.
- README changelog: backfill the missing 0.3.6.2 row; promote the
  Unreleased Trash row to 0.3.7.0.
2026-08-20 11:52:43 +12:00

263 lines
12 KiB
Kotlin

package org.incido.compose
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import kotlinx.coroutines.launch
import org.incido.compose.data.security.SecurityManager
import org.incido.compose.data.settings.SettingsRepository
import org.incido.compose.share.SharedIntentBuffer
import org.incido.compose.ui.drawer.IncidoDrawer
import org.incido.compose.ui.editor.NoteEditorScreen
import org.incido.compose.ui.editor.SharedImage
import org.incido.compose.ui.notelist.NoteListScreen
import org.incido.compose.ui.settings.SettingsScreen
import org.incido.compose.ui.settings.auth.PinChangeScreen
import org.incido.compose.ui.settings.auth.PinEntryScreen
import org.incido.compose.ui.settings.auth.PinSetupScreen
import org.incido.compose.ui.theme.IncidoTheme
import org.incido.domain.model.NoteFilter
import org.incido.domain.repository.NoteRepository
/** Top-level navigation destinations. */
private object Routes {
const val LIST = "list"
const val EDITOR = "editor/{noteId}"
const val SETTINGS = "settings"
const val PIN_SETUP = "pin_setup"
/** v0.3.3.0: three-step "Reset PIN" flow. */
const val PIN_CHANGE = "pin_change"
fun editor(noteId: Long?): String = "editor/${noteId ?: -1L}"
}
/** Stable serialisation for [NoteFilter] so `rememberSaveable` works. */
private fun NoteFilter.toSaveableKey(): String = key()
private fun String.toNoteFilter(): NoteFilter = when {
this == "home" -> NoteFilter.Home
this == "favorites" -> NoteFilter.Favorites
this == "untagged" -> NoteFilter.Untagged
this == "trash" -> NoteFilter.Trash
startsWith("tag:") -> NoteFilter.ByTag(this.removePrefix("tag:"))
else -> NoteFilter.Home
}
/**
* v0.3.1.0: Holds the share-into payload (text + pre-loaded image
* bytes) so the [NoteEditorScreen] can read it on first composition.
* Cleared as soon as the editor is created.
*
* v0.3.6.0: also carries [subject] (`Intent.EXTRA_SUBJECT`) so the
* editor can put the page title on the note's first line for URL
* shares — still offline-only, no network fetch.
*/
private class ShareIntoHolder {
var text: String? = null
var images: List<SharedImage> = emptyList()
var subject: String? = null
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun IncidoApp(
repository: NoteRepository? = null,
securityManager: SecurityManager? = null,
settingsRepository: SettingsRepository? = null,
) {
val context = LocalContext.current
val app = context.applicationContext as IncidoApplication
// v0.3.2.0: prefer explicit params, fall back to the application
// container. Both hosts (`MainActivity` + tests) supply them in
// production — the fallback is just a robustness net.
val resolvedSecurity = securityManager ?: app.container.securityManager
val resolvedSettings = settingsRepository ?: app.container.settingsRepository
// v0.3.2.0: collect user settings to drive theme + the auth gate.
val settings by resolvedSettings.userSettingsFlow.collectAsStateWithLifecycle(
initialValue = org.incido.compose.data.settings.UserSettings(),
)
val lockState by resolvedSecurity.lockState.collectAsStateWithLifecycle()
IncidoTheme(
darkTheme = settings.themeMode.resolveDark(isSystemInDarkTheme()),
dynamicColor = settings.dynamicColor,
) {
// v0.3.2.0: hard lock gate. If the app is locked we render
// the PIN entry screen in place of the regular UI.
if (lockState is SecurityManager.LockState.Locked) {
PinEntryScreen()
return@IncidoTheme
}
val navController = rememberNavController()
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope()
var filterMode by rememberSaveable(
stateSaver = androidx.compose.runtime.saveable.Saver(
save = { it.toSaveableKey() },
restore = { it.toNoteFilter() },
),
) { mutableStateOf<NoteFilter>(NoteFilter.Home) }
// v0.3.1.0: consume any inbound share-into payload once at
// composition. The buffer is single-shot — first composition
// wins. If a payload is present, we open a fresh editor with
// the shared content pre-filled.
val shareHolder = remember { ShareIntoHolder() }
LaunchedEffect(Unit) {
val payload = SharedIntentBuffer.consume()
if (payload != null && !payload.isEmpty) {
shareHolder.text = payload.text
shareHolder.images = payload.images
shareHolder.subject = payload.subject
navController.navigate(Routes.editor(null)) {
popUpTo(Routes.LIST)
}
}
}
val currentBackStack by navController.currentBackStackEntryAsState()
val currentRoute = currentBackStack?.destination?.route?.substringBefore("/")
// Disable the drawer on the editor so back arrow / gesture do
// exactly what the user expects there.
val isListRoute = currentRoute == Routes.LIST
LaunchedEffect(currentRoute) {
// If we land on the editor via deep link without a list in
// the back stack, snap the drawer closed.
if (!isListRoute && drawerState.isOpen) drawerState.close()
}
// v0.3.2.0: forward app foreground/background transitions to
// the SecurityManager so it can re-lock when the timeout
// elapses. LifecycleEventObserver wires ON_RESUME / ON_STOP
// automatically.
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_RESUME -> resolvedSecurity.onAppForegrounded()
Lifecycle.Event.ON_STOP -> resolvedSecurity.onAppBackgrounded()
else -> Unit
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
}
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = isListRoute,
drawerContent = {
IncidoDrawer(
currentFilter = filterMode,
onSelectFilter = { picked ->
filterMode = picked
scope.launch { drawerState.close() }
},
onOpenSettings = {
scope.launch { drawerState.close() }
navController.navigate(Routes.SETTINGS)
},
)
},
) {
NavHost(navController = navController, startDestination = Routes.LIST) {
composable(Routes.LIST) {
NoteListScreen(
filterMode = filterMode,
onOpenDrawer = { scope.launch { drawerState.open() } },
onOpenEditor = { id -> navController.navigate(Routes.editor(id)) },
// v0.3.1.0: thread the NoteRepository through so the
// card thumbnail composables can fetch image bytes.
repository = repository,
)
}
composable(
route = Routes.EDITOR,
arguments = listOf(
navArgument("noteId") {
type = NavType.LongType
defaultValue = -1L
},
),
) { entry ->
val raw = entry.arguments?.getLong("noteId") ?: -1L
val id = if (raw <= 0L) null else raw
// Read the share payload ONCE (consume) so back-then-reopen
// doesn't re-route us into the editor with stale data.
val sharedText = shareHolder.text.also { shareHolder.text = null }
val sharedImages = shareHolder.images.also { shareHolder.images = emptyList() }
// v0.3.6.0: also drain the page-title subject from the
// share intent so the editor can format the note
// with title on the first line.
val sharedSubject = shareHolder.subject.also { shareHolder.subject = null }
NoteEditorScreen(
noteId = id,
onClose = { navController.popBackStack() },
initialSharedText = sharedText,
initialSharedImages = sharedImages,
initialSharedSubject = sharedSubject,
repository = repository,
// v0.3.4.0 hotfix: suppress auto-lock around
// the editor's picker / camera / system-viewer
// round-trips.
securityManager = resolvedSecurity,
)
}
composable(Routes.SETTINGS) {
SettingsScreen(
onBack = { navController.popBackStack() },
onRequestPinSetup = { navController.navigate(Routes.PIN_SETUP) },
// v0.3.3.0: "Reset PIN" row navigates here.
onRequestPinChange = { navController.navigate(Routes.PIN_CHANGE) },
// v0.3.4.0 hotfix: suppress auto-lock around
// the backup export/import SAF pickers.
securityManager = resolvedSecurity,
)
}
composable(Routes.PIN_SETUP) {
PinSetupScreen(
onDone = { navController.popBackStack() },
onCancel = { navController.popBackStack() },
)
}
composable(Routes.PIN_CHANGE) {
PinChangeScreen(
onDone = { navController.popBackStack() },
onCancel = { navController.popBackStack() },
)
}
}
}
}
}
// Suppress unused-import lint for the snapshots helper that may be
// re-introduced in future filter-state extensions.
@Suppress("unused")
private fun <T> List<T>.toSnapshotStateList(): SnapshotStateList<T> = toMutableStateList<T>()