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.controller.data.entries.api 17 18 import android.health.connect.datatypes.MedicalResource 19 import android.util.Log 20 import com.android.healthconnect.controller.data.entries.FormattedEntry 21 import com.android.healthconnect.controller.dataentries.formatters.medical.MedicalEntryFormatter 22 import com.android.healthconnect.controller.permissions.data.MedicalPermissionType 23 import com.android.healthconnect.controller.service.IoDispatcher 24 import com.android.healthconnect.controller.shared.usecase.BaseUseCase 25 import com.android.healthconnect.controller.shared.usecase.UseCaseResults 26 import javax.inject.Inject 27 import javax.inject.Singleton 28 import kotlinx.coroutines.CoroutineDispatcher 29 30 /** Use case to load medical data type entries. */ 31 @Singleton 32 class LoadMedicalEntriesUseCase 33 @Inject 34 constructor( 35 @IoDispatcher private val dispatcher: CoroutineDispatcher, 36 private val medicalEntryFormatter: MedicalEntryFormatter, 37 private val loadEntriesHelper: LoadEntriesHelper, 38 ) : 39 BaseUseCase<LoadMedicalEntriesInput, List<FormattedEntry>>(dispatcher), 40 ILoadMedicalEntriesUseCase { 41 42 companion object { 43 private const val TAG = "LoadMedicalEntriesUseCase" 44 } 45 executenull46 override suspend fun execute(input: LoadMedicalEntriesInput): List<FormattedEntry> { 47 val medicalResources = loadEntriesHelper.readMedicalRecords(input) 48 return medicalResources.mapNotNull { getFormatterResource(it, input.showDataOrigin) } 49 } 50 getFormatterResourcenull51 private suspend fun getFormatterResource( 52 resource: MedicalResource, 53 showDataOrigin: Boolean, 54 ): FormattedEntry.FormattedMedicalDataEntry? { 55 return try { 56 medicalEntryFormatter.formatResource(resource, showDataOrigin) 57 } catch (ex: Exception) { 58 Log.i(TAG, "Failed to format medical resource!") 59 null 60 } 61 } 62 } 63 64 data class LoadMedicalEntriesInput( 65 val medicalPermissionType: MedicalPermissionType, 66 val packageName: String?, 67 val showDataOrigin: Boolean, 68 ) 69 70 interface ILoadMedicalEntriesUseCase { invokenull71 suspend fun invoke(input: LoadMedicalEntriesInput): UseCaseResults<List<FormattedEntry>> 72 73 suspend fun execute(input: LoadMedicalEntriesInput): List<FormattedEntry> 74 } 75