File tree Expand file tree Collapse file tree
core/src/main/kotlin/com/flipcash/app/core
activityfeed/src/main/kotlin/com/flipcash/app/activityfeed
tokens/src/main/kotlin/com/flipcash/app/tokens Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,34 +2,12 @@ package com.flipcash.app.core.internal.updater
22
33import com.flipcash.app.core.updater.NetworkUpdater
44import com.flipcash.services.controllers.ProfileController
5- import kotlinx.coroutines.CoroutineScope
6- import kotlinx.coroutines.coroutineScope
7- import kotlinx.coroutines.launch
85import javax.inject.Inject
9- import kotlin.concurrent.fixedRateTimer
10- import kotlin.time.Duration
116
127class ProfileUpdater @Inject constructor(
138 private val profileController : ProfileController ,
149) : NetworkUpdater() {
15-
16- override fun poll (
17- key : Any? ,
18- scope : CoroutineScope ,
19- frequency : Duration ,
20- startIn : Duration
21- ) {
22- stop()
23- updater = fixedRateTimer(
24- name = " update profile" ,
25- initialDelay = startIn.inWholeMilliseconds,
26- period = frequency.inWholeMilliseconds
27- ) {
28- scope.launch {
29- coroutineScope {
30- profileController.updateUserProfile()
31- }
32- }
33- }
10+ override suspend fun doUpdate () {
11+ profileController.updateUserProfile()
3412 }
35- }
13+ }
Original file line number Diff line number Diff line change 11package com.flipcash.app.core.updater
22
33import kotlinx.coroutines.CoroutineScope
4- import java.util.Timer
4+ import kotlinx.coroutines.Job
5+ import kotlinx.coroutines.delay
6+ import kotlinx.coroutines.isActive
7+ import kotlinx.coroutines.launch
58import kotlin.time.Duration
69import kotlin.time.Duration.Companion.seconds
710
811abstract class NetworkUpdater {
9- protected var updater : Timer ? = null
12+ private var job : Job ? = null
1013
11- abstract fun poll (
14+ protected abstract suspend fun doUpdate ()
15+
16+ fun poll (
1217 key : Any? = null,
1318 scope : CoroutineScope ,
1419 frequency : Duration ,
1520 startIn : Duration = 0.seconds,
16- )
17-
18- open fun stop () {
19- updater?.cancel()
20- updater = null
21+ ) {
22+ stop()
23+ job = scope.launch {
24+ delay(startIn)
25+ while (isActive) {
26+ doUpdate()
27+ delay(frequency)
28+ }
29+ }
30+ }
2131
32+ fun stop () {
33+ job?.cancel()
34+ job = null
2235 }
23- }
36+ }
Original file line number Diff line number Diff line change 11package com.flipcash.app.activityfeed
22
33import com.flipcash.app.core.updater.NetworkUpdater
4- import kotlinx.coroutines.CoroutineScope
5- import kotlinx.coroutines.launch
64import javax.inject.Inject
7- import kotlin.concurrent.fixedRateTimer
8- import kotlin.time.Duration
95
106class ActivityFeedUpdater @Inject constructor(
117 private val coordinator : ActivityFeedCoordinator ,
128): NetworkUpdater() {
13- override fun poll (
14- key : Any? ,
15- scope : CoroutineScope ,
16- frequency : Duration ,
17- startIn : Duration ,
18- ) {
19- stop()
20- updater = fixedRateTimer(
21- name = " update activity feed" ,
22- initialDelay = startIn.inWholeMilliseconds,
23- period = frequency.inWholeMilliseconds
24- ) {
25- scope.launch {
26- coordinator.fetchSinceLatest(count = 50 )
27- }
28- }
9+ override suspend fun doUpdate () {
10+ coordinator.fetchSinceLatest(count = 50 )
2911 }
30- }
12+ }
Original file line number Diff line number Diff line change 11package com.flipcash.app.tokens
22
33import com.flipcash.app.core.updater.NetworkUpdater
4- import kotlinx.coroutines.CoroutineScope
5- import kotlinx.coroutines.launch
64import javax.inject.Inject
7- import kotlin.concurrent.fixedRateTimer
8- import kotlin.time.Duration
95
106class TokenUpdater @Inject constructor(
117 private val coordinator : TokenCoordinator ,
128) : NetworkUpdater() {
13- override fun poll (
14- key : Any? ,
15- scope : CoroutineScope ,
16- frequency : Duration ,
17- startIn : Duration ,
18- ) {
19- stop()
20- updater = fixedRateTimer(
21- name = " update tokens" ,
22- initialDelay = startIn.inWholeMilliseconds,
23- period = frequency.inWholeMilliseconds
24- ) {
25- scope.launch {
26- coordinator.update()
27- }
28- }
9+ override suspend fun doUpdate () {
10+ coordinator.update()
2911 }
30- }
12+ }
You can’t perform that action at this time.
0 commit comments