Skip to content

Commit

Permalink
change record comment selection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Dec 8, 2024
1 parent 61ce510 commit 83f829c
Show file tree
Hide file tree
Showing 36 changed files with 173 additions and 394 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.example.util.simpletimetracker.feature_change_record.adapter

import android.content.res.ColorStateList
import android.text.TextWatcher
import androidx.annotation.ColorInt
import androidx.core.view.ViewCompat
import androidx.core.widget.doAfterTextChanged
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.createRecyclerBindingAdapterDelegate
Expand All @@ -10,7 +13,7 @@ import com.example.util.simpletimetracker.feature_change_record.databinding.Chan

fun createChangeRecordCommentFieldAdapterDelegate(
afterTextChange: (String) -> Unit,
onSearchClick: () -> Unit,
onFavouriteClick: () -> Unit,
) = createRecyclerBindingAdapterDelegate<ViewData, Binding>(
Binding::inflate,
) { binding, item, _ ->
Expand All @@ -20,8 +23,13 @@ fun createChangeRecordCommentFieldAdapterDelegate(

if (item.text != etChangeRecordCommentField.text.toString()) {
etChangeRecordCommentField.setText(item.text)
etChangeRecordCommentField.setSelection(item.text.length)
}
btnChangeRecordSearchCommentField.setOnClick { onSearchClick() }
ViewCompat.setBackgroundTintList(
binding.ivChangeRecordFavouriteComment,
ColorStateList.valueOf(item.iconColor),
)
btnChangeRecordCommentField.setOnClick { onFavouriteClick() }

etChangeRecordCommentField.removeTextChangedListener(textWatcher)
textWatcher = etChangeRecordCommentField.doAfterTextChanged { afterTextChange(it.toString()) }
Expand All @@ -31,6 +39,7 @@ fun createChangeRecordCommentFieldAdapterDelegate(
data class ChangeRecordCommentFieldViewData(
val id: Long,
val text: String,
@ColorInt val iconColor: Int,
) : ViewHolderType {

override fun getUniqueId(): Long = id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import com.example.util.simpletimetracker.domain.interactor.RunningRecordInterac
import com.example.util.simpletimetracker.domain.model.Record
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_base_adapter.loader.LoaderViewData
import com.example.util.simpletimetracker.feature_change_record.R
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordCommentFieldViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordCommentViewData
import com.example.util.simpletimetracker.feature_change_record.mapper.ChangeRecordViewDataMapper
import com.example.util.simpletimetracker.feature_change_record.model.ChangeRecordDateTimeFieldsState
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordFavCommentState
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordSearchCommentState
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordViewData
import javax.inject.Inject

Expand Down Expand Up @@ -61,11 +58,48 @@ class ChangeRecordViewDataInteractor @Inject constructor(
)
}

suspend fun getLastCommentsViewData(
// TODO replace fav to text button?
// TODO add button to hide similar comments?
suspend fun getCommentsViewData(
comment: String,
typeId: Long,
): List<ViewHolderType> {
data class Data(val timeStarted: Long, val comment: String)

val items = mutableListOf<ViewHolderType>()
val isDarkTheme = prefsInteractor.getDarkMode()
val isFavourite = favouriteCommentInteractor.get(comment) != null

ChangeRecordCommentFieldViewData(
id = 1L, // Only one at the time.
text = comment,
iconColor = if (isFavourite) {
resourceRepo.getColor(R.color.colorSecondary)
} else {
colorMapper.toInactiveColor(isDarkTheme)
}
).let(items::add)

val searchResults = if (comment.isNotEmpty()) {
recordInteractor.searchComment(comment)
.asSequence()
.sortedByDescending { it.timeStarted }
.map { it.comment }
.toSet()
.mapNotNull {
if (it == comment) return@mapNotNull null
ChangeRecordCommentViewData.Last(it)
}
.takeUnless { it.isEmpty() }
?.let {
HintViewData(
text = resourceRepo.getString(R.string.change_record_similar_comments_hint)
).let(::listOf) + it
}.orEmpty()
} else {
emptyList()
}

val favouriteComments = favouriteCommentInteractor.getAll()
.map { ChangeRecordCommentViewData.Favourite(it.comment) }
.takeUnless { it.isEmpty() }
Expand Down Expand Up @@ -95,51 +129,11 @@ class ChangeRecordViewDataInteractor @Inject constructor(
).let(::listOf) + it
}.orEmpty()

return favouriteComments + lastComments
}
items += searchResults
items += favouriteComments
items += lastComments

suspend fun getFavCommentViewData(
comment: String,
): ChangeRecordFavCommentState {
val isDarkTheme = prefsInteractor.getDarkMode()
val isFavourite = favouriteCommentInteractor.get(comment) != null

return ChangeRecordFavCommentState(
iconColor = if (isFavourite) {
resourceRepo.getColor(R.color.colorSecondary)
} else {
colorMapper.toInactiveColor(isDarkTheme)
},
isVisible = comment.isNotEmpty(),
)
}

suspend fun getSearchCommentViewData(
isEnabled: Boolean,
isLoading: Boolean,
search: String,
): ChangeRecordSearchCommentState {
val items = mutableListOf<ViewHolderType>()

ChangeRecordCommentFieldViewData(
id = 1L, // Only one at the time.
text = search,
).let(items::add)

when {
isLoading -> LoaderViewData().let(::listOf)
search.isEmpty() -> emptyList()
else -> {
recordInteractor.searchComment(search)
.sortedByDescending { it.timeStarted }
.map { ChangeRecordCommentViewData.Last(it.comment) }
}
}.let(items::addAll)

return ChangeRecordSearchCommentState(
enabled = isEnabled,
items = items,
)
return items
}

fun getTimeAdjustmentItems(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.util.simpletimetracker.feature_change_record.view

import android.content.res.ColorStateList
import android.annotation.SuppressLint
import android.view.View
import android.widget.EditText
import androidx.appcompat.widget.AppCompatTextView
import androidx.cardview.widget.CardView
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.extension.addOnBackPressedListener
Expand All @@ -28,8 +28,8 @@ import com.example.util.simpletimetracker.feature_base_adapter.hint.createHintAc
import com.example.util.simpletimetracker.feature_base_adapter.hint.createHintAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.hintBig.createHintBigAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.info.createInfoAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.loader.createLoaderAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.recordType.createRecordTypeAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.R
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordButtonAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordChangePreviewAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordCommentAdapterDelegate
Expand All @@ -45,11 +45,10 @@ import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeR
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordChooserState.State.Closed
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordChooserState.State.Comment
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordChooserState.State.Tag
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordFavCommentState
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordSearchCommentState
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordTagsViewData
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordBaseViewModel
import com.example.util.simpletimetracker.feature_views.extension.dpToPx
import com.example.util.simpletimetracker.feature_views.extension.postDelayed
import com.example.util.simpletimetracker.feature_views.extension.rotateDown
import com.example.util.simpletimetracker.feature_views.extension.rotateUp
import com.example.util.simpletimetracker.feature_views.extension.setOnClick
Expand Down Expand Up @@ -87,17 +86,13 @@ class ChangeRecordCore(
private val commentsAdapter: BaseRecyclerAdapter by lazy {
BaseRecyclerAdapter(
createHintAdapterDelegate(),
createChangeRecordCommentAdapterDelegate(viewModel::onCommentClick),
)
}
private val searchCommentsAdapter: BaseRecyclerAdapter by lazy {
BaseRecyclerAdapter(
createLoaderAdapterDelegate(),
createChangeRecordCommentFieldAdapterDelegate(
afterTextChange = viewModel::onSearchCommentChange,
onSearchClick = viewModel::onSearchCommentClick,
afterTextChange = viewModel::onCommentChange,
onFavouriteClick = viewModel::onFavouriteCommentClick,
),
createChangeRecordCommentAdapterDelegate(
onItemClick = viewModel::onCommentClick,
),
createChangeRecordCommentAdapterDelegate(viewModel::onCommentClick),
)
}
private val actionsAdapter: BaseRecyclerAdapter by lazy {
Expand Down Expand Up @@ -140,22 +135,14 @@ class ChangeRecordCore(
}
adapter = categoriesAdapter
}
rvChangeRecordLastComments.apply {
rvChangeRecordComments.apply {
layoutManager = FlexboxLayoutManager(context).apply {
flexDirection = FlexDirection.ROW
justifyContent = JustifyContent.CENTER
flexWrap = FlexWrap.WRAP
}
adapter = commentsAdapter
}
rvChangeRecordSearchComments.apply {
layoutManager = FlexboxLayoutManager(context).apply {
flexDirection = FlexDirection.ROW
justifyContent = JustifyContent.CENTER
flexWrap = FlexWrap.WRAP
}
adapter = searchCommentsAdapter
}
rvChangeRecordAction.apply {
layoutManager = LinearLayoutManagerWithExtraLayoutSpace(context)
adapter = actionsAdapter
Expand All @@ -166,9 +153,6 @@ class ChangeRecordCore(
fragment: BaseFragment<T>,
binding: ChangeRecordCoreLayoutBinding,
) = with(binding) {
etChangeRecordComment.doAfterTextChanged { viewModel.onCommentChange(it.toString()) }
btnChangeRecordFavouriteComment.setOnClick(viewModel::onFavouriteCommentClick)
btnChangeRecordSearchComment.setOnClick(viewModel::onSearchCommentClick)
fieldChangeRecordType.setOnClick(viewModel::onTypeChooserClick)
fieldChangeRecordCategory.setOnClick(viewModel::onCategoryChooserClick)
fieldChangeRecordComment.setOnClick(viewModel::onCommentChooserClick)
Expand Down Expand Up @@ -217,17 +201,8 @@ class ChangeRecordCore(
timeStartedAdjustmentItems.observe(containerChangeRecordTimeStartedAdjust.adapter::replace)
timeEndedAdjustmentItems.observe(containerChangeRecordTimeEndedAdjust.adapter::replace)
chooserState.observe { updateChooserState(it, binding) }
keyboardVisibility.observe { visible ->
if (visible) {
showKeyboard(etChangeRecordComment)
} else {
hideKeyboard()
}
}
lastComments.observe(commentsAdapter::replace)
comment.observe { updateUi(binding, it) }
favCommentViewData.observe { setFavCommentState(it, binding) }
searchCommentViewData.observe { setSearchCommentState(it, binding) }
keyboardVisibility.observe { onKeyboardVisibility(binding, it) }
comments.observe(commentsAdapter::replace)
actionsViewData.observe(::setActionsViewData)
}
}
Expand All @@ -244,14 +219,6 @@ class ChangeRecordCore(
}
}

fun updateUi(
binding: ChangeRecordCoreLayoutBinding,
comment: String,
) = with(binding) {
etChangeRecordComment.setText(comment)
etChangeRecordComment.setSelection(comment.length)
}

fun setDateTime(
state: ChangeRecordDateTimeState,
dateView: AppCompatTextView,
Expand Down Expand Up @@ -292,7 +259,7 @@ class ChangeRecordCore(
)
updateChooser<Comment>(
state = state,
chooserData = containerChangeRecordComment,
chooserData = rvChangeRecordComments,
chooserView = fieldChangeRecordComment,
chooserArrow = arrowChangeRecordComment,
)
Expand Down Expand Up @@ -330,31 +297,6 @@ class ChangeRecordCore(
actionsAdapter.replace(data)
}

private fun setFavCommentState(
data: ChangeRecordFavCommentState,
binding: ChangeRecordCoreLayoutBinding,
) {
ViewCompat.setBackgroundTintList(
binding.ivChangeRecordFavouriteComment,
ColorStateList.valueOf(data.iconColor),
)
binding.btnChangeRecordFavouriteComment.visible = data.isVisible
}

private fun setSearchCommentState(
data: ChangeRecordSearchCommentState,
binding: ChangeRecordCoreLayoutBinding,
) {
if (data.enabled) {
binding.containerChangeRecordCommentField.visible = false
binding.rvChangeRecordSearchComments.visible = true
} else {
binding.containerChangeRecordCommentField.visible = true
binding.rvChangeRecordSearchComments.visible = false
}
searchCommentsAdapter.replaceAsNew(data.items)
}

private fun setTimeEndedVisibility(
isVisible: Boolean,
binding: ChangeRecordCoreLayoutBinding,
Expand All @@ -364,6 +306,7 @@ class ChangeRecordCore(
binding.btnChangeRecordTimeEndedAdjust.isVisible = isVisible
}

@SuppressLint("SetTextI18n")
private fun updateCategories(
data: ChangeRecordTagsViewData,
binding: ChangeRecordCoreLayoutBinding,
Expand All @@ -373,6 +316,20 @@ class ChangeRecordCore(
tvChangeRecordTagPreview.text = data.selectedCount.toString()
}

private fun Fragment.onKeyboardVisibility(
binding: ChangeRecordCoreLayoutBinding,
visible: Boolean,
) {
if (visible) {
binding.rvChangeRecordComments.postDelayed(500) {
findViewById<EditText>(R.id.etChangeRecordCommentField)
?.let(::showKeyboard)
}
} else {
hideKeyboard()
}
}

private inline fun <reified T : ChangeRecordChooserState.State> updateChooser(
state: ChangeRecordChooserState,
chooserData: View,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ class ChangeRecordFragment :
override fun initViewModel() = with(binding) {
with(viewModel) {
extra = this@ChangeRecordFragment.extra
record.observeOnce(viewLifecycleOwner) {
core.updateUi(layoutChangeRecordCore, it.comment)
}
record.observe(::updatePreview)
core.initViewModel(this@ChangeRecordFragment, layoutChangeRecordCore)
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 83f829c

Please sign in to comment.