From 3c68bde62b363fe1fb120efc15900ad8141684a6 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Wed, 28 Dec 2022 22:09:31 +0100 Subject: [PATCH] Source update screen. --- androidApp/src/main/AndroidManifest.xml | 2 +- .../android/SourcesActivity.kt | 2 +- ...rceActivity.kt => UpsertSourceActivity.kt} | 119 ++++++++++++------ .../android/adapters/SourcesListAdapter.kt | 10 ++ ..._source.xml => activity_upsert_source.xml} | 32 ++--- .../src/main/res/values-ca-rES/strings.xml | 1 + .../src/main/res/values-de-rDE/strings.xml | 1 + .../src/main/res/values-es-rES/strings.xml | 1 + .../src/main/res/values-fa-rIR/strings.xml | 1 + .../src/main/res/values-fr-rFR/strings.xml | 1 + .../src/main/res/values-gl-rES/strings.xml | 1 + .../src/main/res/values-in-rID/strings.xml | 1 + .../src/main/res/values-it-rIT/strings.xml | 1 + .../src/main/res/values-ko-rKR/strings.xml | 1 + .../src/main/res/values-night/strings.xml | 1 + .../src/main/res/values-nl-rNL/strings.xml | 1 + .../src/main/res/values-pt-rBR/strings.xml | 1 + .../src/main/res/values-pt-rPT/strings.xml | 1 + .../src/main/res/values-si-rLK/strings.xml | 1 + .../src/main/res/values-tr-rTR/strings.xml | 1 + .../src/main/res/values-zh-rCN/strings.xml | 1 + .../src/main/res/values-zh-rTW/strings.xml | 1 + androidApp/src/main/res/values/strings.xml | 1 + androidApp/src/test/kotlin/RepositoryTest.kt | 36 +++--- .../readerforselfossv2/model/SelfossModel.kt | 12 +- .../repository/RepositoryImpl.kt | 30 ++++- .../apps/readerforselfossv2/rest/RestUtils.kt | 3 + .../readerforselfossv2/rest/SelfossApi.kt | 61 +++++++-- .../readerforselfossv2/utils/EntityUtils.kt | 6 +- .../amine/apps/readerforselfossv2/dao/2.sqm | 1 + .../apps/readerforselfossv2/dao/Sources.sq | 1 + 31 files changed, 231 insertions(+), 102 deletions(-) rename androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/{AddSourceActivity.kt => UpsertSourceActivity.kt} (54%) rename androidApp/src/main/res/layout/{activity_add_source.xml => activity_upsert_source.xml} (84%) create mode 100644 shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/2.sqm diff --git a/androidApp/src/main/AndroidManifest.xml b/androidApp/src/main/AndroidManifest.xml index 80f4b0f..6709e23 100644 --- a/androidApp/src/main/AndroidManifest.xml +++ b/androidApp/src/main/AndroidManifest.xml @@ -52,7 +52,7 @@ android:value=".HomeActivity" /> ) { + binding.nameInput.setText(existingSource!!.title) + binding.tags.setText(existingSource!!.tags.joinToString(", ")) + binding.sourceUri.setText(existingSource!!.params?.url) + binding.spoutsSpinner.setSelection(items.keys.indexOf(existingSource!!.spout)) + binding.progress.visibility = View.GONE + binding.formContainer.visibility = View.VISIBLE + } + override fun onResume() { super.onResume() @@ -58,17 +82,13 @@ class AddSourceActivity : AppCompatActivity(), DIAware { if (baseUrl.isEmpty() || baseUrl.isBaseUrlInvalid()) { mustLoginToAddSource() } else { - handleSpoutsSpinner(binding.spoutsSpinner, binding.progress, binding.formContainer) + handleSpoutsSpinner() } } - private fun handleSpoutsSpinner( - spoutsSpinner: Spinner, - mProgress: ProgressBar, - formContainer: ConstraintLayout - ) { + private fun handleSpoutsSpinner() { val spoutsKV = HashMap() - spoutsSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + binding.spoutsSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(adapterView: AdapterView<*>, view: View?, i: Int, l: Long) { if (view != null) { val spoutName = (view as TextView).text.toString() @@ -84,11 +104,11 @@ class AddSourceActivity : AppCompatActivity(), DIAware { fun handleSpoutFailure(networkIssue: Boolean = false) { Toast.makeText( - this@AddSourceActivity, + this@UpsertSourceActivity, if (networkIssue) R.string.cant_get_spouts_no_network else R.string.cant_get_spouts, Toast.LENGTH_SHORT ).show() - mProgress.visibility = View.GONE + binding.progress.visibility = View.GONE } CoroutineScope(Dispatchers.Main).launch { @@ -100,17 +120,21 @@ class AddSourceActivity : AppCompatActivity(), DIAware { spoutsKV[value.name] = key } - mProgress.visibility = View.GONE - formContainer.visibility = View.VISIBLE + binding.progress.visibility = View.GONE + binding.formContainer.visibility = View.VISIBLE val spinnerArrayAdapter = ArrayAdapter( - this@AddSourceActivity, + this@UpsertSourceActivity, android.R.layout.simple_spinner_item, itemsStrings ) spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) - spoutsSpinner.adapter = spinnerArrayAdapter + binding.spoutsSpinner.adapter = spinnerArrayAdapter + + if (existingSource != null) { + initFields(items) + } } else { handleSpoutFailure() } @@ -121,13 +145,11 @@ class AddSourceActivity : AppCompatActivity(), DIAware { } private fun maybeGetDetailsFromIntentSharing( - intent: Intent, - sourceUri: EditText, - nameInput: EditText + intent: Intent ) { if (Intent.ACTION_SEND == intent.action && "text/plain" == intent.type) { - sourceUri.setText(intent.getStringExtra(Intent.EXTRA_TEXT)) - nameInput.setText(intent.getStringExtra(Intent.EXTRA_TITLE)) + binding.sourceUri.setText(intent.getStringExtra(Intent.EXTRA_TEXT)) + binding.nameInput.setText(intent.getStringExtra(Intent.EXTRA_TITLE)) } } @@ -138,7 +160,8 @@ class AddSourceActivity : AppCompatActivity(), DIAware { finish() } - private fun handleSaveSource(tags: EditText, title: String, url: String) { + private fun handleSaveSource() { + val url = binding.sourceUri.text.toString() val sourceDetailsUnavailable = title.isEmpty() || url.isEmpty() || mSpoutsValue == null || mSpoutsValue!!.isEmpty() @@ -149,18 +172,27 @@ class AddSourceActivity : AppCompatActivity(), DIAware { } else -> { CoroutineScope(Dispatchers.Main).launch { - val successfullyAddedSource = repository.createSource( - title, - url, - mSpoutsValue!!, - tags.text.toString(), - "", - ) + val successfullyAddedSource = if (existingSource != null) { + repository.updateSource( + existingSource!!.id, + binding.nameInput.text.toString(), + url, + mSpoutsValue!!, + binding.tags.text.toString() + ) + } else { + repository.createSource( + binding.nameInput.text.toString(), + url, + mSpoutsValue!!, + binding.tags.text.toString(), + ) + } if (successfullyAddedSource) { finish() } else { Toast.makeText( - this@AddSourceActivity, + this@UpsertSourceActivity, R.string.cant_create_source, Toast.LENGTH_SHORT ).show() @@ -169,4 +201,9 @@ class AddSourceActivity : AppCompatActivity(), DIAware { } } } + + override fun onDestroy() { + super.onDestroy() + repository.unsetSelectedSource() + } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt index e3e5857..cffb8e7 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt @@ -2,12 +2,14 @@ package bou.amine.apps.readerforselfossv2.android.adapters import android.app.Activity import android.content.Context +import android.content.Intent import android.view.LayoutInflater import android.view.ViewGroup import android.widget.Button import android.widget.Toast import androidx.constraintlayout.widget.ConstraintLayout import androidx.recyclerview.widget.RecyclerView +import bou.amine.apps.readerforselfossv2.android.UpsertSourceActivity import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.databinding.SourceListItemBinding import bou.amine.apps.readerforselfossv2.android.model.toTextDrawableString @@ -94,6 +96,14 @@ class SourcesListAdapter( } } } + + mView.setOnClickListener { + val source = items[bindingAdapterPosition] + + repository.setSelectedSource(source) + app.startActivity(Intent(app, UpsertSourceActivity::class.java)) + + } } } } diff --git a/androidApp/src/main/res/layout/activity_add_source.xml b/androidApp/src/main/res/layout/activity_upsert_source.xml similarity index 84% rename from androidApp/src/main/res/layout/activity_add_source.xml rename to androidApp/src/main/res/layout/activity_upsert_source.xml index 7767d7a..547b88c 100644 --- a/androidApp/src/main/res/layout/activity_add_source.xml +++ b/androidApp/src/main/res/layout/activity_upsert_source.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="bou.amine.apps.readerforselfossv2.android.AddSourceActivity"> + tools:context="bou.amine.apps.readerforselfossv2.android.UpsertSourceActivity"> - + app:layout_constraintTop_toTopOf="parent" /> Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-de-rDE/strings.xml b/androidApp/src/main/res/values-de-rDE/strings.xml index 4250332..0ec144a 100644 --- a/androidApp/src/main/res/values-de-rDE/strings.xml +++ b/androidApp/src/main/res/values-de-rDE/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-es-rES/strings.xml b/androidApp/src/main/res/values-es-rES/strings.xml index 05537da..f8cc8e6 100644 --- a/androidApp/src/main/res/values-es-rES/strings.xml +++ b/androidApp/src/main/res/values-es-rES/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-fa-rIR/strings.xml b/androidApp/src/main/res/values-fa-rIR/strings.xml index 443987d..f5967ef 100644 --- a/androidApp/src/main/res/values-fa-rIR/strings.xml +++ b/androidApp/src/main/res/values-fa-rIR/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-fr-rFR/strings.xml b/androidApp/src/main/res/values-fr-rFR/strings.xml index 9510659..21c972a 100644 --- a/androidApp/src/main/res/values-fr-rFR/strings.xml +++ b/androidApp/src/main/res/values-fr-rFR/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-gl-rES/strings.xml b/androidApp/src/main/res/values-gl-rES/strings.xml index 5d54f73..56118d4 100644 --- a/androidApp/src/main/res/values-gl-rES/strings.xml +++ b/androidApp/src/main/res/values-gl-rES/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-in-rID/strings.xml b/androidApp/src/main/res/values-in-rID/strings.xml index 44e2694..a276cd0 100644 --- a/androidApp/src/main/res/values-in-rID/strings.xml +++ b/androidApp/src/main/res/values-in-rID/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-it-rIT/strings.xml b/androidApp/src/main/res/values-it-rIT/strings.xml index 0c788b8..9c5a95f 100644 --- a/androidApp/src/main/res/values-it-rIT/strings.xml +++ b/androidApp/src/main/res/values-it-rIT/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-ko-rKR/strings.xml b/androidApp/src/main/res/values-ko-rKR/strings.xml index 2d79ce7..bd2e722 100644 --- a/androidApp/src/main/res/values-ko-rKR/strings.xml +++ b/androidApp/src/main/res/values-ko-rKR/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-night/strings.xml b/androidApp/src/main/res/values-night/strings.xml index abbe117..aa33111 100644 --- a/androidApp/src/main/res/values-night/strings.xml +++ b/androidApp/src/main/res/values-night/strings.xml @@ -7,4 +7,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source \ No newline at end of file diff --git a/androidApp/src/main/res/values-nl-rNL/strings.xml b/androidApp/src/main/res/values-nl-rNL/strings.xml index 8b3c826..f57a72b 100644 --- a/androidApp/src/main/res/values-nl-rNL/strings.xml +++ b/androidApp/src/main/res/values-nl-rNL/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-pt-rBR/strings.xml b/androidApp/src/main/res/values-pt-rBR/strings.xml index 2c15237..cdcb12b 100644 --- a/androidApp/src/main/res/values-pt-rBR/strings.xml +++ b/androidApp/src/main/res/values-pt-rBR/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-pt-rPT/strings.xml b/androidApp/src/main/res/values-pt-rPT/strings.xml index e3fee58..a77e8a7 100644 --- a/androidApp/src/main/res/values-pt-rPT/strings.xml +++ b/androidApp/src/main/res/values-pt-rPT/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-si-rLK/strings.xml b/androidApp/src/main/res/values-si-rLK/strings.xml index 93f8145..bc46c4b 100644 --- a/androidApp/src/main/res/values-si-rLK/strings.xml +++ b/androidApp/src/main/res/values-si-rLK/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-tr-rTR/strings.xml b/androidApp/src/main/res/values-tr-rTR/strings.xml index e18c11a..be4fa36 100644 --- a/androidApp/src/main/res/values-tr-rTR/strings.xml +++ b/androidApp/src/main/res/values-tr-rTR/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-zh-rCN/strings.xml b/androidApp/src/main/res/values-zh-rCN/strings.xml index 0d351a0..d6a17ad 100644 --- a/androidApp/src/main/res/values-zh-rCN/strings.xml +++ b/androidApp/src/main/res/values-zh-rCN/strings.xml @@ -127,4 +127,5 @@ 筛选器 This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values-zh-rTW/strings.xml b/androidApp/src/main/res/values-zh-rTW/strings.xml index 0d586dd..523da1f 100644 --- a/androidApp/src/main/res/values-zh-rTW/strings.xml +++ b/androidApp/src/main/res/values-zh-rTW/strings.xml @@ -127,4 +127,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml index a649edf..ab8b0bf 100644 --- a/androidApp/src/main/res/values/strings.xml +++ b/androidApp/src/main/res/values/strings.xml @@ -130,4 +130,5 @@ Filters This app only works with a Selfoss instance, and no other RSS feed. Sources + Update source diff --git a/androidApp/src/test/kotlin/RepositoryTest.kt b/androidApp/src/test/kotlin/RepositoryTest.kt index 28091c4..21f7028 100644 --- a/androidApp/src/test/kotlin/RepositoryTest.kt +++ b/androidApp/src/test/kotlin/RepositoryTest.kt @@ -231,7 +231,8 @@ class RepositoryTest { listOf("tags"), "spouts\\rss\\fulltextrss", "", - "b3aa8a664d08eb15d6ff1db2fa83e0d9.png" + "b3aa8a664d08eb15d6ff1db2fa83e0d9.png", + SelfossModel.SourceParams("url") )) runBlocking { repository.getNewerItems() @@ -551,7 +552,8 @@ class RepositoryTest { listOf("Test", "second"), "spouts\\rss\\fulltextrss", "", - "d8c92cdb1ef119ea85c4b9205c879ca7.png" + "d8c92cdb1ef119ea85c4b9205c879ca7.png", + SelfossModel.SourceParams("url") ), SelfossModel.Source( 2, @@ -559,7 +561,8 @@ class RepositoryTest { listOf("second"), "spouts\\rss\\fulltextrss", "", - "b3aa8a664d08eb15d6ff1db2fa83e0d9.png" + "b3aa8a664d08eb15d6ff1db2fa83e0d9.png", + SelfossModel.SourceParams("url") ) ) val sourcesDB = listOf( @@ -569,7 +572,8 @@ class RepositoryTest { "Test,second", "spouts\\rss\\fulltextrss", "", - "d8c92cdb1ef119ea85c4b9205c879ca7.png" + "d8c92cdb1ef119ea85c4b9205c879ca7.png", + "url" ), SOURCE( "2", @@ -577,7 +581,8 @@ class RepositoryTest { "second", "spouts\\rss\\fulltextrss", "", - "b3aa8a664d08eb15d6ff1db2fa83e0d9.png" + "b3aa8a664d08eb15d6ff1db2fa83e0d9.png", + "url" ) ) @@ -707,7 +712,7 @@ class RepositoryTest { @Test fun create_source() { - coEvery { api.createSourceForVersion(any(), any(), any(), any(), any()) } returns + coEvery { api.createSourceForVersion(any(), any(), any(), any()) } returns SuccessResponse(true) initializeRepository() @@ -718,7 +723,6 @@ class RepositoryTest { "https://test.com/feed", "spouts\\rss\\fulltextrss", "Test, New", - "" ) } @@ -728,7 +732,6 @@ class RepositoryTest { any(), any(), any(), - any(), ) } assertSame(true, response) @@ -736,7 +739,7 @@ class RepositoryTest { @Test fun create_source_but_response_fails() { - coEvery { api.createSourceForVersion(any(), any(), any(), any(), any()) } returns + coEvery { api.createSourceForVersion(any(), any(), any(), any()) } returns SuccessResponse(false) initializeRepository() @@ -746,8 +749,7 @@ class RepositoryTest { "test", "https://test.com/feed", "spouts\\rss\\fulltextrss", - "Test, New", - "" + "Test, New" ) } @@ -756,8 +758,7 @@ class RepositoryTest { any(), any(), any(), - any(), - any(), + any() ) } assertSame(false, response) @@ -765,7 +766,7 @@ class RepositoryTest { @Test fun create_source_without_connection() { - coEvery { api.createSourceForVersion(any(), any(), any(), any(), any()) } returns + coEvery { api.createSourceForVersion(any(), any(), any(), any()) } returns SuccessResponse(true) initializeRepository(MutableStateFlow(false)) @@ -775,8 +776,7 @@ class RepositoryTest { "test", "https://test.com/feed", "spouts\\rss\\fulltextrss", - "Test, New", - "" + "Test, New" ) } @@ -786,7 +786,6 @@ class RepositoryTest { any(), any(), any(), - any() ) } assertSame(false, response) @@ -1034,7 +1033,8 @@ class RepositoryTest { listOf("Test", "second"), "spouts\\rss\\fulltextrss", "", - "d8c92cdb1ef119ea85c4b9205c879ca7.png" + "d8c92cdb1ef119ea85c4b9205c879ca7.png", + SelfossModel.SourceParams("url") ) ) repository.searchFilter = "search" diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt index 5d4d5c4..aec8acd 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt @@ -9,6 +9,7 @@ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.encoding.encodeCollection import kotlinx.serialization.json.* class SelfossModel { @@ -55,7 +56,12 @@ class SelfossModel { val tags: List, val spout: String, val error: String, - val icon: String? + val icon: String?, + val params: SourceParams? + ) + @Serializable + data class SourceParams( + val url: String ) @Serializable data class Item( @@ -122,7 +128,7 @@ class SelfossModel { object TagsListSerializer : KSerializer> { override fun deserialize(decoder: Decoder): List { return when(val json = ((decoder as JsonDecoder).decodeJsonElement())) { - is JsonArray -> json.toList().map { it.toString() } + is JsonArray -> json.toList().map { it.toString().replace("^\"|\"$".toRegex(), "") } else -> json.toString().split(",") } @@ -132,7 +138,7 @@ class SelfossModel { get() = PrimitiveSerialDescriptor("tags", PrimitiveKind.STRING) override fun serialize(encoder: Encoder, value: List) { - TODO("Not yet implemented") + encoder.encodeCollection(PrimitiveSerialDescriptor("tags", PrimitiveKind.STRING), value.size) { this.toString() } } } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index 9270167..04457ac 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -48,6 +48,7 @@ class Repository( private var fetchedTags = false private var _readerItems = ArrayList() + private var _selectedSource: SelfossModel.Source? = null suspend fun getNewerItems(): ArrayList { // TODO: Use the updatedSince parameter @@ -343,7 +344,6 @@ class Repository( url: String, spout: String, tags: String, - filter: String ): Boolean { var response = false if (isNetworkAvailable()) { @@ -352,13 +352,27 @@ class Repository( url, spout, tags, - filter ).isSuccess == true } return response } + suspend fun updateSource( + id: Int, + title: String, + url: String, + spout: String, + tags: String + ): Boolean { + var response = false + if (isNetworkAvailable()) { + response = api.updateSourceForVersion(id, title, url, spout, tags).isSuccess == true + } + + return response + } + suspend fun deleteSource(id: Int, title: String): Boolean { var success = false if (isNetworkAvailable()) { @@ -580,4 +594,16 @@ class Repository( fun migrate(driverFactory: DriverFactory) { ReaderForSelfossDB.Schema.migrate(driverFactory.createDriver(), 0, 1) } + + fun setSelectedSource(source: SelfossModel.Source) { + _selectedSource = source + } + + fun unsetSelectedSource() { + _selectedSource = null + } + + fun getSelectedSource(): SelfossModel.Source? { + return _selectedSource + } } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/RestUtils.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/RestUtils.kt index 4fc3fbc..05b194a 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/RestUtils.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/RestUtils.kt @@ -23,6 +23,9 @@ suspend fun maybeResponse(r: HttpResponse?): SuccessResponse { return if (r != null && r.status.isSuccess()) { r.body() } else { + if (r != null) { + Napier.i("Error ${r.status}", tag = "maybeResponse") + } SuccessResponse(false) } } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt index c36fcff..c41e157 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt @@ -76,10 +76,14 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { // Api version was introduces after the POST login, so when there is a version, it should be available private fun shouldHavePostLogin() = appSettingsService.getApiVersion() != -1 - private fun hasLoginInfo() = appSettingsService.getUserName().isNotEmpty() && appSettingsService.getPassword().isNotEmpty() + private fun hasLoginInfo() = + appSettingsService.getUserName().isNotEmpty() && appSettingsService.getPassword() + .isNotEmpty() suspend fun login(): SuccessResponse = - if (appSettingsService.getUserName().isNotEmpty() && appSettingsService.getPassword().isNotEmpty()) { + if (appSettingsService.getUserName().isNotEmpty() && appSettingsService.getPassword() + .isNotEmpty() + ) { if (shouldHavePostLogin()) { postLogin() } else { @@ -99,7 +103,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("password", appSettingsService.getPassword()) }) - private fun shouldHaveNewLogout() = appSettingsService.getApiVersion() >= 5 // We are missing 4.1.0 + private fun shouldHaveNewLogout() = + appSettingsService.getApiVersion() >= 5 // We are missing 4.1.0 + suspend fun logout(): SuccessResponse = if (shouldHaveNewLogout()) { doLogout() @@ -107,7 +113,8 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { maybeLogoutIfAvailable() } - private suspend fun maybeLogoutIfAvailable() = responseOrSuccessIf404(client.tryToGet(url("/logout"))) + private suspend fun maybeLogoutIfAvailable() = + responseOrSuccessIf404(client.tryToGet(url("/logout"))) private suspend fun doLogout() = maybeResponse(client.tryToDelete(url("/api/session/current"))) @@ -236,13 +243,12 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { url: String, spout: String, tags: String, - filter: String ): SuccessResponse = maybeResponse( if (appSettingsService.getApiVersion() > 1) { - createSource("tags[]", title, url, spout, tags, filter) + createSource("tags[]", title, url, spout, tags) } else { - createSource("tags", title, url, spout, tags, filter) + createSource("tags", title, url, spout, tags) } ) @@ -251,8 +257,7 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { title: String, url: String, spout: String, - tags: String, - filter: String + tags: String ): HttpResponse? = client.tryToSubmitForm( url = url("/source"), @@ -265,7 +270,43 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { append("url", url) append("spout", spout) append(tagsParamName, tags) - append("filter", filter) + } + ) + + suspend fun updateSourceForVersion( + id: Int, + title: String, + url: String, + spout: String, + tags: String + ): SuccessResponse = + maybeResponse( + if (appSettingsService.getApiVersion() > 1) { + updateSource(id, "tags[]", title, url, spout, tags) + } else { + updateSource(id, "tags", title, url, spout, tags) + } + ) + + private suspend fun updateSource( + id: Int, + tagsParamName: String, + title: String, + url: String, + spout: String, + tags: String, + ): HttpResponse? = + client.tryToSubmitForm( + url = url("/source/$id"), + formParameters = Parameters.build { + if (!shouldHavePostLogin()) { + append("username", appSettingsService.getUserName()) + append("password", appSettingsService.getPassword()) + } + append("title", title) + append("url", url) + append("spout", spout) + append(tagsParamName, tags) } ) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt index 248e463..875da49 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt @@ -19,7 +19,8 @@ fun SOURCE.toView(): SelfossModel.Source = this.tags.split(","), this.spout, this.error, - this.icon + this.icon, + if (this.url != null) SelfossModel.SourceParams(this.url) else null ) fun SelfossModel.Source.toEntity(): SOURCE = @@ -29,7 +30,8 @@ fun SelfossModel.Source.toEntity(): SOURCE = this.tags.joinToString(","), this.spout, this.error, - this.icon.orEmpty() + this.icon.orEmpty(), + this.params?.url ) fun SelfossModel.Tag.toEntity(): TAG = diff --git a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/2.sqm b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/2.sqm new file mode 100644 index 0000000..21bc99d --- /dev/null +++ b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/2.sqm @@ -0,0 +1 @@ +ALTER TABLE SOURCE ADD COLUMN `url` TEXT; \ No newline at end of file diff --git a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq index 7bd882b..9dd5082 100644 --- a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq +++ b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq @@ -5,6 +5,7 @@ CREATE TABLE SOURCE ( `spout` TEXT NOT NULL, `error` TEXT NOT NULL, `icon` TEXT NOT NULL, + `url` TEXT, PRIMARY KEY(`id`) );