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.categories 17 18 import android.os.Bundle 19 import android.view.View 20 import androidx.core.os.bundleOf 21 import androidx.fragment.app.viewModels 22 import androidx.navigation.fragment.findNavController 23 import androidx.preference.Preference 24 import androidx.preference.PreferenceGroup 25 import com.android.healthconnect.controller.R 26 import com.android.healthconnect.controller.categories.HealthDataCategoryViewModel.CategoriesFragmentState.WithData 27 import com.android.healthconnect.controller.shared.HealthDataCategoryExtensions.icon 28 import com.android.healthconnect.controller.shared.HealthDataCategoryExtensions.uppercaseTitle 29 import com.android.healthconnect.controller.shared.preference.HealthPreference 30 import com.android.healthconnect.controller.shared.preference.HealthPreferenceFragment 31 import com.android.healthconnect.controller.utils.logging.CategoriesElement 32 import com.android.healthconnect.controller.utils.logging.PageName 33 import dagger.hilt.android.AndroidEntryPoint 34 35 /** Fragment for all health data categories. */ 36 @AndroidEntryPoint(HealthPreferenceFragment::class) 37 class HealthDataAllCategoriesFragment : Hilt_HealthDataAllCategoriesFragment() { 38 39 companion object { 40 private const val ALL_DATA_CATEGORY = "all_data_categories" 41 } 42 43 init { 44 this.setPageName(PageName.ALL_CATEGORIES_PAGE) 45 } 46 47 private val viewModel: HealthDataCategoryViewModel by viewModels() 48 49 private val mAllDataCategories: PreferenceGroup? by lazy { 50 preferenceScreen.findPreference(ALL_DATA_CATEGORY) 51 } 52 53 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 54 super.onCreatePreferences(savedInstanceState, rootKey) 55 setPreferencesFromResource(R.xml.health_data_all_categories_screen, rootKey) 56 } 57 58 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 59 super.onViewCreated(view, savedInstanceState) 60 viewModel.loadCategories() 61 62 viewModel.categoriesData.observe(viewLifecycleOwner) { state -> 63 when (state) { 64 is WithData -> { 65 setLoading(false) 66 updateAllDataList(state.categories) 67 } 68 HealthDataCategoryViewModel.CategoriesFragmentState.Error -> { 69 setError(true) 70 } 71 HealthDataCategoryViewModel.CategoriesFragmentState.Loading -> { 72 setLoading(true) 73 } 74 } 75 } 76 } 77 78 private fun updateAllDataList(categoriesList: List<HealthCategoryUiState>) { 79 val sortedAllCategoriesList: List<HealthCategoryUiState> = 80 categoriesList.sortedBy { getString(it.category.uppercaseTitle()) } 81 mAllDataCategories?.removeAll() 82 if (sortedAllCategoriesList.isEmpty()) { 83 mAllDataCategories?.addPreference( 84 Preference(requireContext()).also { it.setSummary(R.string.no_categories) }) 85 } else { 86 sortedAllCategoriesList.forEach { categoryInfo -> 87 val newPreference = 88 HealthPreference(requireContext()).also { 89 it.setTitle(categoryInfo.category.uppercaseTitle()) 90 it.icon = categoryInfo.category.icon(requireContext()) 91 it.logName = CategoriesElement.CATEGORY_BUTTON 92 if (!categoryInfo.hasData) { 93 it.setSummary(R.string.no_data) 94 it.isEnabled = false 95 } else { 96 it.setOnPreferenceClickListener { 97 findNavController() 98 .navigate( 99 R.id 100 .action_healthDataAllCategories_to_healthPermissionTypes, 101 bundleOf( 102 HealthDataCategoriesFragment.CATEGORY_KEY to 103 categoryInfo.category)) 104 true 105 } 106 } 107 } 108 mAllDataCategories?.addPreference(newPreference) 109 } 110 } 111 } 112 } 113