mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Add edit group view model
This commit is contained in:
@@ -82,7 +82,6 @@ import com.kunzisoft.keepass.viewmodels.GroupViewModel
|
||||
import org.joda.time.DateTime
|
||||
|
||||
class GroupActivity : LockingActivity(),
|
||||
GroupEditDialogFragment.EditGroupListener,
|
||||
DatePickerDialog.OnDateSetListener,
|
||||
TimePickerDialog.OnTimeSetListener,
|
||||
GroupFragment.NodeClickListener,
|
||||
@@ -109,7 +108,6 @@ class GroupActivity : LockingActivity(),
|
||||
private var mGroupFragment: GroupFragment? = null
|
||||
private var mRecyclingBinEnabled = false
|
||||
private var mRecyclingBinIsCurrentGroup = false
|
||||
private var mGroupNamesNotAllowed: List<String>? = null
|
||||
private var mRequestStartupSearch = true
|
||||
|
||||
private var actionNodeMode: ActionMode? = null
|
||||
@@ -300,6 +298,22 @@ class GroupActivity : LockingActivity(),
|
||||
.show(supportFragmentManager, "DatePickerFragment")
|
||||
}
|
||||
}
|
||||
|
||||
mGroupEditViewModel.onGroupCreated.observe(this) { groupInfo ->
|
||||
if (groupInfo.title.isNotEmpty()) {
|
||||
mCurrentGroup?.let { currentGroup ->
|
||||
createGroup(currentGroup, groupInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mGroupEditViewModel.onGroupUpdated.observe(this) { groupInfo ->
|
||||
if (groupInfo.title.isNotEmpty()) {
|
||||
mOldGroupToUpdate?.let { oldGroupToUpdate ->
|
||||
updateGroup(oldGroupToUpdate, groupInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDatabaseRetrieved(database: Database?) {
|
||||
@@ -308,10 +322,10 @@ class GroupActivity : LockingActivity(),
|
||||
rootContainerView?.resetAppTimeoutWhenViewFocusedOrChanged(this, database)
|
||||
|
||||
mGroupViewModel.setDatabase(database)
|
||||
mGroupEditViewModel.setGroupNamesNotAllowed(database?.groupNamesNotAllowed)
|
||||
|
||||
mRecyclingBinEnabled = !mReadOnly
|
||||
&& database?.isRecycleBinEnabled == true
|
||||
mGroupNamesNotAllowed = database?.groupNamesNotAllowed
|
||||
|
||||
mRootGroup = database?.rootGroup
|
||||
if (mCurrentGroupId == null) {
|
||||
@@ -981,43 +995,6 @@ class GroupActivity : LockingActivity(),
|
||||
}
|
||||
}
|
||||
|
||||
override fun isValidGroupName(name: String): GroupEditDialogFragment.Error {
|
||||
if (name.isEmpty()) {
|
||||
return GroupEditDialogFragment.Error(true, R.string.error_no_name)
|
||||
}
|
||||
if (mGroupNamesNotAllowed?.find { it.equals(name, ignoreCase = true) } != null) {
|
||||
return GroupEditDialogFragment.Error(true, R.string.error_word_reserved)
|
||||
}
|
||||
return GroupEditDialogFragment.Error(false, null)
|
||||
}
|
||||
|
||||
override fun approveEditGroup(action: GroupEditDialogFragment.EditGroupDialogAction,
|
||||
groupInfo: GroupInfo) {
|
||||
|
||||
if (groupInfo.title.isNotEmpty()) {
|
||||
when (action) {
|
||||
GroupEditDialogFragment.EditGroupDialogAction.CREATION -> {
|
||||
// If group creation
|
||||
mCurrentGroup?.let { currentGroup ->
|
||||
createGroup(currentGroup, groupInfo)
|
||||
}
|
||||
}
|
||||
GroupEditDialogFragment.EditGroupDialogAction.UPDATE -> {
|
||||
// If update add new elements
|
||||
mOldGroupToUpdate?.let { oldGroupToUpdate ->
|
||||
updateGroup(oldGroupToUpdate, groupInfo)
|
||||
}
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun cancelEditGroup(action: GroupEditDialogFragment.EditGroupDialogAction,
|
||||
groupInfo: GroupInfo) {
|
||||
// Do nothing here
|
||||
}
|
||||
|
||||
override fun onSortSelected(sortNodeEnum: SortNodeEnum, sortNodeParameters: SortNodeEnum.SortNodeParameters) {
|
||||
mGroupFragment?.onSortSelected(sortNodeEnum, sortNodeParameters)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
package com.kunzisoft.keepass.activities.dialogs
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
@@ -31,8 +30,7 @@ import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.activities.dialogs.GroupEditDialogFragment.EditGroupDialogAction.CREATION
|
||||
import com.kunzisoft.keepass.activities.dialogs.GroupEditDialogFragment.EditGroupDialogAction.UPDATE
|
||||
import com.kunzisoft.keepass.activities.dialogs.GroupEditDialogFragment.EditGroupDialogAction.*
|
||||
import com.kunzisoft.keepass.activities.fragments.DatabaseDialogFragment
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
@@ -44,12 +42,12 @@ import org.joda.time.DateTime
|
||||
|
||||
class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
|
||||
private var mEditGroupListener: EditGroupListener? = null
|
||||
private val mGroupEditViewModel: GroupEditViewModel by activityViewModels()
|
||||
|
||||
private var populateIconMethod: ((ImageView, IconImage) -> Unit)? = null
|
||||
private var mEditGroupDialogAction = EditGroupDialogAction.NONE
|
||||
private var mPopulateIconMethod: ((ImageView, IconImage) -> Unit)? = null
|
||||
private var mEditGroupDialogAction = NONE
|
||||
private var mGroupInfo = GroupInfo()
|
||||
private var mGroupNamesNotAllowed: List<String>? = null
|
||||
|
||||
private lateinit var iconButtonView: ImageView
|
||||
private var mIconColor: Int = 0
|
||||
@@ -69,31 +67,11 @@ class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
// Verify that the host activity implements the callback interface
|
||||
try {
|
||||
// Instantiate the NoticeDialogListener so we can send events to the host
|
||||
mEditGroupListener = context as EditGroupListener
|
||||
} catch (e: ClassCastException) {
|
||||
// The activity doesn't implement the interface, throw exception
|
||||
throw ClassCastException(
|
||||
context.toString()
|
||||
+ " must implement " + GroupEditDialogFragment::class.java.name
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDetach() {
|
||||
mEditGroupListener = null
|
||||
super.onDetach()
|
||||
}
|
||||
|
||||
override fun onDatabaseRetrieved(database: Database?) {
|
||||
populateIconMethod = { imageView, icon ->
|
||||
mPopulateIconMethod = { imageView, icon ->
|
||||
database?.iconDrawableFactory?.assignDatabaseIcon(imageView, icon, mIconColor)
|
||||
}
|
||||
populateIconMethod?.invoke(iconButtonView, mGroupInfo.icon)
|
||||
mPopulateIconMethod?.invoke(iconButtonView, mGroupInfo.icon)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@@ -101,7 +79,7 @@ class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
|
||||
mGroupEditViewModel.onIconSelected.observe(this) { iconImage ->
|
||||
mGroupInfo.icon = iconImage
|
||||
populateIconMethod?.invoke(iconButtonView, mGroupInfo.icon)
|
||||
mPopulateIconMethod?.invoke(iconButtonView, mGroupInfo.icon)
|
||||
}
|
||||
|
||||
mGroupEditViewModel.onDateSelected.observe(this) { viewModelDate ->
|
||||
@@ -129,6 +107,10 @@ class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
.toDate(), mGroupInfo.expiryTime.type)
|
||||
expirationView.dateTime = mGroupInfo.expiryTime
|
||||
}
|
||||
|
||||
mGroupEditViewModel.groupNamesNotAllowed.observe(this) { namesNotAllowed ->
|
||||
this.mGroupNamesNotAllowed = namesNotAllowed
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
@@ -175,10 +157,7 @@ class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
builder.setView(root)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.setNegativeButton(android.R.string.cancel) { _, _ ->
|
||||
retrieveGroupInfoFromViews()
|
||||
mEditGroupListener?.cancelEditGroup(
|
||||
mEditGroupDialogAction,
|
||||
mGroupInfo)
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
return builder.create()
|
||||
@@ -190,16 +169,20 @@ class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
super.onResume()
|
||||
|
||||
// To prevent auto dismiss
|
||||
val d = dialog as AlertDialog?
|
||||
if (d != null) {
|
||||
val positiveButton = d.getButton(Dialog.BUTTON_POSITIVE) as Button
|
||||
val alertDialog = dialog as AlertDialog?
|
||||
if (alertDialog != null) {
|
||||
val positiveButton = alertDialog.getButton(Dialog.BUTTON_POSITIVE) as Button
|
||||
positiveButton.setOnClickListener {
|
||||
retrieveGroupInfoFromViews()
|
||||
if (isValid()) {
|
||||
mEditGroupListener?.approveEditGroup(
|
||||
mEditGroupDialogAction,
|
||||
mGroupInfo)
|
||||
d.dismiss()
|
||||
when (mEditGroupDialogAction) {
|
||||
CREATION ->
|
||||
mGroupEditViewModel.approveGroupCreation(mGroupInfo)
|
||||
UPDATE ->
|
||||
mGroupEditViewModel.approveGroupUpdate(mGroupInfo)
|
||||
NONE -> {}
|
||||
}
|
||||
alertDialog.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,7 +218,21 @@ class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
}
|
||||
|
||||
private fun isValid(): Boolean {
|
||||
val error = mEditGroupListener?.isValidGroupName(nameTextView.text.toString()) ?: Error(false, null)
|
||||
val name = nameTextView.text.toString()
|
||||
val error = when {
|
||||
name.isEmpty() -> {
|
||||
Error(true, R.string.error_no_name)
|
||||
}
|
||||
mGroupNamesNotAllowed == null -> {
|
||||
Error(true, R.string.error_word_reserved)
|
||||
}
|
||||
mGroupNamesNotAllowed?.find { it.equals(name, ignoreCase = true) } != null -> {
|
||||
Error(true, R.string.error_word_reserved)
|
||||
}
|
||||
else -> {
|
||||
Error(false, null)
|
||||
}
|
||||
}
|
||||
error.messageId?.let { messageId ->
|
||||
nameTextLayoutView.error = getString(messageId)
|
||||
} ?: kotlin.run {
|
||||
@@ -246,14 +243,6 @@ class GroupEditDialogFragment : DatabaseDialogFragment() {
|
||||
|
||||
data class Error(val isError: Boolean, val messageId: Int?)
|
||||
|
||||
interface EditGroupListener {
|
||||
fun isValidGroupName(name: String): Error
|
||||
fun approveEditGroup(action: EditGroupDialogAction,
|
||||
groupInfo: GroupInfo)
|
||||
fun cancelEditGroup(action: EditGroupDialogAction,
|
||||
groupInfo: GroupInfo)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
const val TAG_CREATE_GROUP = "TAG_CREATE_GROUP"
|
||||
|
||||
@@ -1,6 +1,29 @@
|
||||
package com.kunzisoft.keepass.viewmodels
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.kunzisoft.keepass.model.GroupInfo
|
||||
|
||||
class GroupEditViewModel: NodeEditViewModel() {
|
||||
|
||||
// TODO Move fragment listeners
|
||||
val groupNamesNotAllowed : LiveData<List<String>?> get() = _groupNamesNotAllowed
|
||||
private val _groupNamesNotAllowed = MutableLiveData<List<String>?>()
|
||||
|
||||
val onGroupCreated : LiveData<GroupInfo> get() = _onGroupCreated
|
||||
private val _onGroupCreated = SingleLiveEvent<GroupInfo>()
|
||||
|
||||
val onGroupUpdated : LiveData<GroupInfo> get() = _onGroupUpdated
|
||||
private val _onGroupUpdated = SingleLiveEvent<GroupInfo>()
|
||||
|
||||
fun setGroupNamesNotAllowed(groupNames: List<String>?) {
|
||||
this._groupNamesNotAllowed.value = groupNames
|
||||
}
|
||||
|
||||
fun approveGroupCreation(groupInfo: GroupInfo) {
|
||||
this._onGroupCreated.value = groupInfo
|
||||
}
|
||||
|
||||
fun approveGroupUpdate(groupInfo: GroupInfo) {
|
||||
this._onGroupUpdated.value = groupInfo
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user