Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/src/main/java/com/getcode/model/PrefBool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sealed class PrefsBool(val value: String) {
data object IS_ELIGIBLE_GIVE_FIRST_KIN_AIRDROP: PrefsBool("is_eligible_give_first_kin_airdrop"), InternalRouting
data object HAS_REMOVED_LOCAL_CURRENCY: PrefsBool("removed_local_currency"), InternalRouting
data object SEEN_TIP_CARD : PrefsBool("seen_tip_card"), InternalRouting
data object STARTED_TIP_CONNECT: PrefsBool("started_tip_connect"), InternalRouting

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

Expand Down
52 changes: 37 additions & 15 deletions api/src/main/java/com/getcode/network/TipController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TipController @Inject constructor(
) {

companion object {
private const val POLL_FREQUENCY_SECS = 5L
private const val POLL_FREQUENCY_LOOKING_SECS = 5L
}

private var pollTimer: Timer? = null
Expand All @@ -61,8 +61,6 @@ class TipController @Inject constructor(
var userMetadata: TwitterUser? = null
private set

private var confirmedConnection = false

val connectedAccount: StateFlow<TipMetadata?> = prefRepository.observeOrDefault(PrefsString.KEY_TIP_ACCOUNT, "")
.map { runCatching { Json.decodeFromString<TwitterUser>(it) }.getOrNull() }
.distinctUntilChanged()
Expand All @@ -72,6 +70,14 @@ class TipController @Inject constructor(
initialValue = null
)

val verificationInProgress: StateFlow<Boolean> = prefRepository.observeOrDefault(PrefsBool.STARTED_TIP_CONNECT, false)
.distinctUntilChanged()
.stateIn(
scope = scope,
started = SharingStarted.Eagerly,
initialValue = false
)

val showTwitterSplat: Flow<Boolean> =
combine(
connectedAccount,
Expand All @@ -82,18 +88,22 @@ class TipController @Inject constructor(
}

private fun startPollTimer() {
if (connectedAccount.value != null) return

Timber.d("twitter poll start")
pollTimer?.cancel()
pollTimer = fixedRateTimer("twitterPollTimer", false, 0, 1000 * POLL_FREQUENCY_SECS) {
scope.launch {
val time = System.currentTimeMillis()
val isPastThrottle = time - lastPoll > 1000 * (POLL_FREQUENCY_SECS / 2.0) || lastPoll == 0L

if (isPastThrottle) {
callForConnectedUser()
pollTimer =
fixedRateTimer("twitterPollTimer", false, 0, 1000 * POLL_FREQUENCY_LOOKING_SECS) {
scope.launch {
val time = System.currentTimeMillis()
val isPastThrottle =
time - lastPoll > 1000 * (POLL_FREQUENCY_LOOKING_SECS / 2.0) || lastPoll == 0L

if (isPastThrottle) {
callForConnectedUser()
}
}
}
}
}

private suspend fun callForConnectedUser() {
Expand All @@ -105,8 +115,7 @@ class TipController @Inject constructor(
.onSuccess {
Timber.d("current user twitter connected @ ${it.username}")
prefRepository.set(PrefsString.KEY_TIP_ACCOUNT, Json.encodeToString(it))
confirmedConnection = true
stopTimerInternal()
stopTimer()
}
.onFailure {
when (it) {
Expand All @@ -127,7 +136,12 @@ class TipController @Inject constructor(
}

fun checkForConnection() {
if (confirmedConnection) return
if (!verificationInProgress.value) {
scope.launch {
callForConnectedUser()
}
return
}
startPollTimer()
}

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

fun clearTwitterSplat() {
prefRepository.set(PrefsBool.SEEN_TIP_CARD, true)
endVerification()
}

fun generateTipVerification(): String? {
Expand All @@ -175,8 +190,15 @@ class TipController @Inject constructor(
return null
}

fun startVerification() {
prefRepository.set(PrefsBool.STARTED_TIP_CONNECT, true)
}

private fun endVerification() {
prefRepository.set(PrefsBool.STARTED_TIP_CONNECT, false)
}

fun stopTimer() {
confirmedConnection = false
stopTimerInternal()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class HomeViewModel @Inject constructor(
.filter { it }
.onEach { delay(500) }
.flatMapLatest { tipController.connectedAccount }
.filter { tipController.verificationInProgress.value }
.filterNotNull()
.distinctUntilChanged()
.filter { uiFlow.value.isCameraScanEnabled }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TipConnectViewModel @Inject constructor(
.map { IntentUtils.tweet(it) }
.onEach {
dispatchEvent(Event.OpenX(it))
tipController.startVerification()
}.launchIn(viewModelScope)
}

Expand Down