Skip to content

Commit

Permalink
Prevent empty note from being saved by treating note with empty text/…
Browse files Browse the repository at this point in the history
…voice/drawing as empty note
  • Loading branch information
tuancoltech committed Jun 20, 2024
1 parent be50939 commit 2c270bc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
tools:targetApi="31">
<activity
android:name=".ui.activities.MainActivity"
android:windowSoftInputMode="adjustPan"
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -372,7 +377,7 @@ class ArkRecorderFragment: BaseEditNoteFragment() {
}
binding.edtTitle.setText(note?.title)
binding.edtTitle.addTextChangedListener {
enableSaveText(it.toString() != note?.title)
enableSaveText(isContentChanged() && !isContentEmpty())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ abstract class BaseEditNoteFragment: Fragment() {
}

private fun handleBackPressed() {
if (isContentChanged()) {
if (isContentChanged() && !isContentEmpty()) {
showSaveNoteDialog()
} else {
hostActivity.onBackPressedDispatcher.onBackPressed()
Expand All @@ -147,4 +147,5 @@ abstract class BaseEditNoteFragment: Fragment() {
abstract fun createNewNote(): Note
abstract fun getCurrentNote(): Note
abstract fun isContentChanged(): Boolean
abstract fun isContentEmpty(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {}
Expand All @@ -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)
Expand All @@ -86,6 +91,7 @@ class EditTextNotesFragment: BaseEditNoteFragment() {
hostActivity.showProgressBar(show)
}
}
enableSaveText(false)

binding.tvPaste.setOnClickListener(pasteNoteClickListener)

Expand All @@ -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()
Expand All @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,6 +36,8 @@ class GraphicNotesViewModel @Inject constructor(): ViewModel() {
private val editPaths = ArrayDeque<DrawPath>()

private var svg = SVG()
private val svgLiveData = MutableLiveData<SVG>()
val observableSvgLiveData = svgLiveData as LiveData<SVG>

fun onNoteOpened(note: GraphicNote) {
viewModelScope.launch {
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/layout_audio_record.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<com.google.android.material.imageview.ShapeableImageView
Expand Down

0 comments on commit 2c270bc

Please sign in to comment.