Skip to content

Instantly share code, notes, and snippets.

@ColtonIdle
Created February 28, 2022 03:52
Show Gist options
  • Save ColtonIdle/b198d3623c46b455050059b02bc5c4d6 to your computer and use it in GitHub Desktop.
Save ColtonIdle/b198d3623c46b455050059b02bc5c4d6 to your computer and use it in GitHub Desktop.
package com.app.googleformsapp
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import com.app.googleformsapp.ui.theme.GoogleFormsAppTheme
//This fakes out what a sample response from a server would be if we were building a Google Forms app
val networkResponse1 = listOf("name", "address")
// This is a fake second response. Even though it looks similar to the above, it should be considered an entirely new response from the server
val networkResponse2 = listOf("name", "address", "nickname")
var currentCall by mutableStateOf(1)
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalAnimationApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
GoogleFormsAppTheme {
val myState = remember { MyState() }
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Column {
Button(onClick = {
// Button click pretends to be a network call. We clear the response first and add all new form fields from the server
if (currentCall == 1) {
myState.list.clear()
myState.list.addAll(networkResponse1.map { it.mapToMyFormFields() })
currentCall++
} else {
myState.list.clear()
myState.list.addAll(networkResponse2.map { it.mapToMyFormFields() })
}
}) {
Text(text = "Make network call: $currentCall")
}
myState.list.forEach {
Box(Modifier.clickable {
it.label = it.text
it.text = "Adam powell"
}) {
MyInputField(
modifier = Modifier.fillMaxWidth(),
label = it.label,
value = it.text,
error = it.isError
)
}
}
}
}
}
}
}
}
// This maps from "network" models (which are just strings) into MyFormField models
private fun String.mapToMyFormFields(): MyFormField {
if (this == "name") {
return MyFormField(this, text = "This should be your name")
} else if (this == "address") {
return MyFormField(this, text = "Where you live")
} else if (this == "nickname") {
return MyFormField(this, text = "What can I call you alt")
}
return MyFormField(this, text = "not found")
}
//The state of my screen
class MyState {
var list = mutableStateListOf<MyFormField>()
}
// The representation of a form field
class MyFormField(
val id: String = "",
label: String = "",
val type: String = "",
text: String = ""
) {
var label by mutableStateOf(label)
var text by mutableStateOf(text)
var isError by mutableStateOf(false)
}
@ExperimentalAnimationApi
@Composable
fun MyInputField(
modifier: Modifier,
label: String,
value: String,
error: Boolean,
) {
Column(modifier = modifier) {
Box() {
OutlinedTextField(
modifier = Modifier.fillMaxWidth(1f),
value = value,
onValueChange = { },
enabled = false,
label =
if (label.isNotEmpty()) {
{ Text(text = label) }
} else {
null
},
isError = error,
singleLine = true,
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment