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.intentresolver.contentpreview.payloadtoggle.domain.interactor
18 
19 import com.android.intentresolver.contentpreview.payloadtoggle.data.repository.CursorPreviewsRepository
20 import com.android.intentresolver.contentpreview.payloadtoggle.domain.model.LoadDirection
21 import com.android.intentresolver.contentpreview.payloadtoggle.shared.model.PreviewModel
22 import com.android.intentresolver.contentpreview.payloadtoggle.shared.model.PreviewsModel
23 import javax.inject.Inject
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.MutableStateFlow
26 import kotlinx.coroutines.flow.asStateFlow
27 
28 /** Updates [CursorPreviewsRepository] with new previews. */
29 class SetCursorPreviewsInteractor
30 @Inject
31 constructor(private val previewsRepo: CursorPreviewsRepository) {
32     /** Stores new [previews], and returns a flow of load requests triggered by Shareousel. */
setPreviewsnull33     fun setPreviews(
34         previews: List<PreviewModel>,
35         startIndex: Int,
36         hasMoreLeft: Boolean,
37         hasMoreRight: Boolean,
38         leftTriggerIndex: Int,
39         rightTriggerIndex: Int
40     ): Flow<LoadDirection?> {
41         val loadingState = MutableStateFlow<LoadDirection?>(null)
42         previewsRepo.previewsModel.value =
43             PreviewsModel(
44                 previewModels = previews,
45                 startIdx = startIndex,
46                 loadMoreLeft =
47                     if (hasMoreLeft) {
48                         ({ loadingState.value = LoadDirection.Left })
49                     } else {
50                         null
51                     },
52                 loadMoreRight =
53                     if (hasMoreRight) {
54                         ({ loadingState.value = LoadDirection.Right })
55                     } else {
56                         null
57                     },
58                 leftTriggerIndex = leftTriggerIndex,
59                 rightTriggerIndex = rightTriggerIndex,
60             )
61         return loadingState.asStateFlow()
62     }
63 }
64