kotlin migration

This commit is contained in:
Mariotaku Lee 2016-08-28 12:24:48 +08:00
parent 5d6b5ff882
commit a9a3758a29
16 changed files with 304 additions and 337 deletions

View File

@ -165,8 +165,9 @@ dependencies {
compile 'com.github.mariotaku.ObjectCursor:core:0.9.9'
compile 'com.github.mariotaku:MultiValueSwitch:0.9.7'
compile 'com.github.mariotaku:AbstractTask:0.9.4'
compile 'com.github.mariotaku.CommonsLibrary:parcel:0.9.8'
compile 'com.github.mariotaku.CommonsLibrary:io:0.9.8'
compile 'com.github.mariotaku.CommonsLibrary:parcel:0.9.9-SNAPSHOT'
compile 'com.github.mariotaku.CommonsLibrary:io:0.9.9-SNAPSHOT'
compile 'com.github.mariotaku.CommonsLibrary:text:0.9.9-SNAPSHOT'
compile 'com.github.mariotaku:KPreferences:0.9.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'nl.komponents.kovenant:kovenant:3.3.0'

View File

@ -58,7 +58,7 @@ public class ComposeAutoCompleteAdapter extends SimpleCursorAdapter implements C
private final boolean mDisplayProfileImage;
private int mTypeIdx, mIconIdx, mTitleIdx, mSummaryIdx, mExtraIdIdx, mValueIdx;
private UserKey mAccountKey;
private UserKey accountKey;
private char mToken;
public ComposeAutoCompleteAdapter(final Context context) {
@ -144,14 +144,18 @@ public class ComposeAutoCompleteAdapter extends SimpleCursorAdapter implements C
return null;
}
}
builder.appendQueryParameter(QUERY_PARAM_ACCOUNT_KEY, String.valueOf(mAccountKey));
builder.appendQueryParameter(QUERY_PARAM_ACCOUNT_KEY, String.valueOf(accountKey));
return mContext.getContentResolver().query(builder.build(), Suggestions.AutoComplete.COLUMNS,
null, null, null);
}
public void setAccountKey(UserKey accountKey) {
mAccountKey = accountKey;
this.accountKey = accountKey;
}
public UserKey getAccountKey() {
return accountKey;
}
@Override

View File

@ -1,26 +0,0 @@
package org.mariotaku.twidere.text;
import android.text.SpannableString;
import org.mariotaku.twidere.util.CheckUtils;
import org.mariotaku.twidere.util.TwidereStringUtils;
/**
* Created by Ningyuan on 2015/5/1.
*/
public class SafeSpannableString extends SpannableString {
public SafeSpannableString(CharSequence source) {
super(source);
TwidereStringUtils.fixSHY(this);
}
@Override
public void setSpan(Object what, int start, int end, int flags) {
if (!CheckUtils.checkRange(this, start, end)) {
// Silently ignore
return;
}
super.setSpan(what, start, end, flags);
}
}

View File

@ -0,0 +1,24 @@
package org.mariotaku.twidere.text
import android.text.SpannableString
import org.mariotaku.twidere.util.CheckUtils
import org.mariotaku.twidere.util.TwidereStringUtils
/**
* Created by Ningyuan on 2015/5/1.
*/
class SafeSpannableString(source: CharSequence) : SpannableString(source) {
init {
TwidereStringUtils.fixSHY(this)
}
override fun setSpan(what: Any, start: Int, end: Int, flags: Int) {
if (!CheckUtils.checkRange(this, start, end)) {
// Silently ignore
return
}
super.setSpan(what, start, end, flags)
}
}

View File

@ -1,77 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 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.text.style;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.style.DynamicDrawableSpan;
/**
* Created by mariotaku on 15/12/22.
*/
public class EmojiSpan extends DynamicDrawableSpan {
private final Drawable drawable;
private Paint.FontMetrics fontMetrics;
public EmojiSpan(Drawable drawable) {
super(ALIGN_BOTTOM);
this.drawable = drawable;
this.fontMetrics = new Paint.FontMetrics();
}
@Override
public Drawable getDrawable() {
return drawable;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
final Drawable drawable = getDrawable();
if (drawable == null) return 0;
paint.getFontMetrics(fontMetrics);
final int textHeightPx = Math.round(fontMetrics.descent - fontMetrics.ascent);
final float intrinsicWidth = drawable.getIntrinsicWidth(),
intrinsicHeight = drawable.getIntrinsicHeight();
final int scaledWidth;
if (intrinsicWidth > intrinsicHeight) {
scaledWidth = Math.round(textHeightPx * (intrinsicWidth / intrinsicHeight));
} else {
scaledWidth = Math.round(intrinsicWidth * (textHeightPx / intrinsicHeight));
}
final int top = Math.round(fontMetrics.bottom) - textHeightPx, left = 0;
drawable.setBounds(left, top, left + scaledWidth, top + textHeightPx);
return scaledWidth;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start,
int end, float x, int top, int y, int bottom,
Paint paint) {
final Drawable b = getDrawable();
if (b == null) return;
canvas.save();
canvas.translate(x, y);
b.draw(canvas);
canvas.restore();
}
}

View File

@ -1,69 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 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.text.util;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextWatcher;
import android.widget.TextView;
import org.mariotaku.twidere.util.EmojiSupportUtils;
import org.mariotaku.twidere.util.ExternalThemeManager;
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper;
import javax.inject.Inject;
/**
* Created by mariotaku on 15/12/20.
*/
public class EmojiEditableFactory extends SafeEditableFactory {
@Inject
ExternalThemeManager externalThemeManager;
public EmojiEditableFactory(TextView textView) {
GeneralComponentHelper.build(textView.getContext()).inject(this);
}
@Override
public Editable newEditable(CharSequence source) {
final Editable editable = super.newEditable(source);
EmojiSupportUtils.applyEmoji(externalThemeManager, editable);
editable.setSpan(new 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) {
if (count <= 0) return;
EmojiSupportUtils.applyEmoji(externalThemeManager, editable,
start, count);
}
@Override
public void afterTextChanged(Editable s) {
}
}, 0, editable.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
return editable;
}
}

View File

@ -1,108 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 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;
import android.content.Context;
import android.content.res.ColorStateList;
import android.support.annotation.NonNull;
import android.text.InputType;
import android.text.Selection;
import android.text.method.ArrowKeyMovementMethod;
import android.text.method.MovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import com.afollestad.appthemeengine.inflation.ATEMultiAutoCompleteTextView;
import org.mariotaku.twidere.adapter.ComposeAutoCompleteAdapter;
import org.mariotaku.twidere.model.UserKey;
import org.mariotaku.twidere.util.EmojiSupportUtils;
import org.mariotaku.twidere.util.widget.StatusTextTokenizer;
import org.mariotaku.twidere.view.iface.IThemeBackgroundTintView;
public class ComposeEditText extends ATEMultiAutoCompleteTextView implements IThemeBackgroundTintView {
private ComposeAutoCompleteAdapter mAdapter;
private UserKey mAccountKey;
public ComposeEditText(final Context context) {
this(context, null);
}
public ComposeEditText(final Context context, final AttributeSet attrs) {
super(context, attrs);
EmojiSupportUtils.initForTextView(this);
setTokenizer(new StatusTextTokenizer());
setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
removeIMESuggestions();
}
});
// HACK: remove AUTO_COMPLETE flag to force IME show auto completion
setRawInputType(getInputType() & ~InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
}
@Override
public void setBackgroundTintColor(@NonNull ColorStateList color) {
setSupportBackgroundTintList(color);
}
public void setAccountKey(UserKey accountKey) {
mAccountKey = accountKey;
updateAccountId();
}
@Override
protected MovementMethod getDefaultMovementMethod() {
return ArrowKeyMovementMethod.getInstance();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!isInEditMode() && mAdapter == null) {
mAdapter = new ComposeAutoCompleteAdapter(getContext());
}
setAdapter(mAdapter);
updateAccountId();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAdapter != null) {
mAdapter.closeCursor();
mAdapter = null;
}
}
private void updateAccountId() {
if (mAdapter == null) return;
mAdapter.setAccountKey(mAccountKey);
}
private void removeIMESuggestions() {
final int selectionEnd = getSelectionEnd(), selectionStart = getSelectionStart();
Selection.removeSelection(getText());
setSelection(selectionStart, selectionEnd);
}
}

View File

@ -1073,9 +1073,9 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
val accounts = accountsAdapter!!.selectedAccounts
setSelectedAccounts(*accounts)
if (ArrayUtils.isEmpty(accounts)) {
editText.setAccountKey(Utils.getDefaultAccountKey(this))
editText.accountKey = Utils.getDefaultAccountKey(this)
} else {
editText.setAccountKey(accounts[0].account_key)
editText.accountKey = accounts[0].account_key
}
statusTextCount.maxLength = TwidereValidator.getTextLimit(accounts)
setMenu()

View File

@ -569,7 +569,7 @@ class MessagesConversationFragment : BaseSupportFragment(), LoaderCallbacks<Curs
if (activity !is BaseActivity) return false
if (activity.keyMetaState != 0) return false
val account = actionBarCustomView.accountSpinner.selectedItem as ParcelableCredentials
editText.setAccountKey(account.account_key)
editText.accountKey = account.account_key
searchUsers(account.account_key, ParseUtils.parseString(actionBarCustomView.editUserQuery.text), false)
return true
}
@ -584,7 +584,7 @@ class MessagesConversationFragment : BaseSupportFragment(), LoaderCallbacks<Curs
override fun afterTextChanged(s: Editable) {
val account = (actionBarCustomView.accountSpinner.selectedItem ?: return) as ParcelableCredentials
editText.setAccountKey(account.account_key)
editText.accountKey = account.account_key
searchUsers(account.account_key, ParseUtils.parseString(s), true)
}
})

View File

@ -79,31 +79,28 @@ class RetweetQuoteDialogFragment : BaseDialogFragment() {
dialog.setOnShowListener {
val alertDialog = it as AlertDialog
val itemContent = alertDialog.findViewById(R.id.itemContent)
val textCountView = alertDialog.findViewById(R.id.comment_text_count) as StatusTextCountView?
val itemMenu = alertDialog.findViewById(R.id.itemMenu)
val actionButtons = alertDialog.findViewById(R.id.actionButtons)
val commentContainer = alertDialog.findViewById(R.id.comment_container)
val editComment = alertDialog.findViewById(R.id.edit_comment) as ComposeEditText?
val commentMenu = alertDialog.findViewById(R.id.comment_menu)
assert(itemContent != null && textCountView != null && itemMenu != null
&& actionButtons != null && commentContainer != null && editComment != null
&& commentMenu != null)
val itemContent = alertDialog.findViewById(R.id.itemContent)!!
val textCountView = alertDialog.findViewById(R.id.comment_text_count) as StatusTextCountView
val itemMenu = alertDialog.findViewById(R.id.itemMenu)!!
val actionButtons = alertDialog.findViewById(R.id.actionButtons)!!
val commentContainer = alertDialog.findViewById(R.id.comment_container)!!
val editComment = alertDialog.findViewById(R.id.edit_comment) as ComposeEditText
val commentMenu = alertDialog.findViewById(R.id.comment_menu)!!
val adapter = DummyItemAdapter(context)
adapter.setShouldShowAccountsColor(true)
val holder = StatusViewHolder(adapter, itemContent!!)
val holder = StatusViewHolder(adapter, itemContent)
holder.displayStatus(status, false, true)
textCountView!!.maxLength = TwidereValidator.getTextLimit(credentials)
textCountView.maxLength = TwidereValidator.getTextLimit(credentials)
itemMenu!!.visibility = View.GONE
actionButtons!!.visibility = View.GONE
itemMenu.visibility = View.GONE
actionButtons.visibility = View.GONE
itemContent.isFocusable = false
val useQuote = useQuote(!status.user_is_protected, credentials)
commentContainer!!.visibility = if (useQuote) View.VISIBLE else View.GONE
editComment!!.setAccountKey(status.account_key)
commentContainer.visibility = if (useQuote) View.VISIBLE else View.GONE
editComment.accountKey = (status.account_key)
val sendByEnter = preferences.getBoolean(KEY_QUICK_SEND)
val enterHandler = EditTextEnterHandler.attach(editComment, object : EditTextEnterHandler.EnterListener {
@ -133,9 +130,9 @@ class RetweetQuoteDialogFragment : BaseDialogFragment() {
}
})
popupMenu = PopupMenu(context, commentMenu!!, Gravity.NO_GRAVITY,
popupMenu = PopupMenu(context, commentMenu, Gravity.NO_GRAVITY,
R.attr.actionOverflowMenuStyle, 0)
commentMenu.setOnClickListener(View.OnClickListener { popupMenu!!.show() })
commentMenu.setOnClickListener { popupMenu!!.show() }
commentMenu.setOnTouchListener(popupMenu!!.dragToOpenListener)
popupMenu!!.inflate(R.menu.menu_dialog_comment)
val menu = popupMenu!!.menu

View File

@ -0,0 +1,70 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 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.text.style
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.style.DynamicDrawableSpan
/**
* Created by mariotaku on 15/12/22.
*/
class EmojiSpan(private val drawable: Drawable) : DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {
private val fontMetrics: Paint.FontMetrics
init {
this.fontMetrics = Paint.FontMetrics()
}
override fun getDrawable(): Drawable? {
return drawable
}
override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt): Int {
val drawable = getDrawable() ?: return 0
paint.getFontMetrics(fontMetrics)
val textHeightPx = Math.round(fontMetrics.descent - fontMetrics.ascent)
val intrinsicWidth = drawable.intrinsicWidth.toFloat()
val intrinsicHeight = drawable.intrinsicHeight.toFloat()
val scaledWidth: Int
if (intrinsicWidth > intrinsicHeight) {
scaledWidth = Math.round(textHeightPx * (intrinsicWidth / intrinsicHeight))
} else {
scaledWidth = Math.round(intrinsicWidth * (textHeightPx / intrinsicHeight))
}
val top = Math.round(fontMetrics.bottom) - textHeightPx
val left = 0
drawable.setBounds(left, top, left + scaledWidth, top + textHeightPx)
return scaledWidth
}
override fun draw(canvas: Canvas, text: CharSequence, start: Int,
end: Int, x: Float, top: Int, y: Int, bottom: Int,
paint: Paint) {
val b = getDrawable() ?: return
canvas.save()
canvas.translate(x, y.toFloat())
b.draw(canvas)
canvas.restore()
}
}

View File

@ -0,0 +1,65 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 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.text.util
import android.text.Editable
import android.text.Spanned
import android.text.TextWatcher
import android.widget.TextView
import org.mariotaku.twidere.util.EmojiSupportUtils
import org.mariotaku.twidere.util.ExternalThemeManager
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
import javax.inject.Inject
/**
* Created by mariotaku on 15/12/20.
*/
class EmojiEditableFactory(textView: TextView) : SafeEditableFactory() {
@Inject
lateinit internal var externalThemeManager: ExternalThemeManager
init {
GeneralComponentHelper.build(textView.context).inject(this)
}
override fun newEditable(source: CharSequence): Editable {
val editable = super.newEditable(source)
EmojiSupportUtils.applyEmoji(externalThemeManager, editable)
editable.setSpan(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (count <= 0) return
EmojiSupportUtils.applyEmoji(externalThemeManager, editable,
start, count)
}
override fun afterTextChanged(s: Editable) {
}
}, 0, editable.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
return editable
}
}

View File

@ -17,33 +17,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.text.util;
package org.mariotaku.twidere.text.util
import android.text.Spannable;
import android.widget.TextView;
import android.text.Spannable
import android.widget.TextView
import org.mariotaku.twidere.util.EmojiSupportUtils;
import org.mariotaku.twidere.util.ExternalThemeManager;
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper;
import org.mariotaku.twidere.util.EmojiSupportUtils
import org.mariotaku.twidere.util.ExternalThemeManager
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
import javax.inject.Inject;
import javax.inject.Inject
/**
* Created by mariotaku on 15/12/20.
*/
public class EmojiSpannableFactory extends SafeSpannableFactory {
class EmojiSpannableFactory(textView: TextView) : SafeSpannableFactory() {
@Inject
ExternalThemeManager externalThemeManager;
lateinit internal var externalThemeManager: ExternalThemeManager
public EmojiSpannableFactory(TextView textView) {
GeneralComponentHelper.build(textView.getContext()).inject(this);
init {
GeneralComponentHelper.build(textView.context).inject(this)
}
@Override
public Spannable newSpannable(CharSequence source) {
final Spannable spannable = super.newSpannable(source);
EmojiSupportUtils.applyEmoji(externalThemeManager, spannable);
return spannable;
override fun newSpannable(source: CharSequence): Spannable {
val spannable = super.newSpannable(source)
EmojiSupportUtils.applyEmoji(externalThemeManager, spannable)
return spannable
}
}

View File

@ -17,18 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.text.util;
package org.mariotaku.twidere.text.util
import android.text.Spannable;
import android.text.Editable
import org.mariotaku.twidere.text.SafeSpannableString;
import org.mariotaku.twidere.text.SafeSpannableStringBuilder
/**
* Created by mariotaku on 15/12/20.
*/
public class SafeSpannableFactory extends Spannable.Factory {
@Override
public Spannable newSpannable(CharSequence source) {
return new SafeSpannableString(source);
open class SafeEditableFactory : Editable.Factory() {
override fun newEditable(source: CharSequence): Editable {
return SafeSpannableStringBuilder(source)
}
}

View File

@ -17,18 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.text.util;
package org.mariotaku.twidere.text.util
import android.text.Editable;
import android.text.Spannable
import org.mariotaku.twidere.text.SafeSpannableStringBuilder;
import org.mariotaku.twidere.text.SafeSpannableString
/**
* Created by mariotaku on 15/12/20.
*/
public class SafeEditableFactory extends Editable.Factory {
@Override
public Editable newEditable(CharSequence source) {
return new SafeSpannableStringBuilder(source);
open class SafeSpannableFactory : Spannable.Factory() {
override fun newSpannable(source: CharSequence): Spannable {
return SafeSpannableString(source)
}
}

View File

@ -0,0 +1,89 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 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
import android.content.Context
import android.content.res.ColorStateList
import android.text.InputType
import android.text.Selection
import android.text.method.ArrowKeyMovementMethod
import android.text.method.MovementMethod
import android.util.AttributeSet
import android.widget.AdapterView
import com.afollestad.appthemeengine.inflation.ATEMultiAutoCompleteTextView
import org.mariotaku.twidere.adapter.ComposeAutoCompleteAdapter
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.util.EmojiSupportUtils
import org.mariotaku.twidere.util.widget.StatusTextTokenizer
import org.mariotaku.twidere.view.iface.IThemeBackgroundTintView
class ComposeEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ATEMultiAutoCompleteTextView(context, attrs), IThemeBackgroundTintView {
private var adapter: ComposeAutoCompleteAdapter? = null
var accountKey: UserKey? = null
set(value) {
field = value
updateAccountKey()
}
init {
EmojiSupportUtils.initForTextView(this)
setTokenizer(StatusTextTokenizer())
onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
removeIMESuggestions()
}
// HACK: remove AUTO_COMPLETE flag to force IME show auto completion
setRawInputType(inputType and InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE.inv())
}
override fun setBackgroundTintColor(color: ColorStateList) {
supportBackgroundTintList = color
}
override fun getDefaultMovementMethod(): MovementMethod {
return ArrowKeyMovementMethod.getInstance()
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (!isInEditMode && adapter == null) {
adapter = ComposeAutoCompleteAdapter(context)
}
setAdapter<ComposeAutoCompleteAdapter>(adapter)
updateAccountKey()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
adapter?.closeCursor()
adapter = null
}
private fun updateAccountKey() {
adapter?.accountKey = accountKey
}
private fun removeIMESuggestions() {
val selectionEnd = selectionEnd
val selectionStart = selectionStart
Selection.removeSelection(text)
setSelection(selectionStart, selectionEnd)
}
}