• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context
20 import android.content.Intent
21 import android.net.Uri
22 import com.android.avatarpicker.R
23 import com.android.avatarpicker.domain.getFileUri
24 import com.android.avatarpicker.domain.getTempPNG
25 import com.android.avatarpicker.domain.toIntent
26 import com.android.avatarpicker.ui.details.items.ResourceViewModel
27 import com.android.avatarpicker.ui.details.items.SelectableType
28 import com.android.avatarpicker.ui.details.items.UiState
29 import com.android.avatarpicker.ui.details.items.UriTypedItem
30 import java.io.File
31 import kotlinx.coroutines.flow.MutableStateFlow
32 import kotlinx.coroutines.flow.asStateFlow
33 import androidx.compose.runtime.getValue
34 
35 class ResultHandlerImpl(context: Context) : ResultHandler {
36     private val resultFile: File
37     private val contentUri: Uri
38     private val _uiState: MutableStateFlow<UiState> = MutableStateFlow(UiState.None())
39     override val uiState = _uiState.asStateFlow()
40 
41     override fun <T : SelectableType> onSelect(result: T) {
42         getSelected()?.unselect()
43         result.select()
44         _uiState.value = UiState.Success(result)
45     }
46 
47     override fun getSelected() = (uiState.value as? UiState.Success<*>)?.result
48 
49     override fun onLoading() {
50         _uiState.value = UiState.Loading()
51     }
52 
53     override fun onError(exception: Exception) {
54         _uiState.value = UiState.Error(exception)
55     }
56 
57     override fun unselect() {
58         _uiState.value = UiState.None()
59     }
60 
61     init {
62         context.apply {
63             resultFile = getTempPNG(getString(R.string.result_file_name))
64             contentUri = getFileUri(resultFile)
65             val packages = resources.getStringArray(R.array.grant_read_uri_permissions_packages)
66             packages.forEach { packageName ->
67                 grantUriPermission(
68                     packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
69                 )
70             }
71         }
72     }
73 
74     override fun getContentUri() = contentUri
75     override fun getTempFile() = resultFile
76 
77     override fun toResultIntent(context: Context): Intent {
78         getSelected().let { result ->
79             return when (result) {
80                 is ResourceViewModel -> result.toIntent(context)
81                 is UriTypedItem -> result.toIntent()
82                 else -> Intent()
83             }
84         }
85     }
86 }
87