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

101 lines
3.3 KiB
Kotlin
Raw Normal View History

2016-07-07 05:42:08 +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.adapter
2017-01-07 15:45:33 +01:00
import android.annotation.SuppressLint
2016-07-07 05:42:08 +02:00
import android.content.Context
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.list_item_simple_user.view.*
2016-07-07 05:42:08 +02:00
import org.mariotaku.twidere.R
2016-12-04 04:58:03 +01:00
import org.mariotaku.twidere.model.AccountDetails
2016-07-07 05:42:08 +02:00
import org.mariotaku.twidere.model.UserKey
2016-12-04 04:58:03 +01:00
class AccountsSpinnerAdapter(
context: Context,
itemViewResource: Int = R.layout.list_item_simple_user
2017-01-07 15:45:33 +01:00
) : BaseArrayAdapter<AccountDetails>(context, itemViewResource) {
2016-07-07 05:42:08 +02:00
private var dummyItemText: String? = null
2016-12-04 04:58:03 +01:00
constructor(context: Context, accounts: Collection<AccountDetails>) : this(context) {
2016-07-07 05:42:08 +02:00
addAll(accounts)
}
override fun getItemId(position: Int): Long {
return getItem(position).hashCode().toLong()
}
2016-08-13 16:04:31 +02:00
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
2016-07-07 05:42:08 +02:00
val view = super.getDropDownView(position, convertView, parent)
bindView(view, getItem(position))
return view
}
2016-07-07 09:39:32 +02:00
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
2016-07-07 05:42:08 +02:00
val view = super.getView(position, convertView, parent)
bindView(view, getItem(position))
return view
}
2016-12-04 04:58:03 +01:00
private fun bindView(view: View, item: AccountDetails) {
val text1 = view.name
val text2 = view.screenName
val icon = view.profileImage
2016-12-04 04:58:03 +01:00
if (!item.dummy) {
2017-01-07 15:45:33 +01:00
text1?.visibility = View.VISIBLE
text1?.text = item.user.name
text2?.visibility = View.VISIBLE
@SuppressLint("SetTextI18n")
text2?.text = "@${item.user.screen_name}"
2016-07-07 05:42:08 +02:00
if (icon != null) {
2017-01-07 15:45:33 +01:00
if (profileImageEnabled) {
icon.visibility = View.VISIBLE
icon.style = profileImageStyle
2016-12-04 04:58:03 +01:00
mediaLoader.displayProfileImage(icon, item.user)
2016-07-07 05:42:08 +02:00
} else {
2017-01-07 15:45:33 +01:00
icon.visibility = View.GONE
2016-07-07 05:42:08 +02:00
mediaLoader.cancelDisplayTask(icon)
}
}
} else {
2017-01-07 15:45:33 +01:00
text1?.visibility = View.VISIBLE
text1?.text = dummyItemText
text2?.visibility = View.GONE
icon?.visibility = View.GONE
2016-07-07 05:42:08 +02:00
}
}
fun setDummyItemText(textRes: Int) {
setDummyItemText(context.getString(textRes))
}
fun setDummyItemText(text: String) {
dummyItemText = text
notifyDataSetChanged()
}
fun findPositionByKey(key: UserKey): Int {
2016-12-04 04:58:03 +01:00
return (0 until count).indexOfFirst { key == getItem(it).key }
2016-07-07 05:42:08 +02:00
}
}