From fb66293713fa613263de839bb209178fdc18df72 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Thu, 29 Feb 2024 09:49:17 +0100 Subject: [PATCH] refactor: Remove duplicate strings from Filter.Action (#479) Simplify code that users `Filter.Action` by deserialising directly into the type instead of storing / using strings throughout the API. --- app/src/main/java/app/pachli/StatusListActivity.kt | 2 +- .../app/pachli/components/filters/EditFilterViewModel.kt | 6 +++--- .../main/kotlin/app/pachli/core/network/model/Filter.kt | 8 ++++---- .../app/pachli/core/network/retrofit/MastodonApi.kt | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/app/pachli/StatusListActivity.kt b/app/src/main/java/app/pachli/StatusListActivity.kt index a7baeaaee..09639b9fe 100644 --- a/app/src/main/java/app/pachli/StatusListActivity.kt +++ b/app/src/main/java/app/pachli/StatusListActivity.kt @@ -290,7 +290,7 @@ class StatusListActivity : BottomSheetActivity(), AppBarLayoutHost, ActionButton mastodonApi.createFilter( title = tagWithHash, context = listOf(FilterContext.HOME), - filterAction = Filter.Action.WARN.action, + filterAction = Filter.Action.WARN, expiresInSeconds = null, ).fold( { filter -> diff --git a/app/src/main/java/app/pachli/components/filters/EditFilterViewModel.kt b/app/src/main/java/app/pachli/components/filters/EditFilterViewModel.kt index c9611f761..060174a83 100644 --- a/app/src/main/java/app/pachli/components/filters/EditFilterViewModel.kt +++ b/app/src/main/java/app/pachli/components/filters/EditFilterViewModel.kt @@ -87,7 +87,7 @@ class EditFilterViewModel @Inject constructor(val api: MastodonApi, val eventHub val contexts = contexts.value val title = title.value val durationIndex = duration.value - val action = action.value.action + val action = action.value return withContext(viewModelScope.coroutineContext) { val success = originalFilter?.let { filter -> @@ -108,7 +108,7 @@ class EditFilterViewModel @Inject constructor(val api: MastodonApi, val eventHub } } - private suspend fun createFilter(title: String, contexts: List, action: String, durationIndex: Int, context: Context): Boolean { + private suspend fun createFilter(title: String, contexts: List, action: Filter.Action, durationIndex: Int, context: Context): Boolean { val expiresInSeconds = EditFilterActivity.getSecondsForDurationIndex(durationIndex, context) api.createFilter( title = title, @@ -132,7 +132,7 @@ class EditFilterViewModel @Inject constructor(val api: MastodonApi, val eventHub ) } - private suspend fun updateFilter(originalFilter: Filter, title: String, contexts: List, action: String, durationIndex: Int, context: Context): Boolean { + private suspend fun updateFilter(originalFilter: Filter, title: String, contexts: List, action: Filter.Action, durationIndex: Int, context: Context): Boolean { val expiresInSeconds = EditFilterActivity.getSecondsForDurationIndex(durationIndex, context) api.updateFilter( id = originalFilter.id, diff --git a/core/network/src/main/kotlin/app/pachli/core/network/model/Filter.kt b/core/network/src/main/kotlin/app/pachli/core/network/model/Filter.kt index 41e12092f..9035bc13f 100644 --- a/core/network/src/main/kotlin/app/pachli/core/network/model/Filter.kt +++ b/core/network/src/main/kotlin/app/pachli/core/network/model/Filter.kt @@ -25,15 +25,15 @@ data class Filter( // val statuses: List, ) : Parcelable { @HasDefault - enum class Action(val action: String) { + enum class Action { @Json(name = "none") - NONE("none"), + NONE, @Json(name = "warn") @Default - WARN("warn"), + WARN, @Json(name = "hide") - HIDE("hide"), + HIDE, } } diff --git a/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt b/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt index 341830761..9b2dc6436 100644 --- a/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt +++ b/core/network/src/main/kotlin/app/pachli/core/network/retrofit/MastodonApi.kt @@ -642,7 +642,7 @@ interface MastodonApi { suspend fun createFilter( @Field("title") title: String, @Field("context[]") context: List, - @Field("filter_action") filterAction: String, + @Field("filter_action") filterAction: Filter.Action, @Field("expires_in") expiresInSeconds: Int?, ): NetworkResult @@ -652,7 +652,7 @@ interface MastodonApi { @Path("id") id: String, @Field("title") title: String? = null, @Field("context[]") context: List? = null, - @Field("filter_action") filterAction: String? = null, + @Field("filter_action") filterAction: Filter.Action? = null, @Field("expires_in") expiresInSeconds: Int? = null, ): NetworkResult