1 /*
2  * Copyright (C) 2023 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.controller.data.entries.api
17 
18 import com.android.healthconnect.controller.data.entries.FormattedEntry
19 import com.android.healthconnect.controller.data.entries.datenavigation.DateNavigationPeriod
20 import com.android.healthconnect.controller.permissions.data.HealthPermissionType
21 import com.android.healthconnect.controller.service.IoDispatcher
22 import com.android.healthconnect.controller.shared.usecase.BaseUseCase
23 import com.android.healthconnect.controller.shared.usecase.UseCaseResults
24 import java.time.Instant
25 import javax.inject.Inject
26 import javax.inject.Singleton
27 import kotlinx.coroutines.CoroutineDispatcher
28 
29 /** Use case to load data entries except for menstruation data types. */
30 @Singleton
31 class LoadDataEntriesUseCase
32 @Inject
33 constructor(
34     @IoDispatcher private val dispatcher: CoroutineDispatcher,
35     private val loadEntriesHelper: LoadEntriesHelper
36 ) : BaseUseCase<LoadDataEntriesInput, List<FormattedEntry>>(dispatcher), ILoadDataEntriesUseCase {
37 
executenull38     override suspend fun execute(input: LoadDataEntriesInput): List<FormattedEntry> {
39         val entryRecords = loadEntriesHelper.readRecords(input)
40 
41         return loadEntriesHelper.maybeAddDateSectionHeaders(
42             entryRecords, input.period, input.showDataOrigin)
43     }
44 }
45 
46 data class LoadDataEntriesInput(
47     val permissionType: HealthPermissionType,
48     val packageName: String?,
49     val displayedStartTime: Instant,
50     val period: DateNavigationPeriod,
51     val showDataOrigin: Boolean
52 )
53 
54 interface ILoadDataEntriesUseCase {
invokenull55     suspend fun invoke(input: LoadDataEntriesInput): UseCaseResults<List<FormattedEntry>>
56 
57     suspend fun execute(input: LoadDataEntriesInput): List<FormattedEntry>
58 }
59