1 /**
<lambda>null2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * ```
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * ```
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.healthconnect.controller.autodelete
17 
18 import android.app.Dialog
19 import android.icu.text.MessageFormat
20 import android.os.Bundle
21 import androidx.core.os.bundleOf
22 import androidx.fragment.app.DialogFragment
23 import androidx.fragment.app.activityViewModels
24 import androidx.fragment.app.setFragmentResult
25 import com.android.healthconnect.controller.R
26 import com.android.healthconnect.controller.shared.dialog.AlertDialogBuilder
27 import com.android.healthconnect.controller.utils.logging.AutoDeleteElement
28 import dagger.hilt.android.AndroidEntryPoint
29 
30 /** A {@link DialogFragment} to get confirmation from user to turn auto-delete on. */
31 @AndroidEntryPoint(DialogFragment::class)
32 class AutoDeleteConfirmationDialogFragment : Hilt_AutoDeleteConfirmationDialogFragment() {
33 
34     companion object {
35         const val TAG = "AutoDeleteConfirmationDialogFragment"
36         const val AUTO_DELETE_SAVED_EVENT = "AUTO_DELETE_SAVED_EVENT"
37         const val AUTO_DELETE_CANCELLED_EVENT = "AUTO_DELETE_CANCELLED_EVENT"
38         const val AUTO_DELETE_CONFIRMATION_DIALOG_EVENT = "AUTO_DELETE_CONFIRMATION_DIALOG_EVENT"
39         const val NEW_AUTO_DELETE_RANGE_BUNDLE = "NEW_AUTO_DELETE_RANGE_BUNDLE"
40         const val OLD_AUTO_DELETE_RANGE_BUNDLE = "OLD_AUTO_DELETE_RANGE_BUNDLE"
41     }
42 
43     private val viewModel: AutoDeleteViewModel by activityViewModels()
44 
45     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
46         check(viewModel.newAutoDeleteRange.value != AutoDeleteRange.AUTO_DELETE_RANGE_NEVER) {
47             "ConfirmationDialog not supported for AUTO_DELETE_RANGE_NEVER."
48         }
49         val alertDialog =
50             AlertDialogBuilder(this, AutoDeleteElement.AUTO_DELETE_DIALOG_CONTAINER)
51                 .setIcon(R.attr.deletionSettingsIcon)
52 
53         viewModel.newAutoDeleteRange.value?.let {
54             alertDialog
55                 .setTitle(buildTitle(it))
56                 .setMessage(buildMessage(it))
57                 .setPositiveButton(
58                     R.string.set_auto_delete_button,
59                     AutoDeleteElement.AUTO_DELETE_DIALOG_CONFIRM_BUTTON) { _, _ ->
60                         setFragmentResult(
61                             AUTO_DELETE_SAVED_EVENT,
62                             bundleOf(AUTO_DELETE_SAVED_EVENT to viewModel.newAutoDeleteRange.value))
63                     }
64                 .setNeutralButton(
65                     android.R.string.cancel, AutoDeleteElement.AUTO_DELETE_DIALOG_CANCEL_BUTTON) {
66                         _,
67                         _ ->
68                         setFragmentResult(
69                             AUTO_DELETE_CANCELLED_EVENT,
70                             bundleOf(
71                                 AUTO_DELETE_CANCELLED_EVENT to viewModel.oldAutoDeleteRange.value))
72                     }
73         }
74         val dialog = alertDialog.create()
75         dialog.setCanceledOnTouchOutside(false)
76         return dialog
77     }
78 
79     private fun buildTitle(autoDeleteRange: AutoDeleteRange): String {
80         val count = autoDeleteRange.numberOfMonths
81         return MessageFormat.format(
82             requireContext().getString(R.string.confirming_question_x_months),
83             mapOf("count" to count))
84     }
85 
86     private fun buildMessage(autoDeleteRange: AutoDeleteRange): String {
87         val count = autoDeleteRange.numberOfMonths
88         return MessageFormat.format(
89             requireContext().getString(R.string.confirming_message_x_months),
90             mapOf("count" to count))
91     }
92 }
93