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 com.android.photopicker.core.features 18 19 import com.android.photopicker.data.model.Media 20 import kotlinx.coroutines.CompletableDeferred 21 import kotlinx.coroutines.flow.Flow 22 23 /** 24 * Parameter interface for passing additional parameters to a [Location]'s implementer via 25 * [FeatureManager#composeLocation]. 26 * 27 * By default all Locations receive the None parameter, but this interface can be extended and then 28 * location code can cast to the expected type with a pattern such as: 29 * ``` 30 * val clickAction = params as? LocationParams.WithClickAction 31 * clickAction?.onClick() 32 * ``` 33 * 34 * Or narrow the type using a `when` block. These interfaces can be combined into custom types to 35 * ensure compile time type-checking of parameter types. `Any` should not be used to pass 36 * parameters. 37 */ 38 sealed interface LocationParams { 39 40 /** The default parameters, which represents no additional parameters provided. */ 41 object None : LocationParams 42 43 /** 44 * A generic click handler parameter. Including this as a parameter doesn't attach the click 45 * handler to anything, the implementer must call this method in response to the click action. 46 */ interfacenull47 fun interface WithClickAction : LocationParams { 48 fun onClick() 49 } 50 51 /** Requirements for attaching a [MediaPreloader] to the compose UI. */ 52 interface WithMediaPreloader : LocationParams { 53 54 // Method which can be called to obtain a deferred for the currently requested preload 55 // operation. obtainDeferrednull56 fun obtainDeferred(): CompletableDeferred<Boolean> 57 58 // Flow to trigger the start of media preloads. 59 val preloadMedia: Flow<Set<Media>> 60 } 61 } 62