Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.37 KB | None | 0 0
  1. package com.krungsri.uchoose.digitallending.product.list.presenter
  2.  
  3. import android.content.Context
  4. import android.os.Parcelable
  5. import android.util.AttributeSet
  6. import android.util.SparseArray
  7. import android.view.animation.AnimationUtils
  8. import android.widget.LinearLayout
  9. import com.krungsri.uchoose.R
  10. import kotlinx.android.parcel.Parcelize
  11. import kotlinx.android.synthetic.main.view_product_list.view.*
  12. import kotlinx.android.synthetic.main.view_section_product_header.view.*
  13.  
  14.  
  15. class EntityProductView @JvmOverloads constructor(
  16.         context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
  17. ) : LinearLayout(context, attrs, defStyleAttr) {
  18.     private var entityName = ""
  19.     private var entityDescription = ""
  20.     var onExpand: () -> Unit = {}
  21.     var onClickDetail: (Int) -> Unit = {}
  22.         set(value) {
  23.             field = value
  24.             productListView.onClickDetail = value
  25.         }
  26.  
  27.     var onClickApply: (Int) -> Unit = {}
  28.         set(value) {
  29.             field = value
  30.             productListView.onClickApply = value
  31.         }
  32.  
  33.     init {
  34.         inflate(context, R.layout.view_product_list, this)
  35.         setLayout(attrs)
  36.         setHeader()
  37.         productHeaderView.setOnClickListener {
  38.             toggle()
  39.         }
  40.     }
  41.  
  42.     private fun setHeader() {
  43.         productHeaderView.entityName.text = entityName
  44.         productHeaderView.entityDescription.text = entityDescription
  45.     }
  46.  
  47.     private fun setLayout(attrs: AttributeSet?) {
  48.         val attributes = context.obtainStyledAttributes(attrs, R.styleable.EntityProductView)
  49.         val entityIcon = attributes.getDrawable(R.styleable.EntityProductView_entityIcon)
  50.         entityIcon?.let(productHeaderView.entityIcon::setImageDrawable)
  51.         entityName = attributes.getString(R.styleable.EntityProductView_entityName) ?: ""
  52.         entityDescription = attributes.getString(R.styleable.EntityProductView_entityDescription)
  53.                 ?: ""
  54.         attributes.recycle()
  55.     }
  56.  
  57.     fun expand() {
  58.         onExpand.invoke()
  59.         expandableForProductListLayout.expand()
  60.         toggleArrowIcon()
  61.     }
  62.  
  63.     fun collapse(animate: Boolean = true) {
  64.         expandableForProductListLayout.collapse(animate)
  65.         toggleArrowIcon()
  66.     }
  67.  
  68.     private fun toggle() {
  69.         if (expandableForProductListLayout.isExpanded) {
  70.             collapse()
  71.         } else {
  72.             expand()
  73.         }
  74.     }
  75.  
  76.     private fun toggleArrowIcon() {
  77.         if (expandableForProductListLayout.isExpanded) {
  78.             val expand = AnimationUtils.loadAnimation(context, R.anim.expand_list)
  79.             arrowIcon.startAnimation(expand)
  80.         } else {
  81.             val collapse = AnimationUtils.loadAnimation(context,
  82.                     R.anim.collapse_list)
  83.             arrowIcon.startAnimation(collapse)
  84.         }
  85.     }
  86.  
  87.     override fun onSaveInstanceState(): Parcelable? {
  88.         val instanceState = InstanceState(superState = super.onSaveInstanceState())
  89.         expandableForProductListLayout.saveHierarchyState(instanceState.childrenState)
  90.         return instanceState
  91.     }
  92.  
  93.     override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>?) {
  94.         dispatchFreezeSelfOnly(container)
  95.     }
  96.  
  97.     override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>?) {
  98.         dispatchThawSelfOnly(container)
  99.     }
  100.  
  101.     override fun onRestoreInstanceState(state: Parcelable?) {
  102.         (state as? InstanceState)?.let {
  103.             super.onRestoreInstanceState(it.superState)
  104.             expandableForProductListLayout.restoreHierarchyState(it.childrenState)
  105.         }
  106.         toggleArrowIcon()
  107.     }
  108.  
  109.     @Parcelize
  110.     private class InstanceState(val childrenState: SparseArray<Parcelable> = SparseArray(),
  111.                                 val superState: Parcelable?) : Parcelable
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement