Remove network checks from the home activity

This commit is contained in:
davidoskky 2022-08-17 20:12:45 +02:00
parent c5826466ad
commit 86dbe06eaa

View File

@ -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.maybeShow
import bou.amine.apps.readerforselfossv2.android.utils.bottombar.removeBadge 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.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.toEntity
import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView
import bou.amine.apps.readerforselfossv2.repository.Repository 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 recyclerAdapter: RecyclerView.Adapter<*>? = null
private var fromTabShortcut: Boolean = false private var fromTabShortcut: Boolean = false
private var offlineShortcut: Boolean = false
private lateinit var tagsBadge: Map<Long, Int> private lateinit var tagsBadge: Map<Long, Int>
@ -689,30 +687,26 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
var sources: List<SelfossModel.Source>? var sources: List<SelfossModel.Source>?
fun sourcesApiCall() { fun sourcesApiCall() {
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.Main).launch { val response = repository.getSources()
val response = repository.getSources() if (response != null) {
if (response != null) { sources = response
sources = response val apiDrawerData = DrawerData(tags, sources)
val apiDrawerData = DrawerData(tags, sources) if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) {
if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) { handleDrawerData(apiDrawerData)
handleDrawerData(apiDrawerData) }
} } else {
} else { val apiDrawerData = DrawerData(tags, null)
val apiDrawerData = DrawerData(tags, null) if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) {
if ((maybeDrawerData != null && maybeDrawerData != apiDrawerData) || maybeDrawerData == null) { handleDrawerData(apiDrawerData)
handleDrawerData(apiDrawerData)
}
} }
} }
} }
} }
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { CoroutineScope(Dispatchers.IO).launch {
CoroutineScope(Dispatchers.IO).launch { tags = repository.getTags()
tags = repository.getTags() sourcesApiCall()
sourcesApiCall()
}
} }
} }
@ -944,10 +938,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun reloadBadges() { private fun reloadBadges() {
if (displayUnreadCount || displayAllCount) { if (displayUnreadCount || displayAllCount) {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
if (applicationContext.isNetworkAvailable()) { repository.reloadBadges()
repository.reloadBadges() reloadBadgeContent()
reloadBadgeContent()
}
} }
} }
} }
@ -1021,61 +1013,56 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.refresh -> { R.id.refresh -> {
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) {
needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) { Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show()
Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show() // TODO: Use Dispatchers.IO
// TODO: Use Dispatchers.IO CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.Main).launch { val updatedRemote = repository.updateRemote()
val updatedRemote = repository.updateRemote() if (updatedRemote) {
if (updatedRemote) { // TODO: Send toast messages from the repository
Toast.makeText( Toast.makeText(
this@HomeActivity, this@HomeActivity,
R.string.refresh_success_response, Toast.LENGTH_LONG R.string.refresh_success_response, Toast.LENGTH_LONG
) )
.show() .show()
} else { } else {
Toast.makeText( Toast.makeText(
this@HomeActivity, this@HomeActivity,
R.string.refresh_failer_message, R.string.refresh_failer_message,
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
}
} }
} }
return true
} else {
return false
} }
return true
} }
R.id.readAll -> { R.id.readAll -> {
if (elementsShown == ItemType.UNREAD) { if (elementsShown == ItemType.UNREAD) {
needsConfirmation(R.string.readAll, R.string.markall_dialog_message) { needsConfirmation(R.string.readAll, R.string.markall_dialog_message) {
binding.swipeRefreshLayout.isRefreshing = true binding.swipeRefreshLayout.isRefreshing = true
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.Main).launch { val success = repository.markAllAsRead(items)
val success = repository.markAllAsRead(items) if (success) {
if (success) { Toast.makeText(
Toast.makeText( this@HomeActivity,
this@HomeActivity, R.string.all_posts_read,
R.string.all_posts_read, Toast.LENGTH_SHORT
Toast.LENGTH_SHORT ).show()
).show() tabNewBadge.removeBadge()
tabNewBadge.removeBadge()
handleDrawerItems() handleDrawerItems()
getElementsAccordingToTab() getElementsAccordingToTab()
} else { } else {
Toast.makeText( Toast.makeText(
this@HomeActivity, this@HomeActivity,
R.string.all_posts_not_read, R.string.all_posts_not_read,
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
}
handleListResult()
binding.swipeRefreshLayout.isRefreshing = false
} }
handleListResult()
binding.swipeRefreshLayout.isRefreshing = false
} }
} }
} }
@ -1127,17 +1114,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
} }
} }
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.Main).launch { val actions = db.actionsDao().actions()
val actions = db.actionsDao().actions()
actions.forEach { action -> actions.forEach { action ->
when { when {
action.read -> doAndReportOnFail(repository.markAsReadById(action.articleId.toInt()), action) action.read -> doAndReportOnFail(repository.markAsReadById(action.articleId.toInt()), action)
action.unread -> doAndReportOnFail(repository.unmarkAsReadById(action.articleId.toInt()), action) action.unread -> doAndReportOnFail(repository.unmarkAsReadById(action.articleId.toInt()), action)
action.starred -> doAndReportOnFail(repository.starrById(action.articleId.toInt()), action) action.starred -> doAndReportOnFail(repository.starrById(action.articleId.toInt()), action)
action.unstarred -> doAndReportOnFail(repository.unstarrById(action.articleId.toInt()), action) action.unstarred -> doAndReportOnFail(repository.unstarrById(action.articleId.toInt()), action)
}
} }
} }
} }