Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/util/MultiSelectEventHandler.kt

225 lines
8.4 KiB
Kotlin
Raw Normal View History

2016-08-17 15:46:18 +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.util
2016-12-04 04:58:03 +01:00
import android.accounts.AccountManager
2016-08-17 15:46:18 +02:00
import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Intent
import android.os.Bundle
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
2020-06-27 22:43:38 +02:00
import com.twitter.twittertext.Extractor
2016-08-17 15:46:18 +02:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.activity.BaseActivity
import org.mariotaku.twidere.constant.IntentConstants.*
2016-12-29 06:50:18 +01:00
import org.mariotaku.twidere.extension.model.getAccountUser
2016-08-17 15:46:18 +02:00
import org.mariotaku.twidere.menu.AccountActionProvider
2016-12-04 04:58:03 +01:00
import org.mariotaku.twidere.model.AccountDetails
2016-08-17 15:46:18 +02:00
import org.mariotaku.twidere.model.ParcelableStatus
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.UserKey
2016-12-04 04:58:03 +01:00
import org.mariotaku.twidere.model.util.AccountUtils
2016-08-17 15:46:18 +02:00
import org.mariotaku.twidere.provider.TwidereDataStore.Filters
import org.mariotaku.twidere.util.content.ContentResolverUtils
2017-04-16 15:09:56 +02:00
import org.mariotaku.twidere.util.dagger.GeneralComponent
2016-08-17 15:46:18 +02:00
import java.util.*
import javax.inject.Inject
@SuppressLint("Registered")
2016-08-18 15:02:49 +02:00
class MultiSelectEventHandler(
private val activity: BaseActivity
) : ActionMode.Callback, MultiSelectManager.Callback {
2016-08-17 15:46:18 +02:00
@Inject
lateinit var twitterWrapper: AsyncTwitterWrapper
@Inject
lateinit var multiSelectManager: MultiSelectManager
2016-08-18 15:02:49 +02:00
private var actionMode: ActionMode? = null
2016-08-17 15:46:18 +02:00
2016-08-18 15:02:49 +02:00
private var accountActionProvider: AccountActionProvider? = null
2016-08-17 15:46:18 +02:00
init {
2017-04-16 15:09:56 +02:00
GeneralComponent.get(activity).inject(this)
2016-08-17 15:46:18 +02:00
}
/**
* Call before super.onCreate
*/
fun dispatchOnCreate() {
}
/**
* Call after super.onStart
*/
fun dispatchOnStart() {
multiSelectManager.registerCallback(this)
updateMultiSelectState()
}
/**
* Call before super.onStop
*/
fun dispatchOnStop() {
multiSelectManager.unregisterCallback(this)
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
val selectedItems = multiSelectManager.selectedItems
if (selectedItems.isEmpty()) return false
when (item.itemId) {
R.id.reply -> {
val extractor = Extractor()
2016-12-04 04:58:03 +01:00
val am = AccountManager.get(activity)
2016-08-17 15:46:18 +02:00
val intent = Intent(INTENT_ACTION_REPLY_MULTIPLE)
val bundle = Bundle()
2017-04-14 11:34:36 +02:00
val accountScreenNames = AccountUtils.getAccounts(am).map { it.getAccountUser(am).screen_name }
2016-08-17 15:46:18 +02:00
val allMentions = TreeSet(String.CASE_INSENSITIVE_ORDER)
2016-12-04 04:58:03 +01:00
for (selected in selectedItems) {
if (selected is ParcelableStatus) {
allMentions.add(selected.user_screen_name)
allMentions.addAll(extractor.extractMentionedScreennames(selected.text_plain))
} else if (selected is ParcelableUser) {
allMentions.add(selected.screen_name)
2016-08-17 15:46:18 +02:00
}
}
2017-04-14 11:34:36 +02:00
allMentions.removeAll(accountScreenNames)
2016-08-17 15:46:18 +02:00
val firstObj = selectedItems[0]
if (firstObj is ParcelableStatus) {
bundle.putString(EXTRA_IN_REPLY_TO_ID, firstObj.id)
}
bundle.putParcelable(EXTRA_ACCOUNT_KEY, multiSelectManager.accountKey)
bundle.putStringArray(EXTRA_SCREEN_NAMES, allMentions.toTypedArray())
intent.putExtras(bundle)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
2016-08-18 15:02:49 +02:00
activity.startActivity(intent)
2016-08-17 15:46:18 +02:00
mode.finish()
}
R.id.mute_user -> {
2016-08-18 15:02:49 +02:00
val resolver = activity.contentResolver
2016-08-17 15:46:18 +02:00
val valuesList = ArrayList<ContentValues>()
2017-04-12 14:58:08 +02:00
val userKeys = HashSet<UserKey>()
for (selectedItem in selectedItems) {
when (selectedItem) {
is ParcelableStatus -> {
userKeys.add(selectedItem.user_key)
valuesList.add(ContentValuesCreator.createFilteredUser(selectedItem))
}
is ParcelableUser -> {
userKeys.add(selectedItem.key)
valuesList.add(ContentValuesCreator.createFilteredUser(selectedItem))
}
2016-08-17 15:46:18 +02:00
}
}
ContentResolverUtils.bulkDelete(resolver, Filters.Users.CONTENT_URI,
2017-04-12 14:58:08 +02:00
Filters.Users.USER_KEY, false, userKeys, null, null)
2016-08-17 15:46:18 +02:00
ContentResolverUtils.bulkInsert(resolver, Filters.Users.CONTENT_URI, valuesList)
2017-01-27 08:43:25 +01:00
Toast.makeText(activity, R.string.message_toast_users_filters_added, Toast.LENGTH_SHORT).show()
2016-08-17 15:46:18 +02:00
mode.finish()
}
R.id.block -> {
val accountKey = multiSelectManager.accountKey
2017-04-12 14:58:08 +02:00
val userIds = UserKey.getIds(MultiSelectManager.getSelectedUserKeys(selectedItems))
2016-08-17 15:46:18 +02:00
if (accountKey != null && userIds != null) {
twitterWrapper.createMultiBlockAsync(accountKey, userIds)
}
mode.finish()
}
R.id.report_spam -> {
val accountKey = multiSelectManager.accountKey
2017-04-12 14:58:08 +02:00
val userIds = UserKey.getIds(MultiSelectManager.getSelectedUserKeys(selectedItems))
2016-08-17 15:46:18 +02:00
if (accountKey != null && userIds != null) {
twitterWrapper.reportMultiSpam(accountKey, userIds)
}
mode.finish()
}
}
if (item.groupId == AccountActionProvider.MENU_GROUP) {
val intent = item.intent
if (intent == null || !intent.hasExtra(EXTRA_ACCOUNT)) return false
val account: AccountDetails = intent.getParcelableExtra(EXTRA_ACCOUNT) ?: return false
2016-12-04 04:58:03 +01:00
multiSelectManager.accountKey = account.key
2017-04-12 14:58:08 +02:00
accountActionProvider?.selectedAccountKeys = arrayOf(account.key)
2016-08-17 15:46:18 +02:00
mode.invalidate()
}
return true
}
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
mode.menuInflater.inflate(R.menu.action_multi_select_contents, menu)
2016-08-18 15:02:49 +02:00
accountActionProvider = menu.findItem(R.id.select_account).actionProvider as AccountActionProvider
2017-04-12 14:58:08 +02:00
accountActionProvider?.selectedAccountKeys = arrayOf(multiSelectManager.firstSelectAccountKey)
2016-08-17 15:46:18 +02:00
return true
}
override fun onDestroyActionMode(mode: ActionMode) {
if (multiSelectManager.count != 0) {
multiSelectManager.clearSelectedItems()
}
2016-08-18 15:02:49 +02:00
accountActionProvider = null
actionMode = null
2016-08-17 15:46:18 +02:00
}
override fun onItemsCleared() {
updateMultiSelectState()
}
override fun onItemSelected(item: Any) {
updateMultiSelectState()
}
override fun onItemUnselected(item: Any) {
updateMultiSelectState()
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
updateSelectedCount(mode)
return true
}
private fun updateMultiSelectState() {
if (multiSelectManager.isActive) {
2016-08-18 15:02:49 +02:00
if (actionMode == null) {
actionMode = activity.startActionMode(this)
2016-08-17 15:46:18 +02:00
}
2016-08-18 15:02:49 +02:00
updateSelectedCount(actionMode)
2016-08-17 15:46:18 +02:00
} else {
2016-08-18 15:02:49 +02:00
actionMode?.finish()
actionMode = null
2016-08-17 15:46:18 +02:00
}
}
private fun updateSelectedCount(mode: ActionMode?) {
2016-08-18 15:02:49 +02:00
if (mode == null) return
2016-08-17 15:46:18 +02:00
val count = multiSelectManager.count
mode.title = activity.resources.getQuantityString(R.plurals.Nitems_selected, count, count)
}
companion object {
2020-06-08 23:11:06 +02:00
const val MENU_GROUP = 201
2016-08-17 15:46:18 +02:00
}
}