From 2a44162c5a171ba9ca1dc988e774fda674c2e532 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sun, 21 Aug 2022 14:11:28 +0200 Subject: [PATCH] Send toast messages to the home activity on connectivity changes --- androidApp/build.gradle.kts | 5 ++-- .../android/HomeActivity.kt | 12 +++++++++ .../apps/readerforselfossv2/android/MyApp.kt | 2 ++ .../android/viewmodel/AppViewModel.kt | 25 +++++++++++++++++++ .../repository/RepositoryImpl.kt | 5 +++- 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/viewmodel/AppViewModel.kt diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 28281e1..9e42a73 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -181,8 +181,9 @@ dependencies { implementation("androidx.viewpager2:viewpager2:1.1.0-beta01") //Dependency Injection - implementation("org.kodein.di:kodein-di:7.12.0") - implementation("org.kodein.di:kodein-di-framework-android-x:7.12.0") + implementation("org.kodein.di:kodein-di:7.14.0") + implementation("org.kodein.di:kodein-di-framework-android-x:7.14.0") + implementation("org.kodein.di:kodein-di-framework-android-x-viewmodel:7.14.0") //Settings implementation("com.russhwolf:multiplatform-settings-no-arg:0.9") 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..ea1d756 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 @@ -46,6 +47,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.bottombar.removeBadge import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.android.utils.persistence.toEntity import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView +import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.utils.ItemType @@ -134,6 +136,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar override val di by closestDI() private val repository : Repository by instance() + private val viewModel : AppViewModel by instance() data class DrawerData(val tags: List?, val sources: List?) @@ -173,6 +176,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar AppDatabase::class.java, "selfoss-database" ).addMigrations(MIGRATION_1_2).addMigrations(MIGRATION_2_3).addMigrations(MIGRATION_3_4).build() + lifecycleScope.launch { + viewModel.toastMessageProvider.collect { toastMessage -> + Toast.makeText( + this@HomeActivity, + toastMessage, + Toast.LENGTH_SHORT + ).show() + } + } customTabActivityHelper = CustomTabActivityHelper() diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt index 2974263..22192f4 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt @@ -15,6 +15,7 @@ import androidx.preference.PreferenceManager import bou.amine.apps.readerforselfossv2.DI.networkModule import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth +import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel import bou.amine.apps.readerforselfossv2.repository.Repository import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions @@ -32,6 +33,7 @@ class MyApp : MultiDexApplication(), DIAware { override val di by DI.lazy { import(networkModule) bind() with singleton { Repository(instance(), instance(), ConnectivityStatus(applicationContext)) } + bindProvider { AppViewModel(repository = instance()) } } private val repository: Repository by instance() diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/viewmodel/AppViewModel.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/viewmodel/AppViewModel.kt new file mode 100644 index 0000000..2bd2d2e --- /dev/null +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/viewmodel/AppViewModel.kt @@ -0,0 +1,25 @@ +package bou.amine.apps.readerforselfossv2.android.viewmodel + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import bou.amine.apps.readerforselfossv2.repository.Repository +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.launch + +class AppViewModel(private val repository: Repository) : ViewModel() { + private val _toastMessageProvider = MutableSharedFlow() + val toastMessageProvider = _toastMessageProvider.asSharedFlow() + + init { + viewModelScope.launch { + repository.isConnectionAvailable.collect { isConnected -> + if (isConnected && repository.connectionMonitored) { + _toastMessageProvider.emit("Network connection is now available") + } else if (repository.connectionMonitored){ + _toastMessageProvider.emit("Network connection lost") + } + } + } + } +} \ No newline at end of file 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 57c702b..06639c9 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 @@ -16,7 +16,8 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails val settings = Settings() var items = ArrayList() - private val isConnectionAvailable = connectivityStatus.isNetworkConnected + val isConnectionAvailable = connectivityStatus.isNetworkConnected + var connectionMonitored = false var baseUrl = apiDetails.getBaseUrl() lateinit var dateUtils: DateUtils @@ -348,9 +349,11 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails // https://github.com/ln-12/multiplatform-connectivity-status/issues/2 fun startNetwork() { connectivityStatus.start() + connectionMonitored = true } fun stopNetwork() { + connectionMonitored = false connectivityStatus.stop() }