Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.slash.fragment.profile.ui
- import android.support.v4.app.ActivityCompat
- import android.support.v7.widget.RecyclerView
- import android.util.Log
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import android.widget.ImageView
- import android.widget.ProgressBar
- import android.widget.RelativeLayout
- import android.widget.TextView
- import butterknife.BindView
- import butterknife.ButterKnife
- import com.slash.R
- import com.slash.app.state.AccountState
- import com.slash.app.util.MoneyHelper
- import com.slash.fragment.account.model.AccountModel
- import com.squareup.picasso.Picasso
- import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
- import java.util.*
- /**
- * Created by eliaszkubala on 27/07/2017.
- */
- class CardsAdapter(dataset: ArrayList<Any>, private val listener: Interactor) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
- private val VIEW_HEADER = 0
- private val VIEW_CARD = 1
- var mItems: ArrayList<Any>
- init {
- if (dataset.size > 0) {
- dataset.sortBy { (it as AccountModel).name }
- }
- var beforeModel: AccountModel? = null
- dataset.forEachIndexed { i, model ->
- run {
- if (model is AccountModel) {
- if (beforeModel == null || beforeModel?.name != model.name) {
- dataset.add(i, model.name.toString())
- }
- beforeModel = model
- }
- }
- }
- dataset.add("Test")
- dataset.add("Test 2")
- dataset.add("Test 3")
- mItems = dataset
- }
- class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
- @BindView(R.id.logo)
- lateinit var logo: ImageView
- @BindView(R.id.label)
- lateinit var label: TextView
- @BindView(R.id.label2)
- lateinit var label2: TextView
- @BindView(R.id.root)
- lateinit var root: RelativeLayout
- @BindView(R.id.loading)
- lateinit var loading: ProgressBar
- @BindView(R.id.notification)
- lateinit var notification: TextView
- @BindView(R.id.balance)
- lateinit var balance: TextView
- init {
- ButterKnife.bind(this, view)
- }
- }
- class ViewHeader(view: View) : RecyclerView.ViewHolder(view) {
- @BindView(R.id.label)
- lateinit var label: TextView
- init {
- ButterKnife.bind(this, view)
- }
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
- Log.d("eliasz", viewType.toString())
- return if (viewType == VIEW_CARD) {
- ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_card,
- parent, false))
- } else {
- //VIEW_HEADER
- ViewHeader(LayoutInflater.from(parent.context).inflate(R.layout.item_footer,
- parent, false))
- }
- }
- override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
- val item = mItems[position]
- Log.d("eliasz", "test kurwa");
- if (item is AccountModel && holder is ViewHolder) {
- val context = holder.logo.context
- holder.label.text = item.name
- var cardState = AccountState.MAINTENANCE.toString()
- val state = item.state
- state?.let {
- cardState = state.toString()
- if (state == AccountState.ELIGIBLE) cardState = "Authorized"
- if (state == AccountState.NON_ELIGIBLE) cardState = "Non Eligible"
- }
- holder.label2.text = cardState
- Picasso.with(context).load(item.name)
- .placeholder(R.drawable.ic_card_black)
- .transform(RoundedCornersTransformation(20, 0))
- .into(holder.logo)
- holder.notification.text = 1.toString()
- holder.root.isClickable = true
- if (state == AccountState.ELIGIBLE) {
- val color = if (item.balance >= 0) R.color.app_green else R.color.app_red
- holder.balance.setTextColor(ActivityCompat.getColor(context, color))
- holder.balance.visibility = if (item.balance != 0f) View.VISIBLE else View.GONE
- holder.balance.text = MoneyHelper.format(item.balance)
- }
- if (state == AccountState.ELIGIBLE || state == AccountState.DUPLICATED || state == AccountState.NON_ELIGIBLE) {
- show(holder, STATE.NONE)
- } else if (item.isActionRequired) {
- show(holder, STATE.NOTIFICATION)
- holder.label2.text = item.actionRequired.toString()
- } else if (state == AccountState.PROCESSING) {
- show(holder, STATE.LOADING)
- } else if (state == AccountState.UNAUTHORIZED || state == AccountState.MAINTENANCE) {
- show(holder, STATE.NOTIFICATION)
- holder.root.isClickable = false
- }
- holder.root.setOnClickListener { _ -> listener.onClick(position) }
- } else if (item is String && holder is ViewHeader) {
- holder.label.text = mItems[position] as String
- }
- }
- override fun getItemCount(): Int {
- return mItems.size
- }
- fun show(holder: ViewHolder, state: STATE) {
- when (state) {
- STATE.NONE -> {
- holder.loading.visibility = View.GONE
- holder.notification.visibility = View.GONE
- }
- STATE.LOADING -> {
- holder.loading.visibility = View.VISIBLE
- holder.notification.visibility = View.GONE
- holder.balance.visibility = View.GONE
- }
- STATE.NOTIFICATION -> {
- holder.loading.visibility = View.GONE
- holder.notification.visibility = View.VISIBLE
- holder.balance.visibility = View.GONE
- }
- }
- }
- fun removeItem(position: Int) {
- mItems.removeAt(position)
- notifyItemRemoved(position)
- }
- fun restoreItem(item: AccountModel, position: Int) {
- mItems.add(position, item)
- notifyItemInserted(position)
- }
- enum class STATE {
- NONE, LOADING, NOTIFICATION
- }
- override fun getItemViewType(position: Int): Int {
- Log.d("eliasz", mItems[position].javaClass.simpleName)
- return when {
- mItems[position] is String -> VIEW_HEADER
- mItems[position] is AccountModel -> VIEW_CARD
- else -> super.getItemViewType(position)
- }
- }
- interface Interactor {
- fun onClick(position: Int)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement