From 0cec538d18db45ecfcd6b8e268ccb0ee4ebf5762 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sat, 20 Aug 2022 12:15:36 +0200 Subject: [PATCH] Stop monitoring the network when the app goes in background --- androidApp/build.gradle.kts | 8 +++++--- .../apps/readerforselfossv2/android/MyApp.kt | 19 +++++++++++++++++++ .../android/background/background.kt | 3 ++- .../android/utils/network/NetworkUtils.kt | 2 +- .../repository/RepositoryImpl.kt | 11 +++++++++-- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index b37680b..0f12485 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -190,10 +190,12 @@ dependencies { //PhotoView implementation("com.github.chrisbanes:PhotoView:2.3.0") - implementation("androidx.core:core-ktx:1.7.0") + implementation("androidx.core:core-ktx:1.8.0") - implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.4.0") - implementation("androidx.lifecycle:lifecycle-common-java8:2.4.0") + implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.5.1") + implementation("androidx.lifecycle:lifecycle-common-java8:2.5.1") + implementation("androidx.lifecycle:lifecycle-runtime:2.5.1") + implementation("androidx.lifecycle:lifecycle-extensions:2.2.0") implementation("androidx.room:room-ktx:2.4.0-beta01") kapt("androidx.room:room-compiler:2.4.0-beta01") 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 70d94af..07345b3 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 @@ -7,6 +7,9 @@ import android.graphics.drawable.Drawable import android.net.Uri import android.os.Build import android.widget.ImageView +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ProcessLifecycleOwner import androidx.multidex.MultiDexApplication import androidx.preference.PreferenceManager import bou.amine.apps.readerforselfossv2.DI.networkModule @@ -29,6 +32,7 @@ class MyApp : MultiDexApplication(), DIAware { bind() with singleton { Repository(instance(), instance(), ConnectivityStatus(applicationContext)) } } + private val repository: Repository by instance() private lateinit var config: Config private lateinit var settings : Settings @@ -44,6 +48,8 @@ class MyApp : MultiDexApplication(), DIAware { tryToHandleBug() handleNotificationChannels() + + ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifeCycleObserver(repository)) } private fun handleNotificationChannels() { @@ -103,4 +109,17 @@ class MyApp : MultiDexApplication(), DIAware { } } } + + class AppLifeCycleObserver(val repository: Repository) : DefaultLifecycleObserver { + + override fun onResume(owner: LifecycleOwner) { + super.onResume(owner) + repository.startNetwork() + } + + override fun onPause(owner: LifecycleOwner) { + repository.stopNetwork() + super.onPause(owner) + } + } } \ No newline at end of file diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt index 3fb4d84..03a0097 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt @@ -21,6 +21,7 @@ import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATIO import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_2_3 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_3_4 import bou.amine.apps.readerforselfossv2.android.utils.Config +import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAccessible import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.utils.ItemType @@ -43,7 +44,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con override fun doWork(): Result { val settings = Settings() val periodicRefresh = settings.getBoolean("periodic_refresh", false) - if (periodicRefresh && repository.isNetworkAvailable()) { + if (periodicRefresh && isNetworkAccessible(context)) { CoroutineScope(Dispatchers.IO).launch { val notificationManager = diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/network/NetworkUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/network/NetworkUtils.kt index 2edfc1c..4463626 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/network/NetworkUtils.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/network/NetworkUtils.kt @@ -46,7 +46,7 @@ fun Context.isNetworkAvailable( return if(overrideOffline) overrideOffline else networkIsAccessible } -private fun isNetworkAccessible(context: Context): Boolean { +fun isNetworkAccessible(context: Context): Boolean { val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 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 f0f7839..2ea43c4 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 @@ -15,7 +15,7 @@ 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) { +class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, val connectivityStatus: ConnectivityStatus) { val settings = Settings() var items = ArrayList() @@ -45,7 +45,6 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails set(value) {field = if (value < 0) { 0 } else { value } } init { - connectivityStatus.start() // TODO: Dispatchers.IO not available in KMM, an alternative solution should be found CoroutineScope(Dispatchers.Main).launch { updateApiVersion() @@ -379,5 +378,13 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride + fun startNetwork() { + connectivityStatus.start() + } + + fun stopNetwork() { + connectivityStatus.stop() + } + // TODO: Handle offline actions } \ No newline at end of file