1 /**
2  * 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.fragment.app.DialogFragment
22 import androidx.fragment.app.activityViewModels
23 import com.android.healthconnect.controller.R
24 import com.android.healthconnect.controller.shared.dialog.AlertDialogBuilder
25 import com.android.healthconnect.controller.utils.logging.AutoDeleteElement
26 import dagger.hilt.android.AndroidEntryPoint
27 
28 /** A {@link DialogFragment} to inform the user about data deletion. */
29 @AndroidEntryPoint(DialogFragment::class)
30 class DeletionStartedDialogFragment : Hilt_DeletionStartedDialogFragment() {
31 
32     companion object {
33         const val TAG = "DeletionStartedDialogFragment"
34     }
35 
36     private val viewModel: AutoDeleteViewModel by activityViewModels()
37 
onCreateDialognull38     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
39         check(viewModel.newAutoDeleteRange.value != AutoDeleteRange.AUTO_DELETE_RANGE_NEVER) {
40             "DeletionStartedDialog not supported for AUTO_DELETE_RANGE_NEVER."
41         }
42         val alertDialog =
43             AlertDialogBuilder(this, AutoDeleteElement.AUTO_DELETE_CONFIRMATION_DIALOG_CONTAINER)
44                 .setTitle(R.string.deletion_started_title)
45                 .setIcon(R.attr.successIcon)
46                 .setPositiveButton(
47                     R.string.deletion_started_done_button,
48                     AutoDeleteElement.AUTO_DELETE_CONFIRMATION_DIALOG_DONE_BUTTON)
49         viewModel.newAutoDeleteRange.value?.let { alertDialog.setMessage(buildMessage(it)) }
50         return alertDialog.create()
51     }
52 
buildMessagenull53     private fun buildMessage(autoDeleteRange: AutoDeleteRange): String {
54         val count = autoDeleteRange.numberOfMonths
55         return MessageFormat.format(
56             requireContext().getString(R.string.deletion_started_x_months), mapOf("count" to count))
57     }
58 }
59