From 6ede718a9f9cbf7549f90ba19f94b5dcc91bb913 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Wed, 9 Jan 2019 21:28:55 +0100 Subject: [PATCH] Closes #269. --- .../bou/readerforselfoss/HomeActivity.kt | 6 +- .../readerforselfoss/adapters/ItemsAdapter.kt | 127 ++++++++++++++++-- app/src/main/res/values/strings.xml | 2 + 3 files changed, 124 insertions(+), 11 deletions(-) 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 0c027a8..3dfb1bc 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -227,7 +227,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder ): Int = - if (elementsShown != UNREAD_SHOWN) { + if (elementsShown != UNREAD_SHOWN && elementsShown != READ_SHOWN) { 0 } else { super.getSwipeDirs( @@ -250,8 +250,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { val adapter = recyclerView.adapter when (adapter) { - is ItemCardAdapter -> adapter.removeItemAtIndex(position) - is ItemListAdapter -> adapter.removeItemAtIndex(position) + is ItemCardAdapter -> adapter.handleItemAtIndex(position) + is ItemListAdapter -> adapter.handleItemAtIndex(position) } badgeNew-- diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt index ca96fae..054db7f 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt @@ -41,7 +41,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte updateItems(items) } - private fun doUnmark(i: Item, position: Int) { + private fun unmarkSnackbar(i: Item, position: Int) { val s = Snackbar .make( app.findViewById(R.id.coordLayout), @@ -71,12 +71,11 @@ abstract class ItemsAdapter : RecyclerView.Adapte } notifyItemRemoved(position) updateItems(items) - doUnmark(i, position) } }) } else { thread { - db.actionsDao().deleteReadActionForArticle(i.id) + db.actionsDao().insertAllActions(ActionEntity(i.id, false, true, false, false)) } } } @@ -87,7 +86,62 @@ abstract class ItemsAdapter : RecyclerView.Adapte s.show() } - fun removeItemAtIndex(position: Int) { + private fun markSnackbar(i: Item, position: Int) { + val s = Snackbar + .make( + app.findViewById(R.id.coordLayout), + R.string.marked_as_unread, + Snackbar.LENGTH_LONG + ) + .setAction(R.string.undo_string) { + items.add(position, i) + thread { + db.itemsDao().delete(i.toEntity()) + } + notifyItemInserted(position) + updateItems(items) + + if (app.isNetworkAccessible(null)) { + api.markItem(i.id).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + } + + override fun onFailure(call: Call, t: Throwable) { + items.remove(i) + thread { + db.itemsDao().insertAllItems(i.toEntity()) + } + notifyItemRemoved(position) + updateItems(items) + } + }) + } else { + thread { + db.actionsDao().insertAllActions(ActionEntity(i.id, true, false, false, false)) + } + } + } + + val view = s.view + val tv: TextView = view.findViewById(com.google.android.material.R.id.snackbar_text) + tv.setTextColor(Color.WHITE) + s.show() + } + + fun handleItemAtIndex(position: Int) { + val i = items[position] + + if (i.unread) { + readItemAtIndex(position) + } else { + unreadItemAtIndex(position) + } + } + + private fun readItemAtIndex(position: Int) { val i = items[position] items.remove(i) notifyItemRemoved(position) @@ -105,7 +159,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte ) { if (!response.succeeded() && debugReadingItems) { val message = - "message: ${response.message()} " + + "MARK message: ${response.message()} " + "response isSuccess: ${response.isSuccessful} " + "response code: ${response.code()} " + "response message: ${response.message()} " + @@ -116,7 +170,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte Toast.makeText(app.baseContext, message, Toast.LENGTH_LONG).show() } - doUnmark(i, position) + unmarkSnackbar(i, position) } override fun onFailure(call: Call, t: Throwable) { @@ -129,7 +183,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte app.getString(R.string.cant_mark_read), Toast.LENGTH_SHORT ).show() - items.add(i) + items.add(position, i) notifyItemInserted(position) updateItems(items) @@ -141,7 +195,64 @@ abstract class ItemsAdapter : RecyclerView.Adapte } else { thread { db.actionsDao().insertAllActions(ActionEntity(i.id, true, false, false, false)) - doUnmark(i, position) + } + } + } + + private fun unreadItemAtIndex(position: Int) { + val i = items[position] + items.remove(i) + notifyItemRemoved(position) + updateItems(items) + + thread { + db.itemsDao().insertAllItems(i.toEntity()) + } + + if (app.isNetworkAccessible(null)) { + api.unmarkItem(i.id).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (!response.succeeded() && debugReadingItems) { + val message = + "UNMARK message: ${response.message()} " + + "response isSuccess: ${response.isSuccessful} " + + "response code: ${response.code()} " + + "response message: ${response.message()} " + + "response errorBody: ${response.errorBody()?.string()} " + + "body success: ${response.body()?.success} " + + "body isSuccess: ${response.body()?.isSuccess}" + ACRA.getErrorReporter().maybeHandleSilentException(Exception(message), app) + Toast.makeText(app.baseContext, message, Toast.LENGTH_LONG).show() + } + + markSnackbar(i, position) + } + + override fun onFailure(call: Call, t: Throwable) { + if (debugReadingItems) { + ACRA.getErrorReporter().maybeHandleSilentException(t, app) + Toast.makeText(app.baseContext, t.message, Toast.LENGTH_LONG).show() + } + Toast.makeText( + app, + app.getString(R.string.cant_mark_unread), + Toast.LENGTH_SHORT + ).show() + items.add(i) + notifyItemInserted(position) + updateItems(items) + + thread { + db.itemsDao().delete(i.toEntity()) + } + } + }) + } else { + thread { + db.actionsDao().insertAllActions(ActionEntity(i.id, false, true, false, false)) } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e28001..0fe5506 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,6 +34,7 @@ "Favorites" "About" "Item read" + "Item unread" "Undo" "Log in to add sources." "Can't get sources list." @@ -82,6 +83,7 @@ Card height will be fixed Source code Can\'t mark article as read + Can\'t mark article as unread Error loading tags… Error loading sources… Filters