You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.2 KiB
125 lines
4.2 KiB
import androidx.compose.foundation.Image |
|
import androidx.compose.foundation.layout.Box |
|
import androidx.compose.foundation.layout.Column |
|
import androidx.compose.foundation.layout.fillMaxSize |
|
import androidx.compose.material.LocalTextStyle |
|
import androidx.compose.material.MaterialTheme |
|
import androidx.compose.material.ProvideTextStyle |
|
import androidx.compose.material.Scaffold |
|
import androidx.compose.material.Surface |
|
import androidx.compose.material.Text |
|
import androidx.compose.material.TopAppBar |
|
import androidx.compose.material.lightColors |
|
import androidx.compose.runtime.Composable |
|
import androidx.compose.runtime.LaunchedEffect |
|
import androidx.compose.runtime.collectAsState |
|
import androidx.compose.runtime.getValue |
|
import androidx.compose.ui.Modifier |
|
import androidx.compose.ui.graphics.Color |
|
import androidx.compose.ui.layout.ContentScale |
|
import androidx.compose.ui.unit.sp |
|
import kotlinx.coroutines.CoroutineScope |
|
import kotlinx.coroutines.SupervisorJob |
|
import kotlinx.coroutines.delay |
|
import org.jetbrains.compose.resources.ExperimentalResourceApi |
|
import org.jetbrains.compose.resources.painterResource |
|
|
|
val myUser = User("Me", picture = null) |
|
val friends = listOf( |
|
User("Alex", picture = "stock1.jpg"), |
|
User("Casey", picture = "stock2.jpg"), |
|
User("Sam", picture = "stock3.jpg") |
|
) |
|
val friendMessages = listOf( |
|
"How's everybody doing today?", |
|
"I've been meaning to chat!", |
|
"When do we hang out next? 😋", |
|
"We really need to catch up!", |
|
"It's been too long!", |
|
"I can't\nbelieve\nit! 😱", |
|
"Did you see that ludicrous\ndisplay last night?", |
|
"We should meet up in person!", |
|
"How about a round of pinball?", |
|
"I'd love to:\n🍔 Eat something\n🎥 Watch a movie, maybe?\nWDYT?" |
|
) |
|
val store = CoroutineScope(SupervisorJob()).createStore() |
|
|
|
@Composable |
|
fun ChatAppWithScaffold(displayTextField: Boolean = true) { |
|
Theme { |
|
Scaffold( |
|
topBar = { |
|
TopAppBar( |
|
title = { Text("The Composers Chat") }, |
|
backgroundColor = MaterialTheme.colors.background, |
|
) |
|
}) { |
|
ChatApp(displayTextField = displayTextField) |
|
} |
|
} |
|
} |
|
|
|
@OptIn(ExperimentalResourceApi::class) |
|
@Composable |
|
fun ChatApp(displayTextField: Boolean = true) { |
|
val state by store.stateFlow.collectAsState() |
|
Theme { |
|
Surface { |
|
Box(modifier = Modifier.fillMaxSize()) { |
|
Image(painterResource("background.jpg"), null, contentScale = ContentScale.Crop) |
|
Column( |
|
modifier = Modifier.fillMaxSize() |
|
) { |
|
Box(Modifier.weight(1f)) { |
|
Messages(state.messages) |
|
} |
|
if (displayTextField) { |
|
SendMessage { text -> |
|
store.send( |
|
Action.SendMessage( |
|
Message(myUser, timeMs = timestampMs(), text) |
|
) |
|
) |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
LaunchedEffect(Unit) { |
|
var lastFriend = friends.random() |
|
var lastMessage = friendMessages.random() |
|
while (true) { |
|
val thisFriend = friends.random() |
|
val thisMessage = friendMessages.random() |
|
if(thisFriend == lastFriend) continue |
|
if(thisMessage == lastMessage) continue |
|
lastFriend = thisFriend |
|
lastMessage = thisMessage |
|
store.send( |
|
Action.SendMessage( |
|
message = Message( |
|
user = thisFriend, |
|
timeMs = timestampMs(), |
|
text = thisMessage |
|
) |
|
) |
|
) |
|
delay(5000) |
|
} |
|
} |
|
} |
|
|
|
@Composable |
|
fun Theme(content: @Composable () -> Unit) { |
|
MaterialTheme( |
|
colors = lightColors( |
|
surface = Color(ChatColors.SURFACE), |
|
background = Color(ChatColors.TOP_GRADIENT.last()), |
|
), |
|
) { |
|
ProvideTextStyle(LocalTextStyle.current.copy(letterSpacing = 0.sp)) { |
|
content() |
|
} |
|
} |
|
}
|
|
|