From 2c270bc9a780cfd1a194f78df7618eb160b71664 Mon Sep 17 00:00:00 2001 From: tuancoltech Date: Thu, 20 Jun 2024 22:14:23 +0700 Subject: [PATCH] Prevent empty note from being saved by treating note with empty text/voice/drawing as empty note --- app/src/main/AndroidManifest.xml | 2 +- .../ui/fragments/ArkMediaPlayerFragment.kt | 4 ++++ .../ui/fragments/ArkRecorderFragment.kt | 7 +++++- .../ui/fragments/BaseEditNoteFragment.kt | 3 ++- .../ui/fragments/EditGraphicNotesFragment.kt | 21 +++++++++++++++++ .../ui/fragments/EditTextNotesFragment.kt | 23 ++++++++++++++++++- .../ui/viewmodels/GraphicNotesViewModel.kt | 5 ++++ .../main/res/layout/layout_audio_record.xml | 1 + 8 files changed, 62 insertions(+), 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2a7363ab..3f03c1cb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,7 +26,7 @@ tools:targetApi="31"> diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkMediaPlayerFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkMediaPlayerFragment.kt index be167114..f74ca8b2 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkMediaPlayerFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkMediaPlayerFragment.kt @@ -53,6 +53,10 @@ class ArkMediaPlayerFragment: BaseEditNoteFragment() { return note.title != binding.edtTitle.text.toString() } + override fun isContentEmpty(): Boolean { + return false + } + private fun initUI() { binding.toolbar.ivRightActionIcon.setOnClickListener { diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt index 5c771a2b..a78bf056 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt @@ -316,6 +316,11 @@ class ArkRecorderFragment: BaseEditNoteFragment() { } } + override fun isContentEmpty(): Boolean { + return (!arkRecorderViewModel.isRecordExisting() + && ((note?.path?.toFile()?.length() ?: 0L) == 0L)) + } + private fun saveNote() { notesViewModel.onSaveClick(createNewNote(), parentNote = note) { show -> activity.showProgressBar(show) @@ -372,7 +377,7 @@ class ArkRecorderFragment: BaseEditNoteFragment() { } binding.edtTitle.setText(note?.title) binding.edtTitle.addTextChangedListener { - enableSaveText(it.toString() != note?.title) + enableSaveText(isContentChanged() && !isContentEmpty()) } } diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/BaseEditNoteFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/BaseEditNoteFragment.kt index 9efab801..64d1395c 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/BaseEditNoteFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/BaseEditNoteFragment.kt @@ -133,7 +133,7 @@ abstract class BaseEditNoteFragment: Fragment() { } private fun handleBackPressed() { - if (isContentChanged()) { + if (isContentChanged() && !isContentEmpty()) { showSaveNoteDialog() } else { hostActivity.onBackPressedDispatcher.onBackPressed() @@ -147,4 +147,5 @@ abstract class BaseEditNoteFragment: Fragment() { abstract fun createNewNote(): Note abstract fun getCurrentNote(): Note abstract fun isContentChanged(): Boolean + abstract fun isContentEmpty(): Boolean } \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditGraphicNotesFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditGraphicNotesFragment.kt index 5a309c71..9b4e8be8 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditGraphicNotesFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditGraphicNotesFragment.kt @@ -82,6 +82,7 @@ class EditGraphicNotesFragment: BaseEditNoteFragment() { if (title.isEmpty()) { binding.edtTitle.hint = getString(R.string.hint_new_graphical_note) } + enableSaveText(isContentChanged() && !isContentEmpty()) } override fun afterTextChanged(s: Editable?) {} @@ -103,11 +104,18 @@ class EditGraphicNotesFragment: BaseEditNoteFragment() { hostActivity.showProgressBar(show) } } + enableSaveText(false) binding.tvLastModified.gone() binding.editTextDescription.setText(this.note.description) initBottomControls() + observeDrawEvent() + } + private fun observeDrawEvent() { + graphicNotesViewModel.observableSvgLiveData.observe(viewLifecycleOwner) { + enableSaveText(it.getPaths().isNotEmpty()) + } } override fun createNewNote(): Note { @@ -131,6 +139,10 @@ class EditGraphicNotesFragment: BaseEditNoteFragment() { || ((newPaths.size != originalPaths.size) || (!newPaths.containsAll(originalPaths))) } + override fun isContentEmpty(): Boolean { + return graphicNotesViewModel.svg().getPaths().isEmpty() + } + private fun initBottomControls() { val tvBrushSize = binding.layoutGraphicsControl.tvBrushSize tvBrushSize.setOnClickListener { @@ -236,6 +248,15 @@ class EditGraphicNotesFragment: BaseEditNoteFragment() { } } + private fun enableSaveText(enabled: Boolean) { + binding.toolbar.tvRightActionText.isEnabled = enabled + if (enabled) { + binding.toolbar.tvRightActionText.alpha = 1f + } else { + binding.toolbar.tvRightActionText.alpha = 0.4f + } + } + companion object { const val TAG = "EditGraphicNotesFragment" private const val GRAPHICAL_NOTE_KEY = "graphical note" diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt index 5f817067..dbb35a87 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt @@ -7,6 +7,7 @@ import android.text.TextWatcher import android.view.View import android.widget.Toast import androidx.core.view.isVisible +import androidx.core.widget.addTextChangedListener import dagger.hilt.android.AndroidEntryPoint import dev.arkbuilders.arkmemo.R import dev.arkbuilders.arkmemo.models.Note @@ -63,6 +64,7 @@ class EditTextNotesFragment: BaseEditNoteFragment() { if (title.isEmpty()) { binding.edtTitle.hint = getString(R.string.hint_new_text_note) } + enableSaveText(isContentChanged() && !isContentEmpty()) } override fun afterTextChanged(s: Editable?) {} @@ -74,6 +76,9 @@ class EditTextNotesFragment: BaseEditNoteFragment() { noteTitle.setText(this.note.title) noteTitle.addTextChangedListener(noteTitleChangeListener) + editNote.addTextChangedListener { + enableSaveText(isContentChanged() && !isContentEmpty()) + } editNote.isVisible = true editNote.requestFocus() editNote.setText(this.note.text) @@ -86,6 +91,7 @@ class EditTextNotesFragment: BaseEditNoteFragment() { hostActivity.showProgressBar(show) } } + enableSaveText(false) binding.tvPaste.setOnClickListener(pasteNoteClickListener) @@ -101,6 +107,10 @@ class EditTextNotesFragment: BaseEditNoteFragment() { || note.text != binding.editNote.text.toString() } + override fun isContentEmpty(): Boolean { + return binding.editNote.text.toString().trim().isEmpty() + } + override fun onResume() { super.onResume() observeClipboardContent() @@ -121,13 +131,24 @@ class EditTextNotesFragment: BaseEditNoteFragment() { private fun observeClipboardContent() { context?.getTextFromClipBoard(view) { - if (it.isNullOrEmpty()) { + val clipboardTextEmpty = it.isNullOrEmpty() + if (clipboardTextEmpty) { binding.tvPaste.alpha = 0.4f binding.tvPaste.isClickable = false } else { binding.tvPaste.alpha = 1f binding.tvPaste.isClickable = true } + enableSaveText(!clipboardTextEmpty && isContentChanged()) + } + } + + private fun enableSaveText(enabled: Boolean) { + binding.tvSave.isEnabled = enabled + if (enabled) { + binding.tvSave.alpha = 1f + } else { + binding.tvSave.alpha = 0.4f } } diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/GraphicNotesViewModel.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/GraphicNotesViewModel.kt index 1b65ce0f..0a8976ce 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/GraphicNotesViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/GraphicNotesViewModel.kt @@ -3,6 +3,8 @@ package dev.arkbuilders.arkmemo.ui.viewmodels import android.graphics.Color import android.graphics.Paint import android.graphics.Path +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel @@ -34,6 +36,8 @@ class GraphicNotesViewModel @Inject constructor(): ViewModel() { private val editPaths = ArrayDeque() private var svg = SVG() + private val svgLiveData = MutableLiveData() + val observableSvgLiveData = svgLiveData as LiveData fun onNoteOpened(note: GraphicNote) { viewModelScope.launch { @@ -46,6 +50,7 @@ class GraphicNotesViewModel @Inject constructor(): ViewModel() { fun onDrawPath(path: DrawPath) { editPaths.addLast(path) svg.addPath(path) + svgLiveData.postValue(svg) } fun onChangeColor(color: Int) { diff --git a/app/src/main/res/layout/layout_audio_record.xml b/app/src/main/res/layout/layout_audio_record.xml index 15d12374..462de521 100644 --- a/app/src/main/res/layout/layout_audio_record.xml +++ b/app/src/main/res/layout/layout_audio_record.xml @@ -3,6 +3,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" + android:background="@color/white" xmlns:tools="http://schemas.android.com/tools">