mirror of
https://github.com/TwidereProject/Twidere-Android
synced 2025-01-19 03:29:58 +01:00
implementing new conversation
This commit is contained in:
parent
735a322d2a
commit
4713127c56
@ -139,9 +139,9 @@ public final class Utils implements Constants {
|
||||
}
|
||||
|
||||
public static void addIntentToMenuForExtension(final Context context, final Menu menu,
|
||||
final int groupId, final String action,
|
||||
final String parcelableKey, final String parcelableJSONKey,
|
||||
final Parcelable parcelable) {
|
||||
final int groupId, final String action,
|
||||
final String parcelableKey, final String parcelableJSONKey,
|
||||
final Parcelable parcelable) {
|
||||
if (context == null || menu == null || action == null || parcelableKey == null || parcelable == null)
|
||||
return;
|
||||
final PackageManager pm = context.getPackageManager();
|
||||
@ -184,7 +184,7 @@ public final class Utils implements Constants {
|
||||
}
|
||||
|
||||
public static void announceForAccessibilityCompat(final Context context, final View view, final CharSequence text,
|
||||
final Class<?> cls) {
|
||||
final Class<?> cls) {
|
||||
final AccessibilityManager accessibilityManager = (AccessibilityManager) context
|
||||
.getSystemService(Context.ACCESSIBILITY_SERVICE);
|
||||
if (!accessibilityManager.isEnabled()) return;
|
||||
@ -213,7 +213,7 @@ public final class Utils implements Constants {
|
||||
}
|
||||
|
||||
public static int calculateInSampleSize(final int width, final int height, final int preferredWidth,
|
||||
final int preferredHeight) {
|
||||
final int preferredHeight) {
|
||||
if (preferredHeight > height && preferredWidth > width) return 1;
|
||||
final int result = Math.round(Math.max(width, height) / (float) Math.max(preferredWidth, preferredHeight));
|
||||
return Math.max(1, result);
|
||||
@ -317,7 +317,7 @@ public final class Utils implements Constants {
|
||||
|
||||
@NonNull
|
||||
public static String getReadPositionTagWithAccount(@NonNull final String tag,
|
||||
@Nullable final UserKey accountKey) {
|
||||
@Nullable final UserKey accountKey) {
|
||||
if (accountKey == null) return tag;
|
||||
return accountKey + ":" + tag;
|
||||
}
|
||||
@ -353,7 +353,7 @@ public final class Utils implements Constants {
|
||||
|
||||
|
||||
public static boolean isOfficialCredentials(@NonNull final Context context,
|
||||
@NonNull final AccountDetails account) {
|
||||
@NonNull final AccountDetails account) {
|
||||
return AccountDetailsExtensionsKt.isOfficial(account, context);
|
||||
}
|
||||
|
||||
@ -449,8 +449,8 @@ public final class Utils implements Constants {
|
||||
|
||||
|
||||
public static String getMediaUploadStatus(@NonNull final Context context,
|
||||
@Nullable final CharSequence[] links,
|
||||
@Nullable final CharSequence text) {
|
||||
@Nullable final CharSequence[] links,
|
||||
@Nullable final CharSequence text) {
|
||||
if (ArrayUtils.isEmpty(links) || text == null) return ParseUtils.parseString(text);
|
||||
return text + " " + TwidereArrayUtils.toString(links, ' ', false);
|
||||
}
|
||||
@ -590,7 +590,7 @@ public final class Utils implements Constants {
|
||||
}
|
||||
|
||||
public static String getTwitterErrorMessage(final Context context, final CharSequence action,
|
||||
final MicroBlogException te) {
|
||||
final MicroBlogException te) {
|
||||
if (context == null) return null;
|
||||
if (te == null) return context.getString(R.string.error_unknown_error);
|
||||
if (te.exceededRateLimitation()) {
|
||||
@ -660,7 +660,12 @@ public final class Utils implements Constants {
|
||||
if (context == null) return false;
|
||||
final Context app = context.getApplicationContext();
|
||||
final IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||||
final Intent intent = app.registerReceiver(null, filter);
|
||||
final Intent intent;
|
||||
try {
|
||||
intent = app.registerReceiver(null, filter);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
if (intent == null) return false;
|
||||
final boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
|
||||
final float level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
|
||||
@ -772,13 +777,13 @@ public final class Utils implements Constants {
|
||||
}
|
||||
|
||||
public static void showErrorMessage(final Context context, final CharSequence action,
|
||||
final CharSequence message, final boolean longMessage) {
|
||||
final CharSequence message, final boolean longMessage) {
|
||||
if (context == null) return;
|
||||
showErrorMessage(context, getErrorMessage(context, action, message), longMessage);
|
||||
}
|
||||
|
||||
public static void showErrorMessage(final Context context, final CharSequence action,
|
||||
@Nullable final Throwable t, final boolean longMessage) {
|
||||
@Nullable final Throwable t, final boolean longMessage) {
|
||||
if (context == null) return;
|
||||
if (t instanceof MicroBlogException) {
|
||||
showTwitterErrorMessage(context, action, (MicroBlogException) t, longMessage);
|
||||
@ -788,14 +793,14 @@ public final class Utils implements Constants {
|
||||
}
|
||||
|
||||
public static void showErrorMessage(final Context context, final int actionRes, final String desc,
|
||||
final boolean longMessage) {
|
||||
final boolean longMessage) {
|
||||
if (context == null) return;
|
||||
showErrorMessage(context, context.getString(actionRes), desc, longMessage);
|
||||
}
|
||||
|
||||
public static void showErrorMessage(final Context context, final int action,
|
||||
@Nullable final Throwable t,
|
||||
final boolean long_message) {
|
||||
@Nullable final Throwable t,
|
||||
final boolean long_message) {
|
||||
if (context == null) return;
|
||||
showErrorMessage(context, context.getString(action), t, long_message);
|
||||
}
|
||||
@ -842,7 +847,7 @@ public final class Utils implements Constants {
|
||||
}
|
||||
|
||||
public static void showTwitterErrorMessage(final Context context, final CharSequence action,
|
||||
final MicroBlogException te, final boolean long_message) {
|
||||
final MicroBlogException te, final boolean long_message) {
|
||||
if (context == null) return;
|
||||
final String message;
|
||||
if (te != null) {
|
||||
|
@ -1,25 +0,0 @@
|
||||
package org.mariotaku.twidere.util.view;
|
||||
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 2016/11/30.
|
||||
*/
|
||||
public abstract class SimpleTextWatcher implements TextWatcher {
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
|
||||
}
|
||||
}
|
@ -60,7 +60,7 @@ class UserSelectorActivity : BaseActivity(), OnItemClickListener, LoaderManager.
|
||||
}
|
||||
setContentView(R.layout.activity_user_selector)
|
||||
|
||||
editScreenName.addTextChangedListener(object : SimpleTextWatcher() {
|
||||
editScreenName.addTextChangedListener(object : SimpleTextWatcher {
|
||||
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
||||
searchUser(accountKey, s.toString(), true)
|
||||
}
|
||||
|
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Twidere - Twitter client for Android
|
||||
*
|
||||
* Copyright (C) 2012-2017 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.adapter
|
||||
|
||||
import android.content.Context
|
||||
import android.support.v4.util.ArrayMap
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import org.mariotaku.twidere.R
|
||||
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter
|
||||
import org.mariotaku.twidere.model.ParcelableUser
|
||||
import org.mariotaku.twidere.model.UserKey
|
||||
import org.mariotaku.twidere.view.holder.LoadIndicatorViewHolder
|
||||
import org.mariotaku.twidere.view.holder.SelectableUserViewHolder
|
||||
|
||||
class SelectableUsersAdapter(context: Context) : LoadMoreSupportAdapter<RecyclerView.ViewHolder>(context) {
|
||||
|
||||
val ITEM_VIEW_TYPE_USER = 2
|
||||
|
||||
private val inflater: LayoutInflater = LayoutInflater.from(context)
|
||||
private val itemStates: MutableMap<UserKey, Boolean> = ArrayMap()
|
||||
var itemCheckedListener: ((Int, Boolean) -> Unit)? = null
|
||||
|
||||
var data: List<ParcelableUser>? = null
|
||||
set(value) {
|
||||
field = value
|
||||
value?.forEach { item ->
|
||||
if (item.key !in itemStates && item.is_filtered) {
|
||||
itemStates[item.key] = true
|
||||
}
|
||||
}
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private fun bindUser(holder: SelectableUserViewHolder, position: Int) {
|
||||
holder.displayUser(getUser(position)!!)
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
val position = loadMoreIndicatorPosition
|
||||
var count = userCount
|
||||
if (position and ILoadMoreSupportAdapter.START !== 0L) {
|
||||
count++
|
||||
}
|
||||
if (position and ILoadMoreSupportAdapter.END !== 0L) {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
fun getUser(position: Int): ParcelableUser? {
|
||||
val dataPosition = position - userStartIndex
|
||||
if (dataPosition < 0 || dataPosition >= userCount) return null
|
||||
return data!![dataPosition]
|
||||
}
|
||||
|
||||
val userStartIndex: Int
|
||||
get() {
|
||||
val position = loadMoreIndicatorPosition
|
||||
var start = 0
|
||||
if (position and ILoadMoreSupportAdapter.START !== 0L) {
|
||||
start += 1
|
||||
}
|
||||
return start
|
||||
}
|
||||
|
||||
fun getUserKey(position: Int): UserKey {
|
||||
return data!![position].key
|
||||
}
|
||||
|
||||
val userCount: Int
|
||||
get() {
|
||||
if (data == null) return 0
|
||||
return data!!.size
|
||||
}
|
||||
|
||||
fun removeUserAt(position: Int): Boolean {
|
||||
val data = this.data as? MutableList ?: return false
|
||||
val dataPosition = position - userStartIndex
|
||||
if (dataPosition < 0 || dataPosition >= userCount) return false
|
||||
data.removeAt(dataPosition)
|
||||
notifyItemRemoved(position)
|
||||
return true
|
||||
}
|
||||
|
||||
fun setUserAt(position: Int, user: ParcelableUser): Boolean {
|
||||
val data = this.data as? MutableList ?: return false
|
||||
val dataPosition = position - userStartIndex
|
||||
if (dataPosition < 0 || dataPosition >= userCount) return false
|
||||
data[dataPosition] = user
|
||||
notifyItemChanged(position)
|
||||
return true
|
||||
}
|
||||
|
||||
fun findPosition(accountKey: UserKey, userKey: UserKey): Int {
|
||||
if (data == null) return RecyclerView.NO_POSITION
|
||||
for (i in userStartIndex until userStartIndex + userCount) {
|
||||
val user = data!![i]
|
||||
if (accountKey == user.account_key && userKey == user.key) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return RecyclerView.NO_POSITION
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
||||
when (viewType) {
|
||||
ITEM_VIEW_TYPE_USER -> {
|
||||
val view = inflater.inflate(R.layout.list_item_simple_user, parent, false)
|
||||
val holder = SelectableUserViewHolder(view, this)
|
||||
return holder
|
||||
}
|
||||
ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR -> {
|
||||
val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false)
|
||||
return LoadIndicatorViewHolder(view)
|
||||
}
|
||||
}
|
||||
throw IllegalStateException("Unknown view type " + viewType)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
when (holder.itemViewType) {
|
||||
ITEM_VIEW_TYPE_USER -> {
|
||||
bindUser(holder as SelectableUserViewHolder, position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
if (loadMoreIndicatorPosition and ILoadMoreSupportAdapter.START !== 0L && position == 0) {
|
||||
return ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR
|
||||
}
|
||||
if (position == userCount) {
|
||||
return ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR
|
||||
}
|
||||
return ITEM_VIEW_TYPE_USER
|
||||
}
|
||||
|
||||
val checkedCount: Int get() {
|
||||
return data?.count { !it.is_filtered && itemStates[it.key] ?: false } ?: 0
|
||||
}
|
||||
|
||||
fun setItemChecked(position: Int, value: Boolean) {
|
||||
val userKey = getUserKey(position)
|
||||
itemStates[userKey] = value
|
||||
itemCheckedListener?.invoke(position, value)
|
||||
}
|
||||
|
||||
fun clearCheckState() {
|
||||
itemStates.clear()
|
||||
}
|
||||
|
||||
fun setCheckState(userKey: UserKey, value: Boolean) {
|
||||
itemStates[userKey] = value
|
||||
}
|
||||
|
||||
fun isItemChecked(position: Int): Boolean {
|
||||
return itemStates[getUserKey(position)] ?: false
|
||||
}
|
||||
|
||||
fun clearSelection() {
|
||||
itemStates.clear()
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ import org.mariotaku.ktextension.*
|
||||
import org.mariotaku.twidere.R
|
||||
import org.mariotaku.twidere.activity.BaseActivity
|
||||
import org.mariotaku.twidere.adapter.LoadMoreSupportAdapter
|
||||
import org.mariotaku.twidere.adapter.SelectableUsersAdapter
|
||||
import org.mariotaku.twidere.adapter.iface.IContentAdapter
|
||||
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter
|
||||
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter.IndicatorPosition
|
||||
@ -47,7 +48,7 @@ import kotlin.collections.set
|
||||
* Created by mariotaku on 2016/12/26.
|
||||
*/
|
||||
|
||||
abstract class BaseFiltersImportFragment : AbsContentListRecyclerViewFragment<BaseFiltersImportFragment.SelectableUsersAdapter>(),
|
||||
abstract class BaseFiltersImportFragment : AbsContentListRecyclerViewFragment<SelectableUsersAdapter>(),
|
||||
LoaderManager.LoaderCallbacks<List<ParcelableUser>?> {
|
||||
|
||||
protected var nextCursor: Long = -1
|
||||
@ -245,174 +246,4 @@ abstract class BaseFiltersImportFragment : AbsContentListRecyclerViewFragment<Ba
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SelectableUsersAdapter(context: Context) : LoadMoreSupportAdapter<RecyclerView.ViewHolder>(context), IContentAdapter {
|
||||
|
||||
val ITEM_VIEW_TYPE_USER = 2
|
||||
|
||||
private val inflater: LayoutInflater = LayoutInflater.from(context)
|
||||
private val itemStates: MutableMap<UserKey, Boolean> = ArrayMap()
|
||||
var itemCheckedListener: ((Int, Boolean) -> Unit)? = null
|
||||
|
||||
var data: List<ParcelableUser>? = null
|
||||
set(value) {
|
||||
field = value
|
||||
value?.forEach { item ->
|
||||
if (item.key !in itemStates && item.is_filtered) {
|
||||
itemStates[item.key] = true
|
||||
}
|
||||
}
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private fun bindUser(holder: SelectableUserViewHolder, position: Int) {
|
||||
holder.displayUser(getUser(position)!!)
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
val position = loadMoreIndicatorPosition
|
||||
var count = userCount
|
||||
if (position and ILoadMoreSupportAdapter.START !== 0L) {
|
||||
count++
|
||||
}
|
||||
if (position and ILoadMoreSupportAdapter.END !== 0L) {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
fun getUser(position: Int): ParcelableUser? {
|
||||
val dataPosition = position - userStartIndex
|
||||
if (dataPosition < 0 || dataPosition >= userCount) return null
|
||||
return data!![dataPosition]
|
||||
}
|
||||
|
||||
val userStartIndex: Int
|
||||
get() {
|
||||
val position = loadMoreIndicatorPosition
|
||||
var start = 0
|
||||
if (position and ILoadMoreSupportAdapter.START !== 0L) {
|
||||
start += 1
|
||||
}
|
||||
return start
|
||||
}
|
||||
|
||||
fun getUserKey(position: Int): UserKey {
|
||||
return data!![position].key
|
||||
}
|
||||
|
||||
val userCount: Int
|
||||
get() {
|
||||
if (data == null) return 0
|
||||
return data!!.size
|
||||
}
|
||||
|
||||
fun removeUserAt(position: Int): Boolean {
|
||||
val data = this.data as? MutableList ?: return false
|
||||
val dataPosition = position - userStartIndex
|
||||
if (dataPosition < 0 || dataPosition >= userCount) return false
|
||||
data.removeAt(dataPosition)
|
||||
notifyItemRemoved(position)
|
||||
return true
|
||||
}
|
||||
|
||||
fun setUserAt(position: Int, user: ParcelableUser): Boolean {
|
||||
val data = this.data as? MutableList ?: return false
|
||||
val dataPosition = position - userStartIndex
|
||||
if (dataPosition < 0 || dataPosition >= userCount) return false
|
||||
data[dataPosition] = user
|
||||
notifyItemChanged(position)
|
||||
return true
|
||||
}
|
||||
|
||||
fun findPosition(accountKey: UserKey, userKey: UserKey): Int {
|
||||
if (data == null) return RecyclerView.NO_POSITION
|
||||
for (i in userStartIndex until userStartIndex + userCount) {
|
||||
val user = data!![i]
|
||||
if (accountKey == user.account_key && userKey == user.key) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return RecyclerView.NO_POSITION
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
||||
when (viewType) {
|
||||
ITEM_VIEW_TYPE_USER -> {
|
||||
val view = inflater.inflate(R.layout.list_item_simple_user, parent, false)
|
||||
val holder = SelectableUserViewHolder(view, this)
|
||||
return holder
|
||||
}
|
||||
ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR -> {
|
||||
val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false)
|
||||
return LoadIndicatorViewHolder(view)
|
||||
}
|
||||
}
|
||||
throw IllegalStateException("Unknown view type " + viewType)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
when (holder.itemViewType) {
|
||||
ITEM_VIEW_TYPE_USER -> {
|
||||
bindUser(holder as SelectableUserViewHolder, position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
if (loadMoreIndicatorPosition and ILoadMoreSupportAdapter.START !== 0L && position == 0) {
|
||||
return ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR
|
||||
}
|
||||
if (position == userCount) {
|
||||
return ILoadMoreSupportAdapter.ITEM_VIEW_TYPE_LOAD_INDICATOR
|
||||
}
|
||||
return ITEM_VIEW_TYPE_USER
|
||||
}
|
||||
|
||||
val checkedCount: Int get() {
|
||||
return data?.count { !it.is_filtered && itemStates[it.key] ?: false } ?: 0
|
||||
}
|
||||
|
||||
fun setItemChecked(position: Int, value: Boolean) {
|
||||
val userKey = getUserKey(position)
|
||||
itemStates[userKey] = value
|
||||
itemCheckedListener?.invoke(position, value)
|
||||
}
|
||||
|
||||
fun isItemChecked(position: Int): Boolean {
|
||||
return itemStates[getUserKey(position)] ?: false
|
||||
}
|
||||
|
||||
fun clearSelection() {
|
||||
itemStates.clear()
|
||||
}
|
||||
}
|
||||
|
||||
internal class SelectableUserViewHolder(
|
||||
itemView: View,
|
||||
adapter: SelectableUsersAdapter
|
||||
) : SimpleUserViewHolder(itemView, adapter) {
|
||||
val checkChangedListener: CompoundButton.OnCheckedChangeListener
|
||||
|
||||
init {
|
||||
ViewSupport.setBackground(itemView, ThemeUtils.getSelectableItemBackgroundDrawable(itemView.context))
|
||||
checkBox.visibility = View.VISIBLE
|
||||
checkChangedListener = CompoundButton.OnCheckedChangeListener { view, value ->
|
||||
adapter.setItemChecked(layoutPosition, value)
|
||||
}
|
||||
itemView.setOnClickListener {
|
||||
checkBox.toggle()
|
||||
}
|
||||
}
|
||||
|
||||
override fun displayUser(user: ParcelableUser) {
|
||||
super.displayUser(user)
|
||||
checkBox.setOnCheckedChangeListener(null)
|
||||
checkBox.isChecked = (adapter as SelectableUsersAdapter).isItemChecked(layoutPosition)
|
||||
checkBox.setOnCheckedChangeListener(checkChangedListener)
|
||||
itemView.isEnabled = !user.is_filtered
|
||||
checkBox.isEnabled = !user.is_filtered
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -313,8 +313,8 @@ class FiltersSubscriptionsFragment : BaseFragment(), LoaderManager.LoaderCallbac
|
||||
positiveButton.isEnabled = nameValid && urlValid
|
||||
}
|
||||
|
||||
val watcher = object : SimpleTextWatcher() {
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||
val watcher = object : SimpleTextWatcher {
|
||||
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
||||
updateEnableState()
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,206 @@
|
||||
|
||||
package org.mariotaku.twidere.fragment.message
|
||||
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.LoaderManager.LoaderCallbacks
|
||||
import android.support.v4.content.Loader
|
||||
import android.support.v7.widget.LinearLayoutManager
|
||||
import android.text.Editable
|
||||
import android.text.Spannable
|
||||
import android.text.TextUtils
|
||||
import android.text.style.ReplacementSpan
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import kotlinx.android.synthetic.main.fragment_messages_conversation_new.*
|
||||
import org.mariotaku.kpreferences.get
|
||||
import org.mariotaku.ktextension.Bundle
|
||||
import org.mariotaku.ktextension.set
|
||||
import org.mariotaku.twidere.R
|
||||
import org.mariotaku.twidere.adapter.SelectableUsersAdapter
|
||||
import org.mariotaku.twidere.constant.IntentConstants.*
|
||||
import org.mariotaku.twidere.constant.nameFirstKey
|
||||
import org.mariotaku.twidere.fragment.BaseFragment
|
||||
import org.mariotaku.twidere.loader.CacheUserSearchLoader
|
||||
import org.mariotaku.twidere.model.ParcelableUser
|
||||
import org.mariotaku.twidere.model.UserKey
|
||||
import org.mariotaku.twidere.text.MarkForDeleteSpan
|
||||
import org.mariotaku.twidere.util.view.SimpleTextWatcher
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 2017/2/15.
|
||||
*/
|
||||
class MessageNewConversationFragment: BaseFragment() {
|
||||
class MessageNewConversationFragment : BaseFragment(), LoaderCallbacks<List<ParcelableUser>?> {
|
||||
|
||||
private val accountKey: UserKey get() = arguments.getParcelable(EXTRA_ACCOUNT_KEY)
|
||||
private var loaderInitialized: Boolean = false
|
||||
|
||||
private lateinit var usersAdapter: SelectableUsersAdapter
|
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||
super.onActivityCreated(savedInstanceState)
|
||||
usersAdapter = SelectableUsersAdapter(context)
|
||||
recyclerView.adapter = usersAdapter
|
||||
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
||||
|
||||
editParticipants.addTextChangedListener(object : SimpleTextWatcher {
|
||||
override fun afterTextChanged(s: Editable) {
|
||||
s.getSpans(0, s.length, MarkForDeleteSpan::class.java).forEach { span ->
|
||||
val deleteStart = s.getSpanStart(span)
|
||||
val deleteEnd = s.getSpanEnd(span)
|
||||
s.removeSpan(span)
|
||||
s.delete(deleteStart, deleteEnd)
|
||||
}
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
||||
super.beforeTextChanged(s, start, count, after)
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
||||
if (s !is Spannable) return
|
||||
s.getSpans(0, s.length, PendingQuerySpan::class.java).forEach { span ->
|
||||
s.removeSpan(span)
|
||||
}
|
||||
// Processing deletion
|
||||
if (count < before) {
|
||||
val spans = s.getSpans(start, start, ParticipantSpan::class.java)
|
||||
spans.forEach { span ->
|
||||
val deleteStart = s.getSpanStart(span)
|
||||
val deleteEnd = s.getSpanEnd(span)
|
||||
s.removeSpan(span)
|
||||
s.setSpan(MarkForDeleteSpan(), deleteStart, deleteEnd,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
}
|
||||
val spaceNextStart = run {
|
||||
val spaceIdx = s.indexOfLast(Char::isWhitespace)
|
||||
if (spaceIdx < 0) return@run 0
|
||||
return@run spaceIdx + 1
|
||||
}
|
||||
// Skip if last char is space
|
||||
if (spaceNextStart > s.lastIndex) return
|
||||
if (s.getSpans(start, start + count, ParticipantSpan::class.java).isEmpty()) {
|
||||
s.setSpan(PendingQuerySpan(), spaceNextStart, spaceNextStart + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
searchUser(s.substring(spaceNextStart), true)
|
||||
}
|
||||
}
|
||||
})
|
||||
val nameFirst = preferences[nameFirstKey]
|
||||
usersAdapter.itemCheckedListener = itemChecked@ { pos, checked ->
|
||||
val text: Editable = editParticipants.editableText ?: return@itemChecked
|
||||
val user = usersAdapter.getUser(pos) ?: return@itemChecked
|
||||
if (checked) {
|
||||
text.getSpans(0, text.length, PendingQuerySpan::class.java).forEach { pending ->
|
||||
val start = text.getSpanStart(pending)
|
||||
val end = text.getSpanEnd(pending)
|
||||
text.removeSpan(pending)
|
||||
if (start < 0 || end < 0 || end < start) return@forEach
|
||||
text.delete(start, end)
|
||||
}
|
||||
val span = ParticipantSpan(user, userColorNameManager.getDisplayName(user, nameFirst))
|
||||
val start = text.length
|
||||
text.append(user.screen_name)
|
||||
val end = text.length
|
||||
text.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
text.append(' ')
|
||||
} else {
|
||||
text.getSpans(0, text.length, ParticipantSpan::class.java).forEach { span ->
|
||||
if (user != span.user) {
|
||||
return@forEach
|
||||
}
|
||||
val start = text.getSpanStart(span)
|
||||
var end = text.getSpanEnd(span)
|
||||
text.removeSpan(span)
|
||||
// Also remove last whitespace
|
||||
if (end <= text.lastIndex && text[end].isWhitespace()) {
|
||||
end += 1
|
||||
}
|
||||
text.delete(start, end)
|
||||
}
|
||||
}
|
||||
editParticipants.clearComposingText()
|
||||
updateCheckState()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
return inflater.inflate(R.layout.fragment_messages_conversation_new, container, false)
|
||||
}
|
||||
|
||||
override fun onCreateLoader(id: Int, args: Bundle): Loader<List<ParcelableUser>?> {
|
||||
val query = args.getString(EXTRA_QUERY)
|
||||
val fromCache = args.getBoolean(EXTRA_FROM_CACHE)
|
||||
val fromUser = args.getBoolean(EXTRA_FROM_USER)
|
||||
return CacheUserSearchLoader(context, accountKey, query, fromCache, fromUser)
|
||||
}
|
||||
|
||||
override fun onLoaderReset(loader: Loader<List<ParcelableUser>?>) {
|
||||
usersAdapter.data = null
|
||||
}
|
||||
|
||||
override fun onLoadFinished(loader: Loader<List<ParcelableUser>?>, data: List<ParcelableUser>?) {
|
||||
usersAdapter.data = data
|
||||
updateCheckState()
|
||||
}
|
||||
|
||||
private fun updateCheckState() {
|
||||
val selected = selectedRecipients
|
||||
usersAdapter.clearCheckState()
|
||||
selected.forEach { user ->
|
||||
usersAdapter.setCheckState(user.key, true)
|
||||
}
|
||||
usersAdapter.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private fun searchUser(query: String, fromCache: Boolean) {
|
||||
if (TextUtils.isEmpty(query)) {
|
||||
return
|
||||
}
|
||||
val args = Bundle {
|
||||
this[EXTRA_ACCOUNT_KEY] = accountKey
|
||||
this[EXTRA_QUERY] = query
|
||||
this[EXTRA_FROM_CACHE] = fromCache
|
||||
}
|
||||
if (loaderInitialized) {
|
||||
loaderManager.initLoader(0, args, this)
|
||||
loaderInitialized = true
|
||||
} else {
|
||||
loaderManager.restartLoader(0, args, this)
|
||||
}
|
||||
}
|
||||
|
||||
private val selectedRecipients: List<ParcelableUser>
|
||||
get() {
|
||||
val text = editParticipants.editableText ?: return emptyList()
|
||||
return text.getSpans(0, text.length, ParticipantSpan::class.java).map(ParticipantSpan::user)
|
||||
}
|
||||
|
||||
class PendingQuerySpan {
|
||||
|
||||
}
|
||||
|
||||
class ParticipantSpan(val user: ParcelableUser, val displayName: String) : ReplacementSpan() {
|
||||
|
||||
private var backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||
private var nameWidth: Float = 0f
|
||||
|
||||
init {
|
||||
backgroundPaint.color = Color.GRAY
|
||||
}
|
||||
|
||||
override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
|
||||
canvas.drawRect(x, top.toFloat(), x + nameWidth, bottom.toFloat(), backgroundPaint)
|
||||
canvas.drawText(displayName, x, y.toFloat(), paint)
|
||||
}
|
||||
|
||||
override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
|
||||
nameWidth = paint.measureText(displayName)
|
||||
return nameWidth.toInt()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Twidere - Twitter client for Android
|
||||
*
|
||||
* Copyright (C) 2012-2017 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.view
|
||||
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 2016/11/30.
|
||||
*/
|
||||
interface SimpleTextWatcher : TextWatcher {
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
||||
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
||||
|
||||
}
|
||||
|
||||
override fun afterTextChanged(s: Editable) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Twidere - Twitter client for Android
|
||||
*
|
||||
* Copyright (C) 2012-2017 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.view.holder
|
||||
|
||||
import android.view.View
|
||||
import android.widget.CompoundButton
|
||||
import org.mariotaku.twidere.adapter.SelectableUsersAdapter
|
||||
import org.mariotaku.twidere.model.ParcelableUser
|
||||
import org.mariotaku.twidere.util.ThemeUtils
|
||||
import org.mariotaku.twidere.util.support.ViewSupport
|
||||
|
||||
class SelectableUserViewHolder(
|
||||
itemView: View,
|
||||
adapter: SelectableUsersAdapter
|
||||
) : SimpleUserViewHolder(itemView, adapter) {
|
||||
private val checkChangedListener = CompoundButton.OnCheckedChangeListener { view, value ->
|
||||
adapter.setItemChecked(layoutPosition, value)
|
||||
}
|
||||
|
||||
init {
|
||||
ViewSupport.setBackground(itemView, ThemeUtils.getSelectableItemBackgroundDrawable(itemView.context))
|
||||
checkBox.visibility = View.VISIBLE
|
||||
itemView.setOnClickListener {
|
||||
checkBox.toggle()
|
||||
}
|
||||
}
|
||||
|
||||
override fun displayUser(user: ParcelableUser) {
|
||||
super.displayUser(user)
|
||||
checkBox.setOnCheckedChangeListener(null)
|
||||
checkBox.isChecked = (adapter as SelectableUsersAdapter).isItemChecked(layoutPosition)
|
||||
checkBox.setOnCheckedChangeListener(checkChangedListener)
|
||||
itemView.isEnabled = !user.is_filtered
|
||||
checkBox.isEnabled = !user.is_filtered
|
||||
}
|
||||
|
||||
}
|
@ -65,7 +65,7 @@ class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) :
|
||||
|
||||
fun String.nonSpaceCount(range: IntRange): Int {
|
||||
if (range.isEmpty()) return 0
|
||||
return range.count { !Character.isSpaceChar(this[it]) }
|
||||
return range.count { !this[it].isWhitespace() }
|
||||
}
|
||||
|
||||
val text = message.text_unescaped
|
||||
|
@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Twidere - Twitter client for Android
|
||||
~
|
||||
~ Copyright (C) 2012-2017 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/>.
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<org.mariotaku.twidere.view.FixedEditText
|
||||
android:id="@+id/editParticipants"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="@dimen/element_spacing_normal"
|
||||
android:layout_weight="0"
|
||||
android:hint="@string/hint_message_select_user"
|
||||
android:minLines="1"
|
||||
app:backgroundTint="?colorAccent"/>
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/recyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
</LinearLayout>
|
@ -423,10 +423,10 @@
|
||||
<string name="error_message">Error: <xliff:g id="message">%s</xliff:g>
|
||||
</string>
|
||||
<string name="error_message_device_incompatible">This device is not compatible with Twidere, upgrade to latest Android OS is recommended.\nYou can send information below to help me report this issue to device manufacturer.</string>
|
||||
<string name="error_message_media_upload_failed">Media upload failed.</string>
|
||||
<string name="error_message_media_uploader_not_found">Media uploader not found, maybe it was uninstalled.</string>
|
||||
<string name="error_message_media_message_attachment_not_supported">Attach media to DM is not supported</string>
|
||||
<string name="error_message_media_message_too_many">Too many media</string>
|
||||
<string name="error_message_media_upload_failed">Media upload failed.</string>
|
||||
<string name="error_message_media_uploader_not_found">Media uploader not found, maybe it was uninstalled.</string>
|
||||
<string name="error_message_message_too_long">Message too long</string>
|
||||
<string name="error_message_no_content">No content</string>
|
||||
<string
|
||||
@ -488,7 +488,7 @@
|
||||
<string name="filter_type_sources">Sources</string>
|
||||
<string name="filter_type_users">Users</string>
|
||||
<string name="filter_user_confirm_message">Add <xliff:g
|
||||
example="Username" id="name">%s</xliff:g> to filter?</string>
|
||||
example="Username" id="name">%1$s</xliff:g> to filter?</string>
|
||||
|
||||
<string name="follow_request_sent">Follow request sent</string>
|
||||
|
||||
@ -543,6 +543,7 @@
|
||||
<string name="hint_accounts_dashboard_title">Accounts dashboard</string>
|
||||
<string name="hint_empty_filters_subscriptions">No subscriptions</string>
|
||||
<string name="hint_error_message_no_content">No content</string>
|
||||
<string name="hint_message_select_user">Search users</string>
|
||||
<string name="hint_no_account">No account</string>
|
||||
|
||||
<string name="hints">Hints</string>
|
||||
|
Loading…
Reference in New Issue
Block a user