From 39067d0f53cd40442c5f2c6d65d54ceef95c0b09 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Mon, 14 Jan 2019 17:10:00 +0100 Subject: [PATCH] Use dialog for deleting child users --- .../advanced/DeleteChildDialogFragment.kt | 88 +++++++++++++++++++ .../manage/child/advanced/DeleteChildView.kt | 50 ----------- .../advanced/ManageChildAdvancedFragment.kt | 10 +-- app/src/main/res/layout/delete_child_view.xml | 52 ----------- .../layout/fragment_manage_child_advanced.xml | 27 +++++- .../res/values-de/strings-delete-child.xml | 4 +- .../main/res/values/strings-delete-child.xml | 4 +- 7 files changed, 121 insertions(+), 114 deletions(-) create mode 100644 app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildDialogFragment.kt delete mode 100644 app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildView.kt delete mode 100644 app/src/main/res/layout/delete_child_view.xml diff --git a/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildDialogFragment.kt b/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildDialogFragment.kt new file mode 100644 index 0000000..b43b428 --- /dev/null +++ b/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildDialogFragment.kt @@ -0,0 +1,88 @@ +/* + * Open TimeLimit Copyright 2019 Jonas Lochmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.timelimit.android.ui.manage.child.advanced + +import android.os.Bundle +import android.view.View +import androidx.fragment.app.FragmentManager +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import io.timelimit.android.R +import io.timelimit.android.data.model.User +import io.timelimit.android.data.model.UserType +import io.timelimit.android.extensions.showSafe +import io.timelimit.android.logic.DefaultAppLogic +import io.timelimit.android.sync.actions.RemoveUserAction +import io.timelimit.android.ui.main.ActivityViewModel +import io.timelimit.android.ui.main.ActivityViewModelHolder +import io.timelimit.android.ui.util.ConfirmDeleteDialogFragment + +class DeleteChildDialogFragment: ConfirmDeleteDialogFragment() { + companion object { + private const val DIALOG_TAG = "DeleteChildDialogFragment" + private const val CHILD_ID = "childId" + + fun newInstance(childId: String) = DeleteChildDialogFragment().apply { + arguments = Bundle().apply { + putString(CHILD_ID, childId) + } + } + } + + val auth: ActivityViewModel by lazy { + (activity as ActivityViewModelHolder).getActivityViewModel() + } + val childId: String by lazy { arguments!!.getString(CHILD_ID) } + val userEntry: LiveData by lazy { + DefaultAppLogic.with(context!!).database.user().getChildUserByIdLive(childId) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + auth.authenticatedUser.observe(this, Observer { + if (it?.second?.type != UserType.Parent) { + dismissAllowingStateLoss() + } + }) + + userEntry.observe(this, Observer { + if (it == null) { + dismissAllowingStateLoss() + } + }) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + userEntry.observe(this, Observer { + binding.text = getString(R.string.delete_child_text, it?.name) + }) + } + + override fun onConfirmDeletion() { + auth.tryDispatchParentAction( + RemoveUserAction( + userId = childId + ) + ) + + dismiss() + } + + fun show(fragmentManager: FragmentManager) = showSafe(fragmentManager, DIALOG_TAG) +} diff --git a/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildView.kt b/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildView.kt deleted file mode 100644 index d49082f..0000000 --- a/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/DeleteChildView.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Open TimeLimit Copyright 2019 Jonas Lochmann - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package io.timelimit.android.ui.manage.child.advanced - -import io.timelimit.android.databinding.DeleteChildViewBinding -import io.timelimit.android.sync.actions.RemoveUserAction -import io.timelimit.android.ui.main.ActivityViewModel - -object DeleteChildView { - fun bind( - model: ActivityViewModel, - view: DeleteChildViewBinding, - childId: String - ) { - view.btnConfirmDeleteUser.isEnabled = view.checkConfirmDeleteUser.isChecked - - view.btnConfirmDeleteUser.setOnClickListener { - model.tryDispatchParentAction( - RemoveUserAction( - userId = childId - ) - ) - } - - view.checkConfirmDeleteUser.setOnCheckedChangeListener { _, isChecked -> - if (isChecked) { - if (!model.requestAuthenticationOrReturnTrue()) { - view.checkConfirmDeleteUser.isChecked = false - } else { - view.btnConfirmDeleteUser.isEnabled = true - } - } else { - view.btnConfirmDeleteUser.isEnabled = false - } - } - } -} diff --git a/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/ManageChildAdvancedFragment.kt b/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/ManageChildAdvancedFragment.kt index bcab001..f3206fa 100644 --- a/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/ManageChildAdvancedFragment.kt +++ b/app/src/main/java/io/timelimit/android/ui/manage/child/advanced/ManageChildAdvancedFragment.kt @@ -119,11 +119,11 @@ class ManageChildAdvancedFragment : Fragment() { }) } - DeleteChildView.bind( - model = auth, - view = binding.deleteChild, - childId = params.childId - ) + binding.deleteUserButton.setOnClickListener { + if (auth.requestAuthenticationOrReturnTrue()) { + DeleteChildDialogFragment.newInstance(params.childId).show(fragmentManager!!) + } + } return binding.root } diff --git a/app/src/main/res/layout/delete_child_view.xml b/app/src/main/res/layout/delete_child_view.xml deleted file mode 100644 index c5efcd6..0000000 --- a/app/src/main/res/layout/delete_child_view.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - -