1 /*
<lambda>null2  * Copyright (C) 2023 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.avatarpicker.ui
18 
19 import android.os.Bundle
20 import androidx.activity.ComponentActivity
21 import androidx.activity.compose.setContent
22 import androidx.activity.enableEdgeToEdge
23 import androidx.compose.foundation.background
24 import androidx.compose.foundation.layout.Box
25 import androidx.compose.foundation.layout.fillMaxSize
26 import androidx.compose.foundation.layout.width
27 import androidx.compose.material3.CircularProgressIndicator
28 import androidx.compose.material3.ExperimentalMaterial3Api
29 import androidx.compose.material3.MaterialTheme
30 import androidx.compose.material3.Surface
31 import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
32 import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
33 import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
34 import androidx.compose.runtime.collectAsState
35 import androidx.compose.runtime.getValue
36 import androidx.compose.ui.Alignment
37 import androidx.compose.ui.Modifier
38 import androidx.compose.ui.unit.dp
39 import com.android.avatarpicker.AvatarProviderApp
40 import com.android.avatarpicker.domain.CAMERA
41 import com.android.avatarpicker.domain.PHOTO_PICKER
42 import com.android.avatarpicker.ui.bottombar.BottomActionBar
43 import com.android.avatarpicker.ui.details.DetailsList
44 import com.android.avatarpicker.ui.details.items.UiState
45 import com.android.avatarpicker.ui.info.InfoCard
46 import com.android.avatarpicker.ui.theme.AvatarPickerTheme
47 
48 class AvatarPickerActivity : ComponentActivity() {
49 
50     @OptIn(ExperimentalMaterial3WindowSizeClassApi::class, ExperimentalMaterial3Api::class)
51     override fun onCreate(savedInstanceState: Bundle?) {
52         enableEdgeToEdge()
53         super.onCreate(savedInstanceState)
54 
55         val activityViewModel = (application as AvatarProviderApp).let {
56             ActivityViewModel(it.getGroupedSelectableItems(intent), it.getResultHandler())
57         }
58         val itemViewComposer = (application as AvatarProviderApp).getItemViewComposer()
59 
60         setContent {
61             AvatarPickerTheme {
62                 val windowSize = calculateWindowSizeClass(this)
63                 Surface(
64                     contentColor = MaterialTheme.colorScheme.surfaceContainer,
65                 ) {
66                     val showOnePane = (windowSize.widthSizeClass != WindowWidthSizeClass.Expanded)
67                     val currentResult by activityViewModel.resultHandler.uiState.collectAsState()
68                     activityViewModel.resultHandler.getSelected()?.typeId?.let { typeId: Int ->
69                         if (typeId == CAMERA || (typeId == PHOTO_PICKER)) {
70                             setResultAndFinish(activityViewModel.resultHandler)
71                         }
72                     }
73                     Box(
74                         Modifier.fillMaxSize(), contentAlignment = Alignment.Center
75                     ) {
76                         AdaptivePane(showOnePane = showOnePane,
77                             startPane = { InfoCard(activityViewModel.infoViewModel) },
78                             endPane = {
79                                 it.DetailsList(activityViewModel.detailsViewModel, itemViewComposer)
80                             },
81                             bottom = {
82                                 BottomActionBar(currentResult is UiState.Success<*>, {
83                                     setResultAndFinish(activityViewModel.resultHandler)
84                                 }, {
85                                     activityViewModel.resultHandler.unselect()
86                                     this@AvatarPickerActivity.finish()
87                                 })
88                             })
89 
90                         if (currentResult is UiState.Loading) {
91                             Box(
92                                 Modifier.fillMaxSize()
93                                     .background(MaterialTheme.colorScheme.onSurface.copy(.5f)),
94                                 contentAlignment = Alignment.Center
95                             ) {
96                                 CircularProgressIndicator(
97                                     modifier = Modifier.width(64.dp),
98                                     color = MaterialTheme.colorScheme.secondary,
99                                     trackColor = MaterialTheme.colorScheme.surfaceVariant,
100                                 )
101                             }
102                         }
103                     }
104                 }
105             }
106         }
107     }
108 
109     fun setResultAndFinish(resultHandler: ResultHandler){
110         val result = resultHandler.toResultIntent(this)
111         resultHandler.unselect()
112         this.setResult(RESULT_OK, result)
113         this.finish()
114     }
115 }
116