Remove Selfoss Api from the AddSourceActivity

All network calls of AddSourceActivity are now done through the repository.
This commit is contained in:
davide 2022-07-23 01:28:19 +02:00
parent 7221f11f80
commit 8898e85f02
3 changed files with 48 additions and 54 deletions

View File

@ -2,27 +2,17 @@ package bou.amine.apps.readerforselfossv2.android
import android.content.Intent
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Spinner
import android.widget.TextView
import android.widget.Toast
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import bou.amine.apps.readerforselfossv2.android.databinding.ActivityAddSourceBinding
import bou.amine.apps.readerforselfossv2.android.themes.AppColors
import bou.amine.apps.readerforselfossv2.android.themes.Toppings
import bou.amine.apps.readerforselfossv2.android.utils.Config
import bou.amine.apps.readerforselfossv2.android.utils.isBaseUrlValid
import bou.amine.apps.readerforselfossv2.repository.Repository
import com.ftinc.scoop.Scoop
import bou.amine.apps.readerforselfossv2.android.databinding.ActivityAddSourceBinding
import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
import com.russhwolf.settings.Settings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -34,14 +24,12 @@ import org.kodein.di.instance
class AddSourceActivity : AppCompatActivity(), DIAware {
private var mSpoutsValue: String? = null
private lateinit var api: SelfossApiImpl
private lateinit var appColors: AppColors
private lateinit var binding: ActivityAddSourceBinding
private val settings = Settings()
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@AddSourceActivity)
@ -77,24 +65,16 @@ class AddSourceActivity : AppCompatActivity(), DIAware {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
try {
api = SelfossApiImpl(
// this,
// this@AddSourceActivity,
// settings.getBoolean("isSelfSignedCert", false),
// prefs.getString("api_timeout", "-1")!!.toLong()
apiDetailsService
)
} catch (e: IllegalArgumentException) {
mustLoginToAddSource()
}
maybeGetDetailsFromIntentSharing(intent, binding.sourceUri, binding.nameInput)
binding.saveBtn.setTextColor(appColors.colorAccent)
binding.saveBtn.setOnClickListener {
handleSaveSource(binding.tags, binding.nameInput.text.toString(), binding.sourceUri.text.toString(), api)
handleSaveSource(
binding.tags,
binding.nameInput.text.toString(),
binding.sourceUri.text.toString()
)
}
}
@ -105,13 +85,12 @@ class AddSourceActivity : AppCompatActivity(), DIAware {
if (config.baseUrl.isEmpty() || !config.baseUrl.isBaseUrlValid(this@AddSourceActivity)) {
mustLoginToAddSource()
} else {
handleSpoutsSpinner(binding.spoutsSpinner, api, binding.progress, binding.formContainer)
handleSpoutsSpinner(binding.spoutsSpinner, binding.progress, binding.formContainer)
}
}
private fun handleSpoutsSpinner(
spoutsSpinner: Spinner,
api: SelfossApiImpl?,
mProgress: ProgressBar,
formContainer: ConstraintLayout
) {
@ -131,7 +110,7 @@ class AddSourceActivity : AppCompatActivity(), DIAware {
CoroutineScope(Dispatchers.Main).launch {
var items = api!!.spouts()
val items = repository.getSpouts()
if (items != null) {
val itemsStrings = items.map { it.value.name }
@ -179,7 +158,7 @@ class AddSourceActivity : AppCompatActivity(), DIAware {
finish()
}
private fun handleSaveSource(tags: EditText, title: String, url: String, api: SelfossApiImpl) {
private fun handleSaveSource(tags: EditText, title: String, url: String) {
val sourceDetailsUnavailable =
title.isEmpty() || url.isEmpty() || mSpoutsValue == null || mSpoutsValue!!.isEmpty()
@ -190,15 +169,14 @@ class AddSourceActivity : AppCompatActivity(), DIAware {
}
else -> {
CoroutineScope(Dispatchers.Main).launch {
val response: SelfossModel.SuccessResponse? = api.createSourceForVersion(
val successfullyAddedSource = repository.createSource(
title,
url,
mSpoutsValue!!,
tags.text.toString(),
"",
settings.getInt("apiVersionMajor", 0)
)
if (response != null) {
if (successfullyAddedSource) {
finish()
} else {
Toast.makeText(

View File

@ -15,14 +15,14 @@ interface Repository {
fun getMoreItems(): List<SelfossModel.Item>
fun stats(): SelfossModel.Stats
fun getTags(): List<SelfossModel.Tag>
fun getSpouts(): List<SelfossModel.Spout>
suspend fun getSpouts(): Map<String, SelfossModel.Spout>?
fun getSources(): List<SelfossModel.Source>
suspend fun markAsRead(id: String): Boolean
suspend fun unmarkAsRead(id: String): Boolean
suspend fun starr(id: String): Boolean
suspend fun unstarr(id: String): Boolean
fun markAllAsRead(ids: List<String>): Boolean
fun createSource(title: String,
suspend fun createSource(title: String,
url: String,
spout: String,
tags: String,

View File

@ -37,8 +37,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
TODO("Not yet implemented")
}
override fun getSpouts(): List<SelfossModel.Spout> {
TODO("Not yet implemented")
override suspend fun getSpouts(): Map<String, SelfossModel.Spout>? {
// TODO: Check success, store in DB
return api.spouts()
}
override fun getSources(): List<SelfossModel.Source> {
@ -72,14 +73,29 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
TODO("Not yet implemented")
}
override fun createSource(
override suspend fun createSource(
title: String,
url: String,
spout: String,
tags: String,
filter: String
): Boolean {
TODO("Not yet implemented")
// TODO: Check connectivity
var result = false
val response = api.createSourceForVersion(
title,
url,
spout,
tags,
filter,
apiMajorVersion
)
if (response != null) {
result = true
}
return result
}
override fun deleteSource(id: Int): Boolean {