Skip to content

Commit d8798d9

Browse files
Merge pull request #2102 from session-foundation/fix-offline-dot
Take network connectivity into account when showing path
2 parents 4c9155e + a93f0cd commit d8798d9

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

app/src/main/java/org/session/libsession/network/onion/PathManager.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import org.thoughtcrime.securesms.api.snode.GetInfoApi
3333
import org.thoughtcrime.securesms.api.snode.SnodeApiExecutor
3434
import org.thoughtcrime.securesms.api.snode.SnodeApiRequest
3535
import org.thoughtcrime.securesms.api.snode.execute
36+
import org.thoughtcrime.securesms.util.NetworkConnectivity
3637
import java.util.concurrent.atomic.AtomicBoolean
3738
import javax.inject.Inject
3839
import javax.inject.Provider
@@ -48,6 +49,7 @@ open class PathManager @Inject constructor(
4849
private val prefs: TextSecurePreferences,
4950
private val snodeApiExecutor: Provider<SnodeApiExecutor>,
5051
private val getInfoApi: Provider<GetInfoApi>,
52+
private val networkConnectivity: NetworkConnectivity,
5153
) {
5254
companion object {
5355
private const val STRIKE_THRESHOLD = 3
@@ -75,10 +77,10 @@ open class PathManager @Inject constructor(
7577

7678
@OptIn(FlowPreview::class)
7779
val status: StateFlow<PathStatus> =
78-
combine(_paths, _isBuilding) { paths, building ->
80+
combine(_paths, _isBuilding, networkConnectivity.networkAvailable) { paths, building, hasNetwork ->
7981
when {
80-
building -> PathStatus.BUILDING
81-
paths.isEmpty() -> PathStatus.ERROR
82+
hasNetwork && building -> PathStatus.BUILDING
83+
!hasNetwork || paths.isEmpty() -> PathStatus.ERROR
8284
else -> PathStatus.READY
8385
}
8486
}

app/src/main/java/org/thoughtcrime/securesms/home/PathActivity.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.widget.TextView
1313
import androidx.annotation.ColorRes
1414
import androidx.core.content.ContextCompat
1515
import androidx.core.view.doOnLayout
16+
import androidx.core.view.isVisible
1617
import androidx.lifecycle.Lifecycle
1718
import androidx.lifecycle.lifecycleScope
1819
import androidx.lifecycle.repeatOnLifecycle
@@ -21,28 +22,24 @@ import kotlinx.coroutines.Dispatchers
2122
import kotlinx.coroutines.GlobalScope
2223
import kotlinx.coroutines.Job
2324
import kotlinx.coroutines.delay
24-
import kotlinx.coroutines.flow.Flow
2525
import kotlinx.coroutines.flow.SharingStarted
2626
import kotlinx.coroutines.flow.StateFlow
2727
import kotlinx.coroutines.flow.collectLatest
28-
import kotlinx.coroutines.flow.distinctUntilChanged
29-
import kotlinx.coroutines.flow.flow
3028
import kotlinx.coroutines.flow.map
3129
import kotlinx.coroutines.flow.mapNotNull
3230
import kotlinx.coroutines.flow.stateIn
33-
import kotlinx.coroutines.flow.withIndex
3431
import kotlinx.coroutines.isActive
3532
import kotlinx.coroutines.launch
3633
import kotlinx.coroutines.withContext
3734
import network.loki.messenger.R
3835
import network.loki.messenger.databinding.ActivityPathBinding
36+
import org.session.libsession.network.model.PathStatus
3937
import org.session.libsession.network.onion.PathManager
4038
import org.session.libsession.utilities.NonTranslatableStringConstants.APP_NAME
4139
import org.session.libsession.utilities.StringSubstitutionConstants.APP_NAME_KEY
4240
import org.session.libsession.utilities.getColorFromAttr
4341
import org.session.libsignal.utilities.Snode
4442
import org.thoughtcrime.securesms.ScreenLockActionBarActivity
45-
import org.thoughtcrime.securesms.pro.ProDetailsRepository
4643
import org.thoughtcrime.securesms.reviews.InAppReviewManager
4744
import org.thoughtcrime.securesms.ui.getSubbedString
4845
import org.thoughtcrime.securesms.ui.openUrl
@@ -131,14 +128,17 @@ class PathActivity : ScreenLockActionBarActivity() {
131128

132129
lifecycleScope.launch {
133130
repeatOnLifecycle(Lifecycle.State.STARTED) {
134-
pathState.map { it.isEmpty() }
131+
pathManager.status
132+
.map { it == PathStatus.BUILDING || it == PathStatus.ERROR }
135133
.collectLatest { isLoading ->
136-
if (isLoading) {
137-
binding.spinner.fadeIn()
138-
} else {
139-
binding.spinner.fadeOut()
140-
}
134+
if (isLoading) {
135+
binding.spinner.fadeIn()
136+
} else {
137+
binding.spinner.fadeOut()
141138
}
139+
140+
binding.pathRowsContainer.isVisible = !isLoading
141+
}
142142
}
143143
}
144144
}

app/src/test/java/org/thoughtcrime/securesms/network/PathManagerTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.thoughtcrime.securesms.network
22

33
import com.google.common.truth.Truth.assertThat
4+
import kotlinx.coroutines.flow.MutableStateFlow
45
import kotlinx.coroutines.test.runTest
56
import org.junit.Before
67
import org.junit.Rule
@@ -15,6 +16,7 @@ import org.session.libsignal.utilities.Snode
1516
import org.thoughtcrime.securesms.database.SnodeDatabase
1617
import org.thoughtcrime.securesms.database.SnodeDatabaseTest
1718
import org.thoughtcrime.securesms.util.MockLoggingRule
19+
import org.thoughtcrime.securesms.util.NetworkConnectivity
1820

1921
@RunWith(RobolectricTestRunner::class)
2022
@Config(minSdk = 36) // Setting min sdk 36 to use recent sqlite version as we use some modern features in the app code
@@ -25,9 +27,15 @@ class PathManagerTest {
2527

2628
lateinit var snodeDb: SnodeDatabase
2729

30+
private lateinit var networkConnectivity: NetworkConnectivity
31+
2832
@Before
2933
fun setUp() {
3034
snodeDb = SnodeDatabaseTest.createInMemorySnodeDatabase()
35+
36+
networkConnectivity = mock {
37+
on { networkAvailable } doReturn MutableStateFlow(true)
38+
}
3139
}
3240

3341
private fun snode(id: String): Snode =
@@ -57,6 +65,7 @@ class PathManagerTest {
5765
prefs = mock(),
5866
snodeApiExecutor = { mock() },
5967
getInfoApi = { mock() },
68+
networkConnectivity = networkConnectivity,
6069
)
6170

6271
val chosen = pm.getPath(exclude = b)
@@ -83,6 +92,7 @@ class PathManagerTest {
8392
prefs = mock(),
8493
snodeApiExecutor = { mock() },
8594
getInfoApi = { mock() },
95+
networkConnectivity = networkConnectivity,
8696
)
8797

8898
pm.handleBadSnode(snode = b, forceRemove = true)
@@ -115,6 +125,7 @@ class PathManagerTest {
115125
prefs = mock(),
116126
snodeApiExecutor = { mock() },
117127
getInfoApi = { mock() },
128+
networkConnectivity = networkConnectivity,
118129
)
119130

120131
pm.handleBadSnode(snode = b, forceRemove = true)

0 commit comments

Comments
 (0)