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.os.Bundle 19 import android.view.View 20 import android.widget.Toast 21 import androidx.fragment.app.activityViewModels 22 import androidx.preference.Preference 23 import androidx.preference.PreferenceGroup 24 import com.android.healthconnect.controller.R 25 import com.android.healthconnect.controller.autodelete.AutoDeleteConfirmationDialogFragment.Companion.AUTO_DELETE_CANCELLED_EVENT 26 import com.android.healthconnect.controller.autodelete.AutoDeleteConfirmationDialogFragment.Companion.AUTO_DELETE_CONFIRMATION_DIALOG_EVENT 27 import com.android.healthconnect.controller.autodelete.AutoDeleteConfirmationDialogFragment.Companion.AUTO_DELETE_SAVED_EVENT 28 import com.android.healthconnect.controller.autodelete.AutoDeleteConfirmationDialogFragment.Companion.NEW_AUTO_DELETE_RANGE_BUNDLE 29 import com.android.healthconnect.controller.autodelete.AutoDeleteConfirmationDialogFragment.Companion.OLD_AUTO_DELETE_RANGE_BUNDLE 30 import com.android.healthconnect.controller.autodelete.AutoDeleteRangePickerPreference.Companion.AUTO_DELETE_RANGE_PICKER_PREFERENCE_KEY 31 import com.android.healthconnect.controller.autodelete.AutoDeleteRangePickerPreference.Companion.SET_TO_NEVER_EVENT 32 import com.android.healthconnect.controller.shared.preference.HeaderPreference 33 import com.android.healthconnect.controller.shared.preference.HealthPreferenceFragment 34 import com.android.healthconnect.controller.utils.DeviceInfoUtilsImpl 35 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger 36 import com.android.healthconnect.controller.utils.logging.PageName 37 import dagger.hilt.android.AndroidEntryPoint 38 import javax.inject.Inject 39 40 /** Fragment displaying auto delete settings. */ 41 @AndroidEntryPoint(HealthPreferenceFragment::class) 42 class AutoDeleteFragment : Hilt_AutoDeleteFragment() { 43 44 companion object { 45 private const val AUTO_DELETE_SECTION = "auto_delete_section" 46 private const val HEADER = "header" 47 } 48 49 init { 50 this.setPageName(PageName.AUTO_DELETE_PAGE) 51 } 52 53 @Inject lateinit var logger: HealthConnectLogger 54 55 private val viewModel: AutoDeleteViewModel by activityViewModels() 56 57 private val mAutoDeleteSection: PreferenceGroup? by lazy { 58 preferenceScreen.findPreference(AUTO_DELETE_SECTION) 59 } 60 61 private val mHeaderSection: PreferenceGroup? by lazy { preferenceScreen.findPreference(HEADER) } 62 63 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 64 super.onCreatePreferences(savedInstanceState, rootKey) 65 setPreferencesFromResource(R.xml.auto_delete_screen, rootKey) 66 } 67 68 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 69 super.onViewCreated(view, savedInstanceState) 70 71 viewModel.storedAutoDeleteRange.observe(viewLifecycleOwner) { state -> 72 when (state) { 73 is AutoDeleteViewModel.AutoDeleteState.Loading -> { 74 // do nothing 75 } 76 is AutoDeleteViewModel.AutoDeleteState.LoadingFailed -> { 77 Toast.makeText(activity, R.string.default_error, Toast.LENGTH_LONG).show() 78 } 79 is AutoDeleteViewModel.AutoDeleteState.WithData -> { 80 if (mAutoDeleteSection?.findPreference<Preference>( 81 AUTO_DELETE_RANGE_PICKER_PREFERENCE_KEY) == null) { 82 val autoDeletePreference = 83 AutoDeleteRangePickerPreference( 84 requireContext(), 85 childFragmentManager, 86 state.autoDeleteRange, 87 logger) 88 mAutoDeleteSection?.addPreference(autoDeletePreference) 89 } else { 90 val autoDeletePreference = 91 mAutoDeleteSection?.findPreference<Preference>( 92 AUTO_DELETE_RANGE_PICKER_PREFERENCE_KEY) 93 as AutoDeleteRangePickerPreference 94 autoDeletePreference.updateAutoDeleteRange(state.autoDeleteRange) 95 } 96 } 97 } 98 } 99 100 mHeaderSection?.removeAll() 101 mHeaderSection?.addPreference( 102 HeaderPreference(requireContext()).also { 103 it.setHeaderText(getString(R.string.auto_delete_header)) 104 it.setHeaderLinkText(getString(R.string.auto_delete_learn_more)) 105 it.setHeaderLinkAction { 106 DeviceInfoUtilsImpl().openHCGetStartedLink(requireActivity()) 107 } 108 }) 109 110 childFragmentManager.setFragmentResultListener(SET_TO_NEVER_EVENT, this) { _, _ -> 111 viewModel.updateAutoDeleteRange(AutoDeleteRange.AUTO_DELETE_RANGE_NEVER) 112 } 113 114 childFragmentManager.setFragmentResultListener( 115 AUTO_DELETE_CONFIRMATION_DIALOG_EVENT, this) { _, bundle -> 116 bundle.getSerializable(NEW_AUTO_DELETE_RANGE_BUNDLE)?.let { newAutoDeleteRange -> 117 bundle.getSerializable(OLD_AUTO_DELETE_RANGE_BUNDLE)?.let { oldAutoDeleteRange 118 -> 119 viewModel.updateAutoDeleteDialogArguments( 120 newAutoDeleteRange as AutoDeleteRange, 121 oldAutoDeleteRange as AutoDeleteRange) 122 AutoDeleteConfirmationDialogFragment() 123 .show(childFragmentManager, AutoDeleteConfirmationDialogFragment.TAG) 124 } 125 } 126 } 127 128 childFragmentManager.setFragmentResultListener(AUTO_DELETE_SAVED_EVENT, this) { _, bundle -> 129 bundle.getSerializable(AUTO_DELETE_SAVED_EVENT)?.let { 130 viewModel.updateAutoDeleteRange(it as AutoDeleteRange) 131 } 132 DeletionStartedDialogFragment() 133 .show(childFragmentManager, DeletionStartedDialogFragment.TAG) 134 } 135 136 childFragmentManager.setFragmentResultListener(AUTO_DELETE_CANCELLED_EVENT, this) { 137 _, 138 bundle -> 139 bundle.getSerializable(AUTO_DELETE_CANCELLED_EVENT)?.let { 140 viewModel.updateAutoDeleteRange(it as AutoDeleteRange) 141 } 142 } 143 } 144 } 145