1
+ /*
2
+ * Copyright 2016 Google Inc.
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
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ */
14
+ package com.firebase.uidemo
15
+
16
+ import android.content.Intent
17
+ import android.os.Bundle
18
+ import androidx.activity.ComponentActivity
19
+ import androidx.activity.compose.setContent
20
+ import androidx.annotation.StringRes
21
+ import androidx.compose.foundation.clickable
22
+ import androidx.compose.foundation.layout.*
23
+ import androidx.compose.foundation.lazy.LazyColumn
24
+ import androidx.compose.foundation.lazy.items
25
+ import androidx.compose.material3.*
26
+ import androidx.compose.runtime.Composable
27
+ import androidx.compose.runtime.remember
28
+ import androidx.compose.ui.Modifier
29
+ import androidx.compose.ui.platform.LocalContext
30
+ import androidx.compose.ui.res.stringResource
31
+ import androidx.compose.ui.tooling.preview.Preview
32
+ import androidx.compose.ui.unit.dp
33
+ import com.firebase.ui.auth.AuthUI
34
+ import com.firebase.ui.auth.util.ExtraConstants
35
+ import com.firebase.uidemo.auth.AnonymousUpgradeActivity
36
+ import com.firebase.uidemo.auth.AuthUiActivity
37
+ import com.firebase.uidemo.auth.compose.AuthComposeActivity
38
+ import com.firebase.uidemo.auth.compose.ChooserScreen
39
+ import com.firebase.uidemo.database.firestore.FirestoreChatActivity
40
+ import com.firebase.uidemo.database.firestore.FirestorePagingActivity
41
+ import com.firebase.uidemo.database.realtime.FirebaseDbPagingActivity
42
+ import com.firebase.uidemo.database.realtime.RealtimeDbChatActivity
43
+ import com.firebase.uidemo.storage.ImageActivity
44
+
45
+ class ChooserActivity : ComponentActivity () {
46
+
47
+ override fun onCreate (savedInstanceState : Bundle ? ) {
48
+ super .onCreate(savedInstanceState)
49
+
50
+ // Deep-link handling identical to the original Java implementation
51
+ if (AuthUI .canHandleIntent(intent)) {
52
+ val authIntent = Intent (this , AuthUiActivity ::class .java).apply {
53
+ putExtra(ExtraConstants .EMAIL_LINK_SIGN_IN , intent.data.toString())
54
+ }
55
+ startActivity(authIntent)
56
+ finish()
57
+ return
58
+ }
59
+
60
+ setContent { ChooserScreen () }
61
+ }
62
+
63
+
64
+ @OptIn(ExperimentalMaterial3Api ::class )
65
+ @Composable
66
+ private fun ChooserScreen () {
67
+ val items = remember { activityItems }
68
+
69
+ Scaffold (
70
+ topBar = {
71
+ CenterAlignedTopAppBar (
72
+ title = { Text (stringResource(R .string.app_name)) }
73
+ )
74
+ }
75
+ ) { padding ->
76
+ LazyColumn (
77
+ contentPadding = padding,
78
+ modifier = Modifier .fillMaxSize()
79
+ ) {
80
+ items(items) { entry -> ActivityRow (entry) }
81
+ }
82
+ }
83
+ }
84
+
85
+ @Composable
86
+ private fun ActivityRow (entry : ActivityEntry ) {
87
+ val ctx = LocalContext .current
88
+ Card (
89
+ Modifier
90
+ .fillMaxWidth()
91
+ .padding(horizontal = 16 .dp, vertical = 8 .dp)
92
+ .clickable { ctx.startActivity(Intent (ctx, entry.clazz)) }
93
+ ) {
94
+ Column (Modifier .padding(16 .dp)) {
95
+ Text (
96
+ text = stringResource(entry.titleRes),
97
+ style = MaterialTheme .typography.titleMedium
98
+ )
99
+ Spacer (Modifier .height(4 .dp))
100
+ Text (
101
+ text = stringResource(entry.descRes),
102
+ style = MaterialTheme .typography.bodyMedium
103
+ )
104
+ }
105
+ }
106
+ }
107
+
108
+
109
+ private data class ActivityEntry (
110
+ val clazz : Class <* >,
111
+ @StringRes val titleRes : Int ,
112
+ @StringRes val descRes : Int
113
+ )
114
+
115
+ private val activityItems = listOf (
116
+ ActivityEntry (AuthUiActivity ::class .java,
117
+ R .string.title_auth_activity, R .string.desc_auth),
118
+ ActivityEntry (AuthComposeActivity ::class .java,
119
+ R .string.auth_compose_title, R .string.desc_auth),
120
+ ActivityEntry (AnonymousUpgradeActivity ::class .java,
121
+ R .string.title_anonymous_upgrade, R .string.desc_anonymous_upgrade),
122
+ ActivityEntry (FirestoreChatActivity ::class .java,
123
+ R .string.title_firestore_activity, R .string.desc_firestore),
124
+ ActivityEntry (FirestorePagingActivity ::class .java,
125
+ R .string.title_firestore_paging_activity, R .string.desc_firestore_paging),
126
+ ActivityEntry (RealtimeDbChatActivity ::class .java,
127
+ R .string.title_realtime_database_activity, R .string.desc_realtime_database),
128
+ ActivityEntry (FirebaseDbPagingActivity ::class .java,
129
+ R .string.title_realtime_database_paging_activity, R .string.desc_realtime_database_paging),
130
+ ActivityEntry (ImageActivity ::class .java,
131
+ R .string.title_storage_activity, R .string.desc_storage)
132
+ )
133
+ }
134
+
135
+
136
+ @Preview(showBackground = true , widthDp = 360 , heightDp = 640 )
137
+ @Composable
138
+ private fun ChooserScreenPreview () {
139
+ MaterialTheme { ChooserActivity ().run { ChooserScreen () } }
140
+ }
0 commit comments