Skip to content

Commit

Permalink
[ux] Save folder choice only if it loads successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Dec 2, 2024
1 parent 26fc90c commit a770873
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
21 changes: 14 additions & 7 deletions app/src/main/java/dev/arkbuilders/arkmemo/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import dev.arkbuilders.arkmemo.repo.NotesRepoHelper
import dev.arkbuilders.arkmemo.repo.graphics.GraphicNotesRepo
import dev.arkbuilders.arkmemo.repo.text.TextNotesRepo
import dev.arkbuilders.arkmemo.repo.voices.VoiceNotesRepo
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Named
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
Expand All @@ -27,12 +30,16 @@ abstract class RepositoryModule {

@Binds
abstract fun bindVoiceNotesRepo(impl: VoiceNotesRepo): NotesRepo<VoiceNote>
}

companion object {
@Provides
fun provideNotesRepoHelper(
memoPreferences: MemoPreferences,
propertiesStorageRepo: PropertiesStorageRepo,
) = NotesRepoHelper(memoPreferences, propertiesStorageRepo)
}
@InstallIn(SingletonComponent::class)
@Module
object RepoHelperModule {
@Singleton
@Provides
fun provideNotesRepoHelper(
memoPreferences: MemoPreferences,
propertiesStorageRepo: PropertiesStorageRepo,
@Named(IO_DISPATCHER) coroutineDispatcher: CoroutineDispatcher,
) = NotesRepoHelper(memoPreferences, propertiesStorageRepo, coroutineDispatcher)
}
19 changes: 16 additions & 3 deletions app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import dev.arkbuilders.arklib.data.index.RootIndex
import dev.arkbuilders.arklib.user.properties.Properties
import dev.arkbuilders.arklib.user.properties.PropertiesStorage
import dev.arkbuilders.arklib.user.properties.PropertiesStorageRepo
import dev.arkbuilders.arkmemo.di.IO_DISPATCHER
import dev.arkbuilders.arkmemo.models.Note
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.utils.isEqual
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import java.nio.file.Path
import javax.inject.Inject
import javax.inject.Named
import kotlin.NullPointerException
import kotlin.io.path.deleteIfExists
import kotlin.io.path.extension
Expand All @@ -26,13 +31,21 @@ class NotesRepoHelper
constructor(
private val memoPreferences: MemoPreferences,
private val propertiesStorageRepo: PropertiesStorageRepo,
@Named(IO_DISPATCHER) private val iODispatcher: CoroutineDispatcher,
) {
private lateinit var root: Path
private val root by lazy {
memoPreferences.getNotesStorage()
}

private lateinit var propertiesStorage: PropertiesStorage
private val lazyPropertiesStorage by lazy {
CoroutineScope(iODispatcher).async {
propertiesStorageRepo.provide(RootIndex.provide(root))
}
}

suspend fun init() {
root = memoPreferences.getNotesStorage()
propertiesStorage = propertiesStorageRepo.provide(RootIndex.provide(root))
propertiesStorage = lazyPropertiesStorage.await()
}

suspend fun persistNoteProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.utils.extractDuration
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -43,9 +44,19 @@ class NotesViewModel
@Inject
lateinit var memoPreferences: MemoPreferences

private val initExceptionHandler by lazy {
CoroutineExceptionHandler { _, throwable ->
throwable.printStackTrace()

// If the initialization fails with an exception for current storage path,
// clear the path for users to be able to re-select it next time.
memoPreferences.storePath("")
}
}

fun init(extraBlock: () -> Unit) {
val initJob =
viewModelScope.launch(iODispatcher) {
viewModelScope.launch(iODispatcher + initExceptionHandler) {
textNotesRepo.init()
graphicNotesRepo.init()
voiceNotesRepo.init()
Expand Down

0 comments on commit a770873

Please sign in to comment.