diff --git a/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagActivity.kt b/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagActivity.kt index e2589c74..a1e31ede 100644 --- a/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagActivity.kt +++ b/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagActivity.kt @@ -28,13 +28,13 @@ class ChangeTagActivity : private var _adapter: ChangeTagAdapter? = null private val adapter get() = requireNotNull(_adapter) { getString(R.string.adapter_not_initialized_error_msg) } - private val preferenceAnswers = MutableList(5) { 0 } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initAdapter() + setTripId() initPreferenceList() + observeIsButtonValid() initItemDecoration() initChangeClickListener() initBackClickListener() @@ -46,6 +46,10 @@ class ChangeTagActivity : binding.rvChangeTag.adapter = adapter } + private fun setTripId() { + tagViewModel.tripId = intent.getLongExtra(TRIP_ID, 0) + } + private fun initPreferenceList() { if (intent != null) { val styleA = intent.getIntExtra(STYLE_A, 0) @@ -64,34 +68,28 @@ class ChangeTagActivity : ) ) - preferenceAnswers[0] = styleA - preferenceAnswers[1] = styleB - preferenceAnswers[2] = styleC - preferenceAnswers[3] = styleD - preferenceAnswers[4] = styleE + tagViewModel.setDefaultPreference(styleA, styleB, styleC, styleD, styleE) } } private fun preferenceTagClickListener(item: ProfilePreferenceData, checkedIndex: Int) { - preferenceAnswers[item.number.toInt() - 1] = checkedIndex - setButtonValid() - sendPreferenceInfo() - } - - private fun setButtonValid() { - binding.btnChangeStart.isEnabled = true - binding.btnChangeStart.setTextColor( - colorOf(R.color.white_000) - ) + tagViewModel.checkIsPreferenceChange(item.number.toInt(), checkedIndex) } - private fun sendPreferenceInfo() { - tagViewModel.tripId = intent.getLongExtra(TRIP_ID, 0) - tagViewModel.styleA.value = preferenceAnswers[0] - tagViewModel.styleB.value = preferenceAnswers[1] - tagViewModel.styleC.value = preferenceAnswers[2] - tagViewModel.styleD.value = preferenceAnswers[3] - tagViewModel.styleE.value = preferenceAnswers[4] + private fun observeIsButtonValid() { + tagViewModel.isButtonValid.flowWithLifecycle(lifecycle).onEach { state -> + if (state) { + binding.btnChangeStart.isEnabled = true + binding.btnChangeStart.setTextColor( + colorOf(R.color.white_000) + ) + } else { + binding.btnChangeStart.isEnabled = false + binding.btnChangeStart.setTextColor( + colorOf(R.color.gray_200) + ) + } + }.launchIn(lifecycleScope) } private fun initItemDecoration() { diff --git a/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagViewModel.kt b/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagViewModel.kt index c3ba95c6..5eb45e66 100644 --- a/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagViewModel.kt +++ b/presentation/src/main/java/com/going/presentation/profile/participant/profiletag/changetag/ChangeTagViewModel.kt @@ -8,7 +8,9 @@ import com.going.domain.entity.request.PreferenceChangeRequestModel import com.going.domain.repository.ProfileRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import javax.inject.Inject @@ -20,13 +22,80 @@ class ChangeTagViewModel @Inject constructor( private val _preferencePatchState = MutableSharedFlow() val preferencePatchState: SharedFlow = _preferencePatchState + private val _isButtonValid = MutableStateFlow(false) + val isButtonValid: StateFlow = _isButtonValid + var tripId: Long = 0 - val styleA = MutableLiveData(0) - val styleB = MutableLiveData(0) - val styleC = MutableLiveData(0) - val styleD = MutableLiveData(0) - val styleE = MutableLiveData(0) + private val defaultStyleA = MutableLiveData(0) + private val defaultStyleB = MutableLiveData(0) + private val defaultStyleC = MutableLiveData(0) + private val defaultStyleD = MutableLiveData(0) + private val defaultStyleE = MutableLiveData(0) + + private val styleA = MutableLiveData(0) + private val styleB = MutableLiveData(0) + private val styleC = MutableLiveData(0) + private val styleD = MutableLiveData(0) + private val styleE = MutableLiveData(0) + + private var isStyleAChanged: Boolean = false + private var isStyleBChanged: Boolean = false + private var isStyleCChanged: Boolean = false + private var isStyleDChanged: Boolean = false + private var isStyleEChanged: Boolean = false + + fun setDefaultPreference(styleA: Int, styleB: Int, styleC: Int, styleD: Int, styleE: Int) { + defaultStyleA.value = styleA + this.styleA.value = styleA + + defaultStyleB.value = styleB + this.styleB.value = styleB + + defaultStyleC.value = styleC + this.styleC.value = styleC + + defaultStyleD.value = styleD + this.styleD.value = styleD + + defaultStyleE.value = styleE + this.styleE.value = styleE + } + + fun checkIsPreferenceChange(number: Int, index: Int) { + when (number) { + 1 -> { + styleA.value = index + isStyleAChanged = index != defaultStyleA.value + } + + 2 -> { + styleB.value = index + isStyleBChanged = index != defaultStyleB.value + } + + 3 -> { + styleC.value = index + isStyleCChanged = index != defaultStyleC.value + } + + 4 -> { + styleD.value = index + isStyleDChanged = index != defaultStyleD.value + } + + 5 -> { + styleE.value = index + isStyleEChanged = index != defaultStyleE.value + } + } + checkIsButtonValid() + } + + private fun checkIsButtonValid() { + _isButtonValid.value = + isStyleAChanged || isStyleBChanged || isStyleCChanged || isStyleDChanged || isStyleEChanged + } fun patchPreferenceTagToServer() { viewModelScope.launch { diff --git a/presentation/src/main/res/layout/activity_change_tag.xml b/presentation/src/main/res/layout/activity_change_tag.xml index ec146d3c..e193b149 100644 --- a/presentation/src/main/res/layout/activity_change_tag.xml +++ b/presentation/src/main/res/layout/activity_change_tag.xml @@ -76,7 +76,6 @@ android:layout_marginHorizontal="24dp" android:layout_marginBottom="22dp" android:background="@drawable/sel_rounded_corner_button" - android:enabled="false" android:outlineProvider="none" android:text="@string/change_tag_btn_change" android:textColor="@color/gray_200"