Prompt the user before deleting a filter
Prevent users from accidentally deleting filters by prompting them to confirm. Add an AlertDialog extension that converts AlertDialog callbacks to linear control flow. Fixes #3736.
This commit is contained in:
parent
22b7afb62d
commit
85f0b1f320
|
@ -1,6 +1,7 @@
|
||||||
package com.keylesspalace.tusky.components.filters
|
package com.keylesspalace.tusky.components.filters
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.DialogInterface.BUTTON_POSITIVE
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.AdapterView
|
import android.widget.AdapterView
|
||||||
|
@ -81,7 +82,11 @@ class EditFilterActivity : BaseActivity() {
|
||||||
|
|
||||||
binding.actionChip.setOnClickListener { showAddKeywordDialog() }
|
binding.actionChip.setOnClickListener { showAddKeywordDialog() }
|
||||||
binding.filterSaveButton.setOnClickListener { saveChanges() }
|
binding.filterSaveButton.setOnClickListener { saveChanges() }
|
||||||
binding.filterDeleteButton.setOnClickListener { deleteFilter() }
|
binding.filterDeleteButton.setOnClickListener {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
if (showDeleteFilterDialog(filter.title) == BUTTON_POSITIVE) deleteFilter()
|
||||||
|
}
|
||||||
|
}
|
||||||
binding.filterDeleteButton.visible(originalFilter != null)
|
binding.filterDeleteButton.visible(originalFilter != null)
|
||||||
|
|
||||||
for (switch in contextSwitches.keys) {
|
for (switch in contextSwitches.keys) {
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 Tusky Contributors
|
||||||
|
*
|
||||||
|
* This file is a part of Tusky.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||||
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||||
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||||
|
* Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||||
|
* see <http://www.gnu.org/licenses>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.keylesspalace.tusky.components.filters
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import com.keylesspalace.tusky.R
|
||||||
|
import com.keylesspalace.tusky.util.await
|
||||||
|
|
||||||
|
internal suspend fun Activity.showDeleteFilterDialog(filterTitle: String) = AlertDialog.Builder(this)
|
||||||
|
.setMessage(getString(R.string.dialog_delete_filter_text, filterTitle))
|
||||||
|
.setCancelable(true)
|
||||||
|
.create()
|
||||||
|
.await(R.string.dialog_delete_filter_positive_action, android.R.string.cancel)
|
|
@ -1,5 +1,6 @@
|
||||||
package com.keylesspalace.tusky.components.filters
|
package com.keylesspalace.tusky.components.filters
|
||||||
|
|
||||||
|
import android.content.DialogInterface.BUTTON_POSITIVE
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
|
@ -104,8 +105,12 @@ class FiltersActivity : BaseActivity(), FiltersListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deleteFilter(filter: Filter) {
|
override fun deleteFilter(filter: Filter) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
if (showDeleteFilterDialog(filter.title) == BUTTON_POSITIVE) {
|
||||||
viewModel.deleteFilter(filter, binding.root)
|
viewModel.deleteFilter(filter, binding.root)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun updateFilter(updatedFilter: Filter) {
|
override fun updateFilter(updatedFilter: Filter) {
|
||||||
launchEditFilterActivity(updatedFilter)
|
launchEditFilterActivity(updatedFilter)
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 Tusky Contributors
|
||||||
|
*
|
||||||
|
* This file is a part of Tusky.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||||
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||||
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||||
|
* Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||||
|
* see <http://www.gnu.org/licenses>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.keylesspalace.tusky.util
|
||||||
|
|
||||||
|
import android.content.DialogInterface
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for the alert dialog buttons to be clicked, return the ID of the clicked button
|
||||||
|
*
|
||||||
|
* @param positiveText Text to show on the positive button
|
||||||
|
* @param negativeText Optional text to show on the negative button
|
||||||
|
* @param neutralText Optional text to show on the neutral button
|
||||||
|
*/
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
suspend fun AlertDialog.await(
|
||||||
|
positiveText: String,
|
||||||
|
negativeText: String? = null,
|
||||||
|
neutralText: String? = null
|
||||||
|
) = suspendCancellableCoroutine<Int> { cont ->
|
||||||
|
val listener = DialogInterface.OnClickListener { _, which ->
|
||||||
|
cont.resume(which) { dismiss() }
|
||||||
|
}
|
||||||
|
|
||||||
|
setButton(AlertDialog.BUTTON_POSITIVE, positiveText, listener)
|
||||||
|
negativeText?.let { setButton(AlertDialog.BUTTON_NEGATIVE, it, listener) }
|
||||||
|
neutralText?.let { setButton(AlertDialog.BUTTON_NEUTRAL, it, listener) }
|
||||||
|
|
||||||
|
setOnCancelListener { cont.cancel() }
|
||||||
|
cont.invokeOnCancellation { dismiss() }
|
||||||
|
show()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see [AlertDialog.await]
|
||||||
|
*/
|
||||||
|
suspend fun AlertDialog.await(
|
||||||
|
@StringRes positiveTextResource: Int,
|
||||||
|
@StringRes negativeTextResource: Int? = null,
|
||||||
|
@StringRes neutralTextResource: Int? = null
|
||||||
|
) = await(
|
||||||
|
context.getString(positiveTextResource),
|
||||||
|
negativeTextResource?.let { context.getString(it) },
|
||||||
|
neutralTextResource?.let { context.getString(it) }
|
||||||
|
)
|
|
@ -818,4 +818,6 @@
|
||||||
<string name="about_copy">Copy version and device information</string>
|
<string name="about_copy">Copy version and device information</string>
|
||||||
<string name="about_copied">Copied version and device information</string>
|
<string name="about_copied">Copied version and device information</string>
|
||||||
<string name="error_media_playback">Playback failed: %s</string>
|
<string name="error_media_playback">Playback failed: %s</string>
|
||||||
|
<string name="dialog_delete_filter_text">Delete filter \'%1$s\'?"</string>
|
||||||
|
<string name="dialog_delete_filter_positive_action">Delete</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue