1 /*
<lambda>null2  * 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.intentresolver.contentpreview.payloadtoggle.domain.interactor
18 
19 import android.app.Activity
20 import android.content.ContentResolver
21 import android.content.pm.PackageManager
22 import com.android.intentresolver.contentpreview.payloadtoggle.data.model.CustomActionModel
23 import com.android.intentresolver.contentpreview.payloadtoggle.data.repository.ActivityResultRepository
24 import com.android.intentresolver.contentpreview.payloadtoggle.domain.model.ActionModel
25 import com.android.intentresolver.icon.toComposeIcon
26 import com.android.intentresolver.inject.Background
27 import com.android.intentresolver.logging.EventLog
28 import javax.inject.Inject
29 import kotlinx.coroutines.CoroutineDispatcher
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.conflate
32 import kotlinx.coroutines.flow.flowOn
33 import kotlinx.coroutines.flow.map
34 
35 class CustomActionsInteractor
36 @Inject
37 constructor(
38     private val activityResultRepo: ActivityResultRepository,
39     @Background private val bgDispatcher: CoroutineDispatcher,
40     private val contentResolver: ContentResolver,
41     private val eventLog: EventLog,
42     private val packageManager: PackageManager,
43     private val chooserRequestInteractor: ChooserRequestInteractor,
44 ) {
45     /** List of [ActionModel] that can be presented in Shareousel. */
46     val customActions: Flow<List<ActionModel>>
47         get() =
48             chooserRequestInteractor.customActions
49                 .map { actions ->
50                     actions.map { action ->
51                         ActionModel(
52                             label = action.label,
53                             icon = action.icon.toComposeIcon(packageManager, contentResolver),
54                             performAction = { index -> performAction(action, index) },
55                         )
56                     }
57                 }
58                 .flowOn(bgDispatcher)
59                 .conflate()
60 
61     private fun performAction(action: CustomActionModel, index: Int) {
62         action.performAction()
63         eventLog.logCustomActionSelected(index)
64         activityResultRepo.activityResult.value = Activity.RESULT_OK
65     }
66 }
67