1 /**
2  * Copyright (C) 2024 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.content.Context
19 import android.health.connect.HealthConnectManager
20 import androidx.lifecycle.LiveData
21 import androidx.lifecycle.MutableLiveData
22 import androidx.lifecycle.ViewModel
23 import androidx.lifecycle.viewModelScope
24 import com.android.healthconnect.testapps.toolbox.seed.SeedData
25 import kotlinx.coroutines.Dispatchers
26 import kotlinx.coroutines.launch
27 import kotlinx.coroutines.withContext
28 
29 class HomeFragmentViewModel : ViewModel(){
30 
31     private val _seedAllDataState = MutableLiveData<SeedAllDataState>()
32     val seedAllDataState: LiveData<SeedAllDataState>
33         get() = _seedAllDataState
34 
seedAllDataViewModelnull35     fun seedAllDataViewModel(context: Context, manager: HealthConnectManager) {
36         viewModelScope.launch {
37             try {
38                 withContext(Dispatchers.IO) {
39                     SeedData(context, manager).seedAllData()
40                 }
41                 _seedAllDataState.postValue(SeedAllDataState.Success)
42             } catch (ex: Exception) {
43                 _seedAllDataState.postValue(SeedAllDataState.Error(ex.localizedMessage!!))
44             }
45         }
46     }
47 
48     sealed class SeedAllDataState{
49         data class Error(val errorMessage: String) : SeedAllDataState()
50         object Success : SeedAllDataState()
51     }
52 }