Source update screen.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
aminecmi
2022-12-28 22:09:31 +01:00
parent c38251f5b3
commit 3c68bde62b
31 changed files with 231 additions and 102 deletions

View File

@ -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<String>,
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<List<String>> {
override fun deserialize(decoder: Decoder): List<String> {
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<String>) {
TODO("Not yet implemented")
encoder.encodeCollection(PrimitiveSerialDescriptor("tags", PrimitiveKind.STRING), value.size) { this.toString() }
}
}

View File

@ -48,6 +48,7 @@ class Repository(
private var fetchedTags = false
private var _readerItems = ArrayList<SelfossModel.Item>()
private var _selectedSource: SelfossModel.Source? = null
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
// 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
}
}

View File

@ -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)
}
}

View File

@ -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)
}
)

View File

@ -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 =

View File

@ -0,0 +1 @@
ALTER TABLE SOURCE ADD COLUMN `url` TEXT;

View File

@ -5,6 +5,7 @@ CREATE TABLE SOURCE (
`spout` TEXT NOT NULL,
`error` TEXT NOT NULL,
`icon` TEXT NOT NULL,
`url` TEXT,
PRIMARY KEY(`id`)
);