Skip to content

Commit

Permalink
Handle crash upon app launch when storage folder is removed externally
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Nov 28, 2024
1 parent 8d48ed4 commit 1d57249
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkmemo/models/LoadError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.arkbuilders.arkmemo.models

sealed interface LoadError

data class RootNotFound(val rootPath: String) : LoadError
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ interface MemoPreferences {
fun storeCrashReportEnabled(enabled: Boolean)

fun getCrashReportEnabled(): Boolean

fun storageNotAvailable(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.arkbuilders.arkmemo.utils.CRASH_REPORT_ENABLE
import java.nio.file.Path
import javax.inject.Inject
import kotlin.io.path.Path
import kotlin.io.path.exists

private const val NAME = "memo_prefs"
private const val CURRENT_NOTES_PATH = "current_notes_path"
Expand All @@ -33,4 +34,8 @@ class MemoPreferencesImpl
}

override fun getCrashReportEnabled(): Boolean = sharedPreferences.getBoolean(CRASH_REPORT_ENABLE, true)

override fun storageNotAvailable(): Boolean {
return getPath().isEmpty() || !getNotesStorage().exists()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.contracts.PermissionContract
import dev.arkbuilders.arkmemo.databinding.ActivityMainBinding
import dev.arkbuilders.arkmemo.models.RootNotFound
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.ui.dialogs.CommonActionDialog
import dev.arkbuilders.arkmemo.ui.dialogs.FilePickerDialog
import dev.arkbuilders.arkmemo.ui.fragments.BaseFragment
import dev.arkbuilders.arkmemo.ui.fragments.EditTextNotesFragment
import dev.arkbuilders.arkmemo.ui.fragments.NotesFragment
import dev.arkbuilders.components.filepicker.onArkPathPicked
import javax.inject.Inject
import kotlin.io.path.exists

@AndroidEntryPoint
class MainActivity : AppCompatActivity(R.layout.activity_main) {
Expand Down Expand Up @@ -92,8 +95,13 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}
}

if (memoPreferences.getPath().isEmpty()) {
FilePickerDialog.show(this, supportFragmentManager)
val storageFolderExisting = memoPreferences.getNotesStorage().exists()
if (memoPreferences.storageNotAvailable()) {
if (!storageFolderExisting) {
showNoNoteStorageDialog(RootNotFound(rootPath = memoPreferences.getPath()))
} else {
FilePickerDialog.show(this, supportFragmentManager)
}

supportFragmentManager.onArkPathPicked(this) {
memoPreferences.storePath(it.toString())
Expand All @@ -104,6 +112,27 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}
}

private fun showNoNoteStorageDialog(error: RootNotFound) {
val loadFailDialog =
CommonActionDialog(
title = getString(R.string.error_load_notes_failed_title),
message = getString(R.string.error_load_notes_failed_description, error.rootPath),
positiveText = R.string.error_load_notes_failed_positive_action,
negativeText = R.string.error_load_notes_failed_negative_action,
isAlert = false,
onPositiveClick = {
FilePickerDialog.show(this, supportFragmentManager)
},
onNegativeClicked = {
finish()
},
onCloseClicked = {
finish()
},
)
loadFailDialog.show(supportFragmentManager, CommonActionDialog.TAG)
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putString(CURRENT_FRAGMENT_TAG, fragment.tag)
super.onSaveInstanceState(outState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ class FilePickerDialog : ArkFilePickerFragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (storageNotAvailable()) {
if (memoPreferences.storageNotAvailable()) {
isCancelable = false
}
}

override fun dismiss() {
super.dismiss()
if (storageNotAvailable()) {
if (memoPreferences.storageNotAvailable()) {
activity?.finish()
}
}

private fun storageNotAvailable(): Boolean = memoPreferences.getPath().isEmpty()

companion object {
private const val TAG = "file_picker"
private lateinit var fragmentManager: FragmentManager
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@
</plurals>
<string name="select_all">Select All</string>
<string name="deselect_all">Deselect All</string>
<string name="error_load_notes_failed_title">Cannot find notes</string>
<string name="error_load_notes_failed_description">The folder %s with notes data cannot be located.\nPlease select a new folder.</string>
<string name="error_load_notes_failed_positive_action">Select</string>
<string name="error_load_notes_failed_negative_action">Leave</string>

</resources>

0 comments on commit 1d57249

Please sign in to comment.