From 2a78be69b5f41896d84cd26755561602ea93f944 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sun, 21 Aug 2022 20:38:51 +0200 Subject: [PATCH] Send toast messages at the application level and not on a per activity basis --- .../android/AddSourceActivity.kt | 13 ------------- .../readerforselfossv2/android/HomeActivity.kt | 12 ------------ .../readerforselfossv2/android/LoginActivity.kt | 14 -------------- .../apps/readerforselfossv2/android/MyApp.kt | 16 ++++++++++++++++ .../android/SourcesActivity.kt | 13 ------------- .../android/fragments/ArticleFragment.kt | 13 ------------- 6 files changed, 16 insertions(+), 65 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt index f4d79df..0edd1c2 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt @@ -6,13 +6,11 @@ import android.view.View import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout -import androidx.lifecycle.lifecycleScope import bou.amine.apps.readerforselfossv2.android.databinding.ActivityAddSourceBinding import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.isBaseUrlValid -import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel import bou.amine.apps.readerforselfossv2.repository.Repository import com.ftinc.scoop.Scoop import kotlinx.coroutines.CoroutineScope @@ -32,7 +30,6 @@ class AddSourceActivity : AppCompatActivity(), DIAware { override val di by closestDI() private val repository : Repository by instance() - private val viewModel : AppViewModel by instance() override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@AddSourceActivity) @@ -79,16 +76,6 @@ class AddSourceActivity : AppCompatActivity(), DIAware { binding.sourceUri.text.toString() ) } - - lifecycleScope.launch { - viewModel.toastMessageProvider.collect { toastMessage -> - Toast.makeText( - this@AddSourceActivity, - toastMessage, - Toast.LENGTH_SHORT - ).show() - } - } } override fun onResume() { 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 ea1d756..4934d5f 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,7 +17,6 @@ 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 @@ -136,7 +135,6 @@ 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?) @@ -176,16 +174,6 @@ 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() dataBase = AndroidDeviceDatabase(applicationContext) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt index 6c75f57..544325f 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt @@ -10,14 +10,11 @@ import android.view.MenuItem import android.view.View import android.view.inputmethod.EditorInfo import android.widget.TextView -import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity -import androidx.lifecycle.lifecycleScope import bou.amine.apps.readerforselfossv2.android.databinding.ActivityLoginBinding import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.utils.isBaseUrlValid -import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel import bou.amine.apps.readerforselfossv2.repository.Repository import com.mikepenz.aboutlibraries.LibsBuilder import com.russhwolf.settings.Settings @@ -41,7 +38,6 @@ class LoginActivity() : AppCompatActivity(), DIAware { override val di by closestDI() private val repository : Repository by instance() - private val viewModel : AppViewModel by instance() override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@LoginActivity) @@ -61,16 +57,6 @@ class LoginActivity() : AppCompatActivity(), DIAware { } handleActions() - - lifecycleScope.launch { - viewModel.toastMessageProvider.collect { toastMessage -> - Toast.makeText( - this@LoginActivity, - toastMessage, - Toast.LENGTH_SHORT - ).show() - } - } } private fun handleActions() { 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 2e95d26..50a857b 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,9 +7,11 @@ import android.graphics.drawable.Drawable import android.net.Uri import android.os.Build import android.widget.ImageView +import android.widget.Toast import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner +import androidx.lifecycle.lifecycleScope import androidx.multidex.MultiDexApplication import androidx.preference.PreferenceManager import bou.amine.apps.readerforselfossv2.DI.networkModule @@ -24,6 +26,9 @@ import com.github.ln12.library.ConnectivityStatus import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader import com.mikepenz.materialdrawer.util.DrawerImageLoader import com.russhwolf.settings.Settings +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import io.github.aakira.napier.DebugAntilog import io.github.aakira.napier.Napier import org.kodein.di.* @@ -37,6 +42,7 @@ class MyApp : MultiDexApplication(), DIAware { } private val repository: Repository by instance() + private val viewModel: AppViewModel by instance() private lateinit var config: Config private lateinit var settings : Settings @@ -55,6 +61,16 @@ class MyApp : MultiDexApplication(), DIAware { handleNotificationChannels() ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifeCycleObserver(repository)) + + CoroutineScope(Dispatchers.Main).launch { + viewModel.toastMessageProvider.collect { toastMessage -> + Toast.makeText( + applicationContext, + toastMessage, + Toast.LENGTH_SHORT + ).show() + } + } } private fun handleNotificationChannels() { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt index 2761ae6..10988e9 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt @@ -5,13 +5,11 @@ import android.content.res.ColorStateList import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity -import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import bou.amine.apps.readerforselfossv2.android.adapters.SourcesListAdapter import bou.amine.apps.readerforselfossv2.android.databinding.ActivitySourcesBinding import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings -import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel import com.ftinc.scoop.Scoop @@ -29,7 +27,6 @@ class SourcesActivity : AppCompatActivity(), DIAware { override val di by closestDI() private val repository : Repository by instance() - private val viewModel : AppViewModel by instance() override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@SourcesActivity) @@ -50,16 +47,6 @@ class SourcesActivity : AppCompatActivity(), DIAware { binding.fab.rippleColor = appColors.colorAccentDark binding.fab.backgroundTintList = ColorStateList.valueOf(appColors.colorAccent) - - lifecycleScope.launch { - viewModel.toastMessageProvider.collect { toastMessage -> - Toast.makeText( - this@SourcesActivity, - toastMessage, - Toast.LENGTH_SHORT - ).show() - } - } } override fun onStop() { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index 74d5d03..abc8f2c 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -19,7 +19,6 @@ import androidx.browser.customtabs.CustomTabsIntent import androidx.core.content.res.ResourcesCompat import androidx.core.widget.NestedScrollView import androidx.fragment.app.Fragment -import androidx.lifecycle.lifecycleScope import androidx.room.Room import bou.amine.apps.readerforselfossv2.android.ImageActivity import bou.amine.apps.readerforselfossv2.android.R @@ -36,7 +35,6 @@ import bou.amine.apps.readerforselfossv2.android.utils.* import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.android.utils.glide.getBitmapInputStream 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 bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.utils.isEmptyOrNullOrNullString @@ -81,7 +79,6 @@ class ArticleFragment : Fragment(), DIAware { override val di : DI by closestDI() private val repository: Repository by instance() - private val viewModel : AppViewModel by instance() private var settings = Settings() @@ -111,16 +108,6 @@ class ArticleFragment : Fragment(), DIAware { requireContext(), 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( - context, - toastMessage, - Toast.LENGTH_SHORT - ).show() - } - } } override fun onCreateView(