From 2547ce824aa04752b3043c6f8176e50160626d96 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Tue, 23 Aug 2022 16:19:24 +0200 Subject: [PATCH] Repository should be DB ok. --- .../android/AddSourceActivity.kt | 56 ++++--- .../android/HomeActivity.kt | 13 +- .../android/ReaderActivity.kt | 2 +- .../android/SourcesActivity.kt | 2 +- .../android/adapters/ItemCardAdapter.kt | 2 +- .../android/adapters/ItemListAdapter.kt | 2 +- .../android/adapters/ItemsAdapter.kt | 2 +- .../android/adapters/SourcesListAdapter.kt | 2 +- .../android/background/background.kt | 8 +- .../android/fragments/ArticleFragment.kt | 2 +- .../android/model/AndroidIModelUtils.kt | 2 +- .../android/model/ParecelableItem.kt | 4 +- .../android/utils/ItemsUtils.kt | 2 +- .../android/utils/LinksUtils.kt | 2 +- .../src/main/res/values-ca-rES/strings.xml | 1 + .../src/main/res/values-de-rDE/strings.xml | 1 + .../src/main/res/values-es-rES/strings.xml | 1 + .../src/main/res/values-fa-rIR/strings.xml | 1 + .../src/main/res/values-fr-rFR/strings.xml | 1 + .../src/main/res/values-gl-rES/strings.xml | 1 + .../src/main/res/values-in-rID/strings.xml | 1 + .../src/main/res/values-it-rIT/strings.xml | 1 + .../src/main/res/values-ko-rKR/strings.xml | 1 + .../src/main/res/values-nl-rNL/strings.xml | 1 + .../src/main/res/values-pt-rBR/strings.xml | 1 + .../src/main/res/values-pt-rPT/strings.xml | 1 + .../src/main/res/values-si-rLK/strings.xml | 1 + .../src/main/res/values-tr-rTR/strings.xml | 1 + .../src/main/res/values-zh-rCN/strings.xml | 1 + .../src/main/res/values-zh-rTW/strings.xml | 1 + androidApp/src/main/res/values/strings.xml | 3 +- .../utils/ModelConverters.kt | 2 +- .../model/NetworkUnavailableException.kt | 3 + .../{rest => model}/SelfossModel.kt | 2 +- .../repository/RepositoryImpl.kt | 139 +++++++++++------- .../readerforselfossv2/rest/SelfossApi.kt | 1 + .../readerforselfossv2/utils/DateUtils.kt | 2 +- .../readerforselfossv2/utils/EntityUtils.kt | 2 +- .../utils/ModelConverters.kt | 2 +- .../utils/ModelConverters.kt | 2 +- .../utils/ModelConverters.kt | 2 +- 41 files changed, 175 insertions(+), 102 deletions(-) create mode 100644 shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/NetworkUnavailableException.kt rename shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/{rest => model}/SelfossModel.kt (97%) 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 0edd1c2..7df13e1 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 @@ -11,6 +11,7 @@ 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.model.NetworkUnavailableException import bou.amine.apps.readerforselfossv2.repository.Repository import com.ftinc.scoop.Scoop import kotlinx.coroutines.CoroutineScope @@ -109,33 +110,40 @@ class AddSourceActivity : AppCompatActivity(), DIAware { } + fun handleSpoutFailure(networkIssue: Boolean = false) { + Toast.makeText( + this@AddSourceActivity, + if (networkIssue) R.string.cant_get_spouts_no_network else R.string.cant_get_spouts, + Toast.LENGTH_SHORT + ).show() + mProgress.visibility = View.GONE + } + CoroutineScope(Dispatchers.Main).launch { - val items = repository.getSpouts() - if (items != null) { + try { + val items = repository.getSpouts() + if (items != null) { + val itemsStrings = items.map { it.value.name } + for ((key, value) in items) { + spoutsKV[value.name] = key + } - val itemsStrings = items.map { it.value.name } - for ((key, value) in items) { - spoutsKV[value.name] = key + mProgress.visibility = View.GONE + formContainer.visibility = View.VISIBLE + + val spinnerArrayAdapter = + ArrayAdapter( + this@AddSourceActivity, + android.R.layout.simple_spinner_item, + itemsStrings + ) + spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) + spoutsSpinner.adapter = spinnerArrayAdapter + } else { + handleSpoutFailure() } - - mProgress.visibility = View.GONE - formContainer.visibility = View.VISIBLE - - val spinnerArrayAdapter = - ArrayAdapter( - this@AddSourceActivity, - android.R.layout.simple_spinner_item, - itemsStrings - ) - spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) - spoutsSpinner.adapter = spinnerArrayAdapter - } else { - Toast.makeText( - this@AddSourceActivity, - R.string.cant_get_spouts, - Toast.LENGTH_SHORT - ).show() - mProgress.visibility = View.GONE + } catch (e: NetworkUnavailableException) { + handleSpoutFailure(networkIssue = true) } } } 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 79ed814..217f667 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 @@ -38,10 +38,9 @@ import bou.amine.apps.readerforselfossv2.android.utils.bottombar.removeBadge import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.dao.ACTION import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.utils.ItemType import bou.amine.apps.readerforselfossv2.utils.longHash -import bou.amine.apps.readerforselfossv2.utils.toEntity import bou.amine.apps.readerforselfossv2.utils.toView import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationItem @@ -167,6 +166,16 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar handleSettings() getElementsAccordingToTab() + + + + + // TODO: items caching should be done in the background when starting the app, like background.kt does +// fetchedItems?.let { +// CoroutineScope(Dispatchers.Main).launch { +// insertDBItems(it) +// } +// } } private fun handleSwipeRefreshLayout() { 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 02e046f..0d5e899 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 @@ -16,7 +16,7 @@ import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings import bou.amine.apps.readerforselfossv2.android.utils.toggleStar import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.ftinc.scoop.Scoop import com.russhwolf.settings.Settings import kotlinx.coroutines.CoroutineScope 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 10988e9..90591e4 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 @@ -11,7 +11,7 @@ import bou.amine.apps.readerforselfossv2.android.databinding.ActivitySourcesBind import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.ftinc.scoop.Scoop import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers 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 6fbbf53..7345564 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 @@ -16,7 +16,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActiv import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import com.bumptech.glide.Glide diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt index 356b74d..6f1272b 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt @@ -13,7 +13,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActiv import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import org.kodein.di.DI 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 49c70d3..7dea71a 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 @@ -8,7 +8,7 @@ import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.utils.ItemType import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.CoroutineScope diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt index dd7d9e0..4904fa9 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt @@ -16,7 +16,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.android.utils.toTextDrawableString import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import kotlinx.coroutines.CoroutineScope 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 02ba0b1..fe51e23 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 @@ -18,7 +18,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAccessible import bou.amine.apps.readerforselfossv2.dao.ACTION import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.utils.ItemType import com.russhwolf.settings.Settings import kotlinx.coroutines.CoroutineScope @@ -82,10 +82,10 @@ override fun doWork(): Result { launch { try { - val newItems = repository.allItems(ItemType.UNREAD) + val newItems = repository.getMaxItemsForBackground(ItemType.UNREAD) handleNewItemsNotification(newItems, notifyNewItems, notificationManager) - repository.allItems(ItemType.ALL) - repository.allItems(ItemType.STARRED) + repository.getMaxItemsForBackground(ItemType.ALL) + repository.getMaxItemsForBackground(ItemType.STARRED) } catch (e: Throwable) {} } } 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 97ecee6..aecb5d1 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 @@ -31,7 +31,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActiv import bou.amine.apps.readerforselfossv2.android.utils.glide.getBitmapInputStream import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth import bou.amine.apps.readerforselfossv2.repository.Repository -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.utils.isEmptyOrNullOrNullString import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt index bff553f..f146f2a 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt @@ -4,7 +4,7 @@ import android.content.Context import android.net.Uri import android.text.Html import android.webkit.URLUtil -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.request.RequestOptions diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/ParecelableItem.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/ParecelableItem.kt index b3faf46..2904286 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/ParecelableItem.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/ParecelableItem.kt @@ -1,10 +1,8 @@ package bou.amine.apps.readerforselfossv2.android.model -import android.os.Build import android.os.Parcel import android.os.Parcelable -import androidx.annotation.RequiresApi -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.google.gson.annotations.SerializedName fun SelfossModel.Item.toParcelable() : ParecelableItem = diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt index 8ae737e..4a927c3 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt @@ -2,7 +2,7 @@ package bou.amine.apps.readerforselfossv2.android.utils import android.content.Context import bou.amine.apps.readerforselfossv2.android.model.getSourceTitle -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.parseRelativeDate diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt index 7e0200a..08856a5 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt @@ -20,7 +20,7 @@ import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.ReaderActivity import bou.amine.apps.readerforselfossv2.android.model.getLinkDecoded import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp import okhttp3.HttpUrl.Companion.toHttpUrlOrNull diff --git a/androidApp/src/main/res/values-ca-rES/strings.xml b/androidApp/src/main/res/values-ca-rES/strings.xml index 876c3ad..494e902 100644 --- a/androidApp/src/main/res/values-ca-rES/strings.xml +++ b/androidApp/src/main/res/values-ca-rES/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-de-rDE/strings.xml b/androidApp/src/main/res/values-de-rDE/strings.xml index f756edc..11dc5ff 100644 --- a/androidApp/src/main/res/values-de-rDE/strings.xml +++ b/androidApp/src/main/res/values-de-rDE/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-es-rES/strings.xml b/androidApp/src/main/res/values-es-rES/strings.xml index d70e4e0..13d609e 100644 --- a/androidApp/src/main/res/values-es-rES/strings.xml +++ b/androidApp/src/main/res/values-es-rES/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-fa-rIR/strings.xml b/androidApp/src/main/res/values-fa-rIR/strings.xml index ee845ee..89f34d9 100644 --- a/androidApp/src/main/res/values-fa-rIR/strings.xml +++ b/androidApp/src/main/res/values-fa-rIR/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-fr-rFR/strings.xml b/androidApp/src/main/res/values-fr-rFR/strings.xml index 179ff39..1535812 100644 --- a/androidApp/src/main/res/values-fr-rFR/strings.xml +++ b/androidApp/src/main/res/values-fr-rFR/strings.xml @@ -168,4 +168,5 @@ La barre sera affichée La barre sera affichée grâce au bouton Supprimer la source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-gl-rES/strings.xml b/androidApp/src/main/res/values-gl-rES/strings.xml index 5bcb627..e92fdde 100644 --- a/androidApp/src/main/res/values-gl-rES/strings.xml +++ b/androidApp/src/main/res/values-gl-rES/strings.xml @@ -168,4 +168,5 @@ A barra inferior mostrarase sempre A barra inferior pode mostrarse a través do botón flotante Eliminar fonte + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-in-rID/strings.xml b/androidApp/src/main/res/values-in-rID/strings.xml index 4f291bc..8b9716e 100644 --- a/androidApp/src/main/res/values-in-rID/strings.xml +++ b/androidApp/src/main/res/values-in-rID/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-it-rIT/strings.xml b/androidApp/src/main/res/values-it-rIT/strings.xml index 6fc9bdb..36eaebe 100644 --- a/androidApp/src/main/res/values-it-rIT/strings.xml +++ b/androidApp/src/main/res/values-it-rIT/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-ko-rKR/strings.xml b/androidApp/src/main/res/values-ko-rKR/strings.xml index ed31e83..dfc6eac 100644 --- a/androidApp/src/main/res/values-ko-rKR/strings.xml +++ b/androidApp/src/main/res/values-ko-rKR/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-nl-rNL/strings.xml b/androidApp/src/main/res/values-nl-rNL/strings.xml index e8aeeb0..f879ee5 100644 --- a/androidApp/src/main/res/values-nl-rNL/strings.xml +++ b/androidApp/src/main/res/values-nl-rNL/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-pt-rBR/strings.xml b/androidApp/src/main/res/values-pt-rBR/strings.xml index a1d9183..0068b51 100644 --- a/androidApp/src/main/res/values-pt-rBR/strings.xml +++ b/androidApp/src/main/res/values-pt-rBR/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-pt-rPT/strings.xml b/androidApp/src/main/res/values-pt-rPT/strings.xml index 72fbc2c..ec87e51 100644 --- a/androidApp/src/main/res/values-pt-rPT/strings.xml +++ b/androidApp/src/main/res/values-pt-rPT/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-si-rLK/strings.xml b/androidApp/src/main/res/values-si-rLK/strings.xml index 5fb844b..e2dd876 100644 --- a/androidApp/src/main/res/values-si-rLK/strings.xml +++ b/androidApp/src/main/res/values-si-rLK/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-tr-rTR/strings.xml b/androidApp/src/main/res/values-tr-rTR/strings.xml index 7ddd253..c8ea0b6 100644 --- a/androidApp/src/main/res/values-tr-rTR/strings.xml +++ b/androidApp/src/main/res/values-tr-rTR/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-zh-rCN/strings.xml b/androidApp/src/main/res/values-zh-rCN/strings.xml index b64ffe3..f53ae19 100644 --- a/androidApp/src/main/res/values-zh-rCN/strings.xml +++ b/androidApp/src/main/res/values-zh-rCN/strings.xml @@ -168,4 +168,5 @@ 底部栏将始终显示 底部栏可以通过浮动按钮显示 删除源 + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values-zh-rTW/strings.xml b/androidApp/src/main/res/values-zh-rTW/strings.xml index 00d788e..a9782f3 100644 --- a/androidApp/src/main/res/values-zh-rTW/strings.xml +++ b/androidApp/src/main/res/values-zh-rTW/strings.xml @@ -168,4 +168,5 @@ The bottom bar will always be displayed The bottom bar can be shown through the floating button Remove source + Can\'t get spouts list because of a network issue. diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml index a83ab53..be2052f 100644 --- a/androidApp/src/main/res/values/strings.xml +++ b/androidApp/src/main/res/values/strings.xml @@ -39,7 +39,8 @@ "Log in to add sources." "Can't get sources list." "Can't create source." - "Can't get spouts list." + "Can't get spouts list because of a network issue." + "Can't get spouts list. There may ben an api issue." "The form is not complete" "Links" "Issue Tracker" diff --git a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index 121cdb8..6c6dc63 100644 --- a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,7 +1,7 @@ package bou.amine.apps.readerforselfossv2.utils import android.text.Html -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel actual fun SelfossModel.Item.getTitleDecoded(): String { return Html.fromHtml(title).toString() diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/NetworkUnavailableException.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/NetworkUnavailableException.kt new file mode 100644 index 0000000..a51fb77 --- /dev/null +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/NetworkUnavailableException.kt @@ -0,0 +1,3 @@ +package bou.amine.apps.readerforselfossv2.model + +class NetworkUnavailableException : Exception() \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossModel.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt similarity index 97% rename from shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossModel.kt rename to shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt index b4aa311..bbeb68f 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossModel.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt @@ -1,4 +1,4 @@ -package bou.amine.apps.readerforselfossv2.rest +package bou.amine.apps.readerforselfossv2.model import kotlinx.serialization.Serializable 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 3e2ffbb..511b7d8 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 @@ -1,15 +1,11 @@ package bou.amine.apps.readerforselfossv2.repository -import bou.amine.apps.readerforselfossv2.dao.ACTION -import bou.amine.apps.readerforselfossv2.dao.ReaderForSelfossDB -import bou.amine.apps.readerforselfossv2.dao.SOURCE -import bou.amine.apps.readerforselfossv2.dao.TAG +import bou.amine.apps.readerforselfossv2.dao.* +import bou.amine.apps.readerforselfossv2.model.NetworkUnavailableException import bou.amine.apps.readerforselfossv2.rest.SelfossApi -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.service.ApiDetailsService -import bou.amine.apps.readerforselfossv2.utils.DateUtils -import bou.amine.apps.readerforselfossv2.utils.ItemType -import bou.amine.apps.readerforselfossv2.utils.toEntity +import bou.amine.apps.readerforselfossv2.utils.* import com.github.ln_12.library.ConnectivityStatus import com.russhwolf.settings.Settings import io.github.aakira.napier.Napier @@ -17,7 +13,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, val connectivityStatus: ConnectivityStatus, private val db: ReaderForSelfossDB) { +class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, private val connectivityStatus: ConnectivityStatus, private val db: ReaderForSelfossDB) { val settings = Settings() var items = ArrayList() @@ -67,11 +63,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails null ) } else { - // TODO: Get items from the database + if (itemsCaching) { + fetchedItems = getDBItems().map { it.toView() } + } } if (fetchedItems != null) { items = ArrayList(fetchedItems) + sortItems() } return items } @@ -89,17 +88,16 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails searchFilter, null ) - } else { - // TODO: Get items from the database - } + } // When using the db cache, we load everything the first time, so there should be nothing more to load. if (fetchedItems != null) { - appendItems(fetchedItems) + items.addAll(fetchedItems) + sortItems() } return items } - suspend fun allItems(itemType: ItemType): List? { + suspend fun getMaxItemsForBackground(itemType: ItemType): List? { return if (isNetworkAvailable()) { api.getItems( itemType.type, @@ -111,21 +109,11 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails null ) } else { - // TODO: Provide an error message - null + emptyList() } } - private fun appendItems(fetchedItems: List) { - // TODO: Store in DB if enabled by user - val fetchedIDS = fetchedItems.map { it.id } - val tmpItems = ArrayList(items.filterNot { it.id in fetchedIDS }) - tmpItems.addAll(fetchedItems) - sortItems(tmpItems) - items = tmpItems - } - - private fun sortItems(items: ArrayList) { + private fun sortItems() { items.sortByDescending { dateUtils.parseDate(it.datetime) } } @@ -140,38 +128,37 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails success = true } } else { - // TODO: Compute badges from database + // TODO: do this differently, because it's not efficient + val dbItems = getDBItems() + badgeUnread = dbItems.filter { item -> item.unread }.size + badgeStarred = dbItems.filter { item -> item.starred }.size + badgeAll = items.size } return success } suspend fun getTags(): List? { - // TODO: Store in DB return if (isNetworkAvailable()) { api.tags() } else { - // TODO: Compute from database - null + getDBTags().map { it.toView() } } } suspend fun getSpouts(): Map? { - // TODO: Store in DB return if (isNetworkAvailable()) { api.spouts() } else { - // TODO: Compute from database - null + throw NetworkUnavailableException() } } suspend fun getSources(): ArrayList? { - // TODO: Store in DB + return if (isNetworkAvailable()) { api.sources() } else { - // TODO: Compute from database - null + ArrayList(getDBSources().map { it.toView() }) } } @@ -184,8 +171,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun markAsReadById(id: Int): Boolean = - isNetworkAvailable() && api.markAsRead(id.toString())?.isSuccess == true + suspend fun markAsReadById(id: Int): Boolean { + return if (isNetworkAvailable()) { + api.markAsRead(id.toString())?.isSuccess == true + } else { + insertDBAction(id.toString(), read = true) + true + } + } suspend fun unmarkAsRead(item: SelfossModel.Item): Boolean { @@ -197,8 +190,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun unmarkAsReadById(id: Int): Boolean = - isNetworkAvailable() && api.unmarkAsRead(id.toString())?.isSuccess == true + suspend fun unmarkAsReadById(id: Int): Boolean { + return if (isNetworkAvailable()) { + api.unmarkAsRead(id.toString())?.isSuccess == true + } else { + insertDBAction(id.toString(), unread = true) + true + } + } suspend fun starr(item: SelfossModel.Item): Boolean { val success = starrById(item.id) @@ -209,8 +208,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun starrById(id: Int): Boolean = - isNetworkAvailable() && api.starr(id.toString())?.isSuccess == true + suspend fun starrById(id: Int): Boolean { + return if (isNetworkAvailable()) { + api.starr(id.toString())?.isSuccess == true + } else { + insertDBAction(id.toString(), starred = true) + true + } + } suspend fun unstarr(item: SelfossModel.Item): Boolean { val success = unstarrById(item.id) @@ -221,9 +226,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun unstarrById(id: Int): Boolean = - isNetworkAvailable() && api.unstarr(id.toString())?.isSuccess == true - + suspend fun unstarrById(id: Int): Boolean { + return if (isNetworkAvailable()) { + api.unstarr(id.toString())?.isSuccess == true + } else { + insertDBAction(id.toString(), starred = true) + true + } + } suspend fun markAllAsRead(items: ArrayList): Boolean { var success = false @@ -238,35 +248,47 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } private fun markAsReadLocally(item: SelfossModel.Item) { - // TODO: Mark also in the database if (item.unread) { item.unread = false badgeUnread -= 1 } + + CoroutineScope(Dispatchers.Main).launch { + updateDBItem(item) + } } private fun unmarkAsReadLocally(item: SelfossModel.Item) { - // TODO: Mark also in the database if (!item.unread) { item.unread = true badgeUnread += 1 } + + CoroutineScope(Dispatchers.Main).launch { + updateDBItem(item) + } } private fun starrLocally(item: SelfossModel.Item) { - // TODO: Mark also in the database if (!item.starred) { item.starred = true badgeStarred += 1 } + + CoroutineScope(Dispatchers.Main).launch { + updateDBItem(item) + } } private fun unstarrLocally(item: SelfossModel.Item) { - // TODO: Mark also in the database if (item.starred) { item.starred = false badgeStarred -= 1 } + + CoroutineScope(Dispatchers.Main).launch { + updateDBItem(item) + } } suspend fun createSource( @@ -292,7 +314,6 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } suspend fun deleteSource(id: Int): Boolean { - // TODO: Store in DB var success = false if (isNetworkAvailable()) { val response = api.deleteSource(id) @@ -376,4 +397,20 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } } } + + private fun insertDBItems(items: List) { + db.itemsQueries.transaction { + items.forEach { item -> + db.itemsQueries.insertItem(item.toEntity()) + } + } + } + + private fun getDBItems(): List = db.itemsQueries.items().executeAsList() + + private fun insertDBAction(articleid: String, read: Boolean = false, unread: Boolean = false, starred: Boolean = false, unstarred: Boolean = false) = + db.actionsQueries.insertAction(articleid, read, unread, starred, unstarred) + + private fun updateDBItem(item: SelfossModel.Item) = + db.itemsQueries.updateItem(item.datetime, item.getTitleDecoded(), item.content, item.unread, item.starred, item.thumbnail, item.icon, item.link, item.sourcetitle, item.tags.joinToString(","), item.id.toString()) } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt index 90ba822..0047453 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt @@ -1,5 +1,6 @@ package bou.amine.apps.readerforselfossv2.rest +import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import io.ktor.client.* import io.ktor.client.call.* diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt index 16b95c2..2c58342 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt @@ -1,6 +1,6 @@ package bou.amine.apps.readerforselfossv2.utils -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel fun SelfossModel.Item.parseDate(dateUtils: DateUtils): Long = diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt index ac56c57..5269ba7 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt @@ -3,7 +3,7 @@ package bou.amine.apps.readerforselfossv2.utils import bou.amine.apps.readerforselfossv2.dao.ITEM import bou.amine.apps.readerforselfossv2.dao.SOURCE import bou.amine.apps.readerforselfossv2.dao.TAG -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel fun TAG.toView(): SelfossModel.Tag = SelfossModel.Tag( diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index 804e05d..881c1f1 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,6 +1,6 @@ package bou.amine.apps.readerforselfossv2.utils -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel expect fun SelfossModel.Source.getTitleDecoded(): String expect fun SelfossModel.Item.getTitleDecoded(): String diff --git a/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index 9d82531..e40e4e1 100644 --- a/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,6 +1,6 @@ package bou.amine.apps.readerforselfossv2.utils -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel actual fun SelfossModel.Item.getTitleDecoded(): String { TODO("Not yet implemented") diff --git a/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index 9d82531..e40e4e1 100644 --- a/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,6 +1,6 @@ package bou.amine.apps.readerforselfossv2.utils -import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.model.SelfossModel actual fun SelfossModel.Item.getTitleDecoded(): String { TODO("Not yet implemented")