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 
17 package com.android.healthconnect.controller.exportimport.api
18 
19 import android.health.connect.HealthConnectException
20 import android.util.Log
21 import javax.inject.Inject
22 import javax.inject.Singleton
23 import kotlinx.coroutines.Dispatchers
24 import kotlinx.coroutines.withContext
25 
26 @Singleton
27 class LoadExportSettingsUseCase
28 @Inject
29 constructor(
30     private val healthDataExportManager: HealthDataExportManager,
31 ) : ILoadExportSettingsUseCase {
32     companion object {
33         private const val TAG = "LoadExportSettingsUseCase"
34     }
35 
36     /** Returns the stored export settings. */
invokenull37     override suspend operator fun invoke(): ExportImportUseCaseResult<ExportFrequency> =
38         withContext(Dispatchers.IO) {
39             try {
40                 val periodInDays = healthDataExportManager.getScheduledExportPeriodInDays()
41                 val frequency = fromPeriodInDays(periodInDays)
42                 ExportImportUseCaseResult.Success(frequency)
43             } catch (ex: HealthConnectException) {
44                 Log.e(TAG, "Load export settings error: ", ex)
45                 ExportImportUseCaseResult.Failed(ex)
46             }
47         }
48 }
49 
50 interface ILoadExportSettingsUseCase {
51     /** Returns the stored export settings. */
invokenull52     suspend fun invoke(): ExportImportUseCaseResult<ExportFrequency>
53 }
54