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 894c8f4
Show file tree
Hide file tree
Showing 8 changed files with 59 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.arkbuilders.arkmemo.preferences

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.util.Log
import dagger.hilt.android.qualifiers.ApplicationContext
import dev.arkbuilders.arkmemo.utils.CRASH_REPORT_ENABLE
import java.nio.file.Path
Expand All @@ -22,6 +23,7 @@ class MemoPreferencesImpl
private val prefEditor = sharedPreferences.edit()

override fun storePath(path: String) {
Log.w("tuancoltech", "storePath: " + path)
prefEditor.putString(CURRENT_NOTES_PATH, path).apply()
}

Expand Down
26 changes: 23 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,28 @@ 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 {
Log.w("tuancoltech", "Initializing root from " + this@NotesRepoHelper)
memoPreferences.getNotesStorage()
}

private lateinit var propertiesStorage: PropertiesStorage
private val lazyPropertiesStorage by lazy {
Log.w("tuancoltech", "Initializing propertiesStorage from " + this@NotesRepoHelper)
CoroutineScope(iODispatcher).async {
propertiesStorageRepo.provide(RootIndex.provide(root))
}
}

suspend fun init() {
root = memoPreferences.getNotesStorage()
propertiesStorage = propertiesStorageRepo.provide(RootIndex.provide(root))
// root = memoPreferences.getNotesStorage()
Log.v("tuancoltech1", "Initialize root to: " + root + ". Helper: " + this@NotesRepoHelper)
// throw UnknownError()
// propertiesStorage = propertiesStorageRepo.provide(RootIndex.provide(root))
propertiesStorage = lazyPropertiesStorage.await()
Log.v("tuancoltech1", "NotesRepoHelper init propertiesStorage: " + propertiesStorage)
}

suspend fun persistNoteProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class GraphicNotesRepo
override suspend fun init() {
helper.init()
root = memoPreferences.getNotesStorage()
Log.i("tuancoltech1", "GraphicNotesRepo init helper: " + helper)
}

override suspend fun save(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TextNotesRepo
override suspend fun init() {
root = memoPreferences.getNotesStorage()
helper.init()
Log.i("tuancoltech1", "TextNotesRepo init helper: " + helper)
}

override suspend fun save(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class VoiceNotesRepo
override suspend fun init() {
root = memoPreferences.getNotesStorage()
helper.init()
Log.i("tuancoltech1", "VoiceNotesRepo init helper: " + helper)
}

override suspend fun read(): List<VoiceNote> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.arkbuilders.arkmemo.ui.activities

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.IdRes
Expand Down Expand Up @@ -77,6 +78,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}
} else {
if (savedInstanceState == null) {
Log.d("tuancoltech", "Showing NotesFragment")
supportFragmentManager.beginTransaction().apply {
add(fragContainer, fragment, NotesFragment.TAG)
commit()
Expand All @@ -100,10 +102,12 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
if (!storageFolderExisting) {
showNoNoteStorageDialog(RootNotFound(rootPath = memoPreferences.getPath()))
} else {
Log.i("tuancoltech", "Showing FilePickerDialog")
FilePickerDialog.show(this, supportFragmentManager)
}

supportFragmentManager.onArkPathPicked(this) {
Log.i("tuancoltech", "A path is chosen: " + it.toString() + ". Storing it...")
memoPreferences.storePath(it.toString())
showFragment()
}
Expand All @@ -121,6 +125,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
negativeText = R.string.error_load_notes_failed_negative_action,
isAlert = false,
onPositiveClick = {
Log.i("tuancoltech", "Showing FilePickerDialog from showNoNoteStorageDialog")
FilePickerDialog.show(this, supportFragmentManager)
},
onNegativeClicked = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arkbuilders.arkmemo.ui.viewmodels

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -16,6 +17,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 +45,18 @@ class NotesViewModel
@Inject
lateinit var memoPreferences: MemoPreferences

private val initExceptionHandler by lazy {
CoroutineExceptionHandler { _, throwable ->
Log.e("tuancoltech", "NotesViewModel initExceptionHandler got an exception: " + throwable::class.simpleName)
throwable.printStackTrace()
memoPreferences.storePath("")
}
}

fun init(extraBlock: () -> Unit) {
Log.d("tuancoltech", "NotesViewModel init START")
val initJob =
viewModelScope.launch(iODispatcher) {
viewModelScope.launch(iODispatcher + initExceptionHandler) {
textNotesRepo.init()
graphicNotesRepo.init()
voiceNotesRepo.init()
Expand Down

0 comments on commit 894c8f4

Please sign in to comment.