1 /*
2  * Copyright 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 src.com.android.photopicker.features.test.prefetchfeature
18 
19 import androidx.compose.runtime.Composable
20 import androidx.compose.ui.Modifier
21 import com.android.photopicker.core.configuration.PhotopickerConfiguration
22 import com.android.photopicker.core.events.RegisteredEventClass
23 import com.android.photopicker.core.features.FeatureManager
24 import com.android.photopicker.core.features.FeatureRegistration
25 import com.android.photopicker.core.features.Location
26 import com.android.photopicker.core.features.LocationParams
27 import com.android.photopicker.core.features.PhotopickerUiFeature
28 import com.android.photopicker.core.features.PrefetchResultKey
29 import com.android.photopicker.data.PrefetchDataService
30 import kotlinx.coroutines.Deferred
31 import kotlinx.coroutines.runBlocking
32 
33 class PrefetchFeature : PhotopickerUiFeature {
34 
35     companion object Registration : FeatureRegistration {
36         // Use any valid key
37         private val prefetchResultKey: PrefetchResultKey = PrefetchResultKey.SEARCH_STATE
38 
39         override val TAG: String = "PrefetchFeature"
40 
getPrefetchRequestnull41         override fun getPrefetchRequest(
42             config: PhotopickerConfiguration
43         ): Map<PrefetchResultKey, suspend (PrefetchDataService) -> Any?>? =
44             mapOf(prefetchResultKey to { true })
45 
isEnablednull46         override fun isEnabled(
47             config: PhotopickerConfiguration,
48             deferredPrefetchResultsMap: Map<PrefetchResultKey, Deferred<Any?>>,
49         ): Boolean {
50             return runBlocking {
51                 val featureStatus: Any? = deferredPrefetchResultsMap[prefetchResultKey]?.await()
52                 when (featureStatus) {
53                     null -> false
54                     is Boolean -> featureStatus
55                     else -> false
56                 }
57             }
58         }
59 
buildnull60         override fun build(featureManager: FeatureManager) = PrefetchFeature()
61     }
62 
63     override fun registerLocations(): List<Pair<Location, Int>> = listOf()
64 
65     @Composable
66     override fun compose(location: Location, modifier: Modifier, params: LocationParams) {}
67 
68     override val token: String = TAG
69 
70     override val eventsConsumed: Set<RegisteredEventClass> = emptySet()
71 
72     override val eventsProduced: Set<RegisteredEventClass> = emptySet()
73 }
74