mirror of
https://codeberg.org/timelimit/opentimelimit-android.git
synced 2025-10-05 02:39:34 +02:00
Move category deletion to dialog
This commit is contained in:
parent
aa6266ac3f
commit
96df39eead
7 changed files with 146 additions and 15 deletions
|
@ -66,8 +66,7 @@ class CategorySettingsFragment : Fragment() {
|
|||
}
|
||||
})
|
||||
|
||||
checkbox_delete_category.setOnCheckedChangeListener { _, isChecked -> btn_delete_category.isEnabled = isChecked }
|
||||
btn_delete_category.setOnClickListener { doDeleteCategory() }
|
||||
btn_delete_category.setOnClickListener { deleteCategory() }
|
||||
|
||||
edit_category_title_go.setOnClickListener { doRenameCategory() }
|
||||
edit_category_title.setOnEditorActionListener { _, actionId, _ ->
|
||||
|
@ -126,9 +125,9 @@ class CategorySettingsFragment : Fragment() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun doDeleteCategory() {
|
||||
auth.tryDispatchParentAction(DeleteCategoryAction(
|
||||
categoryId = params.categoryId
|
||||
))
|
||||
private fun deleteCategory() {
|
||||
if (auth.requestAuthenticationOrReturnTrue()) {
|
||||
DeleteCategoryDialogFragment.newInstance(params).show(fragmentManager!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package io.timelimit.android.ui.manage.category.settings
|
||||
|
||||
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.Category
|
||||
import io.timelimit.android.data.model.UserType
|
||||
import io.timelimit.android.extensions.showSafe
|
||||
import io.timelimit.android.logic.AppLogic
|
||||
import io.timelimit.android.logic.DefaultAppLogic
|
||||
import io.timelimit.android.sync.actions.DeleteCategoryAction
|
||||
import io.timelimit.android.ui.main.ActivityViewModel
|
||||
import io.timelimit.android.ui.main.getActivityViewModel
|
||||
import io.timelimit.android.ui.manage.category.ManageCategoryFragmentArgs
|
||||
import io.timelimit.android.ui.util.ConfirmDeleteDialogFragment
|
||||
|
||||
class DeleteCategoryDialogFragment: ConfirmDeleteDialogFragment() {
|
||||
companion object {
|
||||
private const val TAG = "DeleteCategoryDialogFragment"
|
||||
|
||||
fun newInstance(args: ManageCategoryFragmentArgs) = DeleteCategoryDialogFragment().apply {
|
||||
arguments = args.toBundle()
|
||||
}
|
||||
}
|
||||
|
||||
private val params: ManageCategoryFragmentArgs by lazy { ManageCategoryFragmentArgs.fromBundle(arguments!!) }
|
||||
private val appLogic: AppLogic by lazy { DefaultAppLogic.with(context!!) }
|
||||
private val auth: ActivityViewModel by lazy { getActivityViewModel(activity!!) }
|
||||
|
||||
val categoryEntry: LiveData<Category?> by lazy {
|
||||
appLogic.database.category().getCategoryByChildIdAndId(
|
||||
categoryId = params.categoryId,
|
||||
childId = params.childId
|
||||
)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
auth.authenticatedUser.observe(this, Observer {
|
||||
if (it?.second?.type != UserType.Parent) {
|
||||
dismissAllowingStateLoss()
|
||||
}
|
||||
})
|
||||
|
||||
categoryEntry.observe(this, Observer {
|
||||
if (it == null) {
|
||||
dismissAllowingStateLoss()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
categoryEntry.observe(this, Observer {
|
||||
binding.text = getString(R.string.category_settings_delete_dialog, it?.title)
|
||||
})
|
||||
}
|
||||
|
||||
override fun onConfirmDeletion() {
|
||||
auth.tryDispatchParentAction(DeleteCategoryAction(
|
||||
categoryId = params.categoryId
|
||||
))
|
||||
|
||||
dismiss()
|
||||
}
|
||||
|
||||
fun show(fragmentManager: FragmentManager) = showSafe(fragmentManager, TAG)
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package io.timelimit.android.ui.util
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||
import io.timelimit.android.databinding.ConfirmDeleteDialogBinding
|
||||
|
||||
abstract class ConfirmDeleteDialogFragment: BottomSheetDialogFragment(), ConfirmDeleteDialogFragmentHandlers {
|
||||
lateinit var binding: ConfirmDeleteDialogBinding
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
binding = ConfirmDeleteDialogBinding.inflate(inflater, container, false)
|
||||
|
||||
binding.handlers = this
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
abstract override fun onConfirmDeletion()
|
||||
}
|
||||
|
||||
interface ConfirmDeleteDialogFragmentHandlers {
|
||||
fun onConfirmDeletion()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue