Skip to content

Commit

Permalink
fix typing comment jumping
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Feb 1, 2025
1 parent 433942e commit bf63a11
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ fun createChangeRecordCommentFieldAdapterDelegate(
with(binding) {
item as ViewData

if (item.text != etChangeRecordCommentField.text.toString()) {
if (item.text != null &&
item.text != etChangeRecordCommentField.text.toString()
) {
etChangeRecordCommentField.setText(item.text)
etChangeRecordCommentField.setSelection(item.text.length)
}
Expand All @@ -38,7 +40,7 @@ fun createChangeRecordCommentFieldAdapterDelegate(

data class ChangeRecordCommentFieldViewData(
val id: Long,
val text: String,
val text: String?,
@ColorInt val iconColor: Int,
) : ViewHolderType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import com.example.util.simpletimetracker.core.view.timeAdjustment.TimeAdjustmen
import com.example.util.simpletimetracker.domain.favourite.interactor.FavouriteCommentInteractor
import com.example.util.simpletimetracker.domain.prefs.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.record.interactor.RecordInteractor
import com.example.util.simpletimetracker.domain.recordTag.interactor.RecordTagInteractor
import com.example.util.simpletimetracker.domain.recordType.interactor.RecordTypeInteractor
import com.example.util.simpletimetracker.domain.record.interactor.RunningRecordInteractor
import com.example.util.simpletimetracker.domain.record.model.Record
import com.example.util.simpletimetracker.domain.recordTag.interactor.RecordTagInteractor
import com.example.util.simpletimetracker.domain.recordType.interactor.RecordTypeInteractor
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.hint.HintViewData
import com.example.util.simpletimetracker.feature_change_record.R
Expand Down Expand Up @@ -63,6 +63,7 @@ class ChangeRecordViewDataInteractor @Inject constructor(
suspend fun getCommentsViewData(
comment: String,
typeId: Long,
fromCommentChange: Boolean,
): List<ViewHolderType> {
data class Data(val timeStarted: Long, val comment: String)

Expand All @@ -71,8 +72,10 @@ class ChangeRecordViewDataInteractor @Inject constructor(
val isFavourite = favouriteCommentInteractor.get(comment) != null

ChangeRecordCommentFieldViewData(
id = 1L, // Only one at the time.
text = comment,
// Only one at the time.
id = 1L,
// Do not update text if update coming from typing.
text = if (fromCommentChange) null else comment,
iconColor = if (isFavourite) {
resourceRepo.getColor(R.color.colorSecondary)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ abstract class ChangeRecordBaseViewModel(
return@lazy MutableLiveData<List<ViewHolderType>>().let { initial ->
viewModelScope.launch {
initializePreviewViewData()
initial.value = loadCommentsViewData()
initial.value = loadCommentsViewData(fromCommentChange = false)
}
initial
}
Expand Down Expand Up @@ -338,15 +338,21 @@ abstract class ChangeRecordBaseViewModel(
}

fun onCommentClick(item: ChangeRecordCommentViewData) {
onCommentChange(item.text)
viewModelScope.launch {
if (item.text != newComment) {
newComment = item.text
updatePreview()
updateCommentsViewData()
}
}
}

fun onCommentChange(comment: String) {
viewModelScope.launch {
if (comment != newComment) {
newComment = comment
updatePreview()
updateCommentsViewData()
updateCommentsViewData(fromCommentChange = true)
}
}
}
Expand Down Expand Up @@ -845,17 +851,22 @@ abstract class ChangeRecordBaseViewModel(
)
}

private fun updateCommentsViewData() {
private fun updateCommentsViewData(
fromCommentChange: Boolean = false,
) {
searchLoadJob?.cancel()
searchLoadJob = viewModelScope.launch {
comments.set(loadCommentsViewData())
comments.set(loadCommentsViewData(fromCommentChange))
}
}

private suspend fun loadCommentsViewData(): List<ViewHolderType> {
private suspend fun loadCommentsViewData(
fromCommentChange: Boolean,
): List<ViewHolderType> {
return changeRecordViewDataInteractor.getCommentsViewData(
comment = newComment,
typeId = newTypeId,
fromCommentChange = fromCommentChange,
)
}

Expand Down

0 comments on commit bf63a11

Please sign in to comment.