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 = AlertDialogBuilder(this).setIcon(R.attr.deletionSettingsIcon) 50 51 viewModel.newAutoDeleteRange.value?.let { 52 alertDialog 53 .setLogName(AutoDeleteElement.AUTO_DELETE_DIALOG_CONTAINER) 54 .setTitle(buildTitle(it)) 55 .setMessage(buildMessage(it)) 56 .setPositiveButton( 57 R.string.set_auto_delete_button, 58 AutoDeleteElement.AUTO_DELETE_DIALOG_CONFIRM_BUTTON) { _, _ -> 59 setFragmentResult( 60 AUTO_DELETE_SAVED_EVENT, 61 bundleOf(AUTO_DELETE_SAVED_EVENT to viewModel.newAutoDeleteRange.value)) 62 } 63 .setNegativeButton( 64 android.R.string.cancel, AutoDeleteElement.AUTO_DELETE_DIALOG_CANCEL_BUTTON) { 65 _, 66 _ -> 67 setFragmentResult( 68 AUTO_DELETE_CANCELLED_EVENT, 69 bundleOf( 70 AUTO_DELETE_CANCELLED_EVENT to viewModel.oldAutoDeleteRange.value)) 71 } 72 } 73 val dialog = alertDialog.create() 74 dialog.setCanceledOnTouchOutside(false) 75 return dialog 76 } 77 78 private fun buildTitle(autoDeleteRange: AutoDeleteRange): String { 79 val count = autoDeleteRange.numberOfMonths 80 return MessageFormat.format( 81 requireContext().getString(R.string.confirming_question_x_months), 82 mapOf("count" to count)) 83 } 84 85 private fun buildMessage(autoDeleteRange: AutoDeleteRange): String { 86 val count = autoDeleteRange.numberOfMonths 87 return MessageFormat.format( 88 requireContext().getString(R.string.confirming_message_x_months), 89 mapOf("count" to count)) 90 } 91 } 92