Skip to content
Merged
Show file tree
Hide file tree
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
66 changes: 60 additions & 6 deletions composeApp/src/commonMain/kotlin/io/github/smiling_pixel/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -37,6 +38,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
Expand All @@ -54,14 +56,14 @@ import io.github.smiling_pixel.filesystem.FileRepository
import io.github.smiling_pixel.filesystem.InMemoryFileManager
import io.github.smiling_pixel.model.DiaryEntry
import io.github.smiling_pixel.preference.getSettingsRepository
import io.github.smiling_pixel.screens.DiarySyncDialogs
import io.github.smiling_pixel.screens.EntriesScreen
import io.github.smiling_pixel.screens.InsightsScreen
import io.github.smiling_pixel.screens.MomentsScreen
import io.github.smiling_pixel.screens.ProfileScreen
import io.github.smiling_pixel.screens.SearchScreen
import io.github.smiling_pixel.screens.SettingsScreen
import io.github.smiling_pixel.screens.rememberDiarySyncState
import io.github.smiling_pixel.screens.OperationEvent
import io.github.smiling_pixel.sync.startAutoSync
import io.github.smiling_pixel.theme.MarkDayTheme
import io.github.smiling_pixel.theme.ThemeMode
Expand Down Expand Up @@ -125,6 +127,12 @@ fun App(
val isLogPersistenceEnabled by settingsRepository.isLogPersistenceEnabled.collectAsState(initial = null)

if (themeMode == null || isPureBlackEnabled == null || logLevel == null || isLogPersistenceEnabled == null) {
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
androidx.compose.foundation.layout.Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("MarkDay", style = androidx.compose.material3.MaterialTheme.typography.headlineMedium)
CircularProgressIndicator(modifier = Modifier.padding(top = 16.dp))
}
}
return
}

Expand All @@ -150,7 +158,9 @@ fun App(
}
val weatherClient = remember { GoogleWeatherClient(settingsRepository) }
val scope = rememberCoroutineScope()
val diarySyncState = rememberDiarySyncState(repo)
var pendingOperation by remember { mutableStateOf<OperationEvent?>(null) }
var detailsOperation by remember { mutableStateOf<OperationEvent?>(null) }
val diarySyncState = rememberDiarySyncState(repo) { pendingOperation = it }
val snackbarHostState = remember { SnackbarHostState() }
val navController = rememberNavController()
var selected by remember { mutableStateOf<AppRoute>(EntriesRoute) }
Expand All @@ -173,7 +183,30 @@ fun App(
val autoSyncJob = startAutoSync(repo)
onDispose { autoSyncJob?.cancel() }
}
DiarySyncDialogs(diarySyncState)
detailsOperation?.let { event ->
AlertDialog(
onDismissRequest = { detailsOperation = null },
title = { Text("Details") },
text = { Text(event.technicalDetails ?: "No additional details are available.") },
confirmButton = {
if (event.retry != null) {
TextButton(
onClick = {
detailsOperation = null
event.retry.invoke()
},
) { Text("Retry") }
} else {
TextButton(onClick = { detailsOperation = null }) { Text("Close") }
}
},
dismissButton = {
if (event.retry != null) {
TextButton(onClick = { detailsOperation = null }) { Text("Close") }
}
},
)
}

PlatformDraftExitProtection(
guard = editorExitGuard,
Expand Down Expand Up @@ -310,7 +343,26 @@ fun App(
Modifier
.safeContentPadding()
.fillMaxSize(),
snackbarHost = { SnackbarHost(snackbarHostState) },
snackbarHost = {
SnackbarHost(snackbarHostState)
pendingOperation?.let { event ->
LaunchedEffect(event) {
val result = snackbarHostState.showSnackbar(
message = event.message,
actionLabel = if (event.technicalDetails != null) "Details" else event.retry?.let { "Retry" },
duration = SnackbarDuration.Long,
)
when (result) {
SnackbarResult.ActionPerformed -> {
if (event.technicalDetails != null) detailsOperation = event
else event.retry?.invoke()
}
SnackbarResult.Dismissed -> Unit
}
pendingOperation = null
}
}
},
topBar = {
if (isSelectionMode) {
CenterAlignedTopAppBar(
Expand Down Expand Up @@ -454,6 +506,8 @@ fun App(
onSelectionChange = { selectedIds = it },
isSyncing = diarySyncState.isSyncing,
onSyncRequest = diarySyncState::requestSync,
syncAvailability = diarySyncState.availability,
onOpenSettings = { selected = SettingsRoute; navController.navigate(SettingsRoute) },
onListVisibilityChange = { isEntriesListVisible = it },
onExitGuardChange = { editorExitGuard = it },
)
Expand All @@ -471,13 +525,13 @@ fun App(
)
}
composable<MomentsRoute> {
MomentsScreen(fileRepo = fileRepo)
MomentsScreen(fileRepo = fileRepo) { pendingOperation = it }
}
composable<InsightsRoute> {
InsightsScreen()
}
composable<SettingsRoute> {
SettingsScreen(repo = repo)
SettingsScreen(repo = repo) { pendingOperation = it }
}
composable<ProfileRoute> { backStackEntry ->
ProfileScreen(onBack = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ data class UserInfo(
* Client interface for accessing and managing files on cloud drives.
*/
interface CloudDriveClient {
/** Whether this platform can use the cloud-drive provider. */
val isSupported: Boolean
get() = true

companion object {
const val MIME_TYPE_FOLDER = "application/vnd.google-apps.folder"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.smiling_pixel.database

import io.github.smiling_pixel.model.DiaryEntry
import io.github.smiling_pixel.model.LoadState
import io.github.smiling_pixel.preference.SettingsRepository
import io.github.smiling_pixel.preference.getSettingsRepository
import io.github.smiling_pixel.sync.clearLocalDeletionTombstone
Expand All @@ -24,17 +25,40 @@ class DiaryRepository(
private val _entries = MutableStateFlow<List<DiaryEntry>>(emptyList())
val entries: StateFlow<List<DiaryEntry>> = _entries

private val _entriesState = MutableStateFlow<LoadState<List<DiaryEntry>>>(LoadState.Loading)

/** Emits loading, content, or error state for the authoritative entry collection. */
val entriesState: StateFlow<LoadState<List<DiaryEntry>>> = _entriesState

init {
// Collect the DAO's flow and update our StateFlow so Compose can collect it as state
scope.launch {
dao.entriesFlow.collect { list ->
_entries.value = list
try {
dao.entriesFlow.collect { list ->
_entries.value = list
_entriesState.value = LoadState.Content(list)
}
} catch (e: Exception) {
_entriesState.value =
LoadState.Error(
message = "Your entries could not be loaded.",
technicalDetails = e.message,
)
}
}
// initial load in case DAO isn't Flow-backed
scope.launch {
val list = dao.getAll()
if (list.isNotEmpty()) _entries.value = list
try {
val list = dao.getAll()
_entries.value = list
_entriesState.value = LoadState.Content(list)
} catch (e: Exception) {
_entriesState.value =
LoadState.Error(
message = "Your entries could not be loaded.",
technicalDetails = e.message,
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,44 @@ package io.github.smiling_pixel.filesystem

import io.github.smiling_pixel.database.IFileMetadataDao
import io.github.smiling_pixel.model.FileMetadata
import io.github.smiling_pixel.model.LoadState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlin.time.Clock

class FileRepository(
private val fileManager: FileManager,
private val metadataDao: IFileMetadataDao,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
) {
val files: Flow<List<FileMetadata>> = metadataDao.getAllFiles()

private val _filesState = MutableStateFlow<LoadState<List<FileMetadata>>>(LoadState.Loading)

/** Emits loading, content, or error state for file metadata. */
val filesState: StateFlow<LoadState<List<FileMetadata>>> = _filesState

init {
scope.launch {
try {
metadataDao.getAllFiles().collect { value ->
_filesState.value = LoadState.Content(value)
}
} catch (e: Exception) {
_filesState.value =
LoadState.Error(
message = "Moments could not be loaded.",
technicalDetails = e.message,
)
}
}
}

suspend fun saveFile(
fileName: String,
content: ByteArray,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.smiling_pixel.model

/**
* Represents the lifecycle of content loaded from a local repository.
*
* @param T Loaded value type.
*/
sealed interface LoadState<out T> {
/** Content is being loaded for the first time. */
data object Loading : LoadState<Nothing>

/** Content was loaded successfully. */
data class Content<T>(val value: T) : LoadState<T>

/** Content could not be loaded. */
data class Error(
val message: String,
val technicalDetails: String? = null,
) : LoadState<Nothing>
}
Loading
Loading