Newer
Older
CasicSmartTube / app / src / main / java / com / casic / smarttube / widgets / MultiSelectDialog.kt
package com.casic.smarttube.widgets

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import com.casic.smarttube.R
import com.casic.smarttube.adapter.DialogDeviceListAdapter
import com.casic.smarttube.databinding.DialogMultiSelectBinding
import com.casic.smarttube.model.DeviceListModel
import com.pengxh.kt.lite.extensions.binding
import com.pengxh.kt.lite.extensions.resetParams

class MultiSelectDialog private constructor(builder: Builder) : Dialog(
    builder.activity, R.style.UserDefinedDialogStyle
) {

    private val kTag = "MultiSelectDialog"
    private val act: Activity = builder.activity
    private val title: String = builder.title
    private val dataModels: MutableList<DeviceListModel.DataModel> = builder.dataModels
    private val negativeBtn: String = builder.negativeBtn
    private val positiveBtn: String = builder.positiveBtn
    private val listener: OnDialogButtonClickListener = builder.listener

    class Builder {
        lateinit var activity: Activity
        lateinit var title: String
        lateinit var dataModels: MutableList<DeviceListModel.DataModel>
        lateinit var negativeBtn: String
        lateinit var positiveBtn: String
        lateinit var listener: OnDialogButtonClickListener

        fun setContext(activity: Activity): Builder {
            this.activity = activity
            return this
        }

        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setDataSource(dataModels: MutableList<DeviceListModel.DataModel>): Builder {
            this.dataModels = dataModels
            return this
        }

        fun setNegativeButton(name: String): Builder {
            this.negativeBtn = name
            return this
        }

        fun setPositiveButton(name: String): Builder {
            this.positiveBtn = name
            return this
        }

        fun setOnDialogButtonClickListener(listener: OnDialogButtonClickListener): Builder {
            this.listener = listener
            return this
        }

        fun build(): MultiSelectDialog {
            return MultiSelectDialog(this)
        }
    }

    private val binding: DialogMultiSelectBinding by binding()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.resetParams(Gravity.CENTER, R.style.UserDefinedAnimation, 0.85f)
        setContentView(R.layout.dialog_multi_select)
        setCancelable(false)
        setCanceledOnTouchOutside(false)

        if (title.isNotBlank()) {
            binding.dialogTitleView.text = title
        }

        val dialogDeviceListAdapter = DialogDeviceListAdapter(act, dataModels)
        binding.dataRecyclerView.addItemDecoration(
            DividerItemDecoration(
                act, RecyclerView.VERTICAL
            )
        )
        binding.dataRecyclerView.adapter = dialogDeviceListAdapter

        binding.dialogCancelButton.text = negativeBtn
        binding.dialogCancelButton.setOnClickListener {
            listener.onCancelClick()
            this.dismiss()
        }

        binding.dialogConfirmButton.text = positiveBtn
        binding.dialogConfirmButton.setOnClickListener {
            // 处理数据
            listener.onConfirmClick(dialogDeviceListAdapter.selectedModels)
            this.dismiss()
        }
    }

    interface OnDialogButtonClickListener {
        fun onConfirmClick(selectedModels: MutableList<DeviceListModel.DataModel>)

        fun onCancelClick()
    }
}