Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. package com.slash.fragment.profile.ui
  2.  
  3. import android.support.v4.app.ActivityCompat
  4. import android.support.v7.widget.RecyclerView
  5. import android.util.Log
  6. import android.view.LayoutInflater
  7. import android.view.View
  8. import android.view.ViewGroup
  9. import android.widget.ImageView
  10. import android.widget.ProgressBar
  11. import android.widget.RelativeLayout
  12. import android.widget.TextView
  13. import butterknife.BindView
  14. import butterknife.ButterKnife
  15. import com.slash.R
  16. import com.slash.app.state.AccountState
  17. import com.slash.app.util.MoneyHelper
  18. import com.slash.fragment.account.model.AccountModel
  19. import com.squareup.picasso.Picasso
  20. import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
  21. import java.util.*
  22.  
  23. /**
  24. * Created by eliaszkubala on 27/07/2017.
  25. */
  26.  
  27. class CardsAdapter(dataset: ArrayList<Any>, private val listener: Interactor) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
  28.  
  29. private val VIEW_HEADER = 0
  30. private val VIEW_CARD = 1
  31.  
  32. var mItems: ArrayList<Any>
  33.  
  34. init {
  35.  
  36. if (dataset.size > 0) {
  37. dataset.sortBy { (it as AccountModel).name }
  38. }
  39.  
  40. var beforeModel: AccountModel? = null
  41. dataset.forEachIndexed { i, model ->
  42. run {
  43. if (model is AccountModel) {
  44. if (beforeModel == null || beforeModel?.name != model.name) {
  45. dataset.add(i, model.name.toString())
  46. }
  47. beforeModel = model
  48. }
  49. }
  50. }
  51.  
  52. dataset.add("Test")
  53. dataset.add("Test 2")
  54. dataset.add("Test 3")
  55.  
  56. mItems = dataset
  57. }
  58.  
  59. class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
  60.  
  61. @BindView(R.id.logo)
  62. lateinit var logo: ImageView
  63. @BindView(R.id.label)
  64. lateinit var label: TextView
  65. @BindView(R.id.label2)
  66. lateinit var label2: TextView
  67. @BindView(R.id.root)
  68. lateinit var root: RelativeLayout
  69. @BindView(R.id.loading)
  70. lateinit var loading: ProgressBar
  71. @BindView(R.id.notification)
  72. lateinit var notification: TextView
  73. @BindView(R.id.balance)
  74. lateinit var balance: TextView
  75.  
  76. init {
  77. ButterKnife.bind(this, view)
  78. }
  79. }
  80.  
  81. class ViewHeader(view: View) : RecyclerView.ViewHolder(view) {
  82.  
  83. @BindView(R.id.label)
  84. lateinit var label: TextView
  85.  
  86. init {
  87. ButterKnife.bind(this, view)
  88. }
  89. }
  90.  
  91. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
  92. Log.d("eliasz", viewType.toString())
  93. return if (viewType == VIEW_CARD) {
  94. ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_card,
  95. parent, false))
  96. } else {
  97. //VIEW_HEADER
  98. ViewHeader(LayoutInflater.from(parent.context).inflate(R.layout.item_footer,
  99. parent, false))
  100. }
  101. }
  102.  
  103. override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
  104.  
  105. val item = mItems[position]
  106.  
  107. Log.d("eliasz", "test kurwa");
  108.  
  109. if (item is AccountModel && holder is ViewHolder) {
  110. val context = holder.logo.context
  111. holder.label.text = item.name
  112.  
  113. var cardState = AccountState.MAINTENANCE.toString()
  114. val state = item.state
  115. state?.let {
  116. cardState = state.toString()
  117. if (state == AccountState.ELIGIBLE) cardState = "Authorized"
  118. if (state == AccountState.NON_ELIGIBLE) cardState = "Non Eligible"
  119. }
  120.  
  121. holder.label2.text = cardState
  122.  
  123. Picasso.with(context).load(item.name)
  124. .placeholder(R.drawable.ic_card_black)
  125. .transform(RoundedCornersTransformation(20, 0))
  126. .into(holder.logo)
  127.  
  128. holder.notification.text = 1.toString()
  129.  
  130.  
  131. holder.root.isClickable = true
  132.  
  133. if (state == AccountState.ELIGIBLE) {
  134. val color = if (item.balance >= 0) R.color.app_green else R.color.app_red
  135. holder.balance.setTextColor(ActivityCompat.getColor(context, color))
  136. holder.balance.visibility = if (item.balance != 0f) View.VISIBLE else View.GONE
  137. holder.balance.text = MoneyHelper.format(item.balance)
  138. }
  139.  
  140. if (state == AccountState.ELIGIBLE || state == AccountState.DUPLICATED || state == AccountState.NON_ELIGIBLE) {
  141. show(holder, STATE.NONE)
  142. } else if (item.isActionRequired) {
  143. show(holder, STATE.NOTIFICATION)
  144. holder.label2.text = item.actionRequired.toString()
  145. } else if (state == AccountState.PROCESSING) {
  146. show(holder, STATE.LOADING)
  147. } else if (state == AccountState.UNAUTHORIZED || state == AccountState.MAINTENANCE) {
  148. show(holder, STATE.NOTIFICATION)
  149. holder.root.isClickable = false
  150. }
  151.  
  152. holder.root.setOnClickListener { _ -> listener.onClick(position) }
  153. } else if (item is String && holder is ViewHeader) {
  154. holder.label.text = mItems[position] as String
  155. }
  156. }
  157.  
  158. override fun getItemCount(): Int {
  159. return mItems.size
  160. }
  161.  
  162. fun show(holder: ViewHolder, state: STATE) {
  163. when (state) {
  164. STATE.NONE -> {
  165. holder.loading.visibility = View.GONE
  166. holder.notification.visibility = View.GONE
  167. }
  168. STATE.LOADING -> {
  169. holder.loading.visibility = View.VISIBLE
  170. holder.notification.visibility = View.GONE
  171. holder.balance.visibility = View.GONE
  172. }
  173. STATE.NOTIFICATION -> {
  174. holder.loading.visibility = View.GONE
  175. holder.notification.visibility = View.VISIBLE
  176. holder.balance.visibility = View.GONE
  177. }
  178. }
  179. }
  180.  
  181. fun removeItem(position: Int) {
  182. mItems.removeAt(position)
  183. notifyItemRemoved(position)
  184. }
  185.  
  186. fun restoreItem(item: AccountModel, position: Int) {
  187. mItems.add(position, item)
  188. notifyItemInserted(position)
  189. }
  190.  
  191. enum class STATE {
  192. NONE, LOADING, NOTIFICATION
  193. }
  194.  
  195. override fun getItemViewType(position: Int): Int {
  196. Log.d("eliasz", mItems[position].javaClass.simpleName)
  197. return when {
  198. mItems[position] is String -> VIEW_HEADER
  199. mItems[position] is AccountModel -> VIEW_CARD
  200. else -> super.getItemViewType(position)
  201. }
  202. }
  203.  
  204. interface Interactor {
  205.  
  206. fun onClick(position: Int)
  207.  
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement