Skip to content

Commit

Permalink
fix icon category selection not working for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Nov 9, 2024
1 parent 30cb044 commit fd5295e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewbinding.ViewBinding
import com.example.util.simpletimetracker.core.R
import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.databinding.IconSelectionLayoutBinding
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewModelDelegate.IconSelectionViewModelDelegate
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionCategoryInfoViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionScrollViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionSelectorStateViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionStateViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionSwitchViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewModelDelegate.IconSelectionViewModelDelegate
import com.example.util.simpletimetracker.core.repo.DeviceRepo
import com.example.util.simpletimetracker.core.view.buttonsRowView.ButtonsRowViewData
import com.example.util.simpletimetracker.domain.model.IconEmojiType
Expand Down Expand Up @@ -91,6 +94,66 @@ object IconSelectionViewDelegate {
)
}

fun <T : ViewBinding> initViewModel(
fragment: BaseFragment<T>,
viewModel: IconSelectionViewModelDelegate,
layout: IconSelectionLayoutBinding,
iconsAdapter: BaseRecyclerAdapter,
iconCategoriesAdapter: BaseRecyclerAdapter,
iconsLayoutManager: GridLayoutManager?,
) {
fragment.initIconSelectionViewModel(
viewModel = viewModel,
layout = layout,
iconsAdapter = iconsAdapter,
iconCategoriesAdapter = iconCategoriesAdapter,
iconsLayoutManager = iconsLayoutManager,
)
}

private fun <T : ViewBinding> BaseFragment<T>.initIconSelectionViewModel(
viewModel: IconSelectionViewModelDelegate,
layout: IconSelectionLayoutBinding,
iconsAdapter: BaseRecyclerAdapter,
iconCategoriesAdapter: BaseRecyclerAdapter,
iconsLayoutManager: GridLayoutManager?,
) = with(viewModel) {
icons.observe {
updateIconsState(
state = it,
layout = layout,
iconsAdapter = iconsAdapter,
)
}
iconCategories.observe {
updateIconCategories(
data = it,
iconCategoriesAdapter = iconCategoriesAdapter,
)
}
iconsTypeViewData.observe {
updateIconsTypeViewData(
data = it,
layout = layout,
)
}
iconSelectorViewData.observe {
updateIconSelectorViewData(
data = it,
layout = layout,
)
}
expandIconTypeSwitch.observe {
updateBarExpanded(layout)
}
iconsScrollPosition.observe {
if (it is IconSelectionScrollViewData.ScrollTo) {
iconsLayoutManager?.scrollToPositionWithOffset(it.position, 0)
onScrolled()
}
}
}

fun onDestroyView(
textWatcher: TextWatcher?,
layout: IconSelectionLayoutBinding,
Expand All @@ -112,20 +175,20 @@ object IconSelectionViewDelegate {
etIconSelectionText.doAfterTextChanged { viewModel.onIconTextChange(it.toString()) }
}

fun updateBarExpanded(
private fun updateBarExpanded(
layout: IconSelectionLayoutBinding,
) = with(layout) {
appBarIconSelection.setExpanded(true)
}

fun updateIconsTypeViewData(
private fun updateIconsTypeViewData(
data: List<ViewHolderType>,
layout: IconSelectionLayoutBinding,
) = with(layout) {
btnIconSelectionSwitch.adapter.replace(data)
}

fun updateIconsState(
private fun updateIconsState(
state: IconSelectionStateViewData,
layout: IconSelectionLayoutBinding,
iconsAdapter: BaseRecyclerAdapter,
Expand All @@ -143,7 +206,7 @@ object IconSelectionViewDelegate {
}
}

fun updateIconCategories(
private fun updateIconCategories(
data: List<ViewHolderType>,
iconCategoriesAdapter: BaseRecyclerAdapter,
) {
Expand Down Expand Up @@ -199,7 +262,7 @@ object IconSelectionViewDelegate {
?.scrollFlags = scrollFlags
}

fun updateIconSelectorViewData(
private fun updateIconSelectorViewData(
data: IconSelectionSelectorStateViewData,
layout: IconSelectionLayoutBinding,
) = with(layout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.delegates.iconSelection.adapter.createIconSelectionAdapterDelegate
import com.example.util.simpletimetracker.core.delegates.iconSelection.adapter.createIconSelectionCategoryAdapterDelegate
import com.example.util.simpletimetracker.core.delegates.iconSelection.adapter.createIconSelectionCategoryInfoAdapterDelegate
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionSelectorStateViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionStateViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewDelegate.IconSelectionViewDelegate
import com.example.util.simpletimetracker.core.dialog.ColorSelectionDialogListener
import com.example.util.simpletimetracker.core.dialog.EmojiSelectionDialogListener
Expand All @@ -34,7 +32,6 @@ import com.example.util.simpletimetracker.core.view.ViewChooserStateDelegate
import com.example.util.simpletimetracker.domain.extension.orFalse
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.category.CategoryViewData
import com.example.util.simpletimetracker.feature_base_adapter.color.createColorAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.color.createColorFavouriteAdapterDelegate
Expand Down Expand Up @@ -219,11 +216,6 @@ class ChangeRecordTagFragment :
preview.observeOnce(viewLifecycleOwner, ::updateUi)
preview.observe(::updatePreview)
colors.observe(colorsAdapter::replace)
icons.observe(::updateIconsState)
iconCategories.observe(::updateIconCategories)
iconsTypeViewData.observe(::updateIconsTypeViewData)
iconSelectorViewData.observe(::updateIconSelectorViewData)
expandIconTypeSwitch.observe { updateBarExpanded() }
types.observe(::updateTypes)
defaultTypes.observe(::updateDefaultTypes)
chooserState.observe(::updateChooserState)
Expand All @@ -232,6 +224,14 @@ class ChangeRecordTagFragment :
keyboardVisibility.observe { visible ->
if (visible) showKeyboard(etChangeRecordTagName) else hideKeyboard()
}
IconSelectionViewDelegate.initViewModel(
fragment = this@ChangeRecordTagFragment,
viewModel = viewModel,
layout = containerChangeRecordTypeIcon,
iconsAdapter = iconsAdapter,
iconCategoriesAdapter = iconCategoriesAdapter,
iconsLayoutManager = iconsLayoutManager,
)
}
}

Expand Down Expand Up @@ -399,43 +399,6 @@ class ChangeRecordTagFragment :
tvChangeRecordTagDefaultTypePreview.text = data.selectedCount.toString()
}

private fun updateBarExpanded() {
IconSelectionViewDelegate.updateBarExpanded(
layout = binding.containerChangeRecordTypeIcon,
)
}

private fun updateIconsState(state: IconSelectionStateViewData) {
IconSelectionViewDelegate.updateIconsState(
state = state,
layout = binding.containerChangeRecordTypeIcon,
iconsAdapter = iconsAdapter,
)
}

private fun updateIconCategories(data: List<ViewHolderType>) {
IconSelectionViewDelegate.updateIconCategories(
data = data,
iconCategoriesAdapter = iconCategoriesAdapter,
)
}

private fun updateIconSelectorViewData(
data: IconSelectionSelectorStateViewData,
) {
IconSelectionViewDelegate.updateIconSelectorViewData(
data = data,
layout = binding.containerChangeRecordTypeIcon,
)
}

private fun updateIconsTypeViewData(data: List<ViewHolderType>) {
IconSelectionViewDelegate.updateIconsTypeViewData(
data = data,
layout = binding.containerChangeRecordTypeIcon,
)
}

private fun updateNameErrorMessage(error: String) = with(binding) {
inputChangeRecordTagName.error = error
inputChangeRecordTagName.isErrorEnabled = error.isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.delegates.iconSelection.adapter.createIconSelectionAdapterDelegate
import com.example.util.simpletimetracker.core.delegates.iconSelection.adapter.createIconSelectionCategoryAdapterDelegate
import com.example.util.simpletimetracker.core.delegates.iconSelection.adapter.createIconSelectionCategoryInfoAdapterDelegate
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionScrollViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionSelectorStateViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewData.IconSelectionStateViewData
import com.example.util.simpletimetracker.core.delegates.iconSelection.viewDelegate.IconSelectionViewDelegate
import com.example.util.simpletimetracker.core.dialog.ColorSelectionDialogListener
import com.example.util.simpletimetracker.core.dialog.DurationDialogListener
Expand All @@ -34,7 +31,6 @@ import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.core.view.ViewChooserStateDelegate
import com.example.util.simpletimetracker.domain.extension.orFalse
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.category.createCategoryAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.category.createCategoryAddAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.color.createColorAdapterDelegate
Expand Down Expand Up @@ -233,11 +229,6 @@ class ChangeRecordTypeFragment :
recordType.observeOnce(viewLifecycleOwner, ::updateUi)
recordType.observe(::updatePreview)
colors.observe(colorsAdapter::replace)
icons.observe(::updateIconsState)
iconCategories.observe(::updateIconCategories)
iconsTypeViewData.observe(::updateIconsTypeViewData)
iconSelectorViewData.observe(::updateIconSelectorViewData)
expandIconTypeSwitch.observe { updateBarExpanded() }
categories.observe(::updateCategories)
goalsViewData.observe(::updateGoalsState)
nameErrorMessage.observe(::updateNameErrorMessage)
Expand All @@ -253,12 +244,14 @@ class ChangeRecordTypeFragment :
keyboardVisibility.observe { visible ->
if (visible) showKeyboard(etChangeRecordTypeName) else hideKeyboard()
}
iconsScrollPosition.observe {
if (it is IconSelectionScrollViewData.ScrollTo) {
iconsLayoutManager?.scrollToPositionWithOffset(it.position, 0)
onScrolled()
}
}
IconSelectionViewDelegate.initViewModel(
fragment = this@ChangeRecordTypeFragment,
viewModel = viewModel,
layout = containerChangeRecordTypeIcon,
iconsAdapter = iconsAdapter,
iconCategoriesAdapter = iconCategoriesAdapter,
iconsLayoutManager = iconsLayoutManager,
)
}
}

Expand Down Expand Up @@ -441,43 +434,6 @@ class ChangeRecordTypeFragment :
tvChangeRecordTypeCategoryPreview.text = data.selectedCount.toString()
}

private fun updateBarExpanded() {
IconSelectionViewDelegate.updateBarExpanded(
layout = binding.containerChangeRecordTypeIcon,
)
}

private fun updateIconsState(state: IconSelectionStateViewData) {
IconSelectionViewDelegate.updateIconsState(
state = state,
layout = binding.containerChangeRecordTypeIcon,
iconsAdapter = iconsAdapter,
)
}

private fun updateIconCategories(data: List<ViewHolderType>) {
IconSelectionViewDelegate.updateIconCategories(
data = data,
iconCategoriesAdapter = iconCategoriesAdapter,
)
}

private fun updateIconSelectorViewData(
data: IconSelectionSelectorStateViewData,
) {
IconSelectionViewDelegate.updateIconSelectorViewData(
data = data,
layout = binding.containerChangeRecordTypeIcon,
)
}

private fun updateIconsTypeViewData(data: List<ViewHolderType>) {
IconSelectionViewDelegate.updateIconsTypeViewData(
data = data,
layout = binding.containerChangeRecordTypeIcon,
)
}

private fun updateNameErrorMessage(error: String) = with(binding) {
inputChangeRecordTypeName.error = error
inputChangeRecordTypeName.isErrorEnabled = error.isNotEmpty()
Expand Down

0 comments on commit fd5295e

Please sign in to comment.