|
| 1 | +package com.example.nav3recipes.deeplink.parseintent.singleModule |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import androidx.compose.animation.animateContentSize |
| 6 | +import androidx.compose.foundation.layout.Arrangement |
| 7 | +import androidx.compose.foundation.layout.Box |
| 8 | +import androidx.compose.foundation.layout.Column |
| 9 | +import androidx.compose.foundation.layout.fillMaxSize |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.layout.width |
| 12 | +import androidx.compose.foundation.lazy.LazyColumn |
| 13 | +import androidx.compose.material3.Button |
| 14 | +import androidx.compose.material3.DropdownMenuItem |
| 15 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 16 | +import androidx.compose.material3.ExposedDropdownMenuAnchorType |
| 17 | +import androidx.compose.material3.ExposedDropdownMenuBox |
| 18 | +import androidx.compose.material3.ExposedDropdownMenuDefaults |
| 19 | +import androidx.compose.material3.OutlinedTextField |
| 20 | +import androidx.compose.material3.Text |
| 21 | +import androidx.compose.material3.TextField |
| 22 | +import androidx.compose.runtime.Composable |
| 23 | +import androidx.compose.runtime.getValue |
| 24 | +import androidx.compose.runtime.mutableStateOf |
| 25 | +import androidx.compose.runtime.remember |
| 26 | +import androidx.compose.runtime.setValue |
| 27 | +import androidx.compose.ui.Alignment |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.text.font.FontWeight |
| 30 | +import androidx.compose.ui.text.style.TextAlign |
| 31 | +import androidx.compose.ui.unit.TextUnit |
| 32 | +import androidx.compose.ui.unit.dp |
| 33 | +import androidx.compose.ui.unit.sp |
| 34 | +import androidx.core.net.toUri |
| 35 | + |
| 36 | +@Composable |
| 37 | +internal fun EntryScreen(text: String, block: @Composable () -> Unit = { }) { |
| 38 | + Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { |
| 39 | + Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp)) { |
| 40 | + Text(text, fontWeight = FontWeight.Bold, fontSize = FONT_SIZE_TITLE) |
| 41 | + block() |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +@Composable |
| 47 | +internal fun FriendsList(users: List<User>) { |
| 48 | + // display list of matching targets |
| 49 | + if (users.isEmpty()) { |
| 50 | + Text("List is Empty", fontWeight = FontWeight.Bold) |
| 51 | + } else { |
| 52 | + LazyColumn { |
| 53 | + items(users.size) { idx -> |
| 54 | + val user = users[idx] |
| 55 | + TextContent("${user.firstName}(${user.age}), ${user.location}") |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Displays a text input menu, may include several text fields |
| 63 | + */ |
| 64 | +@Composable |
| 65 | +internal fun MenuTextInput( |
| 66 | + menuLabels: List<String>, |
| 67 | + onValueChange: (String, String) -> Unit = { _, _ ->}, |
| 68 | +) { |
| 69 | + Column { |
| 70 | + menuLabels.forEach { label -> |
| 71 | + var inputText by remember { mutableStateOf("") } |
| 72 | + |
| 73 | + OutlinedTextField( |
| 74 | + value = inputText, |
| 75 | + onValueChange = { |
| 76 | + inputText = it |
| 77 | + onValueChange(label, it) |
| 78 | + }, |
| 79 | + placeholder = { Text("enter integer") }, |
| 80 | + label = { Text(label) }, |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Displays a drop down menu, may include multiple drop downs |
| 89 | + */ |
| 90 | +@Composable |
| 91 | +internal fun MenuDropDown( |
| 92 | + menuOptions: Map<String, List<String>>, |
| 93 | + onSelect: (label: String, selection: String) -> Unit = { _, _ ->}, |
| 94 | +) { |
| 95 | + Column( |
| 96 | + modifier = Modifier.animateContentSize(), |
| 97 | + horizontalAlignment = Alignment.CenterHorizontally |
| 98 | + ) { |
| 99 | + menuOptions.forEach { entry -> |
| 100 | + val key = entry.key |
| 101 | + ArgumentDropDownMenu(label = key, menuItemOptions = entry.value) { label, selection -> |
| 102 | + onSelect(key, selection) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Display list of selections for one drop down |
| 109 | +@OptIn(ExperimentalMaterial3Api::class) |
| 110 | +@Composable |
| 111 | +private fun ArgumentDropDownMenu( |
| 112 | + label: String, |
| 113 | + menuItemOptions: List<String>, |
| 114 | + onSelect: (label: String, selection: String) -> Unit, |
| 115 | +) { |
| 116 | + val initValue = menuItemOptions.firstOrNull() ?: "" |
| 117 | + var expanded by remember { mutableStateOf(false) } |
| 118 | + var currSelected by remember { mutableStateOf(initValue) } |
| 119 | + Box( |
| 120 | + modifier = Modifier |
| 121 | + .padding(16.dp) |
| 122 | + ) { |
| 123 | + ExposedDropdownMenuBox( |
| 124 | + expanded = expanded, |
| 125 | + onExpandedChange = { expanded = !expanded } |
| 126 | + ) { |
| 127 | + TextField( |
| 128 | + readOnly = true, |
| 129 | + value = currSelected, |
| 130 | + onValueChange = { }, |
| 131 | + label = { Text(label) }, |
| 132 | + trailingIcon = { |
| 133 | + ExposedDropdownMenuDefaults.TrailingIcon( |
| 134 | + expanded = expanded |
| 135 | + ) |
| 136 | + }, |
| 137 | + modifier = Modifier.menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable, true) |
| 138 | + ) |
| 139 | + ExposedDropdownMenu( |
| 140 | + expanded = expanded, |
| 141 | + onDismissRequest = { |
| 142 | + expanded = false |
| 143 | + } |
| 144 | + ) { |
| 145 | + menuItemOptions.forEach { text -> |
| 146 | + DropdownMenuItem( |
| 147 | + text = { Text(text) }, |
| 148 | + onClick = { |
| 149 | + expanded = false |
| 150 | + currSelected = text |
| 151 | + onSelect(label, text) |
| 152 | + } |
| 153 | + ) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +@Composable |
| 161 | +internal fun DeepLinkButton( |
| 162 | + context: Context, |
| 163 | + targetActivity: Class<*>, |
| 164 | + deepLinkUrl: String, |
| 165 | +) { |
| 166 | + Button( |
| 167 | + onClick = { |
| 168 | + val intent = Intent( |
| 169 | + context, |
| 170 | + targetActivity |
| 171 | + ) |
| 172 | + // start activity with the url |
| 173 | + intent.data = deepLinkUrl.toUri() |
| 174 | + context.startActivity(intent) |
| 175 | + } |
| 176 | + ) { |
| 177 | + Text("Deeplink away!") |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +@Composable |
| 182 | +fun TextContent(text: String) { |
| 183 | + Text( |
| 184 | + text = text, |
| 185 | + modifier = Modifier.width(300.dp), |
| 186 | + textAlign = TextAlign.Center, |
| 187 | + fontSize = FONT_SIZE_TEXT, |
| 188 | + ) |
| 189 | +} |
| 190 | + |
| 191 | +internal val FONT_SIZE_TITLE: TextUnit = 20.sp |
| 192 | +internal val FONT_SIZE_TEXT: TextUnit = 15.sp |
0 commit comments