From 1390ff264f0f4082ee339268c9b6e21e0b5fe714 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 17 Aug 2022 21:17:50 +0200 Subject: [PATCH] Show a message when the network connection is lost --- .../apps/readerforselfossv2/android/HomeActivity.kt | 7 +++++++ .../readerforselfossv2/repository/RepositoryImpl.kt | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index fc49968..2cb4e70 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -17,6 +17,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.SearchView import androidx.core.view.doOnNextLayout import androidx.drawerlayout.widget.DrawerLayout +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.* import androidx.room.Room import androidx.work.Constraints @@ -178,6 +179,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar dataBase = AndroidDeviceDatabase(applicationContext) + lifecycleScope.launch { + repository.toastMessageState.collect { + Toast.makeText(baseContext, it, Toast.LENGTH_SHORT).show() + } + } + handleBottomBar() handleDrawer() diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index fd921ee..78b49de 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -10,6 +10,9 @@ import com.russhwolf.settings.Settings import io.github.aakira.napier.Napier import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, connectivityStatus: ConnectivityStatus) { @@ -18,6 +21,9 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails var items = ArrayList() private val isConnectionAvailable = connectivityStatus.isNetworkConnected + private val _toastMessageState = MutableSharedFlow(0) + val toastMessageState = _toastMessageState.asSharedFlow() + var baseUrl = apiDetails.getBaseUrl() lateinit var dateUtils: DateUtils @@ -45,6 +51,13 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails updateApiVersion() dateUtils = DateUtils(apiMajorVersion) reloadBadges() + + isConnectionAvailable.asStateFlow().collect { connectionAvailable -> + if (!connectionAvailable) { + // TODO: Localize this string + _toastMessageState.emit("Network connection lost") + } + } } }