Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/fragment/HostMappingsListFragment.kt

267 lines
10 KiB
Kotlin
Raw Normal View History

2016-07-04 03:31:17 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.fragment
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.os.Bundle
2020-01-26 08:35:15 +01:00
import androidx.appcompat.app.AlertDialog
2016-07-04 03:31:17 +02:00
import android.text.Editable
import android.text.TextWatcher
import android.view.*
import android.widget.AbsListView.MultiChoiceModeListener
2017-06-19 15:45:41 +02:00
import android.widget.AdapterView
import android.widget.CompoundButton
2016-07-04 03:31:17 +02:00
import android.widget.CompoundButton.OnCheckedChangeListener
2017-06-19 15:45:41 +02:00
import android.widget.ListView
import android.widget.TextView
2017-10-05 05:50:35 +02:00
import com.bumptech.glide.RequestManager
2017-06-19 15:45:41 +02:00
import kotlinx.android.synthetic.main.dialog_add_host_mapping.*
import kotlinx.android.synthetic.main.dialog_user_list_detail_editor.*
2017-01-16 17:38:50 +01:00
import kotlinx.android.synthetic.main.fragment_content_listview.*
2017-06-19 15:45:41 +02:00
import org.mariotaku.ktextension.empty
import org.mariotaku.ktextension.string
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.HOST_MAPPING_PREFERENCES_NAME
import org.mariotaku.twidere.adapter.ArrayAdapter
2017-06-19 15:45:41 +02:00
import org.mariotaku.twidere.extension.applyOnShow
2017-02-05 14:42:20 +01:00
import org.mariotaku.twidere.extension.applyTheme
2017-06-19 15:45:41 +02:00
import org.mariotaku.twidere.extension.positive
import org.mariotaku.twidere.util.net.TwidereDns
2016-07-04 03:31:17 +02:00
2017-01-16 17:38:50 +01:00
class HostMappingsListFragment : AbsContentListViewFragment<HostMappingsListFragment.HostMappingAdapter>(),
AdapterView.OnItemClickListener, MultiChoiceModeListener, OnSharedPreferenceChangeListener {
2016-07-04 03:31:17 +02:00
2017-04-17 15:10:14 +02:00
private lateinit var hostMapping: SharedPreferences
2016-07-04 03:31:17 +02:00
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setHasOptionsMenu(true)
hostMapping = requireActivity().getSharedPreferences(HOST_MAPPING_PREFERENCES_NAME, Context.MODE_PRIVATE)
2017-01-16 17:38:50 +01:00
hostMapping.registerOnSharedPreferenceChangeListener(this)
2016-07-04 03:31:17 +02:00
listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL
listView.setMultiChoiceModeListener(this)
reloadHostMappings()
}
2017-10-05 05:50:35 +02:00
override fun onCreateAdapter(context: Context, requestManager: RequestManager): HostMappingAdapter {
return HostMappingAdapter(requireActivity())
2017-01-16 17:38:50 +01:00
}
2016-07-04 03:31:17 +02:00
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
mode.menuInflater.inflate(R.menu.action_multi_select_items, menu)
return true
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
updateTitle(mode)
return true
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.delete -> {
2017-06-19 15:45:41 +02:00
val array = listView.checkedItemPositions ?: return false
(dns as? TwidereDns)?.beginMappingTransaction {
(0 until array.size()).filter {
array.valueAt(it)
}.forEach {
remove(adapter.getItem(it).first)
2016-07-04 03:31:17 +02:00
}
}
reloadHostMappings()
}
else -> {
return false
}
}
mode.finish()
return true
}
override fun onDestroyActionMode(mode: ActionMode) {
}
2017-04-12 10:17:20 +02:00
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_host_mapping, menu)
2016-07-04 03:31:17 +02:00
}
2017-01-16 17:38:50 +01:00
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val (host, address) = adapter.getItem(position)
2016-07-04 03:31:17 +02:00
val args = Bundle()
args.putString(EXTRA_HOST, host)
args.putString(EXTRA_ADDRESS, address)
2017-04-07 06:31:30 +02:00
args.putBoolean(EXTRA_EXCLUDED, host == address)
2016-07-04 03:31:17 +02:00
args.putBoolean(EXTRA_EDIT_MODE, true)
val df = AddMappingDialogFragment()
df.arguments = args
2020-01-26 08:35:15 +01:00
fragmentManager?.let { df.show(it, "add_mapping") }
2016-07-04 03:31:17 +02:00
}
2017-04-12 10:17:20 +02:00
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
2016-07-04 03:31:17 +02:00
R.id.add -> {
val df = AddMappingDialogFragment()
2020-01-26 08:35:15 +01:00
fragmentManager?.let { df.show(it, "add_mapping") }
2016-07-04 03:31:17 +02:00
}
}
return super.onOptionsItemSelected(item)
}
override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long,
2017-04-07 06:31:30 +02:00
checked: Boolean) {
2016-07-04 03:31:17 +02:00
updateTitle(mode)
}
2016-07-06 15:21:34 +02:00
override fun onSharedPreferenceChanged(preferences: SharedPreferences, key: String) {
2016-07-04 03:31:17 +02:00
reloadHostMappings()
}
fun reloadHostMappings() {
2017-01-16 17:38:50 +01:00
adapter.clear()
adapter.addAll(hostMapping.all.mapNotNull { entry ->
val value = entry.value?.toString() ?: return@mapNotNull null
return@mapNotNull Pair(entry.key, value)
})
if (adapter.isEmpty) {
showEmpty(R.drawable.ic_info_info_generic, getString(R.string.add_host_mapping))
} else {
showContent()
}
2016-07-04 03:31:17 +02:00
}
private fun updateTitle(mode: ActionMode?) {
if (listView == null || mode == null || activity == null) return
2017-06-19 15:45:41 +02:00
val count = listView.checkedItemCount
2016-07-04 03:31:17 +02:00
mode.title = resources.getQuantityString(R.plurals.Nitems_selected, count, count)
}
2017-06-19 15:45:41 +02:00
class AddMappingDialogFragment : BaseDialogFragment(), TextWatcher, OnCheckedChangeListener {
2016-07-04 03:31:17 +02:00
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
updateButton()
}
override fun afterTextChanged(s: Editable) {
}
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
updateAddressField()
updateButton()
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val context = activity
val builder = AlertDialog.Builder(requireContext())
2017-01-12 17:26:44 +01:00
builder.setView(R.layout.dialog_add_host_mapping)
2016-07-04 03:31:17 +02:00
builder.setTitle(R.string.add_host_mapping)
2017-06-19 15:45:41 +02:00
builder.positive(android.R.string.ok, this::onPositiveClick)
2016-07-04 03:31:17 +02:00
builder.setNegativeButton(android.R.string.cancel, null)
val dialog = builder.create()
2017-06-19 15:45:41 +02:00
dialog.applyOnShow {
applyTheme()
editHost.addTextChangedListener(this@AddMappingDialogFragment)
editAddress.addTextChangedListener(this@AddMappingDialogFragment)
isExcluded.setOnCheckedChangeListener(this@AddMappingDialogFragment)
2016-07-04 03:31:17 +02:00
val args = arguments
if (args != null) {
2017-06-19 15:45:41 +02:00
editHost.isEnabled = !args.getBoolean(EXTRA_EDIT_MODE, false)
2016-07-04 03:31:17 +02:00
if (savedInstanceState == null) {
2017-06-19 15:45:41 +02:00
editHost.setText(args.getCharSequence(EXTRA_HOST))
editAddress.setText(args.getCharSequence(EXTRA_ADDRESS))
isExcluded.isChecked = args.getBoolean(EXTRA_EXCLUDED)
2016-07-04 03:31:17 +02:00
}
}
updateButton()
}
return dialog
}
2017-06-19 15:45:41 +02:00
private fun onPositiveClick(dialog: Dialog) {
val host = dialog.editHost.string.takeUnless(String?::isNullOrEmpty) ?: return
val address = (if (dialog.isExcluded.isChecked) {
host
} else {
dialog.editAddress.string
}).takeUnless(String?::isNullOrEmpty) ?: return
(dns as? TwidereDns)?.putMapping(host, address)
}
2016-07-04 03:31:17 +02:00
override fun onSaveInstanceState(outState: Bundle) {
2017-06-19 15:45:41 +02:00
(dialog as? AlertDialog)?.let {
outState.putCharSequence(EXTRA_HOST, it.editHost.text)
outState.putCharSequence(EXTRA_ADDRESS, it.editAddress.text)
outState.putCharSequence(EXTRA_EXCLUDED, it.isPublic.text)
}
2016-07-04 03:31:17 +02:00
super.onSaveInstanceState(outState)
}
private fun updateAddressField() {
2017-06-19 15:45:41 +02:00
val dialog = dialog as AlertDialog
dialog.editAddress.visibility = if (dialog.isExcluded.isChecked) View.GONE else View.VISIBLE
2016-07-04 03:31:17 +02:00
}
private fun updateButton() {
2016-08-25 04:10:53 +02:00
val dialog = dialog as AlertDialog
2017-06-19 15:45:41 +02:00
val hostValid = !dialog.editHost.empty
val addressValid = !dialog.editAddress.empty || dialog.isExcluded.isChecked
2016-07-04 03:31:17 +02:00
val positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
positiveButton.isEnabled = hostValid && addressValid
}
}
2017-01-16 17:38:50 +01:00
class HostMappingAdapter(context: Context) : ArrayAdapter<Pair<String, String>>(context,
android.R.layout.simple_list_item_activated_2) {
2016-07-04 03:31:17 +02:00
2016-07-07 09:39:32 +02:00
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
2016-07-04 03:31:17 +02:00
val view = super.getView(position, convertView, parent)
2017-06-19 06:11:28 +02:00
val text1 = view.findViewById<TextView>(android.R.id.text1)
val text2 = view.findViewById<TextView>(android.R.id.text2)
2017-01-16 17:38:50 +01:00
val (key, value) = getItem(position)
2016-07-04 03:31:17 +02:00
text1.text = key
2017-01-16 17:38:50 +01:00
if (key == value) {
2016-07-04 03:31:17 +02:00
text2.setText(R.string.excluded)
} else {
text2.text = value
}
return view
}
}
companion object {
2020-06-08 23:11:06 +02:00
private const val EXTRA_EDIT_MODE = "edit_mode"
private const val EXTRA_HOST = "host"
private const val EXTRA_ADDRESS = "address"
private const val EXTRA_EXCLUDED = "excluded"
2016-07-04 03:31:17 +02:00
}
}