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.
This commit is contained in:
Nik Clayton 2024-02-29 09:49:17 +01:00 committed by GitHub
parent af58de5a8f
commit fb66293713
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 10 additions and 10 deletions

View File

@ -290,7 +290,7 @@ class StatusListActivity : BottomSheetActivity(), AppBarLayoutHost, ActionButton
mastodonApi.createFilter( mastodonApi.createFilter(
title = tagWithHash, title = tagWithHash,
context = listOf(FilterContext.HOME), context = listOf(FilterContext.HOME),
filterAction = Filter.Action.WARN.action, filterAction = Filter.Action.WARN,
expiresInSeconds = null, expiresInSeconds = null,
).fold( ).fold(
{ filter -> { filter ->

View File

@ -87,7 +87,7 @@ class EditFilterViewModel @Inject constructor(val api: MastodonApi, val eventHub
val contexts = contexts.value val contexts = contexts.value
val title = title.value val title = title.value
val durationIndex = duration.value val durationIndex = duration.value
val action = action.value.action val action = action.value
return withContext(viewModelScope.coroutineContext) { return withContext(viewModelScope.coroutineContext) {
val success = originalFilter?.let { filter -> 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<FilterContext>, action: String, durationIndex: Int, context: Context): Boolean { private suspend fun createFilter(title: String, contexts: List<FilterContext>, action: Filter.Action, durationIndex: Int, context: Context): Boolean {
val expiresInSeconds = EditFilterActivity.getSecondsForDurationIndex(durationIndex, context) val expiresInSeconds = EditFilterActivity.getSecondsForDurationIndex(durationIndex, context)
api.createFilter( api.createFilter(
title = title, 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<FilterContext>, action: String, durationIndex: Int, context: Context): Boolean { private suspend fun updateFilter(originalFilter: Filter, title: String, contexts: List<FilterContext>, action: Filter.Action, durationIndex: Int, context: Context): Boolean {
val expiresInSeconds = EditFilterActivity.getSecondsForDurationIndex(durationIndex, context) val expiresInSeconds = EditFilterActivity.getSecondsForDurationIndex(durationIndex, context)
api.updateFilter( api.updateFilter(
id = originalFilter.id, id = originalFilter.id,

View File

@ -25,15 +25,15 @@ data class Filter(
// val statuses: List<FilterStatus>, // val statuses: List<FilterStatus>,
) : Parcelable { ) : Parcelable {
@HasDefault @HasDefault
enum class Action(val action: String) { enum class Action {
@Json(name = "none") @Json(name = "none")
NONE("none"), NONE,
@Json(name = "warn") @Json(name = "warn")
@Default @Default
WARN("warn"), WARN,
@Json(name = "hide") @Json(name = "hide")
HIDE("hide"), HIDE,
} }
} }

View File

@ -642,7 +642,7 @@ interface MastodonApi {
suspend fun createFilter( suspend fun createFilter(
@Field("title") title: String, @Field("title") title: String,
@Field("context[]") context: List<FilterContext>, @Field("context[]") context: List<FilterContext>,
@Field("filter_action") filterAction: String, @Field("filter_action") filterAction: Filter.Action,
@Field("expires_in") expiresInSeconds: Int?, @Field("expires_in") expiresInSeconds: Int?,
): NetworkResult<Filter> ): NetworkResult<Filter>
@ -652,7 +652,7 @@ interface MastodonApi {
@Path("id") id: String, @Path("id") id: String,
@Field("title") title: String? = null, @Field("title") title: String? = null,
@Field("context[]") context: List<FilterContext>? = null, @Field("context[]") context: List<FilterContext>? = null,
@Field("filter_action") filterAction: String? = null, @Field("filter_action") filterAction: Filter.Action? = null,
@Field("expires_in") expiresInSeconds: Int? = null, @Field("expires_in") expiresInSeconds: Int? = null,
): NetworkResult<Filter> ): NetworkResult<Filter>