Skip to content

Commit

Permalink
Merge pull request #64 from PhilKes/fix/title-change-history
Browse files Browse the repository at this point in the history
Add EditActivity title EditText to ChangeHistory
  • Loading branch information
PhilKes authored Oct 26, 2024
2 parents a17d6bf + 9cdc151 commit 8d5e0a0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.text.Editable
import android.text.TextWatcher
import android.util.TypedValue
import android.view.MenuItem
import android.view.View
Expand All @@ -20,7 +21,6 @@ import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
Expand All @@ -41,11 +41,11 @@ import com.philkes.notallyx.presentation.view.note.audio.AudioAdapter
import com.philkes.notallyx.presentation.view.note.preview.PreviewFileAdapter
import com.philkes.notallyx.presentation.view.note.preview.PreviewImageAdapter
import com.philkes.notallyx.presentation.viewmodel.NotallyModel
import com.philkes.notallyx.presentation.widget.WidgetProvider
import com.philkes.notallyx.utils.FileError
import com.philkes.notallyx.utils.Operations
import com.philkes.notallyx.utils.add
import com.philkes.notallyx.utils.changehistory.ChangeHistory
import com.philkes.notallyx.utils.createTextWatcherWithHistory
import com.philkes.notallyx.utils.displayFormattedTimestamp
import java.io.File
import kotlinx.coroutines.Dispatchers
Expand All @@ -54,6 +54,7 @@ import kotlinx.coroutines.launch
abstract class EditActivity(private val type: Type) : LockedActivity<ActivityEditBinding>() {
internal val model: NotallyModel by viewModels()
internal lateinit var changeHistory: ChangeHistory
internal lateinit var enterTitleTextWatcher: TextWatcher

override fun finish() {
lifecycleScope.launch(Dispatchers.Main) {
Expand All @@ -72,8 +73,6 @@ abstract class EditActivity(private val type: Type) : LockedActivity<ActivityEdi
if (changeHistory.canUndo()) {
model.modifiedTimestamp = System.currentTimeMillis()
}
model.saveNote()
WidgetProvider.sendBroadcast(application, longArrayOf(model.id))
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -241,20 +240,30 @@ abstract class EditActivity(private val type: Type) : LockedActivity<ActivityEdi
abstract fun configureUI()

open fun setupListeners() {
binding.EnterTitle.doAfterTextChanged { text ->
model.title = requireNotNull(text).trim().toString()
enterTitleTextWatcher = run {
binding.EnterTitle.createTextWatcherWithHistory(changeHistory) { text: Editable ->
model.title = text.trim().toString()
}
}
binding.EnterTitle.addTextChangedListener(enterTitleTextWatcher)
}

open fun setStateFromModel() {
binding.DateCreated.displayFormattedTimestamp(model.timestamp, preferences.dateFormat.value)

binding.EnterTitle.setText(model.title)
updateEnterTitle()
Operations.bindLabels(binding.LabelGroup, model.labels, model.textSize)

setColor()
}

private fun updateEnterTitle() {
binding.EnterTitle.apply {
removeTextChangedListener(enterTitleTextWatcher)
setText(model.title)
addTextChangedListener(enterTitleTextWatcher)
}
}

private fun handleSharedNote() {
val title = intent.getStringExtra(Intent.EXTRA_SUBJECT)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class EditListActivity : EditActivity(Type.LIST) {
private lateinit var listManager: ListManager

override suspend fun saveNote() {
super.saveNote()
model.saveNote(items.toMutableList())
WidgetProvider.sendBroadcast(application, longArrayOf(model.id))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.philkes.notallyx.R
import com.philkes.notallyx.data.model.Type
import com.philkes.notallyx.databinding.TextInputDialogBinding
import com.philkes.notallyx.presentation.widget.WidgetProvider
import com.philkes.notallyx.utils.LinkMovementMethod
import com.philkes.notallyx.utils.add
import com.philkes.notallyx.utils.changehistory.EditTextChange
Expand All @@ -37,6 +38,12 @@ class EditNoteActivity : EditActivity(Type.NOTE) {

private lateinit var enterBodyTextWatcher: TextWatcher

override suspend fun saveNote() {
super.saveNote()
model.saveNote()
WidgetProvider.sendBroadcast(application, longArrayOf(model.id))
}

override fun configureUI() {
binding.EnterTitle.setOnNextAction { binding.EnterBody.requestFocus() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ class WidgetFactory(private val app: Application, private val id: Long, private
TypedValue.COMPLEX_UNIT_SP,
TextSize.getDisplayTitleSize(preferences.textSize.value),
)
if (note.title.isNotEmpty()) {
setTextViewText(R.id.Title, note.title)
setViewVisibility(R.id.Title, View.VISIBLE)
} else setViewVisibility(R.id.Title, View.GONE)
setTextViewText(R.id.Title, note.title)

val bodyTextSize = TextSize.getDisplayBodySize(preferences.textSize.value)

Expand All @@ -97,10 +94,7 @@ class WidgetFactory(private val app: Application, private val id: Long, private
TypedValue.COMPLEX_UNIT_SP,
TextSize.getDisplayTitleSize(preferences.textSize.value),
)
if (list.title.isNotEmpty()) {
setTextViewText(R.id.Title, list.title)
setViewVisibility(R.id.Title, View.VISIBLE)
} else setViewVisibility(R.id.Title, View.GONE)
setTextViewText(R.id.Title, list.title)

val bodyTextSize = TextSize.getDisplayBodySize(preferences.textSize.value)

Expand Down

0 comments on commit 8d5e0a0

Please sign in to comment.