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.health.connect.exportimport.ScheduledExportSettings
21 import android.util.Log
22 import javax.inject.Inject
23 import javax.inject.Singleton
24 import kotlinx.coroutines.Dispatchers
25 import kotlinx.coroutines.withContext
26 
27 @Singleton
28 class UpdateExportSettingsUseCase
29 @Inject
30 constructor(
31     private val healthDataExportManager: HealthDataExportManager,
32 ) : IUpdateExportSettingsUseCase {
33     companion object {
34         private const val TAG = "UpdateExportSettingsUseCase"
35     }
36 
37     /** Updates the stored export settings. */
invokenull38     override suspend operator fun invoke(
39         settings: ScheduledExportSettings
40     ): ExportImportUseCaseResult<Unit> =
41         withContext(Dispatchers.IO) {
42             try {
43                 healthDataExportManager.configureScheduledExport(settings)
44                 ExportImportUseCaseResult.Success(Unit)
45             } catch (ex: HealthConnectException) {
46                 Log.e(TAG, "Failed to update export settings ", ex)
47                 ExportImportUseCaseResult.Failed(ex)
48             }
49         }
50 }
51 
52 interface IUpdateExportSettingsUseCase {
53     /** Updates the stored export settings. */
invokenull54     suspend fun invoke(settings: ScheduledExportSettings): ExportImportUseCaseResult<Unit>
55 }
56