From e71f8718d77e604bdba4c92c937db8f2a6e2f19c Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 17 Aug 2022 20:12:45 +0200 Subject: [PATCH] Remove network checks from the home activity --- .../android/HomeActivity.kt | 141 ++++++++---------- 1 file changed, 63 insertions(+), 78 deletions(-) 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 ba31458..4cb1091 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 @@ -44,7 +44,6 @@ import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.bottombar.maybeShow 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.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.android.utils.persistence.toEntity import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView import bou.amine.apps.readerforselfossv2.repository.Repository @@ -126,7 +125,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private var recyclerAdapter: RecyclerView.Adapter<*>? = null private var fromTabShortcut: Boolean = false - private var offlineShortcut: Boolean = false private lateinit var tagsBadge: Map @@ -709,30 +707,26 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar var sources: List? fun sourcesApiCall() { - if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { - CoroutineScope(Dispatchers.Main).launch { - val response = repository.getSources() - if (response != null) { - sources = response - val apiDrawerData = DrawerData(tags, sources) - if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) { - handleDrawerData(apiDrawerData) - } - } else { - val apiDrawerData = DrawerData(tags, null) - if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) { - handleDrawerData(apiDrawerData) - } + CoroutineScope(Dispatchers.Main).launch { + val response = repository.getSources() + if (response != null) { + sources = response + val apiDrawerData = DrawerData(tags, sources) + if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) { + handleDrawerData(apiDrawerData) + } + } else { + val apiDrawerData = DrawerData(tags, null) + if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) { + handleDrawerData(apiDrawerData) } } } } - if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { - CoroutineScope(Dispatchers.IO).launch { - tags = repository.getTags() - sourcesApiCall() - } + CoroutineScope(Dispatchers.IO).launch { + tags = repository.getTags() + sourcesApiCall() } } @@ -964,10 +958,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun reloadBadges() { if (displayUnreadCount || displayAllCount) { CoroutineScope(Dispatchers.Main).launch { - if (applicationContext.isNetworkAvailable()) { - repository.reloadBadges() - reloadBadgeContent() - } + repository.reloadBadges() + reloadBadgeContent() } } } @@ -1041,61 +1033,56 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.refresh -> { - if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { - needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) { - Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show() - // TODO: Use Dispatchers.IO - CoroutineScope(Dispatchers.Main).launch { - val updatedRemote = repository.updateRemote() - if (updatedRemote) { - Toast.makeText( - this@HomeActivity, - R.string.refresh_success_response, Toast.LENGTH_LONG - ) - .show() - } else { - Toast.makeText( - this@HomeActivity, - R.string.refresh_failer_message, - Toast.LENGTH_SHORT - ).show() - } + needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) { + Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show() + // TODO: Use Dispatchers.IO + CoroutineScope(Dispatchers.Main).launch { + val updatedRemote = repository.updateRemote() + if (updatedRemote) { + // TODO: Send toast messages from the repository + Toast.makeText( + this@HomeActivity, + R.string.refresh_success_response, Toast.LENGTH_LONG + ) + .show() + } else { + Toast.makeText( + this@HomeActivity, + R.string.refresh_failer_message, + Toast.LENGTH_SHORT + ).show() } } - return true - } else { - return false } + return true } R.id.readAll -> { if (elementsShown == ItemType.UNREAD) { needsConfirmation(R.string.readAll, R.string.markall_dialog_message) { binding.swipeRefreshLayout.isRefreshing = true - if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { - CoroutineScope(Dispatchers.Main).launch { - val success = repository.markAllAsRead(items) - if (success) { - Toast.makeText( - this@HomeActivity, - R.string.all_posts_read, - Toast.LENGTH_SHORT - ).show() - tabNewBadge.removeBadge() + CoroutineScope(Dispatchers.Main).launch { + val success = repository.markAllAsRead(items) + if (success) { + Toast.makeText( + this@HomeActivity, + R.string.all_posts_read, + Toast.LENGTH_SHORT + ).show() + tabNewBadge.removeBadge() - handleDrawerItems() + handleDrawerItems() - getElementsAccordingToTab() - } else { - Toast.makeText( - this@HomeActivity, - R.string.all_posts_not_read, - Toast.LENGTH_SHORT - ).show() - } - handleListResult() - binding.swipeRefreshLayout.isRefreshing = false + getElementsAccordingToTab() + } else { + Toast.makeText( + this@HomeActivity, + R.string.all_posts_not_read, + Toast.LENGTH_SHORT + ).show() } + handleListResult() + binding.swipeRefreshLayout.isRefreshing = false } } } @@ -1147,17 +1134,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } } - if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { - CoroutineScope(Dispatchers.Main).launch { - val actions = db.actionsDao().actions() + CoroutineScope(Dispatchers.Main).launch { + val actions = db.actionsDao().actions() - actions.forEach { action -> - when { - action.read -> doAndReportOnFail(repository.markAsReadById(action.articleId.toInt()), action) - action.unread -> doAndReportOnFail(repository.unmarkAsReadById(action.articleId.toInt()), action) - action.starred -> doAndReportOnFail(repository.starrById(action.articleId.toInt()), action) - action.unstarred -> doAndReportOnFail(repository.unstarrById(action.articleId.toInt()), action) - } + actions.forEach { action -> + when { + action.read -> doAndReportOnFail(repository.markAsReadById(action.articleId.toInt()), action) + action.unread -> doAndReportOnFail(repository.unmarkAsReadById(action.articleId.toInt()), action) + action.starred -> doAndReportOnFail(repository.starrById(action.articleId.toInt()), action) + action.unstarred -> doAndReportOnFail(repository.unstarrById(action.articleId.toInt()), action) } } }