From 5fe76d735e47d7d0ea12aa5970da0f218ed80650 Mon Sep 17 00:00:00 2001 From: Amine Date: Wed, 17 Oct 2018 20:19:35 +0200 Subject: [PATCH] Remiving items from the cache on swipe. --- .../bou/readerforselfoss/HomeActivity.kt | 2 ++ .../bou/readerforselfoss/ReaderActivity.kt | 26 ++++++++++++++++--- .../adapters/ItemCardAdapter.kt | 2 ++ .../adapters/ItemListAdapter.kt | 2 ++ .../readerforselfoss/adapters/ItemsAdapter.kt | 20 ++++++++++++++ .../persistence/dao/ItemsDao.kt | 6 ++++- 6 files changed, 53 insertions(+), 5 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 7973ce9..f79041d 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -1034,6 +1034,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { this, items, api, + db, customTabActivityHelper, internalBrowser, articleViewer, @@ -1050,6 +1051,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { this, items, api, + db, customTabActivityHelper, internalBrowser, articleViewer, diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 39a7a5e..3c55594 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -13,14 +13,18 @@ import android.view.Menu import android.view.MenuItem import android.view.ViewGroup import android.widget.Toast +import androidx.room.Room 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.fragments.ArticleFragment +import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase +import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_1_2 import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.themes.Toppings import apps.amine.bou.readerforselfoss.transformers.DepthPageTransformer import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException +import apps.amine.bou.readerforselfoss.utils.persistence.toEntity import apps.amine.bou.readerforselfoss.utils.succeeded import apps.amine.bou.readerforselfoss.utils.toggleStar import com.ftinc.scoop.Scoop @@ -30,6 +34,7 @@ import org.acra.ACRA import retrofit2.Call import retrofit2.Callback import retrofit2.Response +import kotlin.concurrent.thread class ReaderActivity : AppCompatActivity() { @@ -42,6 +47,8 @@ class ReaderActivity : AppCompatActivity() { private lateinit var toolbarMenu: Menu + private lateinit var db: AppDatabase + private fun showMenuItem(willAddToFavorite: Boolean) { toolbarMenu.findItem(R.id.save).isVisible = willAddToFavorite toolbarMenu.findItem(R.id.unsave).isVisible = !willAddToFavorite @@ -60,6 +67,11 @@ class ReaderActivity : AppCompatActivity() { setContentView(R.layout.activity_reader) + db = Room.databaseBuilder( + applicationContext, + AppDatabase::class.java, "selfoss-database" + ).addMigrations(MIGRATION_1_2).build() + val scoop = Scoop.getInstance() scoop.bind(this, Toppings.PRIMARY.value, toolBar) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -89,7 +101,7 @@ class ReaderActivity : AppCompatActivity() { currentItem = intent.getIntExtra("currentItem", 0) - readItem(allItems[currentItem].id) + readItem(allItems[currentItem]) pager.adapter = ScreenSlidePagerAdapter(supportFragmentManager, AppColors(this@ReaderActivity)) pager.currentItem = currentItem @@ -113,15 +125,18 @@ class ReaderActivity : AppCompatActivity() { } else { canFavorite() } - readItem(allItems[pager.currentItem].id) + readItem(allItems[pager.currentItem]) } } ) } - fun readItem(id: String) { + fun readItem(item: Item) { if (markOnScroll) { - api.markItem(id).enqueue( + thread { + db.itemsDao().delete(item.toEntity()) + } + api.markItem(item.id).enqueue( object : Callback { override fun onResponse( call: Call, @@ -145,6 +160,9 @@ class ReaderActivity : AppCompatActivity() { call: Call, t: Throwable ) { + thread { + db.itemsDao().insertAllItems(item.toEntity()) + } if (debugReadingItems) { ACRA.getErrorReporter().maybeHandleSilentException(t, this@ReaderActivity) } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index ed2d0a0..09e5967 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -14,6 +14,7 @@ import apps.amine.bou.readerforselfoss.R 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.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper @@ -38,6 +39,7 @@ class ItemCardAdapter( override val app: Activity, override var items: ArrayList, override val api: SelfossApi, + override val db: AppDatabase, private val helper: CustomTabActivityHelper, private val internalBrowser: Boolean, private val articleViewer: Boolean, diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt index 4c93235..291aff7 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt @@ -14,6 +14,7 @@ import apps.amine.bou.readerforselfoss.R 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.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper @@ -39,6 +40,7 @@ class ItemListAdapter( override val app: Activity, override var items: ArrayList, override val api: SelfossApi, + override val db: AppDatabase, private val helper: CustomTabActivityHelper, private val internalBrowser: Boolean, private val articleViewer: Boolean, 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 b428e60..d8831a3 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 @@ -10,17 +10,21 @@ import apps.amine.bou.readerforselfoss.R 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.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException +import apps.amine.bou.readerforselfoss.utils.persistence.toEntity import apps.amine.bou.readerforselfoss.utils.succeeded import org.acra.ACRA import retrofit2.Call import retrofit2.Callback import retrofit2.Response +import kotlin.concurrent.thread abstract class ItemsAdapter : RecyclerView.Adapter() { abstract var items: ArrayList abstract val api: SelfossApi + abstract val db: AppDatabase abstract val debugReadingItems: Boolean abstract val userIdentifier: String abstract val app: Activity @@ -42,6 +46,9 @@ abstract class ItemsAdapter : RecyclerView.Adapte ) .setAction(R.string.undo_string) { items.add(position, i) + thread { + db.itemsDao().insertAllItems(i.toEntity()) + } notifyItemInserted(position) updateItems(items) @@ -54,6 +61,9 @@ abstract class ItemsAdapter : RecyclerView.Adapte override fun onFailure(call: Call, t: Throwable) { items.remove(i) + thread { + db.itemsDao().delete(i.toEntity()) + } notifyItemRemoved(position) updateItems(items) doUnmark(i, position) @@ -75,6 +85,12 @@ abstract class ItemsAdapter : RecyclerView.Adapte notifyItemRemoved(position) updateItems(items) + // TODO: Handle network status. + // IF offline, delete from cached articles, and add to some table that will replay the calls on network activation. + + thread { + db.itemsDao().delete(i.toEntity()) + } api.markItem(i.id).enqueue(object : Callback { override fun onResponse( @@ -93,6 +109,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte ACRA.getErrorReporter().maybeHandleSilentException(Exception(message), app) Toast.makeText(app.baseContext, message, Toast.LENGTH_LONG).show() } + doUnmark(i, position) } @@ -110,6 +127,9 @@ abstract class ItemsAdapter : RecyclerView.Adapte notifyItemInserted(position) updateItems(items) + thread { + db.itemsDao().insertAllItems(i.toEntity()) + } } }) } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/persistence/dao/ItemsDao.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/persistence/dao/ItemsDao.kt index 8845640..dd5d34b 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/persistence/dao/ItemsDao.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/persistence/dao/ItemsDao.kt @@ -1,6 +1,7 @@ package apps.amine.bou.readerforselfoss.persistence.dao import androidx.room.Dao +import androidx.room.Delete import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query @@ -15,11 +16,14 @@ interface ItemsDao { fun items(): List @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertAllItems(vararg tags: ItemEntity) + fun insertAllItems(vararg items: ItemEntity) @Query("DELETE FROM items") fun deleteAllItems() + @Delete + fun delete(item: ItemEntity) + @Update fun updateItem(item: ItemEntity) } \ No newline at end of file