1 /*
2  * 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 package com.android.healthconnect.controller.selectabledeletion
17 
18 import androidx.lifecycle.LiveData
19 import androidx.lifecycle.MutableLiveData
20 import androidx.lifecycle.ViewModel
21 import com.android.healthconnect.controller.permissions.data.HealthPermissionType
22 
23 /** A base class for the shared functionality of [AllDataViewModel] and [AppDataViewModel] */
24 abstract class DeletionDataViewModel : ViewModel() {
25 
26     private val _setOfPermissionTypesToBeDeleted = MutableLiveData<Set<HealthPermissionType>>()
27 
28     val setOfPermissionTypesToBeDeleted: LiveData<Set<HealthPermissionType>>
29         get() = _setOfPermissionTypesToBeDeleted
30 
31     private val _deletionScreenState = MutableLiveData<DeletionScreenState>()
32     val deletionScreenState: LiveData<DeletionScreenState>
33         get() = _deletionScreenState
34 
35     protected var numOfPermissionTypes: Int = 0
36     private val _allPermissionTypesSelected = MutableLiveData<Boolean>()
37     val allPermissionTypesSelected: LiveData<Boolean>
38         get() = _allPermissionTypesSelected
39 
resetDeletionSetnull40     fun resetDeletionSet() {
41         _setOfPermissionTypesToBeDeleted.value = emptySet()
42     }
43 
addToDeletionSetnull44     fun addToDeletionSet(permissionType: HealthPermissionType) {
45         val deleteSet = _setOfPermissionTypesToBeDeleted.value.orEmpty().toMutableSet()
46         deleteSet.add(permissionType)
47         _setOfPermissionTypesToBeDeleted.value = deleteSet.toSet()
48         if (numOfPermissionTypes == deleteSet.size) {
49             _allPermissionTypesSelected.postValue(true)
50         }
51         _deletionScreenState.value = DeletionScreenState.DELETE
52     }
53 
removeFromDeletionSetnull54     fun removeFromDeletionSet(permissionType: HealthPermissionType) {
55         val deleteSet = _setOfPermissionTypesToBeDeleted.value.orEmpty().toMutableSet()
56         deleteSet.remove(permissionType)
57         _setOfPermissionTypesToBeDeleted.value = deleteSet.toSet()
58         if (numOfPermissionTypes != deleteSet.size) {
59             _allPermissionTypesSelected.postValue(false)
60         }
61         _deletionScreenState.value = DeletionScreenState.DELETE
62     }
63 
setDeletionScreenStateValuenull64     fun setDeletionScreenStateValue(screenState: DeletionScreenState) {
65         if (screenState == DeletionScreenState.VIEW) {
66             resetDeletionSet()
67         }
68         _deletionScreenState.value = screenState
69     }
70 
getDeletionScreenStateValuenull71     fun getDeletionScreenStateValue(): DeletionScreenState {
72         return _deletionScreenState.value ?: DeletionScreenState.VIEW
73     }
74 
getTheNumOfPermissionTypesnull75     fun getTheNumOfPermissionTypes(): Int = numOfPermissionTypes
76 
77     enum class DeletionScreenState {
78         VIEW,
79         DELETE,
80     }
81 }
82