Skip to content

Commit

Permalink
fix icon text text watchers being set several times resulting in mult…
Browse files Browse the repository at this point in the history
…iple calls
  • Loading branch information
Razeeman committed Jul 14, 2024
1 parent 46e325a commit 1f6b2e9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.util.simpletimetracker.core.delegates.iconSelection.viewDele
import android.content.Context
import android.content.res.ColorStateList
import android.content.res.Resources
import android.text.TextWatcher
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.core.widget.doAfterTextChanged
Expand Down Expand Up @@ -90,11 +91,20 @@ object IconSelectionViewDelegate {
)
}

fun onDestroyView(
textWatcher: TextWatcher?,
layout: IconSelectionLayoutBinding,
) = with(layout) {
// Remove textWatcher because it will be set again on init ViewModel,
// to avoid several watcher being set on screen navigation forward and backward.
textWatcher?.let(etIconSelectionText::removeTextChangedListener)
}

fun updateUi(
icon: RecordTypeIcon?,
viewModel: IconSelectionViewModelDelegate,
layout: IconSelectionLayoutBinding,
) = with(layout) {
): TextWatcher = with(layout) {
(icon as? RecordTypeIcon.Text)?.text?.let {
etIconSelectionText.setText(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.util.simpletimetracker.feature_change_record_tag.view

import android.animation.ValueAnimator
import android.os.Bundle
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -122,8 +123,9 @@ class ChangeRecordTagFragment :
createHintBigAdapterDelegate(),
)
}
private var typeColorAnimator: ValueAnimator? = null
private var iconsLayoutManager: GridLayoutManager? = null
private var typeColorAnimator: ValueAnimator? = null
private var iconTextWatcher: TextWatcher? = null

private val params: ChangeTagData by fragmentArgumentDelegate(
key = ARGS_PARAMS, default = ChangeTagData.New(),
Expand Down Expand Up @@ -220,6 +222,14 @@ class ChangeRecordTagFragment :
}
}

override fun onDestroyView() {
IconSelectionViewDelegate.onDestroyView(
textWatcher = iconTextWatcher,
layout = binding.containerChangeRecordTypeIcon,
)
super.onDestroyView()
}

override fun onDestroy() {
typeColorAnimator?.cancel()
super.onDestroy()
Expand All @@ -240,7 +250,7 @@ class ChangeRecordTagFragment :
private fun updateUi(item: CategoryViewData.Record) = with(binding) {
etChangeRecordTagName.setText(item.name)
etChangeRecordTagName.setSelection(item.name.length)
IconSelectionViewDelegate.updateUi(
iconTextWatcher = IconSelectionViewDelegate.updateUi(
icon = item.icon,
viewModel = viewModel,
layout = containerChangeRecordTypeIcon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.util.simpletimetracker.feature_change_record_type.view

import android.animation.ValueAnimator
import android.os.Bundle
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -128,6 +129,7 @@ class ChangeRecordTypeFragment :
}
private var iconsLayoutManager: GridLayoutManager? = null
private var typeColorAnimator: ValueAnimator? = null
private var iconTextWatcher: TextWatcher? = null

private val params: ChangeRecordTypeParams by fragmentArgumentDelegate(
key = ARGS_PARAMS,
Expand Down Expand Up @@ -243,6 +245,14 @@ class ChangeRecordTypeFragment :
GoalsViewDelegate.onResume(binding.layoutChangeRecordTypeGoals)
}

override fun onDestroyView() {
IconSelectionViewDelegate.onDestroyView(
textWatcher = iconTextWatcher,
layout = binding.containerChangeRecordTypeIcon,
)
super.onDestroyView()
}

override fun onDestroy() {
typeColorAnimator?.cancel()
super.onDestroy()
Expand Down Expand Up @@ -271,7 +281,7 @@ class ChangeRecordTypeFragment :
private fun updateUi(item: RecordTypeViewData) = with(binding) {
etChangeRecordTypeName.setText(item.name)
etChangeRecordTypeName.setSelection(item.name.length)
IconSelectionViewDelegate.updateUi(
iconTextWatcher = IconSelectionViewDelegate.updateUi(
icon = item.iconId,
viewModel = viewModel,
layout = containerChangeRecordTypeIcon,
Expand Down

0 comments on commit 1f6b2e9

Please sign in to comment.