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