Store items in a public object (#343)
* Added Object SharedItems to store all the articles in one class and allow sharing the data among activities * Introduced functions to set articles as read globally * Start migration of items into SharedItems
This commit is contained in:
parent
6e3381fb61
commit
33fb04956c
@ -39,6 +39,7 @@ import apps.amine.bou.readerforselfoss.settings.SettingsActivity
|
||||
import apps.amine.bou.readerforselfoss.themes.AppColors
|
||||
import apps.amine.bou.readerforselfoss.themes.Toppings
|
||||
import apps.amine.bou.readerforselfoss.utils.Config
|
||||
import apps.amine.bou.readerforselfoss.utils.SharedItems
|
||||
import apps.amine.bou.readerforselfoss.utils.bottombar.maybeShow
|
||||
import apps.amine.bou.readerforselfoss.utils.bottombar.removeBadge
|
||||
import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper
|
||||
@ -384,7 +385,9 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
handleOfflineActions()
|
||||
|
||||
if (markOnScroll) {
|
||||
getElementsAccordingToTab()
|
||||
items = SharedItems.focusedItems
|
||||
allItems = SharedItems.items
|
||||
handleListResult()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1096,6 +1099,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
if (!allItems.contains(it)) allItems.add(it)
|
||||
}
|
||||
}
|
||||
SharedItems.focusedItems = items
|
||||
SharedItems.items = allItems
|
||||
}
|
||||
} else {
|
||||
if (!appendResults) {
|
||||
@ -1234,7 +1239,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
binding.recyclerView.adapter = recyclerAdapter
|
||||
} else {
|
||||
if (!appendResults) {
|
||||
(recyclerAdapter as ItemsAdapter<*>).updateAllItems(items)
|
||||
(recyclerAdapter as ItemsAdapter<*>).updateAllItems()
|
||||
} else {
|
||||
(recyclerAdapter as ItemsAdapter<*>).addItemsAtEnd(items)
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ 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.Config
|
||||
import apps.amine.bou.readerforselfoss.utils.SharedItems
|
||||
import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible
|
||||
import apps.amine.bou.readerforselfoss.utils.persistence.toEntity
|
||||
import apps.amine.bou.readerforselfoss.utils.succeeded
|
||||
@ -146,42 +147,16 @@ class ReaderActivity : AppCompatActivity() {
|
||||
} else {
|
||||
canFavorite()
|
||||
}
|
||||
readItem(allItems[binding.pager.currentItem])
|
||||
readItem(allItems[position])
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun readItem(item: Item) {
|
||||
private fun readItem(item: Item) {
|
||||
if (markOnScroll) {
|
||||
thread {
|
||||
db.itemsDao().delete(item.toEntity())
|
||||
SharedItems.readItem(applicationContext, api, db, item)
|
||||
}
|
||||
if (this@ReaderActivity.isNetworkAccessible(this@ReaderActivity.findViewById(R.id.reader_activity_view))) {
|
||||
api.markItem(item.id).enqueue(
|
||||
object : Callback<SuccessResponse> {
|
||||
override fun onResponse(
|
||||
call: Call<SuccessResponse>,
|
||||
response: Response<SuccessResponse>
|
||||
) {
|
||||
}
|
||||
|
||||
override fun onFailure(
|
||||
call: Call<SuccessResponse>,
|
||||
t: Throwable
|
||||
) {
|
||||
thread {
|
||||
db.itemsDao().insertAllItems(item.toEntity())
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
thread {
|
||||
db.actionsDao().insertAllActions(ActionEntity(item.id, true, false, false, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun notifyAdapter() {
|
||||
|
@ -14,6 +14,7 @@ import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase
|
||||
import apps.amine.bou.readerforselfoss.persistence.entities.ActionEntity
|
||||
import apps.amine.bou.readerforselfoss.themes.AppColors
|
||||
import apps.amine.bou.readerforselfoss.utils.Config
|
||||
import apps.amine.bou.readerforselfoss.utils.SharedItems
|
||||
import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible
|
||||
import apps.amine.bou.readerforselfoss.utils.persistence.toEntity
|
||||
import apps.amine.bou.readerforselfoss.utils.succeeded
|
||||
@ -32,8 +33,8 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
|
||||
abstract val config: Config
|
||||
abstract val updateItems: (ArrayList<Item>) -> Unit
|
||||
|
||||
fun updateAllItems(newItems: ArrayList<Item>) {
|
||||
items = newItems
|
||||
fun updateAllItems() {
|
||||
items = SharedItems.focusedItems
|
||||
notifyDataSetChanged()
|
||||
updateItems(items)
|
||||
}
|
||||
|
@ -74,6 +74,7 @@ fun Context.openItemUrlInternally(
|
||||
) {
|
||||
if (articleViewer) {
|
||||
ReaderActivity.allItems = allItems
|
||||
SharedItems.position = currentItem
|
||||
val intent = Intent(this, ReaderActivity::class.java)
|
||||
intent.putExtra("currentItem", currentItem)
|
||||
app.startActivity(intent)
|
||||
|
@ -0,0 +1,102 @@
|
||||
package apps.amine.bou.readerforselfoss.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
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.persistence.entities.ActionEntity
|
||||
import apps.amine.bou.readerforselfoss.utils.persistence.toEntity
|
||||
import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
/*
|
||||
* Singleton class that contains the articles fetched from Selfoss, it allows sharing the items list
|
||||
* between Activities and Fragments
|
||||
*/
|
||||
object SharedItems {
|
||||
var items: ArrayList<Item> = arrayListOf<Item>()
|
||||
get() {
|
||||
return ArrayList(field)
|
||||
}
|
||||
set(value) {
|
||||
field = ArrayList(value)
|
||||
}
|
||||
var focusedItems: ArrayList<Item> = arrayListOf<Item>()
|
||||
get() {
|
||||
return ArrayList(field)
|
||||
}
|
||||
set(value) {
|
||||
field = ArrayList(value)
|
||||
}
|
||||
var position = 0
|
||||
set(value) {
|
||||
field = when {
|
||||
value < 0 -> 0
|
||||
value > focusedItems.size -> focusedItems.size
|
||||
else -> value
|
||||
}
|
||||
}
|
||||
|
||||
fun readItem(app: Context, api: SelfossApi, db: AppDatabase, item: Item) {
|
||||
if (focusedItems.contains(item)) {
|
||||
position = focusedItems.indexOf(item)
|
||||
readItemAtIndex(app, api, db)
|
||||
}
|
||||
}
|
||||
|
||||
fun readItemAtIndex(app: Context, api: SelfossApi, db: AppDatabase) {
|
||||
val i = focusedItems[position]
|
||||
var tmpItems = items
|
||||
tmpItems.remove(i)
|
||||
items = tmpItems
|
||||
var tmpFocusedItems = focusedItems
|
||||
tmpFocusedItems.remove(i)
|
||||
focusedItems = tmpFocusedItems
|
||||
|
||||
thread {
|
||||
db.itemsDao().delete(i.toEntity())
|
||||
}
|
||||
|
||||
if (app.isNetworkAccessible(null)) {
|
||||
api.markItem(i.id).enqueue(object : Callback<SuccessResponse> {
|
||||
override fun onResponse(
|
||||
call: Call<SuccessResponse>,
|
||||
response: Response<SuccessResponse>
|
||||
) {
|
||||
|
||||
//unmarkSnackbar(i, position)
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<SuccessResponse>, t: Throwable) {
|
||||
Toast.makeText(
|
||||
app,
|
||||
app.getString(R.string.cant_mark_read),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
tmpItems.add(position, i)
|
||||
tmpFocusedItems.add(position, i)
|
||||
items = tmpItems
|
||||
focusedItems = tmpFocusedItems
|
||||
|
||||
thread {
|
||||
db.itemsDao().insertAllItems(i.toEntity())
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
thread {
|
||||
db.actionsDao().insertAllActions(ActionEntity(i.id, true, false, false, false))
|
||||
}
|
||||
}
|
||||
|
||||
if (position > focusedItems.size) {
|
||||
position -= 1
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user