@@ -2,17 +2,29 @@ package com.getcode.network
22
33import com.getcode.manager.SessionManager
44import com.getcode.model.CodePayload
5+ import com.getcode.model.PrefsBool
56import com.getcode.model.PrefsString
67import com.getcode.model.TwitterUser
78import com.getcode.network.client.Client
89import com.getcode.network.client.fetchTwitterUser
910import com.getcode.network.repository.PrefRepository
11+ import com.getcode.utils.combine
1012import com.getcode.utils.getOrPutIfNonNull
13+ import kotlinx.coroutines.CoroutineScope
14+ import kotlinx.coroutines.Dispatchers
1115import kotlinx.coroutines.flow.Flow
16+ import kotlinx.coroutines.flow.SharingStarted
17+ import kotlinx.coroutines.flow.StateFlow
18+ import kotlinx.coroutines.flow.combine
19+ import kotlinx.coroutines.flow.filterNotNull
1220import kotlinx.coroutines.flow.map
21+ import kotlinx.coroutines.flow.stateIn
22+ import kotlinx.coroutines.launch
1323import timber.log.Timber
24+ import java.util.Timer
1425import javax.inject.Inject
1526import javax.inject.Singleton
27+ import kotlin.concurrent.fixedRateTimer
1628
1729typealias TipUser = Pair <String , CodePayload >
1830
@@ -22,9 +34,25 @@ class TipController @Inject constructor(
2234 private val prefRepository : PrefRepository ,
2335) {
2436
25- val connectedAccount: Flow <String ?> = prefRepository.observeOrDefault(PrefsString .KEY_TWITTER_USERNAME , " " )
37+ private var pollTimer: Timer ? = null
38+ private var lastPoll: Long = 0L
39+ private val scope = CoroutineScope (Dispatchers .IO )
40+
41+ val connectedAccount: StateFlow <String ?> = prefRepository.observeOrDefault(PrefsString .KEY_TWITTER_USERNAME , " " )
2642 .map {
2743 it.ifEmpty { null }
44+ }.stateIn(
45+ scope = scope,
46+ started = SharingStarted .Eagerly ,
47+ initialValue = null
48+ )
49+
50+ val showTwitterSplat: Flow <Boolean > =
51+ combine(
52+ connectedAccount,
53+ prefRepository.observeOrDefault(PrefsBool .SEEN_TIP_CARD , false )
54+ ) { connected, seen ->
55+ connected != null && ! seen
2856 }
2957
3058 var scannedUserData: TipUser ? = null
@@ -34,19 +62,37 @@ class TipController @Inject constructor(
3462
3563 private var cachedUsers = mutableMapOf<String , TwitterUser >()
3664
37- suspend fun checkForConnection () {
65+ private fun startPollTimer () {
66+ pollTimer?.cancel()
67+ pollTimer = fixedRateTimer(" twitterPollTimer" , false , 0 , 1000 * 20 ) {
68+ scope.launch {
69+ val time = System .currentTimeMillis()
70+ val isPastThrottle = time - lastPoll > 1000 * 10 || lastPoll == 0L
71+
72+ if (connectedAccount.value == null && isPastThrottle) {
73+ callForConnectedUser()
74+ lastPoll = time
75+ }
76+ }
77+ }
78+ }
79+
80+ private suspend fun callForConnectedUser () {
3881 val tipAddress = SessionManager .getOrganizer()?.primaryVault ? : return
3982 client.fetchTwitterUser(tipAddress)
4083 .onSuccess {
4184 Timber .d(" current user twitter connected @ ${it.username} " )
4285 prefRepository.set(PrefsString .KEY_TWITTER_USERNAME , it.username)
4386 }
4487 .onFailure {
45- it.printStackTrace()
4688 prefRepository.set(PrefsString .KEY_TWITTER_USERNAME , " " )
4789 }
4890 }
4991
92+ fun checkForConnection () {
93+ startPollTimer()
94+ }
95+
5096 suspend fun fetch (username : String , payload : CodePayload ) {
5197 val metadata = fetch(username)
5298 scannedUserData = username to payload
@@ -65,4 +111,12 @@ class TipController @Inject constructor(
65111 scannedUserData = null
66112 userMetadata = null
67113 }
114+
115+ fun clearTwitterSplat () {
116+ prefRepository.set(PrefsBool .SEEN_TIP_CARD , true )
117+ }
118+
119+ fun stopTimer () {
120+ pollTimer?.cancel()
121+ }
68122}
0 commit comments