Skip to content

Commit 31116b4

Browse files
authored
Merge pull request #517 from code-payments/chore/improve-twitter-verification-polling
chore: create a tip verification state
2 parents b909806 + 01b557f commit 31116b4

4 files changed

Lines changed: 40 additions & 15 deletions

File tree

api/src/main/java/com/getcode/model/PrefBool.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ sealed class PrefsBool(val value: String) {
2525
data object IS_ELIGIBLE_GIVE_FIRST_KIN_AIRDROP: PrefsBool("is_eligible_give_first_kin_airdrop"), InternalRouting
2626
data object HAS_REMOVED_LOCAL_CURRENCY: PrefsBool("removed_local_currency"), InternalRouting
2727
data object SEEN_TIP_CARD : PrefsBool("seen_tip_card"), InternalRouting
28+
data object STARTED_TIP_CONNECT: PrefsBool("started_tip_connect"), InternalRouting
2829

2930
data object BUY_MODULE_AVAILABLE : PrefsBool("buy_module_available"), InternalRouting
3031

api/src/main/java/com/getcode/network/TipController.kt

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TipController @Inject constructor(
4747
) {
4848

4949
companion object {
50-
private const val POLL_FREQUENCY_SECS = 5L
50+
private const val POLL_FREQUENCY_LOOKING_SECS = 5L
5151
}
5252

5353
private var pollTimer: Timer? = null
@@ -61,8 +61,6 @@ class TipController @Inject constructor(
6161
var userMetadata: TwitterUser? = null
6262
private set
6363

64-
private var confirmedConnection = false
65-
6664
val connectedAccount: StateFlow<TipMetadata?> = prefRepository.observeOrDefault(PrefsString.KEY_TIP_ACCOUNT, "")
6765
.map { runCatching { Json.decodeFromString<TwitterUser>(it) }.getOrNull() }
6866
.distinctUntilChanged()
@@ -72,6 +70,14 @@ class TipController @Inject constructor(
7270
initialValue = null
7371
)
7472

73+
val verificationInProgress: StateFlow<Boolean> = prefRepository.observeOrDefault(PrefsBool.STARTED_TIP_CONNECT, false)
74+
.distinctUntilChanged()
75+
.stateIn(
76+
scope = scope,
77+
started = SharingStarted.Eagerly,
78+
initialValue = false
79+
)
80+
7581
val showTwitterSplat: Flow<Boolean> =
7682
combine(
7783
connectedAccount,
@@ -82,18 +88,22 @@ class TipController @Inject constructor(
8288
}
8389

8490
private fun startPollTimer() {
91+
if (connectedAccount.value != null) return
92+
8593
Timber.d("twitter poll start")
8694
pollTimer?.cancel()
87-
pollTimer = fixedRateTimer("twitterPollTimer", false, 0, 1000 * POLL_FREQUENCY_SECS) {
88-
scope.launch {
89-
val time = System.currentTimeMillis()
90-
val isPastThrottle = time - lastPoll > 1000 * (POLL_FREQUENCY_SECS / 2.0) || lastPoll == 0L
91-
92-
if (isPastThrottle) {
93-
callForConnectedUser()
95+
pollTimer =
96+
fixedRateTimer("twitterPollTimer", false, 0, 1000 * POLL_FREQUENCY_LOOKING_SECS) {
97+
scope.launch {
98+
val time = System.currentTimeMillis()
99+
val isPastThrottle =
100+
time - lastPoll > 1000 * (POLL_FREQUENCY_LOOKING_SECS / 2.0) || lastPoll == 0L
101+
102+
if (isPastThrottle) {
103+
callForConnectedUser()
104+
}
94105
}
95106
}
96-
}
97107
}
98108

99109
private suspend fun callForConnectedUser() {
@@ -105,8 +115,7 @@ class TipController @Inject constructor(
105115
.onSuccess {
106116
Timber.d("current user twitter connected @ ${it.username}")
107117
prefRepository.set(PrefsString.KEY_TIP_ACCOUNT, Json.encodeToString(it))
108-
confirmedConnection = true
109-
stopTimerInternal()
118+
stopTimer()
110119
}
111120
.onFailure {
112121
when (it) {
@@ -127,7 +136,12 @@ class TipController @Inject constructor(
127136
}
128137

129138
fun checkForConnection() {
130-
if (confirmedConnection) return
139+
if (!verificationInProgress.value) {
140+
scope.launch {
141+
callForConnectedUser()
142+
}
143+
return
144+
}
131145
startPollTimer()
132146
}
133147

@@ -152,6 +166,7 @@ class TipController @Inject constructor(
152166

153167
fun clearTwitterSplat() {
154168
prefRepository.set(PrefsBool.SEEN_TIP_CARD, true)
169+
endVerification()
155170
}
156171

157172
fun generateTipVerification(): String? {
@@ -175,8 +190,15 @@ class TipController @Inject constructor(
175190
return null
176191
}
177192

193+
fun startVerification() {
194+
prefRepository.set(PrefsBool.STARTED_TIP_CONNECT, true)
195+
}
196+
197+
private fun endVerification() {
198+
prefRepository.set(PrefsBool.STARTED_TIP_CONNECT, false)
199+
}
200+
178201
fun stopTimer() {
179-
confirmedConnection = false
180202
stopTimerInternal()
181203
}
182204

app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class HomeViewModel @Inject constructor(
232232
.filter { it }
233233
.onEach { delay(500) }
234234
.flatMapLatest { tipController.connectedAccount }
235+
.filter { tipController.verificationInProgress.value }
235236
.filterNotNull()
236237
.distinctUntilChanged()
237238
.filter { uiFlow.value.isCameraScanEnabled }

app/src/main/java/com/getcode/view/main/tip/TipConnectViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class TipConnectViewModel @Inject constructor(
6969
.map { IntentUtils.tweet(it) }
7070
.onEach {
7171
dispatchEvent(Event.OpenX(it))
72+
tipController.startVerification()
7273
}.launchIn(viewModelScope)
7374
}
7475

0 commit comments

Comments
 (0)