CourseCouponDetailView.kt 2.73 KB
Newer Older
严久程 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.yidianling.course.widget

import android.content.Context
import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.text.style.AbsoluteSizeSpan
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.yidianling.common.tools.RxImageTool
import com.yidianling.course.R
import com.yidianling.course.bean.CourseCouponBean
14
import kotlinx.android.synthetic.main.course_view_course_coupon_detail.view.*
严久程 committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import java.lang.Float
import java.math.BigDecimal


/**
 * @author jiucheng
 * @描述:课程优惠券详情页面
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/3/27
 */
class CourseCouponDetailView : LinearLayout {
    constructor(context: Context) : super(context) {
        initView()
    }

    private fun initView() {
        orientation = LinearLayout.HORIZONTAL
        val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, RxImageTool.dp2px(90f))
        params.bottomMargin = RxImageTool.dp2px(10f)
        layoutParams = params
36 37
        setBackgroundResource(R.drawable.course_img_course_coupon_detail_bg)
        View.inflate(context, R.layout.course_view_course_coupon_detail, this)
严久程 committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    }


    fun setData(beanCourse: CourseCouponBean, hasReceived: Boolean, event: (toLink: String) -> Unit) {
        if (TextUtils.equals(beanCourse.type, "1")) {//立减
            val amountStr = "¥${beanCourse.amount}"
            val sp = SpannableString(amountStr)
            sp.setSpan(AbsoluteSizeSpan(16, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            tv_price.text = sp
        } else {//折扣
            val a1 = BigDecimal(Float.toString(beanCourse.discount.toFloat()))
            val b1 = BigDecimal(Float.toString(10f))
            val result = a1.multiply(b1).toFloat()// 相乘结果
            val discountStr = "${result}折"
            val discountSp = SpannableString(discountStr)
            discountSp.setSpan(AbsoluteSizeSpan(16, true), discountStr.length - 1, discountStr.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            tv_price.text = discountSp
        }
        tv_title.text = beanCourse.title
        tv_duration.text = beanCourse.effective
        tv_use_condition.text = beanCourse.rangeCont
        tv_discountCont.text = beanCourse.discountCont
        if (TextUtils.isEmpty(beanCourse.discountCont) && beanCourse.useCondition == "1") {
            tv_discountCont.text = "无限制"
        }

        tv_use.visibility = if (hasReceived) View.VISIBLE else View.GONE
        tv_use.setOnClickListener {
            event(beanCourse.toLink)
        }
    }

    fun setUseVisibility(hasReceived: Boolean) {
        tv_use.visibility = if (hasReceived) View.VISIBLE else View.GONE
    }
}