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

96 lines
3.1 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.support.v7.widget.RecyclerViewAccessor
2016-07-07 05:42:08 +02:00
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
2017-03-01 15:12:25 +01:00
import com.bumptech.glide.RequestManager
2016-07-07 05:42:08 +02:00
import org.mariotaku.twidere.R
2016-12-03 16:45:13 +01:00
import org.mariotaku.twidere.model.AccountDetails
2016-12-09 03:02:41 +01:00
import org.mariotaku.twidere.model.UserKey
2016-07-07 05:42:08 +02:00
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
import org.mariotaku.twidere.view.holder.AccountViewHolder
2017-03-01 15:12:25 +01:00
class AccountDetailsAdapter(
context: Context,
2017-03-02 07:59:19 +01:00
requestManager: RequestManager
) : BaseArrayAdapter<AccountDetails>(context, R.layout.list_item_account, requestManager = requestManager) {
2016-07-07 05:42:08 +02:00
var sortEnabled: Boolean = false
set(value) {
field = value
notifyDataSetChanged()
}
var switchEnabled: Boolean = false
set(value) {
field = value
notifyDataSetChanged()
}
2016-12-05 01:52:02 +01:00
var accountToggleListener: ((Int, Boolean) -> Unit)? = null
2016-07-07 05:42:08 +02:00
val checkedChangeListener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
2016-12-05 01:52:02 +01:00
val position = buttonView.tag as? Int ?: return@OnCheckedChangeListener
accountToggleListener?.invoke(position, isChecked)
2016-07-07 05:42:08 +02:00
}
init {
GeneralComponentHelper.build(context).inject(this)
}
2016-12-03 16:45:13 +01:00
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = super.getView(position, convertView, parent)
val holder = view.tag as? AccountViewHolder ?: run {
val h = AccountViewHolder(view, this)
2016-12-03 16:45:13 +01:00
view.tag = h
return@run h
}
RecyclerViewAccessor.setLayoutPosition(holder, position)
2016-12-03 16:45:13 +01:00
val details = getItem(position)
holder.display(details)
2016-07-07 05:42:08 +02:00
return view
}
2016-12-05 01:52:02 +01:00
override fun hasStableIds(): Boolean {
return true
}
override fun getItemId(position: Int): Long {
return getItem(position).key.hashCode().toLong()
}
fun drop(from: Int, to: Int) {
val fromItem = getItem(from)
removeAt(from)
insert(fromItem, to)
2016-07-07 05:42:08 +02:00
}
2016-12-05 01:52:02 +01:00
2016-12-09 03:02:41 +01:00
fun findItem(key: UserKey): AccountDetails? {
(0 until count).forEach { i ->
val item = getItem(i)
if (key == item.key) return item
}
return null
}
2016-07-07 05:42:08 +02:00
}