1 /**
2  * 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.testapps.toolbox.viewmodels
17 
18 import android.health.connect.HealthConnectException
19 import android.health.connect.HealthConnectManager
20 import android.health.connect.datatypes.Record
21 import androidx.lifecycle.LiveData
22 import androidx.lifecycle.MutableLiveData
23 import androidx.lifecycle.ViewModel
24 import androidx.lifecycle.viewModelScope
25 import com.android.healthconnect.testapps.toolbox.utils.GeneralUtils
26 import com.android.healthconnect.testapps.toolbox.utils.GeneralUtils.Companion.insertRecords
27 import kotlinx.coroutines.launch
28 
29 class InsertOrUpdateRecordsViewModel : ViewModel() {
30 
31     private val _insertedRecordsState = MutableLiveData<InsertedRecordsState>()
32     val insertedRecordsState: LiveData<InsertedRecordsState>
33         get() = _insertedRecordsState
34 
35     private val _updatedRecordsState = MutableLiveData<UpdatedRecordsState>()
36     val updatedRecordsState: LiveData<UpdatedRecordsState>
37         get() = _updatedRecordsState
38 
insertRecordsViaViewModelnull39     fun insertRecordsViaViewModel(records: List<Record>, manager: HealthConnectManager) {
40         viewModelScope.launch {
41             try {
42                 val response = insertRecords(records, manager)
43                 _insertedRecordsState.postValue(InsertedRecordsState.WithData(response))
44             } catch (exception: HealthConnectException) {
45                 _insertedRecordsState.postValue(
46                     InsertedRecordsState.Error(exception.localizedMessage!!))
47             } catch (exception: SecurityException) {
48                 _insertedRecordsState.postValue(
49                     InsertedRecordsState.Error(exception.localizedMessage!!))
50             }
51         }
52     }
53 
updateRecordsViaViewModelnull54     fun updateRecordsViaViewModel(records: List<Record>, manager: HealthConnectManager) {
55         viewModelScope.launch {
56             try {
57                 GeneralUtils.updateRecords(records, manager)
58                 _updatedRecordsState.postValue(UpdatedRecordsState.Success)
59             } catch (exception: HealthConnectException) {
60                 _updatedRecordsState.postValue(
61                     UpdatedRecordsState.Error(exception.localizedMessage!!))
62             } catch (exception: SecurityException) {
63                 _updatedRecordsState.postValue(
64                     UpdatedRecordsState.Error(exception.localizedMessage!!))
65             }
66         }
67     }
68 
69     sealed class InsertedRecordsState {
70         data class Error(val errorMessage: String) : InsertedRecordsState()
71         data class WithData(val entries: List<Record>) : InsertedRecordsState()
72     }
73 
74     sealed class UpdatedRecordsState {
75         data class Error(val errorMessage: String) : UpdatedRecordsState()
76         object Success : UpdatedRecordsState()
77     }
78 }
79