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 
34 class ResultHandlerImpl(context: Context) : ResultHandler {
35     private val resultFile: File
36     private val contentUri: Uri
37     private val _uiState: MutableStateFlow<UiState> = MutableStateFlow(UiState.None())
38     override val uiState = _uiState.asStateFlow()
39 
40     override fun <T : SelectableType> onSelect(result: T) {
41         getSelected()?.isSelected = false
42         result.isSelected = true
43         _uiState.value = UiState.Success(result)
44     }
45 
46     override fun getSelected() = (uiState.value as? UiState.Success<*>)?.result
47 
48     override fun onLoading() {
49         _uiState.value = UiState.Loading()
50     }
51 
52     override fun onError(exception: Exception) {
53         _uiState.value = UiState.Error(exception)
54     }
55 
56     override fun unselect() {
57         _uiState.value = UiState.None()
58     }
59 
60     init {
61         context.apply {
62             resultFile = getTempPNG(getString(R.string.result_file_name))
63             contentUri = getFileUri(resultFile)
64             val packages = resources.getStringArray(R.array.grant_read_uri_permissions_packages)
65             packages.forEach { packageName ->
66                 grantUriPermission(
67                     packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
68                 )
69             }
70         }
71     }
72 
73     override fun getContentUri() = contentUri
74     override fun getTempFile() = resultFile
75 
76     override fun toResultIntent(context: Context): Intent {
77         getSelected().let { result ->
78             return when (result) {
79                 is ResourceViewModel -> result.toIntent(context)
80                 is UriTypedItem -> result.toIntent()
81                 else -> Intent()
82             }
83         }
84     }
85 }
86