From 58a498868d78beae065b1a86cbdc2ed7cc8e8ebb Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 5 Oct 2021 20:21:44 +0200 Subject: [PATCH] Allow marking articles as read from the article reader (#346) * Add new items according to the selfoss id, to avoid duplicate items. * Migrate setting articles as read from ArticleFragment to SharedItems * Removed unused assertion * Allow marking articles as read from the article reader --- .../bou/readerforselfoss/HomeActivity.kt | 6 +- .../fragments/ArticleFragment.kt | 58 +++++++++---------- .../bou/readerforselfoss/utils/SharedItems.kt | 11 +++- .../drawable/ic_baseline_white_eye_24dp.xml | 5 ++ .../res/drawable/ic_fiber_new_white_24dp.xml | 5 -- app/src/main/res/menu/reader_toolbar.xml | 3 +- 6 files changed, 45 insertions(+), 43 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_white_eye_24dp.xml delete mode 100644 app/src/main/res/drawable/ic_fiber_new_white_24dp.xml diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index 65d3cd5..beb9f50 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -377,11 +377,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { handleOfflineActions() - if (markOnScroll) { - items = SharedItems.focusedItems - allItems = SharedItems.items - handleListResult() - } + getElementsAccordingToTab() } private fun getAndStoreAllItems() { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt index f3fd9a0..ed155eb 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt @@ -14,6 +14,7 @@ import android.os.Bundle import android.preference.PreferenceManager import android.view.* import android.webkit.* +import android.widget.Toast import androidx.browser.customtabs.CustomTabsIntent import com.google.android.material.floatingactionbutton.FloatingActionButton import androidx.fragment.app.Fragment @@ -28,25 +29,17 @@ import apps.amine.bou.readerforselfoss.api.mercury.MercuryApi import apps.amine.bou.readerforselfoss.api.mercury.ParsedContent import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi -import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.databinding.FragmentArticleBinding import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase -import apps.amine.bou.readerforselfoss.persistence.entities.ActionEntity import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_1_2 import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_2_3 import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_3_4 import apps.amine.bou.readerforselfoss.themes.AppColors -import apps.amine.bou.readerforselfoss.utils.Config -import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent +import apps.amine.bou.readerforselfoss.utils.* import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper import apps.amine.bou.readerforselfoss.utils.glide.loadMaybeBasicAuth import apps.amine.bou.readerforselfoss.utils.glide.getBitmapInputStream -import apps.amine.bou.readerforselfoss.utils.isEmptyOrNullOrNullString import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible -import apps.amine.bou.readerforselfoss.utils.openItemUrl -import apps.amine.bou.readerforselfoss.utils.shareLink -import apps.amine.bou.readerforselfoss.utils.sourceAndDateText -import apps.amine.bou.readerforselfoss.utils.succeeded import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.request.RequestOptions @@ -58,7 +51,6 @@ import java.net.MalformedURLException import java.net.URL import java.util.concurrent.ExecutionException import kotlin.collections.ArrayList -import kotlin.concurrent.thread class ArticleFragment : Fragment() { private lateinit var pageNumber: Number @@ -181,25 +173,33 @@ class ArticleFragment : Fragment() { false, requireActivity() ) - R.id.unread_action -> if ((context != null && requireContext().isNetworkAccessible(null)) || context == null) { - api.unmarkItem(allItems[pageNumber.toInt()].id).enqueue( - object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - } - - override fun onFailure( - call: Call, - t: Throwable - ) { - } - } - ) - } else { - thread { - db.actionsDao().insertAllActions(ActionEntity(allItems[pageNumber.toInt()].id, false, true, false, false)) + R.id.unread_action -> if (context != null) { + if (allItems[pageNumber.toInt()].unread) { + SharedItems.readItem( + context!!, + api, + db, + allItems[pageNumber.toInt()] + ) + allItems[pageNumber.toInt()].unread = false + Toast.makeText( + context, + R.string.marked_as_read, + Toast.LENGTH_LONG + ).show() + } else { + SharedItems.unreadItem( + context!!, + api, + db, + allItems[pageNumber.toInt()] + ) + allItems[pageNumber.toInt()].unread = true + Toast.makeText( + context, + R.string.marked_as_unread, + Toast.LENGTH_LONG + ).show() } } else -> Unit diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt index 32a9c34..924be56 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt @@ -72,10 +72,17 @@ object SharedItems { var badgeAll = -1 var badgeStarred = -1 + /** + * Add new items to the SharedItems list + * + * The new items are considered more updated than the ones already in the list. + * The old items present in the new list are discarded and replaced by the new ones. + * Items are compared according to the selfoss id, which should always be unique. + */ fun appendNewItems(newItems: ArrayList) { - val tmpItems = items + var tmpItems = items if (tmpItems != newItems) { - newItems.removeAll(tmpItems) + tmpItems = tmpItems.filter { item -> newItems.find { it.id == item.id } == null } as ArrayList tmpItems.addAll(newItems) items = tmpItems diff --git a/app/src/main/res/drawable/ic_baseline_white_eye_24dp.xml b/app/src/main/res/drawable/ic_baseline_white_eye_24dp.xml new file mode 100644 index 0000000..b2add52 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_white_eye_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_fiber_new_white_24dp.xml b/app/src/main/res/drawable/ic_fiber_new_white_24dp.xml deleted file mode 100644 index be966ef..0000000 --- a/app/src/main/res/drawable/ic_fiber_new_white_24dp.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - diff --git a/app/src/main/res/menu/reader_toolbar.xml b/app/src/main/res/menu/reader_toolbar.xml index 84c9687..38a1d3b 100644 --- a/app/src/main/res/menu/reader_toolbar.xml +++ b/app/src/main/res/menu/reader_toolbar.xml @@ -4,7 +4,7 @@ @@ -17,7 +17,6 @@