From f6139b73725f176ad3f881a426a832f5fe4467e7 Mon Sep 17 00:00:00 2001 From: ShubertMunthali Date: Wed, 6 Dec 2023 08:26:07 +0200 Subject: [PATCH] Minor refacotrs --- .../java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt | 8 +++----- .../arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt | 2 +- .../dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt | 7 +++++-- .../dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt | 1 - app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt | 6 ++++-- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt index 84243767..a6f07fe0 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt @@ -56,9 +56,9 @@ class NotesRepoHelper @Inject constructor( Log.d("notes-repo", "resource renamed to ${resourcePath.name} successfully") } - fun readProperties(id: ResourceId): UserNoteProperties { + fun readProperties(id: ResourceId, defaultTitle: String): UserNoteProperties { val title = propertiesStorage.getProperties(id).titles.let { - if (it.isNotEmpty()) it.elementAt(0) else throw NoteTitlesException() + if (it.isNotEmpty()) it.elementAt(0) else defaultTitle } val description = propertiesStorage.getProperties(id).descriptions.let { if (it.isNotEmpty()) it.elementAt(0) else "" @@ -78,6 +78,4 @@ class NotesRepoHelper @Inject constructor( data class UserNoteProperties( val title: String, val description: String -) - -class NoteTitlesException: Exception("note resource missing at least one title") \ No newline at end of file +) \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt index ba3dd6f9..109f0f2b 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt @@ -94,7 +94,7 @@ class GraphicNotesRepo @Inject constructor( modified = path.getLastModifiedTime() ) - val userNoteProperties = helper.readProperties(id) + val userNoteProperties = helper.readProperties(id, "") GraphicNote( title = userNoteProperties.title, diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt index 87f9a0e6..744839ad 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt @@ -93,9 +93,12 @@ class TextNotesRepo @Inject constructor( modified = path.getLastModifiedTime() ) - val userNoteProperties = helper.readProperties(id) - path.readLines { data -> + val userNoteProperties = helper.readProperties( + id, + data.substringBefore("\n") + ) + TextNote( title = userNoteProperties.title, description = userNoteProperties.description, diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt index 1f976400..750f66c1 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt @@ -84,7 +84,6 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { } } } - } if (memoPreferences.getPath().isEmpty()) { diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt b/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt index 0a329e89..d3a3e51f 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt @@ -19,8 +19,10 @@ fun Fragment.observeSaveResult(result: LiveData) { if (!isResumed) return@observe if (it == SaveNoteResult.SUCCESS) { - toast(requireContext(), getString(R.string.ark_memo_note_saved)) - activity?.onBackPressedDispatcher?.onBackPressed() + context?.let { ctx -> + toast(ctx, getString(R.string.ark_memo_note_saved)) + activity?.onBackPressedDispatcher?.onBackPressed() + } } else { context?.let { ctx -> toast(ctx, getString(R.string.ark_memo_note_existing)) } }