1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.healthconnect.controller.exportimport
18 
19 import android.content.Context
20 import android.widget.RadioGroup
21 import androidx.preference.Preference
22 import androidx.preference.PreferenceViewHolder
23 import com.android.healthconnect.controller.R
24 import com.android.healthconnect.controller.exportimport.api.ExportFrequency
25 
26 /** Preference for updating export frequency. */
27 class ExportFrequencyRadioGroupPreference(
28     context: Context,
29     private var exportFrequency: ExportFrequency,
30     private val updateExportFrequency: (frequency: ExportFrequency) -> Unit
31 ) : Preference(context) {
32     // TODO: b/325914485 - Add proper logging for this preference.
33 
34     companion object {
35         const val EXPORT_FREQUENCY_RADIO_GROUP_PREFERENCE =
36             "export_frequency_radio_group_preference"
37     }
38 
39     init {
40         layoutResource = R.layout.widget_export_frequency_radio_group_preference
41         key = EXPORT_FREQUENCY_RADIO_GROUP_PREFERENCE
42     }
43 
44     override fun onBindViewHolder(holder: PreferenceViewHolder) {
45         super.onBindViewHolder(holder)
46 
47         val radioGroup = holder.findViewById(R.id.radio_group_frequency) as RadioGroup
48 
49         when (exportFrequency) {
50             ExportFrequency.EXPORT_FREQUENCY_WEEKLY -> radioGroup.check(R.id.radio_button_weekly)
51             ExportFrequency.EXPORT_FREQUENCY_MONTHLY -> radioGroup.check(R.id.radio_button_monthly)
52             ExportFrequency.EXPORT_FREQUENCY_DAILY -> radioGroup.check(R.id.radio_button_daily)
53             // This shouldn't happen as the preference will be invisible if the export frequency is
54             // NEVER.
55             ExportFrequency.EXPORT_FREQUENCY_NEVER -> radioGroup.check(R.id.radio_button_daily)
56         }
57 
58         radioGroup.setOnCheckedChangeListener { _, checkedId ->
59             when (checkedId) {
60                 R.id.radio_button_daily -> {
61                     exportFrequency = ExportFrequency.EXPORT_FREQUENCY_DAILY
62                     updateExportFrequency(ExportFrequency.EXPORT_FREQUENCY_DAILY)
63                 }
64                 R.id.radio_button_weekly -> {
65                     exportFrequency = ExportFrequency.EXPORT_FREQUENCY_WEEKLY
66                     updateExportFrequency(ExportFrequency.EXPORT_FREQUENCY_WEEKLY)
67                 }
68                 R.id.radio_button_monthly -> {
69                     exportFrequency = ExportFrequency.EXPORT_FREQUENCY_MONTHLY
70                     updateExportFrequency(ExportFrequency.EXPORT_FREQUENCY_MONTHLY)
71                 }
72             }
73         }
74     }
75 }
76