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

388 lines
15 KiB
Kotlin
Raw Normal View History

2016-06-29 15:47:52 +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.filter
2016-06-29 15:47:52 +02:00
import android.app.Dialog
import android.content.ContentValues
import android.content.Context
import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.database.Cursor
import android.graphics.PorterDuff
2016-06-29 15:47:52 +02:00
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.LoaderManager
import android.support.v4.content.ContextCompat
2016-06-29 15:47:52 +02:00
import android.support.v4.content.CursorLoader
import android.support.v4.content.Loader
import android.support.v4.view.ViewCompat
import android.support.v4.widget.SimpleCursorAdapter
import android.support.v7.app.AlertDialog
import android.text.SpannableStringBuilder
import android.text.Spanned
2016-06-29 15:47:52 +02:00
import android.text.TextUtils
import android.view.*
2017-01-31 15:28:26 +01:00
import android.widget.*
2016-06-29 15:47:52 +02:00
import android.widget.AbsListView.MultiChoiceModeListener
2017-03-02 10:23:34 +01:00
import com.bumptech.glide.Glide
2016-06-29 15:47:52 +02:00
import kotlinx.android.synthetic.main.fragment_content_listview.*
2016-12-28 04:17:11 +01:00
import org.mariotaku.ktextension.setGroupAvailability
2017-03-05 09:08:09 +01:00
import org.mariotaku.library.objectcursor.ObjectCursor
2016-12-28 07:08:17 +01:00
import org.mariotaku.sqliteqb.library.Columns
2016-06-29 15:47:52 +02:00
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.R
2017-01-31 15:28:26 +01:00
import org.mariotaku.twidere.TwidereConstants.EXTRA_ID
2016-12-26 04:25:55 +01:00
import org.mariotaku.twidere.TwidereConstants.EXTRA_URI
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.activity.iface.IControlBarActivity
import org.mariotaku.twidere.adapter.ComposeAutoCompleteAdapter
import org.mariotaku.twidere.adapter.SourceAutoCompleteAdapter
2017-02-05 14:42:20 +01:00
import org.mariotaku.twidere.extension.*
import org.mariotaku.twidere.fragment.AbsContentListViewFragment
import org.mariotaku.twidere.fragment.BaseDialogFragment
2017-01-31 15:28:26 +01:00
import org.mariotaku.twidere.model.FiltersData
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.provider.TwidereDataStore.Filters
import org.mariotaku.twidere.text.style.EmojiSpan
2017-01-31 15:28:26 +01:00
import org.mariotaku.twidere.util.DataStoreUtils
2016-12-26 04:25:55 +01:00
import org.mariotaku.twidere.util.ParseUtils
import org.mariotaku.twidere.util.ThemeUtils
2016-12-26 04:25:55 +01:00
import org.mariotaku.twidere.util.Utils
2016-06-29 15:47:52 +02:00
2016-07-05 15:19:51 +02:00
abstract class BaseFiltersFragment : AbsContentListViewFragment<SimpleCursorAdapter>(),
LoaderManager.LoaderCallbacks<Cursor?>, MultiChoiceModeListener {
2016-12-28 07:08:17 +01:00
override var refreshing: Boolean
get() = false
set(value) {
super.refreshing = value
}
2016-06-29 15:47:52 +02:00
private var actionMode: ActionMode? = null
protected abstract val contentUri: Uri
protected abstract val contentColumns: Array<String>
protected open val sortOrder: String? = "${Filters.SOURCE} >= 0"
2017-01-31 15:28:26 +01:00
protected open val autoCompleteType: Int = 0
protected open val supportsEdit: Boolean = true
private val isQuickReturnEnabled: Boolean
get() = actionMode == null
2016-06-29 15:47:52 +02:00
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setHasOptionsMenu(true)
listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL
2017-01-31 15:28:26 +01:00
listView.setOnItemClickListener { view, child, pos, id ->
if (!supportsEdit) return@setOnItemClickListener
val adapter = this.adapter as FilterListAdapter
val item = adapter.getFilterItem(pos) ?: return@setOnItemClickListener
if (item.source >= 0) return@setOnItemClickListener
addOrEditItem(item.id, item.value)
}
2016-06-29 15:47:52 +02:00
listView.setMultiChoiceModeListener(this)
loaderManager.initLoader(0, null, this)
setRefreshEnabled(false)
showProgress()
}
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
if (!isVisibleToUser && actionMode != null) {
actionMode!!.finish()
}
}
override fun setControlVisible(visible: Boolean) {
super.setControlVisible(visible || !isQuickReturnEnabled)
}
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
actionMode = mode
setControlVisible(true)
mode.menuInflater.inflate(R.menu.action_multi_select_items, menu)
2016-12-28 04:17:11 +01:00
menu.setGroupAvailability(R.id.selection_group, true)
2016-06-29 15:47:52 +02:00
return true
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
updateTitle(mode)
2017-01-01 13:00:15 +01:00
listView.updateSelectionItems(menu)
2016-06-29 15:47:52 +02:00
return true
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.delete -> {
2016-12-28 07:08:17 +01:00
performDeletion()
2016-12-28 04:17:11 +01:00
mode.finish()
2016-06-29 15:47:52 +02:00
}
2016-12-28 04:17:11 +01:00
R.id.select_all -> {
2017-01-01 13:00:15 +01:00
listView.selectAll()
2016-12-28 04:17:11 +01:00
}
R.id.select_none -> {
2017-01-01 13:00:15 +01:00
listView.selectNone()
2016-12-28 04:17:11 +01:00
}
R.id.invert_selection -> {
2017-01-01 13:00:15 +01:00
listView.invertSelection()
2016-06-29 15:47:52 +02:00
}
else -> {
return false
}
}
return true
}
override fun onDestroyActionMode(mode: ActionMode) {
actionMode = null
}
2017-01-31 15:28:26 +01:00
override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) {
val adapter = this.adapter
if (adapter is SelectableItemAdapter) {
if (!adapter.isSelectable(position) && checked) {
listView.setItemChecked(position, false)
}
}
2016-06-29 15:47:52 +02:00
updateTitle(mode)
2016-12-28 04:17:11 +01:00
mode.invalidate()
2016-06-29 15:47:52 +02:00
}
override fun onScroll(view: AbsListView, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) {
super.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount)
if (ViewCompat.isLaidOut(view)) {
val childCount = view.childCount
if (childCount > 0) {
val firstChild = view.getChildAt(0)
val activity = activity
var controlBarHeight = 0
if (activity is IControlBarActivity) {
controlBarHeight = activity.controlBarHeight
}
val visible = firstChild.top > controlBarHeight
setControlVisible(visible)
} else {
setControlVisible(true)
}
}
}
2016-07-05 15:19:51 +02:00
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor?> {
return CursorLoader(activity, contentUri, contentColumns, null, null, sortOrder)
2016-06-29 15:47:52 +02:00
}
2016-07-05 15:19:51 +02:00
override fun onLoadFinished(loader: Loader<Cursor?>, data: Cursor?) {
2016-12-24 16:52:34 +01:00
adapter.swapCursor(data)
2016-06-29 15:47:52 +02:00
if (data != null && data.count > 0) {
showContent()
} else {
showEmpty(R.drawable.ic_info_volume_off, getString(R.string.no_rule))
}
2016-12-28 04:17:11 +01:00
actionMode?.invalidate()
2016-06-29 15:47:52 +02:00
}
2016-07-05 15:19:51 +02:00
override fun onLoaderReset(loader: Loader<Cursor?>) {
2016-12-24 16:52:34 +01:00
adapter.swapCursor(null)
2016-06-29 15:47:52 +02:00
}
2016-12-24 16:52:34 +01:00
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_filters, menu)
2016-06-29 15:47:52 +02:00
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.add -> {
2017-01-31 15:28:26 +01:00
addOrEditItem()
2016-06-29 15:47:52 +02:00
return true
}
}
return super.onOptionsItemSelected(item)
}
override fun onCreateAdapter(context: Context): SimpleCursorAdapter {
return FilterListAdapter(context)
}
2016-12-28 07:08:17 +01:00
protected open fun performDeletion() {
val ids = listView.checkedItemIds
val where = Expression.inArgs(Columns.Column(Filters._ID), ids.size)
context.contentResolver.delete(contentUri, where.sql, Array(ids.size) { ids[it].toString() })
}
2017-01-31 15:28:26 +01:00
protected open fun addOrEditItem(id: Long = -1, value: String? = null) {
val args = Bundle()
args.putParcelable(EXTRA_URI, contentUri)
args.putInt(EXTRA_AUTO_COMPLETE_TYPE, autoCompleteType)
args.putLong(EXTRA_ID, id)
args.putString(EXTRA_VALUE, value)
val dialog = AddEditItemFragment()
dialog.arguments = args
dialog.show(fragmentManager, "add_rule")
}
2016-06-29 15:47:52 +02:00
private fun updateTitle(mode: ActionMode?) {
if (listView == null || mode == null || activity == null) return
val count = listView!!.checkedItemCount
mode.title = resources.getQuantityString(R.plurals.Nitems_selected, count, count)
}
2017-01-31 15:28:26 +01:00
class AddEditItemFragment : BaseDialogFragment(), OnClickListener {
2016-06-29 15:47:52 +02:00
override fun onClick(dialog: DialogInterface, which: Int) {
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
val text = text
if (TextUtils.isEmpty(text)) return
val values = ContentValues()
values.put(Filters.VALUE, text)
2016-11-30 08:18:43 +01:00
val uri: Uri = arguments.getParcelable(EXTRA_URI)
2017-01-31 15:28:26 +01:00
val id = arguments.getLong(EXTRA_ID, -1)
val resolver = context.contentResolver
if (id >= 0) {
2017-02-09 17:38:41 +01:00
val valueWhere = Expression.equalsArgs(Filters.VALUE).sql
val valueWhereArgs = arrayOf(text)
val idWhere = Expression.equalsArgs(Filters._ID).sql
val idWhereArgs = arrayOf(id.toString())
if (DataStoreUtils.queryCount(context, uri, valueWhere, valueWhereArgs) == 0) {
resolver.update(uri, values, idWhere, idWhereArgs)
2017-01-31 15:28:26 +01:00
} else {
Toast.makeText(context, R.string.message_toast_duplicate_filter_rule,
Toast.LENGTH_SHORT).show()
}
} else {
resolver.insert(uri, values)
}
2016-06-29 15:47:52 +02:00
}
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val activity = activity
val context = activity
val builder = AlertDialog.Builder(context)
builder.setView(R.layout.dialog_auto_complete_textview)
2017-01-31 15:28:26 +01:00
if (arguments.getLong(EXTRA_ID, -1) >= 0) {
builder.setTitle(R.string.action_edit_filter_rule)
} else {
builder.setTitle(R.string.action_add_filter_rule)
}
2016-06-29 15:47:52 +02:00
builder.setPositiveButton(android.R.string.ok, this)
builder.setNegativeButton(android.R.string.cancel, this)
val dialog = builder.create()
2017-02-05 14:42:20 +01:00
dialog.setOnShowListener {
val alertDialog = it as AlertDialog
it.applyTheme()
2016-06-29 15:47:52 +02:00
val editText = (alertDialog.findViewById(R.id.edit_text) as AutoCompleteTextView?)!!
2017-01-31 15:28:26 +01:00
if (savedInstanceState == null) {
editText.setText(arguments.getString(EXTRA_VALUE))
}
val autoCompleteType = arguments.getInt(EXTRA_AUTO_COMPLETE_TYPE, 0)
2016-06-29 15:47:52 +02:00
if (autoCompleteType != 0) {
2016-12-08 15:56:21 +01:00
val userAutoCompleteAdapter: SimpleCursorAdapter
2016-06-29 15:47:52 +02:00
if (autoCompleteType == AUTO_COMPLETE_TYPE_SOURCES) {
2016-12-08 15:56:21 +01:00
userAutoCompleteAdapter = SourceAutoCompleteAdapter(activity)
2016-06-29 15:47:52 +02:00
} else {
2017-03-02 10:23:34 +01:00
val adapter = ComposeAutoCompleteAdapter(activity, Glide.with(this))
2016-12-06 06:15:22 +01:00
adapter.accountKey = Utils.getDefaultAccountKey(activity)
2016-12-08 15:56:21 +01:00
userAutoCompleteAdapter = adapter
2016-06-29 15:47:52 +02:00
}
2016-12-08 15:56:21 +01:00
editText.setAdapter(userAutoCompleteAdapter)
2016-06-29 15:47:52 +02:00
editText.threshold = 1
}
}
return dialog
}
2016-12-06 06:15:22 +01:00
private val text: String
2016-06-29 15:47:52 +02:00
get() {
val alertDialog = dialog as AlertDialog
val editText = (alertDialog.findViewById(R.id.edit_text) as AutoCompleteTextView?)!!
return ParseUtils.parseString(editText.text)
}
}
interface SelectableItemAdapter {
fun isSelectable(position: Int): Boolean
}
2016-06-29 15:47:52 +02:00
2016-12-11 07:29:00 +01:00
private class FilterListAdapter(
context: Context
) : SimpleCursorAdapter(context, R.layout.simple_list_item_activated_1, null,
emptyArray(), intArrayOf(), 0), SelectableItemAdapter {
2016-12-11 07:29:00 +01:00
2017-03-05 09:08:09 +01:00
private var indices: ObjectCursor.CursorIndices<FiltersData.BaseItem>? = null
private val secondaryTextColor = ThemeUtils.getTextColorSecondary(context)
2016-12-11 07:29:00 +01:00
override fun swapCursor(c: Cursor?): Cursor? {
2017-03-05 09:08:09 +01:00
indices = c?.let { ObjectCursor.indicesFrom(it, FiltersData.BaseItem::class.java) }
return super.swapCursor(c)
2016-12-11 07:29:00 +01:00
}
override fun bindView(view: View, context: Context, cursor: Cursor) {
super.bindView(view, context, cursor)
val indices = this.indices!!
val text1 = view.findViewById(android.R.id.text1) as TextView
2017-03-05 09:08:09 +01:00
val ssb = SpannableStringBuilder(cursor.getString(indices[Filters.VALUE]))
if (cursor.getLong(indices[Filters.SOURCE]) >= 0) {
val start = ssb.length
ssb.append("*")
val end = start + 1
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_sync)
drawable.setColorFilter(secondaryTextColor, PorterDuff.Mode.SRC_ATOP)
ssb.setSpan(EmojiSpan(drawable), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
text1.text = ssb
}
override fun isSelectable(position: Int): Boolean {
val cursor = this.cursor ?: return false
if (cursor.moveToPosition(position)) {
2017-03-05 09:08:09 +01:00
return cursor.getLong(indices!![Filters.SOURCE]) < 0
}
return false
}
2017-01-31 15:28:26 +01:00
fun getFilterItem(position: Int): FiltersData.BaseItem? {
val cursor = this.cursor ?: return null
if (cursor.moveToPosition(position)) {
return indices!!.newObject(cursor)
}
return null
}
2016-12-11 07:29:00 +01:00
}
2016-06-29 15:47:52 +02:00
companion object {
2016-12-26 04:25:55 +01:00
internal const val EXTRA_AUTO_COMPLETE_TYPE = "auto_complete_type"
2017-01-31 15:28:26 +01:00
internal const val EXTRA_VALUE = "value"
2016-12-26 04:25:55 +01:00
internal const val AUTO_COMPLETE_TYPE_SOURCES = 2
internal const val REQUEST_ADD_USER_SELECT_ACCOUNT = 201
internal const val REQUEST_IMPORT_BLOCKS_SELECT_ACCOUNT = 202
internal const val REQUEST_IMPORT_MUTES_SELECT_ACCOUNT = 203
2017-01-09 06:16:23 +01:00
2016-06-29 15:47:52 +02:00
}
}