Skip to content

Commit

Permalink
Fixed the save button to function the same as the back action
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Schneider committed Dec 2, 2022
1 parent 2de757e commit 1ac367f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,10 @@ class AudioNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_AUDIO) {
return sendIntent
}

override fun determineToSaveOnAction(category: Int): Pair<Boolean, Int> {
override fun determineToSave(title: String, category: Int): Pair<Boolean, Int> {
val intent = intent
return Pair(
seekBar.isEnabled || category != intent.getIntExtra(
EXTRA_CATEGORY,
-1
) && -5 != intent.getIntExtra(
seekBar.isEnabled && -5 != intent.getIntExtra(
EXTRA_CATEGORY, -5
),
R.string.toast_emptyNote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli
protected abstract fun shareNote(name: String): Intent
protected abstract fun onNoteLoadedFromDB(note: Note)
protected abstract fun onNewNote()
protected abstract fun determineToSaveOnAction(category: Int): Pair<Boolean, Int>
protected abstract fun determineToSave(title: String, category: Int): Pair<Boolean, Int>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -284,13 +284,8 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli
finish()
}
R.id.action_save -> {
val (toSave, mes) = determineToSaveOnAction(if (currentCat >= 0) currentCat else savedCat)
if (toSave) {
shouldSave = true
finish()
} else {
Toast.makeText(applicationContext, mes, Toast.LENGTH_SHORT).show()
}
shouldSave = true
finish()
}
else -> {}
}
Expand Down Expand Up @@ -367,8 +362,16 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli
}

private fun saveOrUpdateNote() {
if (edit) updateNote()
else saveNote()
val (toSave, mes) = determineToSave(etName.text.toString(), if (currentCat >= 0) currentCat else savedCat)
if (toSave) {
if (etName.text.isEmpty()) {
etName.setText(generateStandardName())
}
if (edit) updateNote()
else saveNote()
} else {
Toast.makeText(applicationContext, mes, Toast.LENGTH_SHORT).show()
}
}

private fun saveNote() {
Expand All @@ -383,7 +386,7 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli

private fun updateNote() {
val note = updateNoteToSave(
if (etName.text.isEmpty()) generateStandardName() else etName.text.toString(),
etName.text.toString(),
if (currentCat >= 0) currentCat else savedCat
)
insertNoteIntoDB(note)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,10 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI
}
}

override fun determineToSaveOnAction(category: Int): Pair<Boolean, Int> {
override fun determineToSave(title: String, category: Int): Pair<Boolean, Int> {
val intent = intent
return Pair(
category != intent.getIntExtra(
EXTRA_CATEGORY,
-1
) && -5 != intent.getIntExtra(
EXTRA_CATEGORY, -5
),
(title.isNotEmpty() || !checklistAdapter.isEmpty) && -5 != intent.getIntExtra(EXTRA_CATEGORY, -5),
R.string.toast_emptyNote
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) {
return sendIntent
}

override fun determineToSaveOnAction(category: Int): Pair<Boolean, Int> {
override fun determineToSave(title: String, category: Int): Pair<Boolean, Int> {
val emptyBitmap = Bitmap.createBitmap(
drawView.bitmap.width,
drawView.bitmap.height,
drawView.bitmap.config
)
val intent = intent
return Pair(
!drawView.bitmap.sameAs(emptyBitmap) || -5 != intent.getIntExtra(EXTRA_CATEGORY, -5),
!drawView.bitmap.sameAs(emptyBitmap) && -5 != intent.getIntExtra(EXTRA_CATEGORY, -5),
R.string.toast_emptyNote
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ class TextNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_TEXT) {
return sendIntent
}

override fun determineToSaveOnAction(category: Int): Pair<Boolean, Int> {
override fun determineToSave(title: String, category: Int): Pair<Boolean, Int> {
val intent = intent
return Pair<Boolean, Int>(
category != intent.getIntExtra(EXTRA_CATEGORY, -1)
&& -5 != intent.getIntExtra(EXTRA_CATEGORY, -5),
(title.isNotEmpty() || Html.toHtml(etContent.text) != "") && -5 != intent.getIntExtra(EXTRA_CATEGORY, -5),
R.string.toast_emptyNote
)
}
Expand Down

0 comments on commit 1ac367f

Please sign in to comment.