Remove SelfossApi from SourcesActivity

All network calls to access sources go through the repository
This commit is contained in:
davide 2022-07-23 01:44:47 +02:00
parent 8898e85f02
commit 12e174dacd
4 changed files with 33 additions and 29 deletions

View File

@ -11,9 +11,8 @@ import bou.amine.apps.readerforselfossv2.android.databinding.ActivitySourcesBind
import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.AppColors
import bou.amine.apps.readerforselfossv2.android.themes.Toppings import bou.amine.apps.readerforselfossv2.android.themes.Toppings
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 com.ftinc.scoop.Scoop import com.ftinc.scoop.Scoop
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -28,7 +27,7 @@ class SourcesActivity : AppCompatActivity(), DIAware {
private lateinit var binding: ActivitySourcesBinding private lateinit var binding: ActivitySourcesBinding
override val di by closestDI() override val di by closestDI()
private val apiDetailsService : ApiDetailsService by instance() private val repository : Repository by instance()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
appColors = AppColors(this@SourcesActivity) appColors = AppColors(this@SourcesActivity)
@ -60,13 +59,6 @@ class SourcesActivity : AppCompatActivity(), DIAware {
super.onResume() super.onResume()
val mLayoutManager = LinearLayoutManager(this) val mLayoutManager = LinearLayoutManager(this)
val api = SelfossApiImpl(
// this,
// this@SourcesActivity,
// settings.getBoolean("isSelfSignedCert", false),
// prefs.getString("api_timeout", "-1")!!.toLong()
apiDetailsService
)
var items: ArrayList<SelfossModel.Source> var items: ArrayList<SelfossModel.Source>
binding.recyclerView.setHasFixedSize(true) binding.recyclerView.setHasFixedSize(true)
@ -74,11 +66,11 @@ class SourcesActivity : AppCompatActivity(), DIAware {
if (this@SourcesActivity.isNetworkAvailable(binding.recyclerView)) { if (this@SourcesActivity.isNetworkAvailable(binding.recyclerView)) {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
val response = api.sources() val response = repository.getSources()
if (response != null) { if (response != null) {
items = response items = response
val mAdapter = SourcesListAdapter(this@SourcesActivity, items, api, val mAdapter = SourcesListAdapter(
apiDetailsService this@SourcesActivity, items
) )
binding.recyclerView.adapter = mAdapter binding.recyclerView.adapter = mAdapter
mAdapter.notifyDataSetChanged() mAdapter.notifyDataSetChanged()

View File

@ -16,26 +16,30 @@ import bou.amine.apps.readerforselfossv2.android.utils.Config
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.android.utils.toTextDrawableString import bou.amine.apps.readerforselfossv2.android.utils.toTextDrawableString
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 com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.TextDrawable
import com.amulyakhare.textdrawable.util.ColorGenerator import com.amulyakhare.textdrawable.util.ColorGenerator
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.DIAware
import org.kodein.di.android.closestDI
import org.kodein.di.instance
class SourcesListAdapter( class SourcesListAdapter(
private val app: Activity, private val app: Activity,
private val items: ArrayList<SelfossModel.Source>, private val items: ArrayList<SelfossModel.Source>
private val api: SelfossApiImpl, ) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>(), DIAware {
private val apiDetailsService: ApiDetailsService
) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>() {
private val c: Context = app.baseContext private val c: Context = app.baseContext
private val generator: ColorGenerator = ColorGenerator.MATERIAL private val generator: ColorGenerator = ColorGenerator.MATERIAL
private lateinit var config: Config private lateinit var config: Config
private lateinit var binding: SourceListItemBinding private lateinit var binding: SourceListItemBinding
override val di: DI by closestDI(app)
private val repository : Repository by instance()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
binding = SourceListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) binding = SourceListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding.root) return ViewHolder(binding.root)
@ -45,7 +49,7 @@ class SourcesListAdapter(
val itm = items[position] val itm = items[position]
config = Config() config = Config()
if (itm.getIcon(apiDetailsService.getBaseUrl()).isEmpty()) { if (itm.getIcon(repository.baseUrl).isEmpty()) {
val color = generator.getColor(itm.getTitleDecoded()) val color = generator.getColor(itm.getTitleDecoded())
val drawable = val drawable =
@ -55,7 +59,7 @@ class SourcesListAdapter(
.build(itm.getTitleDecoded().toTextDrawableString(c), color) .build(itm.getTitleDecoded().toTextDrawableString(c), color)
binding.itemImage.setImageDrawable(drawable) binding.itemImage.setImageDrawable(drawable)
} else { } else {
c.circularBitmapDrawable(config, itm.getIcon(apiDetailsService.getBaseUrl()), binding.itemImage) c.circularBitmapDrawable(config, itm.getIcon(repository.baseUrl), binding.itemImage)
} }
binding.sourceTitle.text = itm.getTitleDecoded() binding.sourceTitle.text = itm.getTitleDecoded()
@ -77,8 +81,8 @@ class SourcesListAdapter(
if (c.isNetworkAvailable(null)) { if (c.isNetworkAvailable(null)) {
val (id) = items[adapterPosition] val (id) = items[adapterPosition]
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val action = api.deleteSource(id) val successfullyDeletedSource = repository.deleteSource(id)
if (action != null && action.isSuccess) { if (successfullyDeletedSource) {
items.removeAt(adapterPosition) items.removeAt(adapterPosition)
notifyItemRemoved(adapterPosition) notifyItemRemoved(adapterPosition)
notifyItemRangeChanged(adapterPosition, itemCount) notifyItemRangeChanged(adapterPosition, itemCount)

View File

@ -16,7 +16,7 @@ interface Repository {
fun stats(): SelfossModel.Stats fun stats(): SelfossModel.Stats
fun getTags(): List<SelfossModel.Tag> fun getTags(): List<SelfossModel.Tag>
suspend fun getSpouts(): Map<String, SelfossModel.Spout>? suspend fun getSpouts(): Map<String, SelfossModel.Spout>?
fun getSources(): List<SelfossModel.Source> suspend fun getSources(): ArrayList<SelfossModel.Source>?
suspend fun markAsRead(id: String): Boolean suspend fun markAsRead(id: String): Boolean
suspend fun unmarkAsRead(id: String): Boolean suspend fun unmarkAsRead(id: String): Boolean
suspend fun starr(id: String): Boolean suspend fun starr(id: String): Boolean
@ -27,7 +27,7 @@ interface Repository {
spout: String, spout: String,
tags: String, tags: String,
filter: String): Boolean filter: String): Boolean
fun deleteSource(id: Int): Boolean suspend fun deleteSource(id: Int): Boolean
fun updateRemote(): Boolean fun updateRemote(): Boolean
suspend fun login(): Boolean suspend fun login(): Boolean
fun refreshLoginInformation() fun refreshLoginInformation()

View File

@ -42,8 +42,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return api.spouts() return api.spouts()
} }
override fun getSources(): List<SelfossModel.Source> { override suspend fun getSources(): ArrayList<SelfossModel.Source>? {
TODO("Not yet implemented") // TODO: Check success
return api.sources()
} }
override suspend fun markAsRead(id: String): Boolean { override suspend fun markAsRead(id: String): Boolean {
@ -98,8 +99,15 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return result return result
} }
override fun deleteSource(id: Int): Boolean { override suspend fun deleteSource(id: Int): Boolean {
TODO("Not yet implemented") // TODO: Check connectivity, store in DB
var success = false
val response = api.deleteSource(id)
if (response != null) {
success = response.isSuccess
}
return success
} }
override fun updateRemote(): Boolean { override fun updateRemote(): Boolean {