11package com.getcode.network
22
3- import androidx.lifecycle.Lifecycle
4- import androidx.lifecycle.LifecycleEventObserver
5- import androidx.lifecycle.LifecycleOwner
6- import com.getcode.analytics.AnalyticsService
73import com.getcode.manager.SessionManager
84import com.getcode.model.CodePayload
95import com.getcode.model.PrefsBool
@@ -13,7 +9,6 @@ import com.getcode.model.TwitterUser
139import com.getcode.network.client.Client
1410import com.getcode.network.client.fetchTwitterUser
1511import com.getcode.network.repository.BetaFlagsRepository
16- import com.getcode.network.repository.BetaOptions
1712import com.getcode.network.repository.PrefRepository
1813import com.getcode.network.repository.TwitterUserFetchError
1914import com.getcode.network.repository.base58
@@ -27,7 +22,6 @@ import kotlinx.coroutines.flow.SharingStarted
2722import kotlinx.coroutines.flow.StateFlow
2823import kotlinx.coroutines.flow.combine
2924import kotlinx.coroutines.flow.distinctUntilChanged
30- import kotlinx.coroutines.flow.filter
3125import kotlinx.coroutines.flow.filterNotNull
3226import kotlinx.coroutines.flow.launchIn
3327import kotlinx.coroutines.flow.map
@@ -50,7 +44,11 @@ class TipController @Inject constructor(
5044 private val client : Client ,
5145 betaFlags : BetaFlagsRepository ,
5246 private val prefRepository : PrefRepository ,
53- ): LifecycleEventObserver {
47+ ) {
48+
49+ companion object {
50+ private const val POLL_FREQUENCY_SECS = 5L
51+ }
5452
5553 private var pollTimer: Timer ? = null
5654 private var lastPoll: Long = 0L
@@ -63,6 +61,8 @@ class TipController @Inject constructor(
6361 var userMetadata: TwitterUser ? = null
6462 private set
6563
64+ private var confirmedConnection = false
65+
6666 val connectedAccount: StateFlow <TipMetadata ?> = prefRepository.observeOrDefault(PrefsString .KEY_TIP_ACCOUNT , " " )
6767 .map { runCatching { Json .decodeFromString<TwitterUser >(it) }.getOrNull() }
6868 .distinctUntilChanged()
@@ -84,10 +84,10 @@ class TipController @Inject constructor(
8484 private fun startPollTimer () {
8585 Timber .d(" twitter poll start" )
8686 pollTimer?.cancel()
87- pollTimer = fixedRateTimer(" twitterPollTimer" , false , 0 , 1000 * 20 ) {
87+ pollTimer = fixedRateTimer(" twitterPollTimer" , false , 0 , 1000 * POLL_FREQUENCY_SECS ) {
8888 scope.launch {
8989 val time = System .currentTimeMillis()
90- val isPastThrottle = time - lastPoll > 1000 * 10 || lastPoll == 0L
90+ val isPastThrottle = time - lastPoll > 1000 * ( POLL_FREQUENCY_SECS / 2.0 ) || lastPoll == 0L
9191
9292 if (isPastThrottle) {
9393 callForConnectedUser()
@@ -105,6 +105,8 @@ class TipController @Inject constructor(
105105 .onSuccess {
106106 Timber .d(" current user twitter connected @ ${it.username} " )
107107 prefRepository.set(PrefsString .KEY_TIP_ACCOUNT , Json .encodeToString(it))
108+ confirmedConnection = true
109+ stopTimerInternal()
108110 }
109111 .onFailure {
110112 when (it) {
@@ -125,6 +127,7 @@ class TipController @Inject constructor(
125127 }
126128
127129 fun checkForConnection () {
130+ if (confirmedConnection) return
128131 startPollTimer()
129132 }
130133
@@ -172,15 +175,13 @@ class TipController @Inject constructor(
172175 return null
173176 }
174177
175- private fun stopTimer () {
176- pollTimer?.cancel()
178+ fun stopTimer () {
179+ confirmedConnection = false
180+ stopTimerInternal()
177181 }
178182
179- override fun onStateChanged (source : LifecycleOwner , event : Lifecycle .Event ) {
180- when (event) {
181- Lifecycle .Event .ON_RESUME -> checkForConnection()
182- Lifecycle .Event .ON_STOP -> stopTimer()
183- else -> Unit
184- }
183+ private fun stopTimerInternal () {
184+ Timber .d(" twitter poll stop" )
185+ pollTimer?.cancel()
185186 }
186187}
0 commit comments