|
@@ -1,238 +1,168 @@
|
|
|
package com.grkj.iscs.features.main.dialog.data_manage
|
|
package com.grkj.iscs.features.main.dialog.data_manage
|
|
|
|
|
|
|
|
-import android.content.Context
|
|
|
|
|
import android.view.View
|
|
import android.view.View
|
|
|
-import com.drake.brv.BindingAdapter
|
|
|
|
|
import com.drake.brv.utils.linear
|
|
import com.drake.brv.utils.linear
|
|
|
import com.drake.brv.utils.models
|
|
import com.drake.brv.utils.models
|
|
|
import com.drake.brv.utils.setup
|
|
import com.drake.brv.utils.setup
|
|
|
-import com.grkj.data.dao.RoleDao
|
|
|
|
|
-import com.grkj.data.enums.RoleEnum
|
|
|
|
|
import com.grkj.iscs.R
|
|
import com.grkj.iscs.R
|
|
|
-import com.grkj.iscs.databinding.DialogAddRoleBinding
|
|
|
|
|
import com.grkj.iscs.databinding.DialogUpdateRoleBinding
|
|
import com.grkj.iscs.databinding.DialogUpdateRoleBinding
|
|
|
import com.grkj.iscs.databinding.ItemRoleBinding
|
|
import com.grkj.iscs.databinding.ItemRoleBinding
|
|
|
-import com.grkj.iscs.features.main.entity.AddRoleDataEntity
|
|
|
|
|
import com.grkj.iscs.features.main.entity.RoleManageFunctionalPermissionsEntity
|
|
import com.grkj.iscs.features.main.entity.RoleManageFunctionalPermissionsEntity
|
|
|
import com.grkj.iscs.features.main.entity.UpdateRoleDataEntity
|
|
import com.grkj.iscs.features.main.entity.UpdateRoleDataEntity
|
|
|
|
|
+import com.grkj.ui_base.utils.CommonUtils
|
|
|
|
|
+import com.grkj.ui_base.utils.extension.tipDialog
|
|
|
|
|
+import com.kongzue.dialogx.dialogs.CustomDialog
|
|
|
|
|
+import com.kongzue.dialogx.dialogs.PopTip
|
|
|
|
|
+import com.kongzue.dialogx.interfaces.OnBindView
|
|
|
import me.jessyan.autosize.utils.AutoSizeUtils
|
|
import me.jessyan.autosize.utils.AutoSizeUtils
|
|
|
-import razerdp.basepopup.BasePopupWindow
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 更新角色
|
|
|
|
|
|
|
+ * 修改角色对话框,基于 DialogX
|
|
|
|
|
+ * 使用:
|
|
|
|
|
+ * UpdateRoleDialog.show(updateEntity) { updated -> /* handle */ }
|
|
|
*/
|
|
*/
|
|
|
-class UpdateRoleDialog(context: Context) : BasePopupWindow(context) {
|
|
|
|
|
- private var onConfirm: (UpdateRoleDataEntity) -> Unit = {}
|
|
|
|
|
- private lateinit var binding: DialogUpdateRoleBinding
|
|
|
|
|
- private val indentPx: Int by lazy { AutoSizeUtils.dp2px(context, 20f) }
|
|
|
|
|
- private var roleManageFunctionalPermissionsData =
|
|
|
|
|
- RoleManageFunctionalPermissionsEntity.getFunctionalPermissions().reversed()
|
|
|
|
|
- private var selectedPermission: MutableList<String> = mutableListOf()
|
|
|
|
|
- private lateinit var updateRoleDataEntity: UpdateRoleDataEntity
|
|
|
|
|
-
|
|
|
|
|
- init {
|
|
|
|
|
- setContentView(R.layout.dialog_update_role)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+class UpdateRoleDialog(
|
|
|
|
|
+ private val updateEntity: UpdateRoleDataEntity,
|
|
|
|
|
+ private val onConfirm: (UpdateRoleDataEntity) -> Unit
|
|
|
|
|
+) : OnBindView<CustomDialog>(R.layout.dialog_update_role) {
|
|
|
|
|
|
|
|
- override fun onViewCreated(contentView: View) {
|
|
|
|
|
- super.onViewCreated(contentView)
|
|
|
|
|
- binding = DialogUpdateRoleBinding.bind(contentView)
|
|
|
|
|
- binding.closeIv.setOnClickListener {
|
|
|
|
|
- dismiss()
|
|
|
|
|
- clearData()
|
|
|
|
|
- }
|
|
|
|
|
- binding.confirm.setOnClickListener {
|
|
|
|
|
- updateRoleDataEntity.roleName = binding.roleNameEt.text.toString()
|
|
|
|
|
- updateRoleDataEntity.roleKeys = binding.roleKeyEt.text.toString()
|
|
|
|
|
- updateRoleDataEntity.status = if (binding.statusRg.checkedRadioButtonId == -1) {
|
|
|
|
|
- null
|
|
|
|
|
- } else {
|
|
|
|
|
- binding.statusRg.checkedRadioButtonId == binding.activateRb.id
|
|
|
|
|
- }
|
|
|
|
|
- updateRoleDataEntity.functionalPermissions = getSelectedFunctionalPermissions()
|
|
|
|
|
- onConfirm(updateRoleDataEntity)
|
|
|
|
|
- dismiss()
|
|
|
|
|
- clearData()
|
|
|
|
|
- }
|
|
|
|
|
- binding.cancel.setOnClickListener {
|
|
|
|
|
- dismiss()
|
|
|
|
|
- clearData()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private lateinit var binding: DialogUpdateRoleBinding
|
|
|
|
|
+ private val indentPx: Int by lazy { AutoSizeUtils.dp2px(binding.root.context, 20f) }
|
|
|
|
|
+ private var selectedList = mutableListOf<String>()
|
|
|
|
|
+ private var treeData = RoleManageFunctionalPermissionsEntity
|
|
|
|
|
+ .getFunctionalPermissions()
|
|
|
|
|
+ .reversed()
|
|
|
|
|
+ .toMutableList()
|
|
|
|
|
+
|
|
|
|
|
+ override fun onBind(dialog: CustomDialog?, v: View) {
|
|
|
|
|
+ binding = DialogUpdateRoleBinding.bind(v)
|
|
|
|
|
+ dialog?.setMaskColor(CommonUtils.getColor(com.grkj.ui_base.R.color.scrim))
|
|
|
|
|
+
|
|
|
|
|
+ // 预填数据
|
|
|
|
|
+ binding.roleNameEt.setText(updateEntity.roleName)
|
|
|
|
|
+ binding.roleKeyEt.setText(updateEntity.roleKeys)
|
|
|
|
|
+ binding.statusRg.clearCheck()
|
|
|
|
|
+ updateEntity.status?.let { binding.activateRb.isChecked = it }
|
|
|
|
|
+ selectedList.clear()
|
|
|
|
|
+ selectedList.addAll(updateEntity.functionalPermissions.map { it.functionalPermission })
|
|
|
|
|
+ markSelected(treeData)
|
|
|
binding.roleListRv.linear().setup {
|
|
binding.roleListRv.linear().setup {
|
|
|
addType<RoleManageFunctionalPermissionsEntity>(R.layout.item_role)
|
|
addType<RoleManageFunctionalPermissionsEntity>(R.layout.item_role)
|
|
|
onBind {
|
|
onBind {
|
|
|
- onRoleRVListBinding(this)
|
|
|
|
|
|
|
+ val item = getModel<RoleManageFunctionalPermissionsEntity>()
|
|
|
|
|
+ val itemBinding = getBinding<ItemRoleBinding>()
|
|
|
|
|
+ // 缩进
|
|
|
|
|
+ itemBinding.rootLayout.setPadding(indentPx * item.level, 0, 0, 0)
|
|
|
|
|
+ // 展开收起
|
|
|
|
|
+ itemBinding.arrowIv.rotation = if (item.itemExpand) 90f else 0f
|
|
|
|
|
+ itemBinding.rootLayout.setOnClickListener {
|
|
|
|
|
+ if (item.itemExpand) collapse() else expand()
|
|
|
|
|
+ item.itemExpand = !item.itemExpand
|
|
|
|
|
+ itemBinding.arrowIv.rotation = if (item.itemExpand) 90f else 0f
|
|
|
|
|
+ }
|
|
|
|
|
+ // 选中
|
|
|
|
|
+ itemBinding.roleTv.text = item.description
|
|
|
|
|
+ itemBinding.roleCb.setOnCheckedChangeListener(null)
|
|
|
|
|
+ itemBinding.roleCb.isChecked = item.isSelected
|
|
|
|
|
+ itemBinding.roleCb.setOnCheckedChangeListener { _, checked ->
|
|
|
|
|
+ item.isSelected = checked
|
|
|
|
|
+ syncChildren(item, checked)
|
|
|
|
|
+ binding.allSelected.isChecked = treeData.all { it.allSelectedRecursively() }
|
|
|
|
|
+ binding.roleListRv.adapter?.notifyDataSetChanged()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }.models = roleManageFunctionalPermissionsData
|
|
|
|
|
- binding.expandCollapse.setOnCheckedChangeListener { _, isChecked ->
|
|
|
|
|
- roleManageFunctionalPermissionsData =
|
|
|
|
|
- RoleManageFunctionalPermissionsEntity.getFunctionalPermissions().reversed()
|
|
|
|
|
- expandOrCollapseAll(isChecked, roleManageFunctionalPermissionsData)
|
|
|
|
|
- if (binding.allSelected.isChecked) {
|
|
|
|
|
- selectAllOrNone(true, roleManageFunctionalPermissionsData)
|
|
|
|
|
- } else {
|
|
|
|
|
- setSelectedData(roleManageFunctionalPermissionsData)
|
|
|
|
|
- }
|
|
|
|
|
- binding.roleListRv.models = roleManageFunctionalPermissionsData
|
|
|
|
|
- }
|
|
|
|
|
- setSelectAllListener()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ }.models = treeData
|
|
|
|
|
|
|
|
- private fun setSelectedData(dataList: List<RoleManageFunctionalPermissionsEntity>) {
|
|
|
|
|
- dataList.forEach {
|
|
|
|
|
- it.isSelected = selectedPermission.contains(it.functionalPermission)
|
|
|
|
|
- setSelectedData(it.children)
|
|
|
|
|
|
|
+ // 全选展开控件
|
|
|
|
|
+ binding.expandCollapse.setOnCheckedChangeListener { _, expanded ->
|
|
|
|
|
+ toggleExpand(treeData, expanded)
|
|
|
|
|
+ binding.allSelected.isChecked = expanded && treeData.all { it.allSelectedRecursively() }
|
|
|
|
|
+ binding.roleListRv.models = treeData
|
|
|
|
|
+ }
|
|
|
|
|
+ binding.allSelected.setOnCheckedChangeListener { _, selectAll ->
|
|
|
|
|
+ toggleSelect(treeData, selectAll)
|
|
|
|
|
+ binding.roleListRv.adapter?.notifyDataSetChanged()
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- private fun clearData() {
|
|
|
|
|
- binding.roleNameEt.setText("")
|
|
|
|
|
- binding.roleKeyEt.setText("")
|
|
|
|
|
- binding.statusRg.clearCheck()
|
|
|
|
|
- binding.expandCollapse.isChecked = false
|
|
|
|
|
- binding.allSelected.isChecked = false
|
|
|
|
|
- binding.roleListRv.models = null
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 取消
|
|
|
|
|
+ binding.closeIv.setOnClickListener { dialog?.dismiss() }
|
|
|
|
|
+ binding.cancel.setOnClickListener { dialog?.dismiss() }
|
|
|
|
|
|
|
|
- private fun getSelectedData(
|
|
|
|
|
- dataList: List<RoleManageFunctionalPermissionsEntity>,
|
|
|
|
|
- selectedPermission: MutableList<String> = mutableListOf()
|
|
|
|
|
- ): MutableList<String> {
|
|
|
|
|
- dataList.forEach {
|
|
|
|
|
- if (it.isSelected) {
|
|
|
|
|
- selectedPermission.add(it.functionalPermission)
|
|
|
|
|
|
|
+ // 确认
|
|
|
|
|
+ binding.confirm.setOnClickListener {
|
|
|
|
|
+ if (binding.roleNameEt.text.isNullOrBlank()) {
|
|
|
|
|
+ PopTip.build().tipDialog(R.string.please_input_role_name)
|
|
|
|
|
+ return@setOnClickListener
|
|
|
}
|
|
}
|
|
|
- getSelectedData(it.children, selectedPermission)
|
|
|
|
|
|
|
+ updateEntity.roleName = binding.roleNameEt.text.toString()
|
|
|
|
|
+ updateEntity.roleKeys = binding.roleKeyEt.text.toString()
|
|
|
|
|
+ updateEntity.status = binding.activateRb.isChecked
|
|
|
|
|
+ // 收集权限
|
|
|
|
|
+ updateEntity.functionalPermissions = collectSelected(treeData)
|
|
|
|
|
+ onConfirm(updateEntity)
|
|
|
|
|
+ dialog?.dismiss()
|
|
|
}
|
|
}
|
|
|
- return selectedPermission
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private fun setSelectAllListener() {
|
|
|
|
|
- binding.allSelected.setOnCheckedChangeListener { _, isChecked ->
|
|
|
|
|
- selectAllOrNone(isChecked, roleManageFunctionalPermissionsData)
|
|
|
|
|
- binding.roleListRv.adapter?.notifyDataSetChanged()
|
|
|
|
|
|
|
+ private fun markSelected(data: List<RoleManageFunctionalPermissionsEntity>) {
|
|
|
|
|
+ data.forEach {
|
|
|
|
|
+ it.isSelected = selectedList.contains(it.functionalPermission)
|
|
|
|
|
+ markSelected(it.children)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 判断:以当前节点为根,自己和所有子孙节点是否都选中(isSelected == true)。
|
|
|
|
|
- *
|
|
|
|
|
- * @return 如果:当前节点本身 isSelected == true 且 所有 children 递归判断也是 true,则返回 true;否则返回 false。
|
|
|
|
|
- */
|
|
|
|
|
- fun RoleManageFunctionalPermissionsEntity.allSelectedRecursively(): Boolean {
|
|
|
|
|
- // 如果自己没选中,直接返回 false
|
|
|
|
|
- if (!isSelected) return false
|
|
|
|
|
-
|
|
|
|
|
- // 递归检查所有子节点
|
|
|
|
|
- children.forEach { child ->
|
|
|
|
|
- if (!child.allSelectedRecursively()) return false
|
|
|
|
|
|
|
+ private fun syncChildren(item: RoleManageFunctionalPermissionsEntity, checked: Boolean) {
|
|
|
|
|
+ item.children.forEach {
|
|
|
|
|
+ it.isSelected = checked
|
|
|
|
|
+ syncChildren(it, checked)
|
|
|
}
|
|
}
|
|
|
- // 自己选中且所有子节点都选中
|
|
|
|
|
- return true
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private fun expandOrCollapseAll(
|
|
|
|
|
- isExpand: Boolean,
|
|
|
|
|
- roleFunctionalPermissionsData: List<RoleManageFunctionalPermissionsEntity>
|
|
|
|
|
- ) {
|
|
|
|
|
- roleFunctionalPermissionsData.forEach {
|
|
|
|
|
- it.itemExpand = isExpand
|
|
|
|
|
- expandOrCollapseAll(isExpand, it.children)
|
|
|
|
|
|
|
+ private fun toggleExpand(data: List<RoleManageFunctionalPermissionsEntity>, expand: Boolean) {
|
|
|
|
|
+ data.forEach {
|
|
|
|
|
+ it.itemExpand = expand
|
|
|
|
|
+ toggleExpand(it.children, expand)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private fun selectAllOrNone(
|
|
|
|
|
- isSelected: Boolean,
|
|
|
|
|
- roleFunctionalPermissionsData: List<RoleManageFunctionalPermissionsEntity>
|
|
|
|
|
- ) {
|
|
|
|
|
- roleFunctionalPermissionsData.forEach {
|
|
|
|
|
- if (isSelected) {
|
|
|
|
|
- selectedPermission.add(it.functionalPermission)
|
|
|
|
|
- } else {
|
|
|
|
|
- selectedPermission.removeIf { selectedData -> selectedData == it.functionalPermission }
|
|
|
|
|
- }
|
|
|
|
|
- it.isSelected = isSelected
|
|
|
|
|
- selectAllOrNone(isSelected, it.children)
|
|
|
|
|
|
|
+ private fun toggleSelect(data: List<RoleManageFunctionalPermissionsEntity>, select: Boolean) {
|
|
|
|
|
+ data.forEach {
|
|
|
|
|
+ it.isSelected = select
|
|
|
|
|
+ toggleSelect(it.children, select)
|
|
|
}
|
|
}
|
|
|
- selectedPermission.distinct()
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private fun getSelectedFunctionalPermissions(dataList: List<RoleManageFunctionalPermissionsEntity> = roleManageFunctionalPermissionsData): MutableList<RoleManageFunctionalPermissionsEntity> {
|
|
|
|
|
- return mutableListOf<RoleManageFunctionalPermissionsEntity>().apply {
|
|
|
|
|
- addAll(dataList.filter { it.isSelected })
|
|
|
|
|
- dataList.filter { it.isSelected }.forEach {
|
|
|
|
|
- addAll(getSelectedFunctionalPermissions(it.children))
|
|
|
|
|
|
|
+ private fun collectSelected(
|
|
|
|
|
+ data: List<RoleManageFunctionalPermissionsEntity>
|
|
|
|
|
+ ): MutableList<RoleManageFunctionalPermissionsEntity> {
|
|
|
|
|
+ val res = mutableListOf<RoleManageFunctionalPermissionsEntity>()
|
|
|
|
|
+ data.forEach {
|
|
|
|
|
+ if (it.isSelected) {
|
|
|
|
|
+ res += it
|
|
|
|
|
+ res += collectSelected(it.children)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ return res
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private fun BindingAdapter.BindingViewHolder.onRoleRVListBinding(holder: BindingAdapter.BindingViewHolder) {
|
|
|
|
|
- val itemBinding = holder.getBinding<ItemRoleBinding>()
|
|
|
|
|
- val item = holder.getModel<RoleManageFunctionalPermissionsEntity>()
|
|
|
|
|
- // ① 动态缩进
|
|
|
|
|
- itemBinding.rootLayout.setPadding(indentPx * item.level, 0, 0, 0)
|
|
|
|
|
- itemBinding.rootLayout.setOnClickListener {
|
|
|
|
|
- if (item.itemExpand) {
|
|
|
|
|
- itemBinding.arrowIv.rotation = 0f
|
|
|
|
|
- holder.collapse()
|
|
|
|
|
- } else {
|
|
|
|
|
- itemBinding.arrowIv.rotation = 90f
|
|
|
|
|
- holder.expand()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (item.itemExpand) {
|
|
|
|
|
- itemBinding.arrowIv.rotation = 90f
|
|
|
|
|
- } else {
|
|
|
|
|
- itemBinding.arrowIv.rotation = 0f
|
|
|
|
|
- }
|
|
|
|
|
- itemBinding.roleTv.text = item.description
|
|
|
|
|
- itemBinding.roleCb.setOnCheckedChangeListener(null)
|
|
|
|
|
- itemBinding.roleCb.isChecked = item.isSelected || item.children.any { it.isSelected }
|
|
|
|
|
- itemBinding.roleCb.setOnCheckedChangeListener { _, isChecked ->
|
|
|
|
|
- item.isSelected = isChecked
|
|
|
|
|
- if (item.children.isNotEmpty()) {
|
|
|
|
|
- selectAllOrNone(isChecked, item.children)
|
|
|
|
|
- }
|
|
|
|
|
- holder.findParentViewHolder()?.let {
|
|
|
|
|
- val parentItem = it.getModel<RoleManageFunctionalPermissionsEntity>()
|
|
|
|
|
- parentItem.isSelected = parentItem.children.any { it.isSelected }
|
|
|
|
|
- }
|
|
|
|
|
- if (isChecked) {
|
|
|
|
|
- selectedPermission.add(item.functionalPermission)
|
|
|
|
|
- } else {
|
|
|
|
|
- selectedPermission.removeIf { selectedData -> selectedData == item.functionalPermission }
|
|
|
|
|
- }
|
|
|
|
|
- checkAllSelect()
|
|
|
|
|
- adapter.notifyDataSetChanged()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private fun RoleManageFunctionalPermissionsEntity.allSelectedRecursively(): Boolean {
|
|
|
|
|
+ if (!isSelected) return false
|
|
|
|
|
+ return children.all { it.allSelectedRecursively() }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private fun checkAllSelect() {
|
|
|
|
|
- if (roleManageFunctionalPermissionsData.all { it.allSelectedRecursively() }) {
|
|
|
|
|
- binding.allSelected.setOnCheckedChangeListener(null)
|
|
|
|
|
- setSelectAllListener()
|
|
|
|
|
|
|
+ companion object {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 展示修改角色对话框
|
|
|
|
|
+ */
|
|
|
|
|
+ @JvmStatic
|
|
|
|
|
+ fun show(
|
|
|
|
|
+ updateEntity: UpdateRoleDataEntity,
|
|
|
|
|
+ onConfirm: (UpdateRoleDataEntity) -> Unit
|
|
|
|
|
+ ) {
|
|
|
|
|
+ CustomDialog.show(
|
|
|
|
|
+ UpdateRoleDialog(updateEntity, onConfirm),
|
|
|
|
|
+ CustomDialog.ALIGN.CENTER
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 设置角色数据
|
|
|
|
|
- */
|
|
|
|
|
- fun setRoleData(updateRoleDataEntity: UpdateRoleDataEntity) {
|
|
|
|
|
- this.updateRoleDataEntity = updateRoleDataEntity
|
|
|
|
|
- binding.roleNameEt.setText(updateRoleDataEntity.roleName)
|
|
|
|
|
- binding.roleKeyEt.setText(updateRoleDataEntity.roleKeys)
|
|
|
|
|
- binding.activateRb.isChecked = updateRoleDataEntity.status == true
|
|
|
|
|
- binding.roleKeyEt.isEnabled = updateRoleDataEntity.isPreset ==false
|
|
|
|
|
- this.selectedPermission.clear()
|
|
|
|
|
- this.selectedPermission.addAll(getSelectedData(updateRoleDataEntity.functionalPermissions))
|
|
|
|
|
- setSelectedData(roleManageFunctionalPermissionsData)
|
|
|
|
|
- binding.roleListRv.models = roleManageFunctionalPermissionsData
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 设置确认事件
|
|
|
|
|
- */
|
|
|
|
|
- fun setOnConfirmListener(onConfirm: (UpdateRoleDataEntity) -> Unit) {
|
|
|
|
|
- this.onConfirm = onConfirm
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|