Skip to content

Commit 21e3e39

Browse files
authored
Merge pull request #99 from code-payments/fix/get-kin-state-airdrop
fix(getkin): fix state issue when accepting first airdrop
2 parents 80a99de + 124b5a6 commit 21e3e39

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

app/src/main/java/com/getcode/view/main/getKin/GetKinSheetViewModel.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
2222
import io.reactivex.rxjava3.core.Completable
2323
import io.reactivex.rxjava3.schedulers.Schedulers
2424
import kotlinx.coroutines.delay
25+
import kotlinx.coroutines.flow.filter
2526
import kotlinx.coroutines.flow.filterIsInstance
2627
import kotlinx.coroutines.flow.flatMapLatest
2728
import kotlinx.coroutines.flow.launchIn
@@ -52,7 +53,7 @@ class GetKinSheetViewModel @Inject constructor(
5253
)
5354

5455
sealed interface Event {
55-
data class OnGetEligibilityChanged(val eligible: Boolean) : Event
56+
data class OnGetEligibilityChanged(val eligible: Boolean, val fromEvent: Boolean = false) : Event
5657
data class OnGiveEligibilityChanged(val eligible: Boolean) : Event
5758
data class OnLoadingChanged(val loading: Boolean) : Event
5859

@@ -103,14 +104,13 @@ class GetKinSheetViewModel @Inject constructor(
103104
}
104105
.catchSafely(
105106
action = { amount ->
106-
dispatchEvent(Event.OnKinRequestSuccessful(amount))
107-
dispatchEvent(Event.OnGetEligibilityChanged(false))
107+
dispatchEvent(Event.OnGetEligibilityChanged(eligible = false, fromEvent = true))
108108
dispatchEvent(Event.OnLoadingChanged(false))
109+
dispatchEvent(Event.OnKinRequestSuccessful(amount))
109110
},
110111
onFailure = {
111112
if (it is TransactionRepository.AirdropException.AlreadyClaimedException) {
112-
prefsRepository.set(PrefsBool.IS_ELIGIBLE_GET_FIRST_KIN_AIRDROP, false)
113-
dispatchEvent(Event.OnGetEligibilityChanged(false))
113+
dispatchEvent(Event.OnGetEligibilityChanged(eligible = false, fromEvent = true))
114114
} else {
115115
TopBarManager.showMessage(
116116
resources.getString(R.string.title_failed),
@@ -129,6 +129,13 @@ class GetKinSheetViewModel @Inject constructor(
129129
}
130130
.launchIn(viewModelScope)
131131

132+
eventFlow
133+
.filterIsInstance<Event.OnGetEligibilityChanged>()
134+
.filter { it.fromEvent }
135+
.map { it.eligible }
136+
.onEach { prefsRepository.set(PrefsBool.IS_ELIGIBLE_GET_FIRST_KIN_AIRDROP, it) }
137+
.launchIn(viewModelScope)
138+
132139
eventFlow
133140
.filterIsInstance<Event.OnKinRequestSuccessful>()
134141
.onEach { delay(300.milliseconds) }

0 commit comments

Comments
 (0)