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.configuration
18 
19 import android.content.Intent
20 import android.provider.MediaStore
21 import kotlinx.coroutines.CoroutineScope
22 import kotlinx.coroutines.flow.SharingStarted
23 import kotlinx.coroutines.flow.StateFlow
24 import kotlinx.coroutines.flow.flow
25 import kotlinx.coroutines.flow.stateIn
26 
27 /**
28  * A [PhotopickerConfiguration] that allows selection of only a single item.
29  */
30 val SINGLE_SELECT_CONFIG = PhotopickerConfiguration(action = "", selectionLimit = 1)
31 
32 /**
33  * A [PhotopickerConfiguration] that allows selection of multiple (50 in this case) items.
34  */
35 val MULTI_SELECT_CONFIG = PhotopickerConfiguration(action = "", selectionLimit = 50)
36 
37 /**
38  * A [PhotopickerConfiguration] that can be used with most tests, that comes with sensible default
39  * values.
40  */
41 val testPhotopickerConfiguration: PhotopickerConfiguration =
42     PhotopickerConfiguration(
43         action = "TEST_ACTION",
44         intent = Intent("TEST_ACTION"),
45     )
46 
47 /**
48  * A [PhotopickerConfiguration] that can be used for codepaths that utilize
49  * [MediaStore.ACTION_PICK_IMAGES] intent action.
50  */
51 val testActionPickImagesConfiguration: PhotopickerConfiguration =
52     PhotopickerConfiguration(
53         action = MediaStore.ACTION_PICK_IMAGES,
54         intent = Intent(MediaStore.ACTION_PICK_IMAGES),
55     )
56 
57 /**
58  * A [PhotopickerConfiguration] that can be used for codepaths that utilize [Intent.GET_CONTENT]
59  * intent action.
60  */
61 val testGetContentConfiguration: PhotopickerConfiguration =
62     PhotopickerConfiguration(
63         action = Intent.ACTION_GET_CONTENT,
64         intent = Intent(Intent.ACTION_GET_CONTENT),
65     )
66 
67 /**
68  * A [PhotopickerConfiguration] that can be used for codepaths that utilize
69  * [MediaStore.ACTION_USER_SELECT_IMAGES_FOR_APP] intent action.
70  */
71 val testUserSelectImagesForAppConfiguration: PhotopickerConfiguration =
72     PhotopickerConfiguration(
73         action = MediaStore.ACTION_USER_SELECT_IMAGES_FOR_APP,
74         intent = Intent(MediaStore.ACTION_USER_SELECT_IMAGES_FOR_APP),
75     )
76 
77 /**
78  * Helper function to generate a [StateFlow] that mimics the flow emitted by the
79  * [ConfigurationManager]. This flow immediately emits the provided [PhotopickerConfiguration] or a
80  * default test configuration if none is provided.
81  */
provideTestConfigurationFlownull82 fun provideTestConfigurationFlow(
83     scope: CoroutineScope,
84     defaultConfiguration: PhotopickerConfiguration = testPhotopickerConfiguration
85 ): StateFlow<PhotopickerConfiguration> {
86 
87     return flow { emit(defaultConfiguration) }
88         .stateIn(scope, SharingStarted.Eagerly, initialValue = defaultConfiguration)
89 }
90