Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.krungsri.uchoose.digitallending.product.list.presenter
- import android.content.Context
- import android.os.Parcelable
- import android.util.AttributeSet
- import android.util.SparseArray
- import android.view.animation.AnimationUtils
- import android.widget.LinearLayout
- import com.krungsri.uchoose.R
- import kotlinx.android.parcel.Parcelize
- import kotlinx.android.synthetic.main.view_product_list.view.*
- import kotlinx.android.synthetic.main.view_section_product_header.view.*
- class EntityProductView @JvmOverloads constructor(
- context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
- ) : LinearLayout(context, attrs, defStyleAttr) {
- private var entityName = ""
- private var entityDescription = ""
- var onExpand: () -> Unit = {}
- var onClickDetail: (Int) -> Unit = {}
- set(value) {
- field = value
- productListView.onClickDetail = value
- }
- var onClickApply: (Int) -> Unit = {}
- set(value) {
- field = value
- productListView.onClickApply = value
- }
- init {
- inflate(context, R.layout.view_product_list, this)
- setLayout(attrs)
- setHeader()
- productHeaderView.setOnClickListener {
- toggle()
- }
- }
- private fun setHeader() {
- productHeaderView.entityName.text = entityName
- productHeaderView.entityDescription.text = entityDescription
- }
- private fun setLayout(attrs: AttributeSet?) {
- val attributes = context.obtainStyledAttributes(attrs, R.styleable.EntityProductView)
- val entityIcon = attributes.getDrawable(R.styleable.EntityProductView_entityIcon)
- entityIcon?.let(productHeaderView.entityIcon::setImageDrawable)
- entityName = attributes.getString(R.styleable.EntityProductView_entityName) ?: ""
- entityDescription = attributes.getString(R.styleable.EntityProductView_entityDescription)
- ?: ""
- attributes.recycle()
- }
- fun expand() {
- onExpand.invoke()
- expandableForProductListLayout.expand()
- toggleArrowIcon()
- }
- fun collapse(animate: Boolean = true) {
- expandableForProductListLayout.collapse(animate)
- toggleArrowIcon()
- }
- private fun toggle() {
- if (expandableForProductListLayout.isExpanded) {
- collapse()
- } else {
- expand()
- }
- }
- private fun toggleArrowIcon() {
- if (expandableForProductListLayout.isExpanded) {
- val expand = AnimationUtils.loadAnimation(context, R.anim.expand_list)
- arrowIcon.startAnimation(expand)
- } else {
- val collapse = AnimationUtils.loadAnimation(context,
- R.anim.collapse_list)
- arrowIcon.startAnimation(collapse)
- }
- }
- override fun onSaveInstanceState(): Parcelable? {
- val instanceState = InstanceState(superState = super.onSaveInstanceState())
- expandableForProductListLayout.saveHierarchyState(instanceState.childrenState)
- return instanceState
- }
- override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>?) {
- dispatchFreezeSelfOnly(container)
- }
- override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>?) {
- dispatchThawSelfOnly(container)
- }
- override fun onRestoreInstanceState(state: Parcelable?) {
- (state as? InstanceState)?.let {
- super.onRestoreInstanceState(it.superState)
- expandableForProductListLayout.restoreHierarchyState(it.childrenState)
- }
- toggleArrowIcon()
- }
- @Parcelize
- private class InstanceState(val childrenState: SparseArray<Parcelable> = SparseArray(),
- val superState: Parcelable?) : Parcelable
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement