Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tipcontroller): reduce polling frequency to 5s from 20; don't poll forever #514

Merged
merged 1 commit into from
Aug 19, 2024
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
35 changes: 18 additions & 17 deletions api/src/main/java/com/getcode/network/TipController.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.getcode.network

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.getcode.analytics.AnalyticsService
import com.getcode.manager.SessionManager
import com.getcode.model.CodePayload
import com.getcode.model.PrefsBool
Expand All @@ -13,7 +9,6 @@ import com.getcode.model.TwitterUser
import com.getcode.network.client.Client
import com.getcode.network.client.fetchTwitterUser
import com.getcode.network.repository.BetaFlagsRepository
import com.getcode.network.repository.BetaOptions
import com.getcode.network.repository.PrefRepository
import com.getcode.network.repository.TwitterUserFetchError
import com.getcode.network.repository.base58
Expand All @@ -27,7 +22,6 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
Expand All @@ -50,7 +44,11 @@ class TipController @Inject constructor(
private val client: Client,
betaFlags: BetaFlagsRepository,
private val prefRepository: PrefRepository,
): LifecycleEventObserver {
) {

companion object {
private const val POLL_FREQUENCY_SECS = 5L
}

private var pollTimer: Timer? = null
private var lastPoll: Long = 0L
Expand All @@ -63,6 +61,8 @@ 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 @@ -84,10 +84,10 @@ class TipController @Inject constructor(
private fun startPollTimer() {
Timber.d("twitter poll start")
pollTimer?.cancel()
pollTimer = fixedRateTimer("twitterPollTimer", false, 0, 1000 * 20) {
pollTimer = fixedRateTimer("twitterPollTimer", false, 0, 1000 * POLL_FREQUENCY_SECS) {
scope.launch {
val time = System.currentTimeMillis()
val isPastThrottle = time - lastPoll > 1000 * 10 || lastPoll == 0L
val isPastThrottle = time - lastPoll > 1000 * (POLL_FREQUENCY_SECS / 2.0) || lastPoll == 0L

if (isPastThrottle) {
callForConnectedUser()
Expand All @@ -105,6 +105,8 @@ 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()
}
.onFailure {
when (it) {
Expand All @@ -125,6 +127,7 @@ class TipController @Inject constructor(
}

fun checkForConnection() {
if (confirmedConnection) return
startPollTimer()
}

Expand Down Expand Up @@ -172,15 +175,13 @@ class TipController @Inject constructor(
return null
}

private fun stopTimer() {
pollTimer?.cancel()
fun stopTimer() {
confirmedConnection = false
stopTimerInternal()
}

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_RESUME -> checkForConnection()
Lifecycle.Event.ON_STOP -> stopTimer()
else -> Unit
}
private fun stopTimerInternal() {
Timber.d("twitter poll stop")
pollTimer?.cancel()
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/getcode/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.getcode.LocalNetworkObserver
import com.getcode.LocalPhoneFormatter
import com.getcode.R
import com.getcode.analytics.AnalyticsService
import com.getcode.network.TipController
import com.getcode.network.client.Client
import com.getcode.network.exchange.Exchange
import com.getcode.ui.tips.DefinedTips
Expand Down Expand Up @@ -64,6 +65,9 @@ class MainActivity : FragmentActivity() {
@Inject
lateinit var exchange: Exchange

@Inject
lateinit var tipController: TipController

@Inject
lateinit var tipEngine: TipsEngine

Expand Down Expand Up @@ -135,11 +139,13 @@ class MainActivity : FragmentActivity() {
override fun onResume() {
super.onResume()
client.startTimer()
tipController.checkForConnection()
}

override fun onStop() {
super.onStop()
client.stopTimer()
tipController.stopTimer()
}
}

Loading