Remove kotlin-extensions from FilterActivity

This commit is contained in:
Adolfo Santiago 2022-05-08 12:22:34 +02:00
parent cd69679e9a
commit c9b820bb78
No known key found for this signature in database
GPG key ID: 244D6F9A317B4A65
2 changed files with 233 additions and 159 deletions

View file

@ -1,3 +1,23 @@
/*
* Husky -- A Pleroma client for Android
*
* Copyright (C) 2022 The Husky Developers
* Copyright (C) 2019 Tusky Contributors
*
* 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.keylesspalace.tusky package com.keylesspalace.tusky
import android.os.Bundle import android.os.Bundle
@ -8,41 +28,69 @@ import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import com.keylesspalace.tusky.appstore.EventHub import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
import com.keylesspalace.tusky.core.extensions.viewBinding
import com.keylesspalace.tusky.databinding.ActivityFiltersBinding
import com.keylesspalace.tusky.databinding.DialogFilterBinding
import com.keylesspalace.tusky.entity.Filter import com.keylesspalace.tusky.entity.Filter
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.hide import com.keylesspalace.tusky.util.hide
import com.keylesspalace.tusky.util.show import com.keylesspalace.tusky.util.show
import kotlinx.android.synthetic.main.activity_filters.* import java.io.IOException
import kotlinx.android.synthetic.main.dialog_filter.* import javax.inject.Inject
import kotlinx.android.synthetic.main.toolbar_basic.*
import okhttp3.ResponseBody import okhttp3.ResponseBody
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
import java.io.IOException
import javax.inject.Inject
class FiltersActivity : BaseActivity() { class FiltersActivity : BaseActivity() {
@Inject @Inject
lateinit var api: MastodonApi lateinit var api: MastodonApi
@Inject @Inject
lateinit var eventHub: EventHub lateinit var eventHub: EventHub
private val binding by viewBinding(ActivityFiltersBinding::inflate)
private lateinit var context: String private lateinit var context: String
private lateinit var filters: MutableList<Filter> private lateinit var filters: MutableList<Filter>
private lateinit var dialog: AlertDialog
companion object { companion object {
const val FILTERS_CONTEXT = "filters_context" const val FILTERS_CONTEXT = "filters_context"
const val FILTERS_TITLE = "filters_title" const val FILTERS_TITLE = "filters_title"
} }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
setupToolbarBackArrow()
binding.addFilterButton.setOnClickListener {
showAddFilterDialog()
}
title = intent?.getStringExtra(FILTERS_TITLE)
context = intent?.getStringExtra(FILTERS_CONTEXT)!!
loadFilters()
}
private fun updateFilter(filter: Filter, itemIndex: Int) { private fun updateFilter(filter: Filter, itemIndex: Int) {
api.updateFilter(filter.id, MastodonApi.PostFilter(filter.phrase, filter.context, filter.irreversible, filter.wholeWord, filter.expiresAt)) api.updateFilter(
.enqueue(object: Callback<Filter>{ filter.id,
MastodonApi.PostFilter(
filter.phrase,
filter.context,
filter.irreversible,
filter.wholeWord,
filter.expiresAt
)
).enqueue(object : Callback<Filter> {
override fun onFailure(call: Call<Filter>, t: Throwable) { override fun onFailure(call: Call<Filter>, t: Throwable) {
Toast.makeText(this@FiltersActivity, "Error updating filter '${filter.phrase}'", Toast.LENGTH_SHORT).show() Toast.makeText(
this@FiltersActivity,
"Error updating filter '${filter.phrase}'",
Toast.LENGTH_SHORT
).show()
} }
override fun onResponse(call: Call<Filter>, response: Response<Filter>) { override fun onResponse(call: Call<Filter>, response: Response<Filter>) {
@ -64,10 +112,17 @@ class FiltersActivity: BaseActivity() {
// This is the only context for this filter; delete it // This is the only context for this filter; delete it
api.deleteFilter(filters[itemIndex].id).enqueue(object : Callback<ResponseBody> { api.deleteFilter(filters[itemIndex].id).enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) { override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
Toast.makeText(this@FiltersActivity, "Error updating filter '${filters[itemIndex].phrase}'", Toast.LENGTH_SHORT).show() Toast.makeText(
this@FiltersActivity,
"Error updating filter '${filters[itemIndex].phrase}'",
Toast.LENGTH_SHORT
).show()
} }
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) { override fun onResponse(
call: Call<ResponseBody>,
response: Response<ResponseBody>
) {
filters.removeAt(itemIndex) filters.removeAt(itemIndex)
refreshFilterDisplay() refreshFilterDisplay()
eventHub.dispatch(PreferenceChangedEvent(context)) eventHub.dispatch(PreferenceChangedEvent(context))
@ -76,8 +131,10 @@ class FiltersActivity: BaseActivity() {
} else { } else {
// Keep the filter, but remove it from this context // Keep the filter, but remove it from this context
val oldFilter = filters[itemIndex] val oldFilter = filters[itemIndex]
val newFilter = Filter(oldFilter.id, oldFilter.phrase, oldFilter.context.filter { c -> c != context }, val newFilter = Filter(
oldFilter.expiresAt, oldFilter.irreversible, oldFilter.wholeWord) oldFilter.id, oldFilter.phrase, oldFilter.context.filter { c -> c != context },
oldFilter.expiresAt, oldFilter.irreversible, oldFilter.wholeWord
)
updateFilter(newFilter, itemIndex) updateFilter(newFilter, itemIndex)
} }
} }
@ -92,37 +149,61 @@ class FiltersActivity: BaseActivity() {
refreshFilterDisplay() refreshFilterDisplay()
eventHub.dispatch(PreferenceChangedEvent(context)) eventHub.dispatch(PreferenceChangedEvent(context))
} else { } else {
Toast.makeText(this@FiltersActivity, "Error creating filter '$phrase'", Toast.LENGTH_SHORT).show() Toast.makeText(
this@FiltersActivity,
"Error creating filter '$phrase'",
Toast.LENGTH_SHORT
).show()
} }
} }
override fun onFailure(call: Call<Filter>, t: Throwable) { override fun onFailure(call: Call<Filter>, t: Throwable) {
Toast.makeText(this@FiltersActivity, "Error creating filter '$phrase'", Toast.LENGTH_SHORT).show() Toast.makeText(
this@FiltersActivity,
"Error creating filter '$phrase'",
Toast.LENGTH_SHORT
).show()
} }
}) })
} }
private fun showAddFilterDialog() { private fun showAddFilterDialog() {
dialog = AlertDialog.Builder(this@FiltersActivity) val dialogBind = DialogFilterBinding.inflate(layoutInflater)
dialogBind.phraseWholeWord.isChecked = true
val dialog = AlertDialog.Builder(this@FiltersActivity)
.setTitle(R.string.filter_addition_dialog_title) .setTitle(R.string.filter_addition_dialog_title)
.setView(R.layout.dialog_filter) .setView(dialogBind.root)
.setPositiveButton(android.R.string.ok) { _, _ -> .setPositiveButton(android.R.string.ok) { _, _ ->
createFilter(dialog.phraseEditText.text.toString(), dialog.phraseWholeWord.isChecked) createFilter(
dialogBind.phraseEditText.text.toString(),
dialogBind.phraseWholeWord.isChecked
)
} }
.setNeutralButton(android.R.string.cancel, null) .setNeutralButton(android.R.string.cancel, null)
.create() .create()
dialog.show() dialog.show()
dialog.phraseWholeWord.isChecked = true
} }
private fun setupEditDialogForItem(itemIndex: Int) { private fun setupEditDialogForItem(itemIndex: Int) {
dialog = AlertDialog.Builder(this@FiltersActivity) // Need to show the dialog before referencing any elements from its view
val filter = filters[itemIndex]
val dialogBind = DialogFilterBinding.inflate(layoutInflater)
dialogBind.phraseEditText.setText(filter.phrase)
dialogBind.phraseWholeWord.isChecked = filter.wholeWord
val dialog = AlertDialog.Builder(this@FiltersActivity)
.setTitle(R.string.filter_edit_dialog_title) .setTitle(R.string.filter_edit_dialog_title)
.setView(R.layout.dialog_filter) .setView(R.layout.dialog_filter)
.setPositiveButton(R.string.filter_dialog_update_button) { _, _ -> .setPositiveButton(R.string.filter_dialog_update_button) { _, _ ->
val oldFilter = filters[itemIndex] val oldFilter = filters[itemIndex]
val newFilter = Filter(oldFilter.id, dialog.phraseEditText.text.toString(), oldFilter.context, val newFilter = Filter(
oldFilter.expiresAt, oldFilter.irreversible, dialog.phraseWholeWord.isChecked) oldFilter.id,
dialogBind.phraseEditText.text.toString(),
oldFilter.context,
oldFilter.expiresAt,
oldFilter.irreversible,
dialogBind.phraseWholeWord.isChecked
)
updateFilter(newFilter, itemIndex) updateFilter(newFilter, itemIndex)
} }
.setNegativeButton(R.string.filter_dialog_remove_button) { _, _ -> .setNegativeButton(R.string.filter_dialog_remove_button) { _, _ ->
@ -131,74 +212,65 @@ class FiltersActivity: BaseActivity() {
.setNeutralButton(android.R.string.cancel, null) .setNeutralButton(android.R.string.cancel, null)
.create() .create()
dialog.show() dialog.show()
// Need to show the dialog before referencing any elements from its view
val filter = filters[itemIndex]
dialog.phraseEditText.setText(filter.phrase)
dialog.phraseWholeWord.isChecked = filter.wholeWord
} }
private fun refreshFilterDisplay() { private fun refreshFilterDisplay() {
filtersView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, filters.map { filter -> filter.phrase }) binding.filtersView.adapter = ArrayAdapter(
filtersView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ -> setupEditDialogForItem(position) } this,
android.R.layout.simple_list_item_1,
filters.map { filter -> filter.phrase })
binding.filtersView.onItemClickListener =
AdapterView.OnItemClickListener { _, _, position, _ -> setupEditDialogForItem(position) }
} }
private fun loadFilters() { private fun loadFilters() {
binding.filterMessageView.hide()
filterMessageView.hide() binding.filtersView.hide()
filtersView.hide() binding.addFilterButton.hide()
addFilterButton.hide() binding.filterProgressBar.show()
filterProgressBar.show()
api.getFilters().enqueue(object : Callback<List<Filter>> { api.getFilters().enqueue(object : Callback<List<Filter>> {
override fun onResponse(call: Call<List<Filter>>, response: Response<List<Filter>>) { override fun onResponse(call: Call<List<Filter>>, response: Response<List<Filter>>) {
val filterResponse = response.body() val filterResponse = response.body()
if(response.isSuccessful && filterResponse != null) { if(response.isSuccessful && filterResponse != null) {
filters = filterResponse.filter { filter -> filter.context.contains(context) }.toMutableList() filters = filterResponse.filter { filter -> filter.context.contains(context) }
.toMutableList()
refreshFilterDisplay() refreshFilterDisplay()
filtersView.show() binding.filtersView.show()
addFilterButton.show() binding.addFilterButton.show()
filterProgressBar.hide() binding.filterProgressBar.hide()
} else { } else {
filterProgressBar.hide() binding.filterProgressBar.hide()
filterMessageView.show() binding.filterMessageView.show()
filterMessageView.setup(R.drawable.elephant_error, binding.filterMessageView.setup(
R.string.error_generic) { loadFilters() } R.drawable.elephant_error,
R.string.error_generic
) { loadFilters() }
} }
} }
override fun onFailure(call: Call<List<Filter>>, t: Throwable) { override fun onFailure(call: Call<List<Filter>>, t: Throwable) {
filterProgressBar.hide() binding.filterProgressBar.hide()
filterMessageView.show() binding.filterMessageView.show()
if(t is IOException) { if(t is IOException) {
filterMessageView.setup(R.drawable.elephant_offline, binding.filterMessageView.setup(
R.string.error_network) { loadFilters() } R.drawable.elephant_offline,
R.string.error_network
) { loadFilters() }
} else { } else {
filterMessageView.setup(R.drawable.elephant_error, binding.filterMessageView.setup(
R.string.error_generic) { loadFilters() } R.drawable.elephant_error,
R.string.error_generic
) { loadFilters() }
} }
} }
}) })
} }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_filters)
setupToolbarBackArrow()
addFilterButton.setOnClickListener {
showAddFilterDialog()
}
title = intent?.getStringExtra(FILTERS_TITLE)
context = intent?.getStringExtra(FILTERS_CONTEXT)!!
loadFilters()
}
private fun setupToolbarBackArrow() { private fun setupToolbarBackArrow() {
setSupportActionBar(toolbar) setSupportActionBar(binding.includedToolbar.toolbar)
supportActionBar?.run { supportActionBar?.run {
// Back button // Back button
setDisplayHomeAsUpEnabled(true) setDisplayHomeAsUpEnabled(true)

View file

@ -7,7 +7,9 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="com.keylesspalace.tusky.FiltersActivity"> tools:context="com.keylesspalace.tusky.FiltersActivity">
<include layout="@layout/toolbar_basic" /> <include
android:id="@+id/includedToolbar"
layout="@layout/toolbar_basic" />
<ListView <ListView
android:id="@+id/filtersView" android:id="@+id/filtersView"