1 /*
<lambda>null2  * Copyright (C) 2023 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.avatarpicker.domain
18 
19 import com.android.avatarpicker.data.ColoredIconsRepository
20 import com.android.avatarpicker.data.IllustrationsRepository
21 import com.android.avatarpicker.data.MediaRepository
22 import com.android.avatarpicker.ui.details.items.SelectableType
23 import kotlinx.coroutines.CoroutineDispatcher
24 import kotlinx.coroutines.Dispatchers
25 import kotlinx.coroutines.withContext
26 
27 class GroupedSelectableItemsUseCaseImpl(
28     private val mediaRepo: MediaRepository,
29     private val coloredIconsRepo: ColoredIconsRepository,
30     private val illustrationsRepo: IllustrationsRepository,
31     private val dispatcher: CoroutineDispatcher = Dispatchers.Default
32 ) : GroupedSelectableItemsUseCase {
33     override suspend operator fun invoke(): List<List<SelectableType>> = withContext(dispatcher) {
34         val illustrations =
35             illustrationsRepo.getItems().map { entity -> entity.toViewModel(ILLUSTRATION) }
36         val media = listOf(
37             mediaRepo.getCamera().toViewModel(CAMERA), mediaRepo.getPhotoPicker().toViewModel(
38                 PHOTO_PICKER
39             )
40         )
41         if (illustrations.isNotEmpty()) {
42             return@withContext listOf(media, illustrations)
43         } else {
44             val icons =
45                 coloredIconsRepo.getItems().map { entity -> entity.toViewModel(DEFAULT_ICON) }
46             return@withContext listOf(media + icons)
47         }
48     }
49 
50     override suspend fun description() = withContext(dispatcher) { null }
51 }