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.Toppings
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.service.ApiDetailsService
import com.ftinc.scoop.Scoop
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -28,7 +27,7 @@ class SourcesActivity : AppCompatActivity(), DIAware {
private lateinit var binding: ActivitySourcesBinding
override val di by closestDI()
private val apiDetailsService : ApiDetailsService by instance()
private val repository : Repository by instance()
override fun onCreate(savedInstanceState: Bundle?) {
appColors = AppColors(this@SourcesActivity)
@ -60,13 +59,6 @@ class SourcesActivity : AppCompatActivity(), DIAware {
super.onResume()
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>
binding.recyclerView.setHasFixedSize(true)
@ -74,11 +66,11 @@ class SourcesActivity : AppCompatActivity(), DIAware {
if (this@SourcesActivity.isNetworkAvailable(binding.recyclerView)) {
CoroutineScope(Dispatchers.Main).launch {
val response = api.sources()
val response = repository.getSources()
if (response != null) {
items = response
val mAdapter = SourcesListAdapter(this@SourcesActivity, items, api,
apiDetailsService
val mAdapter = SourcesListAdapter(
this@SourcesActivity, items
)
binding.recyclerView.adapter = mAdapter
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.network.isNetworkAvailable
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.service.ApiDetailsService
import com.amulyakhare.textdrawable.TextDrawable
import com.amulyakhare.textdrawable.util.ColorGenerator
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
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(
private val app: Activity,
private val items: ArrayList<SelfossModel.Source>,
private val api: SelfossApiImpl,
private val apiDetailsService: ApiDetailsService
) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>() {
private val items: ArrayList<SelfossModel.Source>
) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>(), DIAware {
private val c: Context = app.baseContext
private val generator: ColorGenerator = ColorGenerator.MATERIAL
private lateinit var config: Config
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 {
binding = SourceListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding.root)
@ -45,7 +49,7 @@ class SourcesListAdapter(
val itm = items[position]
config = Config()
if (itm.getIcon(apiDetailsService.getBaseUrl()).isEmpty()) {
if (itm.getIcon(repository.baseUrl).isEmpty()) {
val color = generator.getColor(itm.getTitleDecoded())
val drawable =
@ -55,7 +59,7 @@ class SourcesListAdapter(
.build(itm.getTitleDecoded().toTextDrawableString(c), color)
binding.itemImage.setImageDrawable(drawable)
} else {
c.circularBitmapDrawable(config, itm.getIcon(apiDetailsService.getBaseUrl()), binding.itemImage)
c.circularBitmapDrawable(config, itm.getIcon(repository.baseUrl), binding.itemImage)
}
binding.sourceTitle.text = itm.getTitleDecoded()
@ -77,8 +81,8 @@ class SourcesListAdapter(
if (c.isNetworkAvailable(null)) {
val (id) = items[adapterPosition]
CoroutineScope(Dispatchers.IO).launch {
val action = api.deleteSource(id)
if (action != null && action.isSuccess) {
val successfullyDeletedSource = repository.deleteSource(id)
if (successfullyDeletedSource) {
items.removeAt(adapterPosition)
notifyItemRemoved(adapterPosition)
notifyItemRangeChanged(adapterPosition, itemCount)

View File

@ -16,7 +16,7 @@ interface Repository {
fun stats(): SelfossModel.Stats
fun getTags(): List<SelfossModel.Tag>
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 unmarkAsRead(id: String): Boolean
suspend fun starr(id: String): Boolean
@ -27,7 +27,7 @@ interface Repository {
spout: String,
tags: String,
filter: String): Boolean
fun deleteSource(id: Int): Boolean
suspend fun deleteSource(id: Int): Boolean
fun updateRemote(): Boolean
suspend fun login(): Boolean
fun refreshLoginInformation()

View File

@ -42,8 +42,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return api.spouts()
}
override fun getSources(): List<SelfossModel.Source> {
TODO("Not yet implemented")
override suspend fun getSources(): ArrayList<SelfossModel.Source>? {
// TODO: Check success
return api.sources()
}
override suspend fun markAsRead(id: String): Boolean {
@ -98,8 +99,15 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return result
}
override fun deleteSource(id: Int): Boolean {
TODO("Not yet implemented")
override suspend fun deleteSource(id: Int): Boolean {
// 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 {