package com.grkj.iscs.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.grkj.iscs.R
import com.grkj.iscs.databinding.LayoutTitleBarBinding

class TitleBar(private val ctx: Context, attrs: AttributeSet) : ConstraintLayout(ctx, attrs) {

    private var mBinding: LayoutTitleBarBinding

    init {
        val root = View.inflate(ctx, R.layout.layout_title_bar, this)
        mBinding = LayoutTitleBarBinding.bind(root)
        mBinding.ivBack.setOnClickListener {
            if (ctx is AppCompatActivity) {
                ctx.finish()
            }
        }

        val attrSet = ctx.obtainStyledAttributes(attrs, R.styleable.TitleBar)
        attrSet.getString(R.styleable.TitleBar_title)?.let { mBinding.tvTitle.text = it }
        attrSet.recycle()
    }

    fun setTitle(title: String) {
        mBinding.tvTitle.text = title
    }
}