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

View File

@ -15,18 +15,18 @@ interface Repository {
fun getMoreItems(): List<SelfossModel.Item> fun getMoreItems(): List<SelfossModel.Item>
fun stats(): SelfossModel.Stats fun stats(): SelfossModel.Stats
fun getTags(): List<SelfossModel.Tag> fun getTags(): List<SelfossModel.Tag>
fun getSpouts(): List<SelfossModel.Spout> suspend fun getSpouts(): Map<String, SelfossModel.Spout>?
fun getSources(): List<SelfossModel.Source> fun getSources(): List<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
suspend 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, suspend fun createSource(title: String,
url: String, url: String,
spout: String, spout: String,
tags: String, tags: String,
filter: String): Boolean filter: String): Boolean
fun deleteSource(id: Int): Boolean fun deleteSource(id: Int): Boolean
fun updateRemote(): Boolean fun updateRemote(): Boolean
suspend fun login(): Boolean suspend fun login(): Boolean

View File

@ -37,8 +37,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun getSpouts(): List<SelfossModel.Spout> { override suspend fun getSpouts(): Map<String, SelfossModel.Spout>? {
TODO("Not yet implemented") // TODO: Check success, store in DB
return api.spouts()
} }
override fun getSources(): List<SelfossModel.Source> { override fun getSources(): List<SelfossModel.Source> {
@ -72,14 +73,29 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun createSource( override suspend fun createSource(
title: String, title: String,
url: String, url: String,
spout: String, spout: String,
tags: String, tags: String,
filter: String filter: String
): Boolean { ): 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 { override fun deleteSource(id: Int): Boolean {