Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ComposeAutoCompleteAdapter.kt

162 lines
6.1 KiB
Kotlin
Raw Normal View History

2017-01-07 15:45:33 +01: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.adapter
import android.content.Context
2017-04-17 15:10:14 +02:00
import android.content.SharedPreferences
2017-01-07 15:45:33 +01:00
import android.database.Cursor
import android.graphics.PorterDuff.Mode
import android.support.v4.widget.SimpleCursorAdapter
import android.view.View
import android.widget.TextView
2017-03-02 10:23:34 +01:00
import com.bumptech.glide.RequestManager
2017-01-07 15:45:33 +01:00
import org.mariotaku.kpreferences.get
2017-05-13 08:19:23 +02:00
import org.mariotaku.ktextension.spannable
2017-01-07 15:45:33 +01:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.*
import org.mariotaku.twidere.constant.displayProfileImageKey
import org.mariotaku.twidere.constant.profileImageStyleKey
2017-03-02 10:23:34 +01:00
import org.mariotaku.twidere.extension.loadProfileImage
2017-01-23 07:56:55 +01:00
import org.mariotaku.twidere.model.SuggestionItem
2017-01-07 15:45:33 +01:00
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.provider.TwidereDataStore.Suggestions
import org.mariotaku.twidere.util.UserColorNameManager
2017-04-16 15:09:56 +02:00
import org.mariotaku.twidere.util.dagger.GeneralComponent
2017-01-07 15:45:33 +01:00
import org.mariotaku.twidere.view.ProfileImageView
import javax.inject.Inject
2017-03-02 10:23:34 +01:00
class ComposeAutoCompleteAdapter(context: Context, val requestManager: RequestManager) : SimpleCursorAdapter(context,
2017-01-07 15:45:33 +01:00
R.layout.list_item_auto_complete, null, emptyArray(), intArrayOf(), 0) {
@Inject
2017-04-17 15:10:14 +02:00
lateinit var preferences: SharedPreferences
2017-01-07 15:45:33 +01:00
@Inject
lateinit var userColorNameManager: UserColorNameManager
private val displayProfileImage: Boolean
private val profileImageStyle: Int
2017-01-23 07:56:55 +01:00
private var indices: SuggestionItem.Indices? = null
2017-01-07 15:45:33 +01:00
var accountKey: UserKey? = null
2017-01-23 07:41:36 +01:00
private var token: Char = ' '
2017-01-07 15:45:33 +01:00
init {
2017-04-16 15:09:56 +02:00
GeneralComponent.get(context).inject(this)
2017-01-07 15:45:33 +01:00
displayProfileImage = preferences[displayProfileImageKey]
profileImageStyle = preferences[profileImageStyleKey]
}
2017-03-02 10:23:34 +01:00
override fun bindView(view: View, context: Context, cursor: Cursor) {
2017-01-23 07:56:55 +01:00
val indices = this.indices!!
2017-01-07 15:45:33 +01:00
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
2017-01-23 07:56:55 +01:00
if (Suggestions.AutoComplete.TYPE_USERS == cursor.getString(indices.type)) {
2017-05-13 08:19:23 +02:00
text1.spannable = userColorNameManager.getUserNickname(cursor.getString(indices.extra_id),
2017-01-23 07:56:55 +01:00
cursor.getString(indices.title))
2017-05-13 08:19:23 +02:00
text2.spannable = String.format("@%s", cursor.getString(indices.summary))
2017-01-07 15:45:33 +01:00
if (displayProfileImage) {
2017-01-23 07:56:55 +01:00
val profileImageUrl = cursor.getString(indices.icon)
requestManager.loadProfileImage(context, profileImageUrl, profileImageStyle).into(icon)
2017-01-07 15:45:33 +01:00
} else {
2017-03-01 15:12:25 +01:00
//TODO cancel image load
2017-01-07 15:45:33 +01:00
}
icon.clearColorFilter()
} else {
2017-05-13 08:19:23 +02:00
text1.spannable = String.format("#%s", cursor.getString(indices.title))
2017-01-07 15:45:33 +01:00
text2.setText(R.string.hashtag)
icon.setImageResource(R.drawable.ic_action_hashtag)
icon.setColorFilter(text1.currentTextColor, Mode.SRC_ATOP)
}
icon.visibility = if (displayProfileImage) View.VISIBLE else View.GONE
super.bindView(view, context, cursor)
}
fun closeCursor() {
val cursor = swapCursor(null) ?: return
if (!cursor.isClosed) {
cursor.close()
}
}
2017-01-23 07:56:55 +01:00
override fun convertToString(cursor: Cursor): CharSequence {
val indices = this.indices!!
when (cursor.getString(indices.type)) {
2017-01-07 15:45:33 +01:00
Suggestions.AutoComplete.TYPE_HASHTAGS -> {
2017-01-23 07:56:55 +01:00
return '#' + cursor.getString(indices.value)
2017-01-07 15:45:33 +01:00
}
Suggestions.AutoComplete.TYPE_USERS -> {
2017-01-23 07:56:55 +01:00
return '@' + cursor.getString(indices.value)
2017-01-07 15:45:33 +01:00
}
}
2017-01-23 07:56:55 +01:00
return cursor.getString(indices.value)
2017-01-07 15:45:33 +01:00
}
2017-01-23 07:41:36 +01:00
override fun runQueryOnBackgroundThread(constraint: CharSequence?): Cursor? {
if (constraint == null || constraint.isEmpty()) return null
2017-01-07 15:45:33 +01:00
val token = constraint[0]
2017-01-23 07:41:36 +01:00
if (getNormalizedSymbol(token) == getNormalizedSymbol(this.token)) {
2017-01-07 15:45:33 +01:00
val filter = filterQueryProvider
if (filter != null) return filter.runQuery(constraint)
}
2017-01-23 07:41:36 +01:00
this.token = token
2017-01-07 15:45:33 +01:00
val builder = Suggestions.AutoComplete.CONTENT_URI.buildUpon()
builder.appendQueryParameter(QUERY_PARAM_QUERY, constraint.subSequence(1, constraint.length).toString())
when (getNormalizedSymbol(token)) {
'#' -> {
builder.appendQueryParameter(QUERY_PARAM_TYPE, Suggestions.AutoComplete.TYPE_HASHTAGS)
}
'@' -> {
builder.appendQueryParameter(QUERY_PARAM_TYPE, Suggestions.AutoComplete.TYPE_USERS)
}
else -> {
return null
}
}
builder.appendQueryParameter(QUERY_PARAM_ACCOUNT_KEY, accountKey.toString())
return mContext.contentResolver.query(builder.build(), Suggestions.AutoComplete.COLUMNS,
null, null, null)
}
override fun swapCursor(cursor: Cursor?): Cursor? {
if (cursor != null) {
2017-01-23 07:56:55 +01:00
indices = SuggestionItem.Indices(cursor)
2017-01-07 15:45:33 +01:00
}
return super.swapCursor(cursor)
}
companion object {
private fun getNormalizedSymbol(character: Char): Char {
when (character) {
'\uff20', '@' -> return '@'
'\uff03', '#' -> return '#'
}
return '\u0000'
}
}
}