-
Notifications
You must be signed in to change notification settings - Fork 13
/
ActivityResults.kt
137 lines (129 loc) · 4.86 KB
/
ActivityResults.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package dev.enro.example.destinations.activity
import android.Manifest
import android.os.Build
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import dev.enro.annotations.NavigationDestination
import dev.enro.annotations.ExperimentalEnroApi
import dev.enro.core.NavigationKey
import dev.enro.core.activity.activityResultDestination
import dev.enro.core.activity.withInput
import dev.enro.core.activity.withMappedResult
import dev.enro.core.compose.navigationHandle
import dev.enro.core.compose.registerForNavigationResult
import dev.enro.core.present
import dev.enro.example.destinations.synthetic.SimpleMessage
import kotlinx.parcelize.Parcelize
@Parcelize
class ActivityResultExample : NavigationKey.SupportsPush
@Composable
@NavigationDestination(ActivityResultExample::class)
fun ActivityResultExampleScreen() {
val navigation = navigationHandle()
val getMediaName = registerForNavigationResult<String>(
onClosed = {
navigation.present(
SimpleMessage(
title = "Activity Result",
message = "GetMediaFileName closed without a result"
)
)
},
onResult = {
navigation.present(
SimpleMessage(
title = "Activity Result",
message = "GetMediaFileName returned a result of: $it"
)
)
}
)
val getCameraPermission = registerForNavigationResult<RequestCameraPermission.Result> {
when(it) {
RequestCameraPermission.Result.GRANTED -> navigation.present(
SimpleMessage(
title = "Activity Result",
message = "Camera permission granted"
)
)
RequestCameraPermission.Result.DENIED -> navigation.present(
SimpleMessage(
title = "Activity Result",
message = "Camera permission denied"
)
)
RequestCameraPermission.Result.DENIED_PERMANENTLY -> navigation.present(
SimpleMessage(
title = "Activity Result",
message = "Camera permission denied forever"
)
)
}
}
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.surface),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Button(onClick = { getMediaName.present(GetVisualMediaFileName(false)) }) {
Text("Get Media File Name")
}
Button(onClick = { getCameraPermission.present(RequestCameraPermission()) }) {
Text("Request Camera Permission")
}
}
}
@Parcelize
class GetVisualMediaFileName(
val imageOnly: Boolean
) : NavigationKey.SupportsPresent.WithResult<String>
@OptIn(ExperimentalEnroApi::class)
@NavigationDestination(GetVisualMediaFileName::class)
val pickFileDestination = activityResultDestination(GetVisualMediaFileName::class) {
ActivityResultContracts.PickVisualMedia()
.withInput(
PickVisualMediaRequest(
when (key.imageOnly) {
true -> ActivityResultContracts.PickVisualMedia.ImageOnly
else -> ActivityResultContracts.PickVisualMedia.ImageAndVideo
}
)
)
.withMappedResult {
it.lastPathSegment ?: "unknown!"
}
}
@Parcelize
class RequestCameraPermission : NavigationKey.SupportsPresent.WithResult<RequestCameraPermission.Result> {
enum class Result {
GRANTED,
DENIED,
DENIED_PERMANENTLY,
}
}
@OptIn(ExperimentalEnroApi::class)
@NavigationDestination(RequestCameraPermission::class)
val requestCameraPermission = activityResultDestination(RequestCameraPermission::class) {
ActivityResultContracts.RequestPermission()
.withInput(Manifest.permission.CAMERA)
.withMappedResult { granted ->
when {
granted -> RequestCameraPermission.Result.GRANTED
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
activity.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> RequestCameraPermission.Result.DENIED
else -> RequestCameraPermission.Result.DENIED_PERMANENTLY
}
}
}