2016-12-26 04:25:55 +01:00
|
|
|
package org.mariotaku.twidere.fragment.filter
|
|
|
|
|
2017-01-07 12:16:17 +01:00
|
|
|
import android.app.Activity
|
2016-12-26 04:25:55 +01:00
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.database.Cursor
|
2017-01-13 07:51:35 +01:00
|
|
|
import android.graphics.PorterDuff
|
2016-12-26 04:25:55 +01:00
|
|
|
import android.net.Uri
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.support.v4.app.FragmentActivity
|
2017-01-13 07:51:35 +01:00
|
|
|
import android.support.v4.content.ContextCompat
|
2016-12-26 04:25:55 +01:00
|
|
|
import android.support.v4.widget.SimpleCursorAdapter
|
2017-01-13 07:51:35 +01:00
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import android.text.Spanned
|
2016-12-26 04:25:55 +01:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuInflater
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.TextView
|
2017-02-13 15:53:38 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_content_listview.*
|
2016-12-26 04:25:55 +01:00
|
|
|
import org.mariotaku.kpreferences.KPreferences
|
|
|
|
import org.mariotaku.ktextension.setItemAvailability
|
|
|
|
import org.mariotaku.twidere.R
|
2016-12-26 15:11:20 +01:00
|
|
|
import org.mariotaku.twidere.TwidereConstants.*
|
2016-12-26 04:25:55 +01:00
|
|
|
import org.mariotaku.twidere.activity.AccountSelectorActivity
|
2016-12-26 15:11:20 +01:00
|
|
|
import org.mariotaku.twidere.activity.LinkHandlerActivity
|
2017-01-29 14:34:22 +01:00
|
|
|
import org.mariotaku.twidere.activity.UserSelectorActivity
|
2016-12-28 08:30:50 +01:00
|
|
|
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_ACCOUNT_HOST
|
2016-12-26 04:25:55 +01:00
|
|
|
import org.mariotaku.twidere.constant.nameFirstKey
|
2017-02-13 15:53:38 +01:00
|
|
|
import org.mariotaku.twidere.fragment.AddUserFilterDialogFragment
|
2016-12-26 04:25:55 +01:00
|
|
|
import org.mariotaku.twidere.fragment.ExtraFeaturesIntroductionDialogFragment
|
|
|
|
import org.mariotaku.twidere.model.ParcelableUser
|
|
|
|
import org.mariotaku.twidere.model.UserKey
|
|
|
|
import org.mariotaku.twidere.model.`FiltersData$UserItemCursorIndices`
|
2017-01-07 12:16:17 +01:00
|
|
|
import org.mariotaku.twidere.model.analyzer.PurchaseFinished
|
2017-01-13 07:51:35 +01: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.Analyzer
|
|
|
|
import org.mariotaku.twidere.util.ThemeUtils
|
|
|
|
import org.mariotaku.twidere.util.UserColorNameManager
|
2017-02-13 15:53:38 +01:00
|
|
|
import org.mariotaku.twidere.util.content.ContentResolverUtils
|
2016-12-26 04:25:55 +01:00
|
|
|
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
|
2017-01-01 16:28:00 +01:00
|
|
|
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
|
2016-12-26 04:25:55 +01:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
class FilteredUsersFragment : BaseFiltersFragment() {
|
|
|
|
|
2017-01-13 07:51:35 +01:00
|
|
|
override val contentUri: Uri = Filters.Users.CONTENT_URI
|
|
|
|
override val contentColumns: Array<String> = Filters.Users.COLUMNS
|
|
|
|
override val sortOrder: String? = "${Filters.Users.SOURCE} >= 0"
|
2017-01-31 15:28:26 +01:00
|
|
|
override val supportsEdit: Boolean = false
|
2016-12-26 04:25:55 +01:00
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
when (requestCode) {
|
|
|
|
REQUEST_SELECT_USER -> {
|
2017-01-31 09:02:44 +01:00
|
|
|
if (resultCode != FragmentActivity.RESULT_OK || data == null) return
|
|
|
|
val user = data.getParcelableExtra<ParcelableUser>(EXTRA_USER)
|
2017-02-13 15:53:38 +01:00
|
|
|
executeAfterFragmentResumed { fragment ->
|
|
|
|
AddUserFilterDialogFragment.show(fragment.childFragmentManager, user)
|
|
|
|
}
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
|
|
|
REQUEST_IMPORT_BLOCKS_SELECT_ACCOUNT -> {
|
2017-01-31 09:02:44 +01:00
|
|
|
if (resultCode != FragmentActivity.RESULT_OK || data == null) return
|
2016-12-26 15:11:20 +01:00
|
|
|
val intent = Intent(context, LinkHandlerActivity::class.java)
|
2016-12-31 18:13:21 +01:00
|
|
|
intent.data = Uri.Builder().scheme(SCHEME_TWIDERE).authority(AUTHORITY_FILTERS).path(PATH_FILTERS_IMPORT_BLOCKS).build()
|
2017-01-31 09:02:44 +01:00
|
|
|
intent.putExtra(EXTRA_ACCOUNT_KEY, data.getParcelableExtra<UserKey>(EXTRA_ACCOUNT_KEY))
|
2016-12-26 15:11:20 +01:00
|
|
|
startActivity(intent)
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
|
|
|
REQUEST_IMPORT_MUTES_SELECT_ACCOUNT -> {
|
2017-01-31 09:02:44 +01:00
|
|
|
if (resultCode != FragmentActivity.RESULT_OK || data == null) return
|
2016-12-26 15:11:20 +01:00
|
|
|
val intent = Intent(context, LinkHandlerActivity::class.java)
|
2016-12-31 18:13:21 +01:00
|
|
|
intent.data = Uri.Builder().scheme(SCHEME_TWIDERE).authority(AUTHORITY_FILTERS).path(PATH_FILTERS_IMPORT_MUTES).build()
|
2017-01-31 09:02:44 +01:00
|
|
|
intent.putExtra(EXTRA_ACCOUNT_KEY, data.getParcelableExtra<UserKey>(EXTRA_ACCOUNT_KEY))
|
2016-12-26 15:11:20 +01:00
|
|
|
startActivity(intent)
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
|
|
|
REQUEST_ADD_USER_SELECT_ACCOUNT -> {
|
2017-01-31 09:02:44 +01:00
|
|
|
if (resultCode != FragmentActivity.RESULT_OK || data == null) return
|
2016-12-26 04:25:55 +01:00
|
|
|
val intent = Intent(INTENT_ACTION_SELECT_USER)
|
2017-01-29 14:34:22 +01:00
|
|
|
intent.setClass(context, UserSelectorActivity::class.java)
|
2017-01-31 09:02:44 +01:00
|
|
|
intent.putExtra(EXTRA_ACCOUNT_KEY, data.getParcelableExtra<UserKey>(EXTRA_ACCOUNT_KEY))
|
|
|
|
startActivityForResult(intent, REQUEST_SELECT_USER)
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
2017-01-07 11:02:32 +01:00
|
|
|
REQUEST_PURCHASE_EXTRA_FEATURES -> {
|
2017-01-07 12:16:17 +01:00
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
2017-01-09 06:16:23 +01:00
|
|
|
Analyzer.log(PurchaseFinished.create(data!!))
|
2017-01-07 12:16:17 +01:00
|
|
|
}
|
2017-01-07 11:02:32 +01:00
|
|
|
}
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
|
|
|
inflater.inflate(R.menu.menu_filters_users, menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPrepareOptionsMenu(menu: Menu) {
|
|
|
|
super.onPrepareOptionsMenu(menu)
|
2017-01-01 16:28:00 +01:00
|
|
|
val isFeaturesSupported = extraFeaturesService.isSupported()
|
2016-12-26 04:25:55 +01:00
|
|
|
menu.setItemAvailability(R.id.add_user_single, !isFeaturesSupported)
|
|
|
|
menu.setItemAvailability(R.id.add_user_submenu, isFeaturesSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
var isExtraFeatures: Boolean = false
|
|
|
|
val intent = Intent(context, AccountSelectorActivity::class.java)
|
|
|
|
intent.putExtra(EXTRA_SINGLE_SELECTION, true)
|
|
|
|
intent.putExtra(EXTRA_SELECT_ONLY_ITEM_AUTOMATICALLY, true)
|
|
|
|
val requestCode = when (item.itemId) {
|
|
|
|
R.id.add_user_single, R.id.add_user -> REQUEST_ADD_USER_SELECT_ACCOUNT
|
|
|
|
R.id.import_from_blocked_users -> {
|
|
|
|
isExtraFeatures = true
|
|
|
|
REQUEST_IMPORT_BLOCKS_SELECT_ACCOUNT
|
|
|
|
}
|
|
|
|
R.id.import_from_muted_users -> {
|
|
|
|
isExtraFeatures = true
|
2016-12-28 08:30:50 +01:00
|
|
|
intent.putExtra(EXTRA_ACCOUNT_HOST, USER_TYPE_TWITTER_COM)
|
2016-12-26 04:25:55 +01:00
|
|
|
REQUEST_IMPORT_MUTES_SELECT_ACCOUNT
|
|
|
|
}
|
|
|
|
else -> return false
|
|
|
|
}
|
|
|
|
|
2017-01-09 06:16:23 +01:00
|
|
|
if (!isExtraFeatures || extraFeaturesService.isEnabled(ExtraFeaturesService.FEATURE_FILTERS_IMPORT)) {
|
2016-12-26 04:25:55 +01:00
|
|
|
startActivityForResult(intent, requestCode)
|
|
|
|
} else {
|
2017-01-09 06:16:23 +01:00
|
|
|
ExtraFeaturesIntroductionDialogFragment.show(childFragmentManager,
|
|
|
|
feature = ExtraFeaturesService.FEATURE_FILTERS_IMPORT,
|
|
|
|
requestCode = REQUEST_PURCHASE_EXTRA_FEATURES)
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateAdapter(context: Context): SimpleCursorAdapter {
|
|
|
|
return FilterUsersListAdapter(context)
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:28:26 +01:00
|
|
|
override fun addOrEditItem(id: Long, value: String?) {
|
|
|
|
// No-op
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:53:38 +01:00
|
|
|
override fun performDeletion() {
|
|
|
|
val positions = listView.checkedItemPositions
|
|
|
|
val keys = (0 until positions.size()).mapNotNull {
|
|
|
|
if (!positions.valueAt(it)) return@mapNotNull null
|
|
|
|
return@mapNotNull (adapter as FilterUsersListAdapter).getUserKeyString(positions.keyAt(it))
|
|
|
|
}
|
|
|
|
super.performDeletion()
|
|
|
|
ContentResolverUtils.bulkDelete(context.contentResolver, Filters.Keywords.CONTENT_URI,
|
|
|
|
Filters.USER_KEY, false, keys, null, null)
|
|
|
|
ContentResolverUtils.bulkDelete(context.contentResolver, Filters.Links.CONTENT_URI,
|
|
|
|
Filters.USER_KEY, false, keys, null, null)
|
|
|
|
}
|
|
|
|
|
2016-12-26 04:25:55 +01:00
|
|
|
class FilterUsersListAdapter(
|
|
|
|
context: Context
|
2017-01-12 18:08:10 +01:00
|
|
|
) : SimpleCursorAdapter(context, R.layout.list_item_two_line, null,
|
2017-01-13 07:51:35 +01:00
|
|
|
emptyArray(), IntArray(0), 0), SelectableItemAdapter {
|
2016-12-26 04:25:55 +01:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
lateinit var userColorNameManager: UserColorNameManager
|
|
|
|
@Inject
|
|
|
|
lateinit var preferences: KPreferences
|
|
|
|
|
|
|
|
private val nameFirst: Boolean
|
|
|
|
|
|
|
|
private var indices: `FiltersData$UserItemCursorIndices`? = null
|
2017-01-13 07:51:35 +01:00
|
|
|
private val secondaryTextColor = ThemeUtils.getTextColorSecondary(context)
|
2016-12-26 04:25:55 +01:00
|
|
|
|
|
|
|
init {
|
|
|
|
GeneralComponentHelper.build(context).inject(this)
|
|
|
|
nameFirst = preferences[nameFirstKey]
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun bindView(view: View, context: Context?, cursor: Cursor) {
|
|
|
|
super.bindView(view, context, cursor)
|
|
|
|
val indices = this.indices!!
|
2017-01-12 18:08:10 +01:00
|
|
|
val icon = view.findViewById(android.R.id.icon)
|
2016-12-26 04:25:55 +01:00
|
|
|
val text1 = view.findViewById(android.R.id.text1) as TextView
|
|
|
|
val text2 = view.findViewById(android.R.id.text2) as TextView
|
2017-01-12 18:08:10 +01:00
|
|
|
|
|
|
|
icon.visibility = View.GONE
|
|
|
|
|
2016-12-26 04:25:55 +01:00
|
|
|
val userId = UserKey.valueOf(cursor.getString(indices.userKey))
|
|
|
|
val name = cursor.getString(indices.name)
|
|
|
|
val screenName = cursor.getString(indices.screenName)
|
|
|
|
val displayName = userColorNameManager.getDisplayName(userId, name, screenName,
|
|
|
|
nameFirst)
|
|
|
|
text1.text = displayName
|
2017-01-13 07:51:35 +01:00
|
|
|
|
|
|
|
val ssb = SpannableStringBuilder(displayName)
|
|
|
|
if (cursor.getLong(indices.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
|
2016-12-26 04:25:55 +01:00
|
|
|
text2.text = userId.host
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun swapCursor(c: Cursor?): Cursor? {
|
|
|
|
val old = super.swapCursor(c)
|
|
|
|
if (c != null) {
|
|
|
|
indices = `FiltersData$UserItemCursorIndices`(c)
|
|
|
|
}
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
2017-01-13 07:51:35 +01:00
|
|
|
override fun isSelectable(position: Int): Boolean {
|
|
|
|
val cursor = this.cursor ?: return false
|
|
|
|
if (cursor.moveToPosition(position)) {
|
|
|
|
return cursor.getLong(indices!!.source) < 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-02-13 15:53:38 +01:00
|
|
|
|
|
|
|
fun getUserKeyString(position: Int): String? {
|
|
|
|
val cursor = this.cursor ?: return null
|
|
|
|
if (cursor.moveToPosition(position)) {
|
|
|
|
return cursor.getString(indices!!.userKey)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|