From 39ea713dd94f3e3ddece7d01d0141b0cd3ddc11a Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Mon, 23 Jan 2017 14:56:55 +0800 Subject: [PATCH] improved auto complete code --- .../activity/QuickSearchBarActivity.kt | 77 ++++--------------- .../adapter/ComposeAutoCompleteAdapter.kt | 41 ++++------ .../mariotaku/twidere/model/SuggestionItem.kt | 31 ++++++++ 3 files changed, 60 insertions(+), 89 deletions(-) create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/model/SuggestionItem.kt diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/QuickSearchBarActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/QuickSearchBarActivity.kt index d8a287ac3..4992280b2 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/QuickSearchBarActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/QuickSearchBarActivity.kt @@ -50,6 +50,7 @@ import org.mariotaku.twidere.constant.KeyboardShortcutConstants.ACTION_NAVIGATIO import org.mariotaku.twidere.constant.KeyboardShortcutConstants.CONTEXT_TAG_NAVIGATION import org.mariotaku.twidere.constant.newDocumentApiKey import org.mariotaku.twidere.model.AccountDetails +import org.mariotaku.twidere.model.SuggestionItem import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.util.AccountUtils import org.mariotaku.twidere.provider.TwidereDataStore.SearchHistory @@ -256,39 +257,16 @@ class QuickSearchBarActivity : BaseActivity(), OnClickListener, LoaderCallbacks< searchQuery.setSelection(query.length) } - internal class SuggestionItem(cursor: Cursor, indices: SuggestionsAdapter.Indices) { - - - val title: String? - val summary: String? - val _id: Long - val extra_id: String? - - init { - _id = cursor.getLong(indices._id) - title = cursor.getString(indices.title) - summary = cursor.getString(indices.summary) - extra_id = cursor.getString(indices.extra_id) - } - } - class SuggestionsAdapter internal constructor( private val activity: QuickSearchBarActivity ) : CursorAdapter(activity, null, 0), OnClickListener { - private val inflater: LayoutInflater - private val mediaLoader: MediaLoaderWrapper - private val userColorNameManager: UserColorNameManager - private val removedPositions: SortableIntList? + private val inflater: LayoutInflater = LayoutInflater.from(activity) + private val mediaLoader: MediaLoaderWrapper = activity.mediaLoader + private val userColorNameManager: UserColorNameManager = activity.userColorNameManager + private val removedPositions: SortableIntList? = SortableIntList() - private var indices: Indices? = null - - init { - removedPositions = SortableIntList() - mediaLoader = activity.mediaLoader - userColorNameManager = activity.userColorNameManager - inflater = LayoutInflater.from(activity) - } + private var indices: SuggestionItem.Indices? = null override fun newView(context: Context, cursor: Cursor, parent: ViewGroup): View { when (getActualItemViewType(cursor.position)) { @@ -389,7 +367,7 @@ class QuickSearchBarActivity : BaseActivity(), OnClickListener, LoaderCallbacks< } override fun swapCursor(newCursor: Cursor?): Cursor? { - indices = if (newCursor != null) Indices(newCursor) else null + indices = if (newCursor != null) SuggestionItem.Indices(newCursor) else null removedPositions!!.clear() return super.swapCursor(newCursor) } @@ -436,47 +414,18 @@ class QuickSearchBarActivity : BaseActivity(), OnClickListener, LoaderCallbacks< internal class SearchViewHolder(view: View) { - internal val icon: ImageView - internal val text1: TextView - internal val edit_query: View - - init { - icon = view.findViewById(android.R.id.icon) as ImageView - text1 = view.findViewById(android.R.id.text1) as TextView - edit_query = view.findViewById(R.id.edit_query) - } + internal val icon: ImageView = view.findViewById(android.R.id.icon) as ImageView + internal val text1: TextView = view.findViewById(android.R.id.text1) as TextView + internal val edit_query: View = view.findViewById(R.id.edit_query) } internal class UserViewHolder(view: View) { - internal val icon: ImageView - internal val text1: TextView - internal val text2: TextView + internal val icon: ImageView = view.findViewById(android.R.id.icon) as ImageView + internal val text1: TextView = view.findViewById(android.R.id.text1) as TextView + internal val text2: TextView = view.findViewById(android.R.id.text2) as TextView - init { - icon = view.findViewById(android.R.id.icon) as ImageView - text1 = view.findViewById(android.R.id.text1) as TextView - text2 = view.findViewById(android.R.id.text2) as TextView - } - } - - internal class Indices(cursor: Cursor) { - internal val _id: Int - internal val type: Int - internal val title: Int - internal val summary: Int - internal val icon: Int - internal val extra_id: Int - - init { - _id = cursor.getColumnIndex(Suggestions._ID) - type = cursor.getColumnIndex(Suggestions.TYPE) - title = cursor.getColumnIndex(Suggestions.TITLE) - summary = cursor.getColumnIndex(Suggestions.SUMMARY) - icon = cursor.getColumnIndex(Suggestions.ICON) - extra_id = cursor.getColumnIndex(Suggestions.EXTRA_ID) - } } companion object { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ComposeAutoCompleteAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ComposeAutoCompleteAdapter.kt index 8f1d0701f..eff916897 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ComposeAutoCompleteAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ComposeAutoCompleteAdapter.kt @@ -23,15 +23,14 @@ import android.content.Context import android.database.Cursor import android.graphics.PorterDuff.Mode import android.support.v4.widget.SimpleCursorAdapter -import android.text.TextUtils import android.view.View import android.widget.TextView -import org.apache.commons.lang3.StringUtils import org.mariotaku.kpreferences.get import org.mariotaku.twidere.R import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.constant.displayProfileImageKey import org.mariotaku.twidere.constant.profileImageStyleKey +import org.mariotaku.twidere.model.SuggestionItem import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.provider.TwidereDataStore.Suggestions import org.mariotaku.twidere.util.MediaLoaderWrapper @@ -54,12 +53,7 @@ class ComposeAutoCompleteAdapter(context: Context) : SimpleCursorAdapter(context private val displayProfileImage: Boolean private val profileImageStyle: Int - private var mTypeIdx: Int = 0 - private var mIconIdx: Int = 0 - private var mTitleIdx: Int = 0 - private var mSummaryIdx: Int = 0 - private var mExtraIdIdx: Int = 0 - private var mValueIdx: Int = 0 + private var indices: SuggestionItem.Indices? = null var accountKey: UserKey? = null private var token: Char = ' ' @@ -70,18 +64,19 @@ class ComposeAutoCompleteAdapter(context: Context) : SimpleCursorAdapter(context } override fun bindView(view: View, context: Context?, cursor: Cursor) { + val indices = this.indices!! val text1 = view.findViewById(android.R.id.text1) as TextView val text2 = view.findViewById(android.R.id.text2) as TextView val icon = view.findViewById(android.R.id.icon) as ProfileImageView icon.style = profileImageStyle - if (Suggestions.AutoComplete.TYPE_USERS == cursor.getString(mTypeIdx)) { - text1.text = userColorNameManager.getUserNickname(cursor.getString(mExtraIdIdx), - cursor.getString(mTitleIdx)) - text2.text = String.format("@%s", cursor.getString(mSummaryIdx)) + if (Suggestions.AutoComplete.TYPE_USERS == cursor.getString(indices.type)) { + text1.text = userColorNameManager.getUserNickname(cursor.getString(indices.extra_id), + cursor.getString(indices.title)) + text2.text = String.format("@%s", cursor.getString(indices.summary)) if (displayProfileImage) { - val profileImageUrl = cursor.getString(mIconIdx) + val profileImageUrl = cursor.getString(indices.icon) mediaLoader.displayProfileImage(icon, profileImageUrl) } else { mediaLoader.cancelDisplayTask(icon) @@ -89,7 +84,7 @@ class ComposeAutoCompleteAdapter(context: Context) : SimpleCursorAdapter(context icon.clearColorFilter() } else { - text1.text = String.format("#%s", cursor.getString(mTitleIdx)) + text1.text = String.format("#%s", cursor.getString(indices.title)) text2.setText(R.string.hashtag) icon.setImageResource(R.drawable.ic_action_hashtag) @@ -106,16 +101,17 @@ class ComposeAutoCompleteAdapter(context: Context) : SimpleCursorAdapter(context } } - override fun convertToString(cursor: Cursor?): CharSequence { - when (StringUtils.defaultIfEmpty(cursor!!.getString(mTypeIdx), "")) { + override fun convertToString(cursor: Cursor): CharSequence { + val indices = this.indices!! + when (cursor.getString(indices.type)) { Suggestions.AutoComplete.TYPE_HASHTAGS -> { - return '#' + cursor.getString(mValueIdx) + return '#' + cursor.getString(indices.value) } Suggestions.AutoComplete.TYPE_USERS -> { - return '@' + cursor.getString(mValueIdx) + return '@' + cursor.getString(indices.value) } } - return cursor.getString(mValueIdx) + return cursor.getString(indices.value) } override fun runQueryOnBackgroundThread(constraint: CharSequence?): Cursor? { @@ -146,12 +142,7 @@ class ComposeAutoCompleteAdapter(context: Context) : SimpleCursorAdapter(context override fun swapCursor(cursor: Cursor?): Cursor? { if (cursor != null) { - mTypeIdx = cursor.getColumnIndex(Suggestions.AutoComplete.TYPE) - mTitleIdx = cursor.getColumnIndex(Suggestions.AutoComplete.TITLE) - mSummaryIdx = cursor.getColumnIndex(Suggestions.AutoComplete.SUMMARY) - mExtraIdIdx = cursor.getColumnIndex(Suggestions.AutoComplete.EXTRA_ID) - mIconIdx = cursor.getColumnIndex(Suggestions.AutoComplete.ICON) - mValueIdx = cursor.getColumnIndex(Suggestions.AutoComplete.VALUE) + indices = SuggestionItem.Indices(cursor) } return super.swapCursor(cursor) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/SuggestionItem.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/SuggestionItem.kt new file mode 100644 index 000000000..cc04a1535 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/SuggestionItem.kt @@ -0,0 +1,31 @@ +package org.mariotaku.twidere.model + +import android.database.Cursor +import org.mariotaku.twidere.provider.TwidereDataStore +import org.mariotaku.twidere.provider.TwidereDataStore.* + +class SuggestionItem(cursor: Cursor, indices: Indices) { + + val title: String? + val summary: String? + val _id: Long + val extra_id: String? + + init { + _id = cursor.getLong(indices._id) + title = cursor.getString(indices.title) + summary = cursor.getString(indices.summary) + extra_id = cursor.getString(indices.extra_id) + } + + class Indices(cursor: Cursor) { + val _id: Int = cursor.getColumnIndex(Suggestions._ID) + val type: Int = cursor.getColumnIndex(Suggestions.TYPE) + val title: Int = cursor.getColumnIndex(Suggestions.TITLE) + val value: Int = cursor.getColumnIndex(Suggestions.VALUE) + val summary: Int = cursor.getColumnIndex(Suggestions.SUMMARY) + val icon: Int = cursor.getColumnIndex(Suggestions.ICON) + val extra_id: Int = cursor.getColumnIndex(Suggestions.EXTRA_ID) + + } +} \ No newline at end of file