@@ -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
0 commit comments