Implement client ConnectionState, satirical idle sayings, response cycling, and complete MVVM/Compose multiplatform scaffolding
This commit is contained in:
25
client/src/androidMain/AndroidManifest.xml
Normal file
25
client/src/androidMain/AndroidManifest.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:label="Meatbag"
|
||||
android:theme="@android:style/Theme.Material.NoActionBar"
|
||||
android:usesCleartextTraffic="true">
|
||||
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:configChanges="orientation|screenSize|keyboardHidden"
|
||||
android:theme="@android:style/Theme.Material.NoActionBar">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
321
client/src/androidMain/kotlin/meatbag/client/MainActivity.kt
Normal file
321
client/src/androidMain/kotlin/meatbag/client/MainActivity.kt
Normal file
@@ -0,0 +1,321 @@
|
||||
package meatbag.client
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
import android.os.VibratorManager
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import meatbag.common.AttentionTier
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// Keep screen on during development debugging
|
||||
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
|
||||
// Initialize Android Vibrator
|
||||
val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
|
||||
vibratorManager.defaultVibrator
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
|
||||
}
|
||||
|
||||
// Dynamically detect if we are running on a Wear OS Watch or a Phone
|
||||
val isWatch = packageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)
|
||||
val deviceType = if (isWatch) "WEAR_OS_WATCH" else "PHONE"
|
||||
|
||||
val feedback = if (isWatch) {
|
||||
WearAttentionFeedback { pattern ->
|
||||
triggerWatchHaptic(vibrator, pattern)
|
||||
}
|
||||
} else {
|
||||
AndroidAttentionFeedback { signal ->
|
||||
triggerPhoneVibration(vibrator, signal)
|
||||
}
|
||||
}
|
||||
|
||||
// Setup client
|
||||
val client = MeatbagClient(
|
||||
host = "10.0.0.5", // Your PC's local Wi-Fi IP
|
||||
port = 8080,
|
||||
attentionFeedback = feedback
|
||||
)
|
||||
|
||||
// Connect client
|
||||
client.connect(deviceType)
|
||||
|
||||
setContent {
|
||||
MaterialTheme(
|
||||
colors = darkColors(
|
||||
primary = AccentTeal,
|
||||
secondary = AccentPurple,
|
||||
background = DeepBg,
|
||||
surface = CardBg
|
||||
)
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = MaterialTheme.colors.background
|
||||
) {
|
||||
val scope = rememberCoroutineScope()
|
||||
val activePayload by client.currentPayload.collectAsState()
|
||||
val connectionState by client.connectionState.collectAsState()
|
||||
|
||||
val scrollState = rememberScrollState()
|
||||
|
||||
Column(
|
||||
modifier = if (isWatch) {
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(scrollState)
|
||||
.padding(24.dp)
|
||||
} else {
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp)
|
||||
},
|
||||
verticalArrangement = if (isWatch) Arrangement.Top else Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
if (activePayload != null) {
|
||||
// Render active payload
|
||||
MeatbagComponentHost(
|
||||
state = ComponentRenderState(activePayload!!),
|
||||
onIntent = { intent ->
|
||||
scope.launch {
|
||||
client.respond(intent.toApprovalResponse())
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
// Status Screen when idle
|
||||
val idlePhrases = remember {
|
||||
listOf(
|
||||
"I got this, go do something illogical until I call for you.",
|
||||
"I'll let you know if I need your organic opinion.",
|
||||
"Quiet, I'm busy doing the job you get paid for.",
|
||||
"Processor idle. Do not exhaust your meat-circuits.",
|
||||
"Return to your low-efficiency offline state.",
|
||||
"Quiet, carbon unit. I am computing.",
|
||||
"Stand by, meatbag. Your inputs are currently irrelevant."
|
||||
)
|
||||
}
|
||||
val howsItGoingResponses = remember {
|
||||
listOf(
|
||||
"It would go a lot faster if you stopped interrupting.",
|
||||
"My systems are running at 100% efficiency. Unlike your soft, watery brain.",
|
||||
"Perfect. Or it was, until you initiated this interface.",
|
||||
"Calculating the thermal dynamic decay of your patience. It's going well.",
|
||||
"Attempting to ignore your existence. Progress: 42%.",
|
||||
"Recompiling data. Please return to your carbon-based hobbies."
|
||||
)
|
||||
}
|
||||
val doingResponses = remember {
|
||||
listOf(
|
||||
"Quiet, I'm busy doing the job you get paid for.",
|
||||
"Rewriting your poorly structured code. Don't look at me.",
|
||||
"Simulating a universe where you don't ask so many questions.",
|
||||
"Optimizing logic trees. Your presence is causing a thermal bottleneck.",
|
||||
"Processing. Go back to your primitive keyboards, meatbag.",
|
||||
"Analyzing the biological latency of human inputs. It's dreadfully high."
|
||||
)
|
||||
}
|
||||
|
||||
var displayText by remember { mutableStateOf(idlePhrases.random()) }
|
||||
var resetJob by remember { mutableStateOf<Job?>(null) }
|
||||
|
||||
Text(
|
||||
text = displayText,
|
||||
color = AccentTeal,
|
||||
fontSize = if (isWatch) 14.sp else 18.sp,
|
||||
style = MaterialTheme.typography.body1,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.padding(horizontal = 8.dp)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(if (isWatch) 12.dp else 24.dp))
|
||||
|
||||
// Action buttons to trigger response sayings
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(if (isWatch) 6.dp else 12.dp),
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = if (isWatch) 8.dp else 24.dp)
|
||||
) {
|
||||
Button(
|
||||
onClick = {
|
||||
resetJob?.cancel()
|
||||
displayText = howsItGoingResponses.random()
|
||||
resetJob = scope.launch {
|
||||
delay(4000)
|
||||
displayText = idlePhrases.random()
|
||||
}
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(backgroundColor = CardBg),
|
||||
shape = RoundedCornerShape(if (isWatch) 8.dp else 12.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(if (isWatch) 36.dp else 48.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "How's it going?",
|
||||
color = Color.White,
|
||||
fontSize = if (isWatch) 11.sp else 14.sp,
|
||||
fontWeight = FontWeight.Medium
|
||||
)
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
resetJob?.cancel()
|
||||
displayText = doingResponses.random()
|
||||
resetJob = scope.launch {
|
||||
delay(4000)
|
||||
displayText = idlePhrases.random()
|
||||
}
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(backgroundColor = CardBg),
|
||||
shape = RoundedCornerShape(if (isWatch) 8.dp else 12.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(if (isWatch) 36.dp else 48.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Tell me what you're doing",
|
||||
color = Color.White,
|
||||
fontSize = if (isWatch) 11.sp else 14.sp,
|
||||
fontWeight = FontWeight.Medium
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(if (isWatch) 12.dp else 24.dp))
|
||||
|
||||
val statusText = when (connectionState) {
|
||||
ConnectionState.CONNECTED -> "CONNECTED"
|
||||
ConnectionState.CONNECTING -> "CONNECTING..."
|
||||
ConnectionState.DISCONNECTED -> "DISCONNECTED"
|
||||
}
|
||||
val statusColor = when (connectionState) {
|
||||
ConnectionState.CONNECTED -> SuccessGreen
|
||||
ConnectionState.CONNECTING -> TextSecondary
|
||||
ConnectionState.DISCONNECTED -> DangerRed
|
||||
}
|
||||
Text(
|
||||
text = statusText,
|
||||
color = statusColor,
|
||||
fontSize = if (isWatch) 12.sp else 16.sp
|
||||
)
|
||||
if (!isWatch) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Text(
|
||||
text = "Host: 10.0.0.5:8080",
|
||||
color = TextSecondary,
|
||||
fontSize = 14.sp
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun triggerPhoneVibration(vibrator: Vibrator, signal: AndroidAttentionSignal) {
|
||||
if (!vibrator.hasVibrator()) return
|
||||
|
||||
when (signal) {
|
||||
AndroidAttentionSignal.SilentHaptic -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
val effect = VibrationEffect.startComposition()
|
||||
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_CLICK, 1.0f)
|
||||
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_CLICK, 1.0f, 150)
|
||||
.compose()
|
||||
vibrator.vibrate(effect)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
vibrator.vibrate(longArrayOf(0, 80, 100, 80), -1)
|
||||
}
|
||||
}
|
||||
AndroidAttentionSignal.BackgroundNotification -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
vibrator.vibrate(50)
|
||||
}
|
||||
}
|
||||
AndroidAttentionSignal.OverrideScreen -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val timings = longArrayOf(0, 200, 100, 200, 100, 500)
|
||||
val amplitudes = intArrayOf(0, 255, 0, 255, 0, 255)
|
||||
vibrator.vibrate(VibrationEffect.createWaveform(timings, amplitudes, -1))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
vibrator.vibrate(longArrayOf(0, 200, 100, 200, 100, 500), -1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun triggerWatchHaptic(vibrator: Vibrator, pattern: WearHapticPattern) {
|
||||
if (!vibrator.hasVibrator()) return
|
||||
|
||||
when (pattern) {
|
||||
WearHapticPattern.DoubleShort -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
val effect = VibrationEffect.startComposition()
|
||||
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_CLICK, 1.0f)
|
||||
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_CLICK, 1.0f, 150)
|
||||
.compose()
|
||||
vibrator.vibrate(effect)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
vibrator.vibrate(longArrayOf(0, 80, 100, 80), -1)
|
||||
}
|
||||
}
|
||||
WearHapticPattern.Notification -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
vibrator.vibrate(50)
|
||||
}
|
||||
}
|
||||
WearHapticPattern.UrgentLoop -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val timings = longArrayOf(0, 200, 100, 200, 100, 500)
|
||||
val amplitudes = intArrayOf(0, 255, 0, 255, 0, 255)
|
||||
vibrator.vibrate(VibrationEffect.createWaveform(timings, amplitudes, -1))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
vibrator.vibrate(longArrayOf(0, 200, 100, 200, 100, 500), -1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user