1 /*
2  * Copyright (C) 2022 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 package com.android.healthconnect.controller.filters
17 
18 import android.content.Context
19 import android.widget.RadioGroup
20 import androidx.preference.Preference
21 import androidx.preference.PreferenceViewHolder
22 import com.android.healthconnect.controller.R
23 import com.android.healthconnect.controller.shared.app.AppMetadata
24 
25 /**
26  * The ChipPreference displays a horizontal list of {@link FilterChip}s.
27  *
28  * Each FilterChip represents a contributing app to the HealthConnect data. A default `All apps`
29  * chip is added to the beginning of the list.
30  */
31 open class ChipPreference
32 @JvmOverloads
33 constructor(
34     context: Context,
35     private val appMetadataList: List<AppMetadata> = listOf(),
36     private val addFilterChip: (appMetadata: AppMetadata, chipGroup: RadioGroup) -> Unit,
37     private val addAllAppsFilterChip: (chipGroup: RadioGroup) -> Unit
38 ) : Preference(context) {
39 
40     init {
41         layoutResource = R.layout.widget_horizontal_chip_preference
42         isSelectable = false
43     }
44 
45     var chipGroup: RadioGroup? = null
46 
onBindViewHoldernull47     override fun onBindViewHolder(holder: PreferenceViewHolder) {
48         super.onBindViewHolder(holder)
49 
50         chipGroup = holder.findViewById(R.id.chip_group) as RadioGroup
51 
52         chipGroup?.removeAllViews()
53 
54         addAllAppsFilterChip(chipGroup!!)
55 
56         for (appMetadata in appMetadataList) {
57             addFilterChip(appMetadata, chipGroup!!)
58         }
59     }
60 }
61