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

99 lines
3.4 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
import android.content.Context
import android.view.View
import android.view.ViewGroup
2017-03-01 15:12:25 +01:00
import com.bumptech.glide.RequestManager
import kotlinx.android.synthetic.main.list_item_simple_user.view.*
2017-05-13 08:19:23 +02:00
import org.mariotaku.ktextension.spannable
2016-07-07 05:42:08 +02:00
import org.mariotaku.twidere.R
2017-03-02 02:08:54 +01:00
import org.mariotaku.twidere.extension.loadProfileImage
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,
2017-03-01 15:12:25 +01:00
itemViewResource: Int = R.layout.list_item_simple_user,
accounts: Collection<AccountDetails>? = null,
2017-03-02 07:59:19 +01:00
requestManager: RequestManager
) : BaseArrayAdapter<AccountDetails>(context, itemViewResource, accounts, requestManager) {
2016-07-07 05:42:08 +02:00
private var dummyItemText: String? = null
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
2017-05-13 08:19:23 +02:00
text1?.spannable = item.user.name
2017-01-07 15:45:33 +01:00
text2?.visibility = View.VISIBLE
2017-05-13 08:19:23 +02:00
text2?.spannable = "@${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
requestManager.loadProfileImage(context, item.user, profileImageStyle).into(icon)
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
}
}
} else {
2017-01-07 15:45:33 +01:00
text1?.visibility = View.VISIBLE
2017-05-13 08:19:23 +02:00
text1?.spannable = dummyItemText
2017-01-07 15:45:33 +01:00
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
}
}