From 4cddd2c5e6e445ba05e685a7a2496d97f443657a Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Sun, 11 Jun 2023 19:59:26 +0200 Subject: [PATCH] Add delete button to edit filter activity. (#3553) Adds workaround for #3545 --- .../components/filters/EditFilterActivity.kt | 32 +++++++++++++++++++ .../main/res/layout/activity_edit_filter.xml | 30 +++++++++++++---- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/filters/EditFilterActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/filters/EditFilterActivity.kt index 66407394..2ef7902c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/filters/EditFilterActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/filters/EditFilterActivity.kt @@ -11,6 +11,7 @@ import androidx.core.content.IntentCompat import androidx.core.view.size import androidx.core.widget.doAfterTextChanged import androidx.lifecycle.lifecycleScope +import at.connyduck.calladapter.networkresult.fold import com.google.android.material.chip.Chip import com.google.android.material.snackbar.Snackbar import com.google.android.material.switchmaterial.SwitchMaterial @@ -24,7 +25,9 @@ import com.keylesspalace.tusky.entity.Filter import com.keylesspalace.tusky.entity.FilterKeyword import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.util.viewBinding +import com.keylesspalace.tusky.util.visible import kotlinx.coroutines.launch +import retrofit2.HttpException import java.util.Date import javax.inject.Inject @@ -78,6 +81,9 @@ class EditFilterActivity : BaseActivity() { binding.actionChip.setOnClickListener { showAddKeywordDialog() } binding.filterSaveButton.setOnClickListener { saveChanges() } + binding.filterDeleteButton.setOnClickListener { deleteFilter() } + binding.filterDeleteButton.visible(originalFilter != null) + for (switch in contextSwitches.keys) { switch.setOnCheckedChangeListener { _, isChecked -> val context = contextSwitches[switch]!! @@ -259,6 +265,32 @@ class EditFilterActivity : BaseActivity() { } } + private fun deleteFilter() { + originalFilter?.let { filter -> + lifecycleScope.launch { + api.deleteFilter(filter.id).fold( + { + finish() + }, + { throwable -> + if (throwable is HttpException && throwable.code() == 404) { + api.deleteFilterV1(filter.id).fold( + { + finish() + }, + { + Snackbar.make(binding.root, "Error deleting filter '${filter.title}'", Snackbar.LENGTH_SHORT).show() + } + ) + } else { + Snackbar.make(binding.root, "Error deleting filter '${filter.title}'", Snackbar.LENGTH_SHORT).show() + } + } + ) + } + } + } + companion object { const val FILTER_TO_EDIT = "FilterToEdit" diff --git a/app/src/main/res/layout/activity_edit_filter.xml b/app/src/main/res/layout/activity_edit_filter.xml index 3aa8e7e4..755aab5a 100644 --- a/app/src/main/res/layout/activity_edit_filter.xml +++ b/app/src/main/res/layout/activity_edit_filter.xml @@ -144,14 +144,30 @@ android:minHeight="48dp" android:text="@string/pref_title_account_filter_keywords" /> -