Skip to content

Commit

Permalink
* Clear storage path before initiating the Root storage folder, and s…
Browse files Browse the repository at this point in the history
…tore it once load is successfully

* Remove redundant initializations of NotesRepoHelper across different note type repositories
  • Loading branch information
tuancoltech committed Dec 17, 2024
1 parent bfd9a96 commit 5e54af1
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.arkbuilders.arkmemo.repo
import dev.arkbuilders.arkmemo.models.SaveNoteResult

interface NotesRepo<Note> {
suspend fun init()
suspend fun init(root: String)

suspend fun save(
note: Note,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import java.nio.file.Path
import javax.inject.Inject
import javax.inject.Named
import kotlin.NullPointerException
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
import kotlin.io.path.extension
import kotlin.io.path.getLastModifiedTime
Expand All @@ -33,18 +34,19 @@ class NotesRepoHelper
private val propertiesStorageRepo: PropertiesStorageRepo,
@Named(IO_DISPATCHER) private val iODispatcher: CoroutineDispatcher,
) {
private val root by lazy {
memoPreferences.getNotesStorage()
}
lateinit var root: Path

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

suspend fun init() {
suspend fun init(root: String) {
this.root = Path(root)
propertiesStorage = lazyPropertiesStorage.await()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import dev.arkbuilders.arkmemo.graphics.ColorCode
import dev.arkbuilders.arkmemo.graphics.SVG
import dev.arkbuilders.arkmemo.models.GraphicNote
import dev.arkbuilders.arkmemo.models.SaveNoteResult
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.repo.NotesRepoHelper
import dev.arkbuilders.arkmemo.utils.dpToPx
Expand All @@ -39,12 +38,11 @@ import kotlin.io.path.name
class GraphicNotesRepo
@Inject
constructor(
private val memoPreferences: MemoPreferences,
@Named(IO_DISPATCHER) private val iODispatcher: CoroutineDispatcher,
private val helper: NotesRepoHelper,
@ApplicationContext private val context: Context,
) : NotesRepo<GraphicNote> {
private lateinit var root: Path
private val root: Path by lazy { helper.root }

private val displayMetrics by lazy { Resources.getSystem().displayMetrics }
private val screenWidth by lazy { displayMetrics.widthPixels }
Expand All @@ -53,9 +51,8 @@ class GraphicNotesRepo

private val thumbDirectory by lazy { context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) }

override suspend fun init() {
helper.init()
root = memoPreferences.getNotesStorage()
override suspend fun init(root: String) {
helper.init(root)
}

override suspend fun save(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.arkbuilders.arklib.data.index.Resource
import dev.arkbuilders.arkmemo.di.IO_DISPATCHER
import dev.arkbuilders.arkmemo.models.SaveNoteResult
import dev.arkbuilders.arkmemo.models.TextNote
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.repo.NotesRepoHelper
import dev.arkbuilders.arkmemo.utils.listFiles
Expand All @@ -27,16 +26,14 @@ import kotlin.io.path.writeLines
class TextNotesRepo
@Inject
constructor(
private val memoPreferences: MemoPreferences,
@Named(IO_DISPATCHER)
private val iODispatcher: CoroutineDispatcher,
private val helper: NotesRepoHelper,
) : NotesRepo<TextNote> {
private lateinit var root: Path
private val root: Path by lazy { helper.root }

override suspend fun init() {
root = memoPreferences.getNotesStorage()
helper.init()
override suspend fun init(root: String) {
helper.init(root)
}

override suspend fun save(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.arkbuilders.arklib.data.index.Resource
import dev.arkbuilders.arkmemo.di.IO_DISPATCHER
import dev.arkbuilders.arkmemo.models.SaveNoteResult
import dev.arkbuilders.arkmemo.models.VoiceNote
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.repo.NotesRepoHelper
import dev.arkbuilders.arkmemo.utils.extractDuration
Expand All @@ -26,15 +25,13 @@ import kotlin.io.path.pathString
class VoiceNotesRepo
@Inject
constructor(
private val memoPreferences: MemoPreferences,
@Named(IO_DISPATCHER) private val iODispatcher: CoroutineDispatcher,
private val helper: NotesRepoHelper,
) : NotesRepo<VoiceNote> {
private lateinit var root: Path
private val root: Path by lazy { helper.root }

override suspend fun init() {
root = memoPreferences.getNotesStorage()
helper.init()
override suspend fun init(root: String) {
helper.init(root)
}

override suspend fun read(): List<VoiceNote> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,6 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
onBackPressedDispatcher.onBackPressed()
}

fun showFragment() {
val textDataFromIntent = intent?.getStringExtra(Intent.EXTRA_TEXT)
if (textDataFromIntent != null) {
fragment = EditTextNotesFragment.newInstance(textDataFromIntent)
supportFragmentManager.beginTransaction().apply {
replace(fragContainer, fragment, EditTextNotesFragment.TAG)
commit()
}
} else {
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().apply {
add(fragContainer, fragment, NotesFragment.TAG)
commit()
}
} else {
supportFragmentManager.apply {
val tag = savedInstanceState.getString(CURRENT_FRAGMENT_TAG)
findFragmentByTag(tag)?.let {
fragment = it
if (!fragment.isInLayout) {
resumeFragment(fragment)
}
}
}
}
}
}

val storageFolderExisting = memoPreferences.getNotesStorage().exists()
if (memoPreferences.storageNotAvailable()) {
if (!storageFolderExisting) {
Expand All @@ -104,11 +76,49 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}

supportFragmentManager.onArkPathPicked(this) {
memoPreferences.storePath(it.toString())
showFragment()
showFragment(savedInstanceState, it.toString())
}
} else {
showFragment(savedInstanceState, memoPreferences.getPath())
}
}

private fun showFragment(
savedInstanceState: Bundle?,
storagePath: String,
) {
val textDataFromIntent = intent?.getStringExtra(Intent.EXTRA_TEXT)
if (textDataFromIntent != null) {
fragment = EditTextNotesFragment.newInstance(textDataFromIntent)
fragment.arguments =
Bundle().apply {
putString(BUNDLE_KEY_STORAGE_PATH, storagePath)
}
supportFragmentManager.beginTransaction().apply {
replace(fragContainer, fragment, EditTextNotesFragment.TAG)
commit()
}
} else {
showFragment()
fragment.arguments =
Bundle().apply {
putString(BUNDLE_KEY_STORAGE_PATH, storagePath)
}
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().apply {
add(fragContainer, fragment, NotesFragment.TAG)
commit()
}
} else {
supportFragmentManager.apply {
val tag = savedInstanceState.getString(CURRENT_FRAGMENT_TAG)
findFragmentByTag(tag)?.let {
fragment = it
if (!fragment.isInLayout) {
resumeFragment(fragment)
}
}
}
}
}
}

Expand Down Expand Up @@ -166,6 +176,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {

companion object {
private const val CURRENT_FRAGMENT_TAG = "current fragment tag"
const val BUNDLE_KEY_STORAGE_PATH = "bundle_key_storage_path"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class ArkRecorderFragment : BaseEditNoteFragment() {
}

private fun observeDataStates() {
notesViewModel.init {}
observeRecordingState()
observeSaveResult(notesViewModel.getSaveNoteResultLiveData())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class EditGraphicNotesFragment : BaseEditNoteFragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
notesViewModel.init {}
arguments?.getParcelableCompat(GRAPHICAL_NOTE_KEY, GraphicNote::class.java)?.let {
note = it
graphicNotesViewModel.onNoteOpened(note)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.models.Note
import dev.arkbuilders.arkmemo.models.TextNote
import dev.arkbuilders.arkmemo.ui.activities.MainActivity.Companion.BUNDLE_KEY_STORAGE_PATH
import dev.arkbuilders.arkmemo.utils.getParcelableCompat
import dev.arkbuilders.arkmemo.utils.getTextFromClipBoard
import dev.arkbuilders.arkmemo.utils.gone
Expand Down Expand Up @@ -69,13 +70,15 @@ class EditTextNotesFragment : BaseEditNoteFragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
notesViewModel.init {}

if (arguments != null) {
requireArguments().getParcelableCompat(NOTE_KEY, TextNote::class.java)?.let {
note = it
}
noteStr = requireArguments().getString(NOTE_STRING_KEY)
arguments?.getString(BUNDLE_KEY_STORAGE_PATH)?.let {
notesViewModel.init(it) {}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ class NotesFragment : BaseFragment() {
initEmptyStateViews()

binding.pbLoading.visible()
val root = arguments?.getString(MainActivity.BUNDLE_KEY_STORAGE_PATH) ?: ""
notesViewModel.apply {
init {
storePath("")
init(root) {
readAllNotes {
onNotesLoaded(it)
storePath(root)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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 All @@ -41,25 +40,18 @@ class NotesViewModel
private val mSaveNoteResultLiveData = MutableLiveData<SaveNoteResult>()
private var searchJob: Job? = null

@Inject
lateinit var memoPreferences: MemoPreferences
@set:Inject
internal 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) {
fun init(
root: String,
extraBlock: () -> Unit,
) {
val initJob =
viewModelScope.launch(iODispatcher + initExceptionHandler) {
textNotesRepo.init()
graphicNotesRepo.init()
voiceNotesRepo.init()
viewModelScope.launch(iODispatcher) {
textNotesRepo.init(root)
graphicNotesRepo.init(root)
voiceNotesRepo.init(root)
}
viewModelScope.launch {
initJob.join()
Expand Down Expand Up @@ -202,4 +194,8 @@ class NotesViewModel
fun getStorageFolderPath(): String {
return memoPreferences.getPath()
}

fun storePath(path: String) {
memoPreferences.storePath(path)
}
}

0 comments on commit 5e54af1

Please sign in to comment.