Strip Selfoss API from Items Adapters

This commit is contained in:
davide 2022-07-20 14:41:27 +02:00
parent dc10cafb1b
commit 9373024147
6 changed files with 45 additions and 29 deletions

View File

@ -1012,7 +1012,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
ItemCardAdapter( ItemCardAdapter(
this, this,
items, items,
api,
apiDetailsService, apiDetailsService,
db, db,
customTabActivityHelper, customTabActivityHelper,
@ -1031,7 +1030,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
ItemListAdapter( ItemListAdapter(
this, this,
items, items,
api,
apiDetailsService, apiDetailsService,
db, db,
customTabActivityHelper, customTabActivityHelper,

View File

@ -17,7 +17,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.bitmapCenterCrop
import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable
import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable
import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
import bou.amine.apps.readerforselfossv2.service.SearchService import bou.amine.apps.readerforselfossv2.service.SearchService
@ -28,11 +28,13 @@ import com.bumptech.glide.Glide
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.kodein.di.DI
import org.kodein.di.android.closestDI
import org.kodein.di.instance
class ItemCardAdapter( class ItemCardAdapter(
override val app: Activity, override val app: Activity,
override var items: ArrayList<SelfossModel.Item>, override var items: ArrayList<SelfossModel.Item>,
override val api: SelfossApiImpl,
override val apiDetailsService: ApiDetailsService, override val apiDetailsService: ApiDetailsService,
override val db: AppDatabase, override val db: AppDatabase,
private val helper: CustomTabActivityHelper, private val helper: CustomTabActivityHelper,
@ -50,6 +52,9 @@ class ItemCardAdapter(
private val imageMaxHeight: Int = private val imageMaxHeight: Int =
c.resources.getDimension(R.dimen.card_image_max_height).toInt() c.resources.getDimension(R.dimen.card_image_max_height).toInt()
override val di: DI by closestDI(app)
override val repository : Repository by instance()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = CardItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) val binding = CardItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding) return ViewHolder(binding)
@ -114,15 +119,15 @@ class ItemCardAdapter(
if (c.isNetworkAvailable()) { if (c.isNetworkAvailable()) {
if (item.starred) { if (item.starred) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
api.unstarr(item.id.toString()) repository.unstarr(item.id.toString())
// TODO: save to db // TODO: Handle failure
} }
item.starred = false item.starred = false
binding.favButton.isSelected = false binding.favButton.isSelected = false
} else { } else {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
api.starr(item.id.toString()) repository.starr(item.id.toString())
// TODO: save to db // TODO: Handle failure
} }
item.starred = true item.starred = true
binding.favButton.isSelected = true binding.favButton.isSelected = true

View File

@ -13,18 +13,20 @@ import bou.amine.apps.readerforselfossv2.android.utils.*
import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper
import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop
import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable
import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
import bou.amine.apps.readerforselfossv2.service.SearchService import bou.amine.apps.readerforselfossv2.service.SearchService
import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.DateUtils
import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.TextDrawable
import com.amulyakhare.textdrawable.util.ColorGenerator import com.amulyakhare.textdrawable.util.ColorGenerator
import org.kodein.di.DI
import org.kodein.di.android.closestDI
import org.kodein.di.instance
class ItemListAdapter( class ItemListAdapter(
override val app: Activity, override val app: Activity,
override var items: ArrayList<SelfossModel.Item>, override var items: ArrayList<SelfossModel.Item>,
override val api: SelfossApiImpl,
override val apiDetailsService: ApiDetailsService, override val apiDetailsService: ApiDetailsService,
override val db: AppDatabase, override val db: AppDatabase,
private val helper: CustomTabActivityHelper, private val helper: CustomTabActivityHelper,
@ -39,6 +41,9 @@ class ItemListAdapter(
private val generator: ColorGenerator = ColorGenerator.MATERIAL private val generator: ColorGenerator = ColorGenerator.MATERIAL
private val c: Context = app.baseContext private val c: Context = app.baseContext
override val di: DI by closestDI(app)
override val repository : Repository by instance()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding) return ViewHolder(binding)

View File

@ -8,7 +8,7 @@ import bou.amine.apps.readerforselfossv2.android.R
import bou.amine.apps.readerforselfossv2.android.persistence.database.AppDatabase import bou.amine.apps.readerforselfossv2.android.persistence.database.AppDatabase
import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.AppColors
import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.Config
import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
import bou.amine.apps.readerforselfossv2.service.SearchService import bou.amine.apps.readerforselfossv2.service.SearchService
@ -16,11 +16,12 @@ import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.kodein.di.DIAware
abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapter<VH>() { abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapter<VH>(), DIAware {
abstract var items: ArrayList<SelfossModel.Item> abstract var items: ArrayList<SelfossModel.Item>
abstract val api: SelfossApiImpl
abstract val apiDetailsService: ApiDetailsService abstract val apiDetailsService: ApiDetailsService
abstract val repository: Repository
abstract val db: AppDatabase abstract val db: AppDatabase
abstract val userIdentifier: String abstract val userIdentifier: String
abstract val app: Activity abstract val app: Activity
@ -82,7 +83,7 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
private fun readItemAtIndex(position: Int, showSnackbar: Boolean = true) { private fun readItemAtIndex(position: Int, showSnackbar: Boolean = true) {
val i = items[position] val i = items[position]
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
api.markAsRead(i.id.toString()) repository.markAsRead(i.id.toString())
// TODO: update db // TODO: update db
} }
@ -101,7 +102,7 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
private fun unreadItemAtIndex(position: Int, showSnackbar: Boolean = true) { private fun unreadItemAtIndex(position: Int, showSnackbar: Boolean = true) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
api.unmarkAsRead(items[position].id.toString()) repository.unmarkAsRead(items[position].id.toString())
// Todo: SharedItems.unreadItem(app, api, db, items[position]) // Todo: SharedItems.unreadItem(app, api, db, items[position])
// TODO: update db // TODO: update db

View File

@ -14,10 +14,10 @@ interface Repository {
fun getTags(): List<SelfossModel.Tag> fun getTags(): List<SelfossModel.Tag>
fun getSpouts(): List<SelfossModel.Spout> fun getSpouts(): List<SelfossModel.Spout>
fun getSources(): List<SelfossModel.Source> fun getSources(): List<SelfossModel.Source>
fun markAsRead(id: String): Boolean suspend fun markAsRead(id: String): Boolean
fun unmarkAsRead(id: String): Boolean suspend fun unmarkAsRead(id: String): Boolean
fun starr(id: String): Boolean suspend fun starr(id: String): Boolean
fun unstarr(id: String): Boolean suspend fun unstarr(id: String): Boolean
fun markAllAsRead(ids: List<String>): Boolean fun markAllAsRead(ids: List<String>): Boolean
fun createSource(title: String, fun createSource(title: String,
url: String, url: String,

View File

@ -31,20 +31,27 @@ class RepositoryImpl(private val api: SelfossApi) : Repository {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun markAsRead(id: String): Boolean { override suspend fun markAsRead(id: String): Boolean {
TODO("Not yet implemented") // TODO: Check success, store in DB
api.markAsRead(id)
return true
} }
override fun unmarkAsRead(id: String): Boolean { override suspend fun unmarkAsRead(id: String): Boolean {
TODO("Not yet implemented") // TODO: Check success, store in DB
api.unmarkAsRead(id)
return true }
override suspend fun starr(id: String): Boolean {
// TODO: Check success, store in DB
api.starr(id)
return true
} }
override fun starr(id: String): Boolean { override suspend fun unstarr(id: String): Boolean {
TODO("Not yet implemented") // TODO: Check success, store in DB
} api.unstarr(id)
return true
override fun unstarr(id: String): Boolean {
TODO("Not yet implemented")
} }
override fun markAllAsRead(ids: List<String>): Boolean { override fun markAllAsRead(ids: List<String>): Boolean {