From 4d29ee0b92c1e812f220dee5d7bc4ef0615445e8 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Tue, 16 Aug 2022 21:34:29 +0200 Subject: [PATCH] Last fixes. --- .../android/HomeActivity.kt | 10 ++-- .../android/ReaderActivity.kt | 6 +- .../android/adapters/ItemCardAdapter.kt | 4 +- .../android/adapters/ItemsAdapter.kt | 4 +- .../android/background/background.kt | 8 +-- .../android/fragments/ArticleFragment.kt | 4 +- .../repository/RepositoryImpl.kt | 56 ++++++++++++------- 7 files changed, 54 insertions(+), 38 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 2d7e628..4ea2268 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 @@ -1074,7 +1074,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { CoroutineScope(Dispatchers.Main).launch { - val success = repository.markAllAsRead(items.map { it.id }) + val success = repository.markAllAsRead(items) if (success) { Toast.makeText( this@HomeActivity, @@ -1153,10 +1153,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar actions.forEach { action -> when { - action.read -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) - action.unread -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) - action.starred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) - action.unstarred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) + 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) } } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt index f4eb03d..2839263 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt @@ -112,7 +112,7 @@ class ReaderActivity : AppCompatActivity(), DIAware { private fun readItem(item: SelfossModel.Item) { if (markOnScroll) { CoroutineScope(Dispatchers.IO).launch { - repository.markAsRead(item.id) + repository.markAsRead(item) // TODO: Handle failure } } @@ -207,13 +207,13 @@ class ReaderActivity : AppCompatActivity(), DIAware { R.id.star -> { if (allItems[binding.pager.currentItem].starred) { CoroutineScope(Dispatchers.IO).launch { - repository.unstarr(allItems[binding.pager.currentItem].id) + repository.unstarr(allItems[binding.pager.currentItem]) // TODO: Handle failure } afterUnsave() } else { CoroutineScope(Dispatchers.IO).launch { - repository.starr(allItems[binding.pager.currentItem].id) + repository.starr(allItems[binding.pager.currentItem]) // TODO: Handle failure } afterSave() diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt index c0b8db6..c4a32fe 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt @@ -113,14 +113,14 @@ class ItemCardAdapter( if (c.isNetworkAvailable()) { if (item.starred) { CoroutineScope(Dispatchers.IO).launch { - repository.unstarr(item.id) + repository.unstarr(item) // TODO: Handle failure } item.starred = false binding.favButton.isSelected = false } else { CoroutineScope(Dispatchers.IO).launch { - repository.starr(item.id) + repository.starr(item) // TODO: Handle failure } item.starred = true diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt index 07d3d80..47d5128 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt @@ -79,7 +79,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte private fun readItemAtIndex(position: Int, showSnackbar: Boolean = true) { val i = items[position] CoroutineScope(Dispatchers.IO).launch { - repository.markAsRead(i.id) + repository.markAsRead(i) } if (repository.displayedItems == ItemType.UNREAD) { items.remove(i) @@ -95,7 +95,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte private fun unreadItemAtIndex(position: Int, showSnackbar: Boolean = true) { CoroutineScope(Dispatchers.IO).launch { - repository.unmarkAsRead(items[position].id) + repository.unmarkAsRead(items[position]) // Todo: SharedItems.unreadItem(app, api, db, items[position]) // TODO: update db 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 860d2c1..ddbf1d8 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 @@ -76,19 +76,19 @@ override fun doWork(): Result { actions.forEach { action -> when { action.read -> doAndReportOnFail( - repository.markAsRead(action.articleId.toInt()), + repository.markAsReadById(action.articleId.toInt()), action ) action.unread -> doAndReportOnFail( - repository.unmarkAsRead(action.articleId.toInt()), + repository.unmarkAsReadById(action.articleId.toInt()), action ) action.starred -> doAndReportOnFail( - repository.starr(action.articleId.toInt()), + repository.starrById(action.articleId.toInt()), action ) action.unstarred -> doAndReportOnFail( - repository.unstarr(action.articleId.toInt()), + repository.unstarrById(action.articleId.toInt()), action ) } 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 0deb6a1..2b9d6b2 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 @@ -169,7 +169,7 @@ class ArticleFragment : Fragment(), DIAware { R.id.unread_action -> if (context != null) { if (this@ArticleFragment.item.unread) { CoroutineScope(Dispatchers.IO).launch { - repository.markAsRead(this@ArticleFragment.item.id) + repository.markAsRead(this@ArticleFragment.item) } this@ArticleFragment.item.unread = false Toast.makeText( @@ -179,7 +179,7 @@ class ArticleFragment : Fragment(), DIAware { ).show() } else { CoroutineScope(Dispatchers.IO).launch { - repository.unmarkAsRead(this@ArticleFragment.item.id) + repository.unmarkAsRead(this@ArticleFragment.item) } this@ArticleFragment.item.unread = true Toast.makeText( 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 c9fed74..2dcaa46 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 @@ -121,54 +121,70 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return api.sources() } - suspend fun markAsRead(id: Int): Boolean { + suspend fun markAsRead(item: SelfossModel.Item): Boolean { // TODO: Check internet connection - val success = api.markAsRead(id.toString())?.isSuccess == true + val success = markAsReadById(item.id) if (success) { - markAsReadLocally(items.first {it.id == id}) + markAsReadLocally(item) } return success } - suspend fun unmarkAsRead(id: Int): Boolean { + suspend fun markAsReadById(id: Int): Boolean { // TODO: Check internet connection - val success = api.unmarkAsRead(id.toString())?.isSuccess == true + return api.markAsRead(id.toString())?.isSuccess == true + } + + suspend fun unmarkAsRead(item: SelfossModel.Item): Boolean { + val success = unmarkAsReadById(item.id) if (success) { - unmarkAsReadLocally(items.first {it.id == id}) + unmarkAsReadLocally(item) } return success } - suspend fun starr(id: Int): Boolean { + suspend fun unmarkAsReadById(id: Int): Boolean { + // TODO: Check internet connection + return api.unmarkAsRead(id.toString())?.isSuccess == true + } + + suspend fun starr(item: SelfossModel.Item): Boolean { + val success = starrById(item.id) + + if (success) { + starrLocally(item) + } + return success + } + + suspend fun starrById(id: Int): Boolean { // TODO: Check success, store in DB - val success = api.starr(id.toString())?.isSuccess == true + return api.starr(id.toString())?.isSuccess == true + } + + suspend fun unstarr(item: SelfossModel.Item): Boolean { + val success = unstarrById(item.id) if (success) { - starrLocally(items.first {it.id == id}) + unstarrLocally(item) } return success } - suspend fun unstarr(id: Int): Boolean { + suspend fun unstarrById(id: Int): Boolean { // TODO: Check internet connection - val success = api.unstarr(id.toString())?.isSuccess == true - - if (success) { - unstarrLocally(items.first {it.id == id}) - } - return success + return api.unstarr(id.toString())?.isSuccess == true } - suspend fun markAllAsRead(ids: List): Boolean { + suspend fun markAllAsRead(items: ArrayList): Boolean { // TODO: Check Internet connectivity, store in DB - val success = api.markAllAsRead(ids.map { it.toString() })?.isSuccess == true + val success = api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true if (success) { - val itemsToMark = items.filter { it.id in ids } - for (item in itemsToMark) { + for (item in items) { markAsReadLocally(item) } }