Skip to content

Commit bd27af0

Browse files
authored
Merge pull request #738 from synonymdev/fix/network-state-sharing
fix: share network callback via StateFlow
2 parents b035334 + aae7160 commit bd27af0

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

app/src/main/java/to/bitkit/repositories/ConnectivityRepo.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import kotlinx.coroutines.CoroutineDispatcher
1212
import kotlinx.coroutines.CoroutineScope
1313
import kotlinx.coroutines.SupervisorJob
1414
import kotlinx.coroutines.channels.awaitClose
15-
import kotlinx.coroutines.flow.Flow
15+
import kotlinx.coroutines.flow.SharingStarted
16+
import kotlinx.coroutines.flow.StateFlow
1617
import kotlinx.coroutines.flow.callbackFlow
1718
import kotlinx.coroutines.flow.distinctUntilChanged
1819
import kotlinx.coroutines.flow.onEach
20+
import kotlinx.coroutines.flow.stateIn
1921
import kotlinx.coroutines.launch
2022
import to.bitkit.di.BgDispatcher
2123
import to.bitkit.utils.Logger
@@ -35,7 +37,7 @@ class ConnectivityRepo @Inject constructor(
3537
private const val DELAY_MS = 250L
3638
}
3739

38-
val isOnline: Flow<ConnectivityState> = callbackFlow {
40+
val isOnline: StateFlow<ConnectivityState> = callbackFlow {
3941
var currentNetwork: Network? = null
4042
var currentCapabilities: NetworkCapabilities? = null
4143

@@ -127,7 +129,11 @@ class ConnectivityRepo @Inject constructor(
127129
}
128130
}.distinctUntilChanged().onEach { state ->
129131
Logger.debug("New network state: $state")
130-
}
132+
}.stateIn(
133+
scope = repoScope,
134+
started = SharingStarted.Eagerly,
135+
initialValue = ConnectivityState.DISCONNECTED,
136+
)
131137

132138
private fun calculateConnectivityState(capabilities: NetworkCapabilities?): ConnectivityState {
133139
if (capabilities == null) return ConnectivityState.DISCONNECTED

app/src/test/java/to/bitkit/repositories/HealthRepoTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HealthRepoTest : BaseUnitTest() {
3636

3737
@Before
3838
fun setUp() {
39-
whenever(connectivityRepo.isOnline).thenReturn(flowOf(ConnectivityState.CONNECTED))
39+
whenever(connectivityRepo.isOnline).thenReturn(MutableStateFlow(ConnectivityState.CONNECTED))
4040
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState()))
4141
whenever(blocktankRepo.blocktankState).thenReturn(MutableStateFlow(BlocktankState()))
4242
whenever(cacheStore.backupStatuses).thenReturn(flowOf(emptyMap()))
@@ -245,7 +245,7 @@ class HealthRepoTest : BaseUnitTest() {
245245

246246
@Test
247247
fun `all statuses except backup go to error when internet connectivity is off`() = test {
248-
whenever(connectivityRepo.isOnline).thenReturn(flowOf(ConnectivityState.DISCONNECTED))
248+
whenever(connectivityRepo.isOnline).thenReturn(MutableStateFlow(ConnectivityState.DISCONNECTED))
249249

250250
// Set up conditions that would normally be READY/PENDING if online
251251
val mockChannel = mock<ChannelDetails> {
@@ -317,7 +317,7 @@ class HealthRepoTest : BaseUnitTest() {
317317

318318
@Test
319319
fun `internet status maps to error when disconnected`() = test {
320-
whenever(connectivityRepo.isOnline).thenReturn(flowOf(ConnectivityState.DISCONNECTED))
320+
whenever(connectivityRepo.isOnline).thenReturn(MutableStateFlow(ConnectivityState.DISCONNECTED))
321321

322322
sut = createSut()
323323

@@ -378,7 +378,7 @@ class HealthRepoTest : BaseUnitTest() {
378378

379379
@Test
380380
fun `isOnline returns false when internet is not ready`() = test {
381-
whenever(connectivityRepo.isOnline).thenReturn(flowOf(ConnectivityState.DISCONNECTED))
381+
whenever(connectivityRepo.isOnline).thenReturn(MutableStateFlow(ConnectivityState.DISCONNECTED))
382382

383383
sut = createSut()
384384

app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.google.firebase.messaging.FirebaseMessaging
55
import com.synonym.bitkitcore.FeeRates
66
import com.synonym.bitkitcore.IBtInfo
77
import com.synonym.bitkitcore.ILspNode
8+
import kotlinx.coroutines.flow.MutableStateFlow
89
import kotlinx.coroutines.flow.flowOf
910
import kotlinx.coroutines.runBlocking
1011
import org.junit.Before
@@ -68,7 +69,7 @@ class LightningRepoTest : BaseUnitTest() {
6869
@Before
6970
fun setUp() = runBlocking {
7071
whenever(coreService.isGeoBlocked()).thenReturn(false)
71-
whenever(connectivityRepo.isOnline).thenReturn(flowOf(ConnectivityState.CONNECTED))
72+
whenever(connectivityRepo.isOnline).thenReturn(MutableStateFlow(ConnectivityState.CONNECTED))
7273
sut = LightningRepo(
7374
bgDispatcher = testDispatcher,
7475
lightningService = lightningService,
@@ -382,7 +383,7 @@ class LightningRepoTest : BaseUnitTest() {
382383
fun `sendOnChain should fail when sync is unhealthy`() = test {
383384
// Start node but make sync fail (isSyncHealthy = false)
384385
// Mock connectivity as disconnected to prevent retry loop from running indefinitely
385-
whenever(connectivityRepo.isOnline).thenReturn(flowOf(ConnectivityState.DISCONNECTED))
386+
whenever(connectivityRepo.isOnline).thenReturn(MutableStateFlow(ConnectivityState.DISCONNECTED))
386387
sut.setInitNodeLifecycleState()
387388
whenever(lightningService.node).thenReturn(mock())
388389
whenever(lightningService.setup(any(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())).thenReturn(Unit)

0 commit comments

Comments
 (0)