|
| 1 | +package com.joebuildsthings.gamedeck |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | +import android.os.Bundle |
| 6 | +import android.provider.Settings |
| 7 | +import androidx.activity.ComponentActivity |
| 8 | +import androidx.activity.compose.setContent |
| 9 | +import androidx.compose.foundation.background |
| 10 | +import androidx.compose.foundation.layout.* |
| 11 | +import androidx.compose.material.icons.Icons |
| 12 | +import androidx.compose.material.icons.filled.Home |
| 13 | +import androidx.compose.material.icons.filled.Settings |
| 14 | +import androidx.compose.material.icons.filled.SportsEsports |
| 15 | +import androidx.compose.material3.* |
| 16 | +import androidx.compose.runtime.* |
| 17 | +import androidx.compose.ui.Modifier |
| 18 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 19 | +import androidx.compose.ui.text.font.FontWeight |
| 20 | +import androidx.compose.ui.unit.dp |
| 21 | +import androidx.compose.ui.unit.sp |
| 22 | +import com.joebuildsthings.gamedeck.screens.GamesScreen |
| 23 | +import com.joebuildsthings.gamedeck.screens.HomeScreen |
| 24 | +import com.joebuildsthings.gamedeck.screens.SettingsScreen |
| 25 | +import com.joebuildsthings.gamedeck.ui.theme.AccentRed |
| 26 | +import com.joebuildsthings.gamedeck.ui.theme.BgDark |
| 27 | +import com.joebuildsthings.gamedeck.ui.theme.GameDeckTheme |
| 28 | +import com.joebuildsthings.gamedeck.ui.theme.InkLight |
| 29 | +import com.joebuildsthings.gamedeck.ui.theme.LineDark |
| 30 | +import com.joebuildsthings.gamedeck.ui.theme.MutedGrey |
| 31 | + |
| 32 | +data class Tab(val label: String, val icon: ImageVector) |
| 33 | + |
| 34 | +class MainActivity : ComponentActivity() { |
| 35 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 36 | + super.onCreate(savedInstanceState) |
| 37 | + setContent { |
| 38 | + GameDeckTheme { |
| 39 | + Surface( |
| 40 | + modifier = Modifier.fillMaxSize(), |
| 41 | + color = BgDark |
| 42 | + ) { |
| 43 | + GameDeckApp( |
| 44 | + onRequestOverlay = { requestOverlayPermission() }, |
| 45 | + onToggleOverlay = { enable -> toggleOverlay(enable) } |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private fun requestOverlayPermission() { |
| 53 | + if (!Settings.canDrawOverlays(this)) { |
| 54 | + startActivity( |
| 55 | + Intent( |
| 56 | + Settings.ACTION_MANAGE_OVERLAY_PERMISSION, |
| 57 | + Uri.parse("package:$packageName") |
| 58 | + ) |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private fun toggleOverlay(enable: Boolean) { |
| 64 | + val intent = Intent(this, OverlayService::class.java).apply { |
| 65 | + action = if (enable) OverlayService.ACTION_START else OverlayService.ACTION_STOP |
| 66 | + } |
| 67 | + if (enable) startForegroundService(intent) else startService(intent) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@OptIn(ExperimentalMaterial3Api::class) |
| 72 | +@Composable |
| 73 | +fun GameDeckApp( |
| 74 | + onRequestOverlay: () -> Unit, |
| 75 | + onToggleOverlay: (Boolean) -> Unit, |
| 76 | +) { |
| 77 | + val tabs = listOf( |
| 78 | + Tab("Home", Icons.Filled.Home), |
| 79 | + Tab("Games", Icons.Filled.SportsEsports), |
| 80 | + Tab("Settings", Icons.Filled.Settings), |
| 81 | + ) |
| 82 | + var current by remember { mutableIntStateOf(0) } |
| 83 | + |
| 84 | + Scaffold( |
| 85 | + containerColor = BgDark, |
| 86 | + topBar = { |
| 87 | + TopAppBar( |
| 88 | + title = { |
| 89 | + Row(verticalAlignment = androidx.compose.ui.Alignment.CenterVertically) { |
| 90 | + Text( |
| 91 | + "GameDeck", |
| 92 | + color = InkLight, |
| 93 | + fontWeight = FontWeight.SemiBold, |
| 94 | + fontSize = 18.sp, |
| 95 | + ) |
| 96 | + Spacer(Modifier.width(10.dp)) |
| 97 | + Text( |
| 98 | + "v0.1", |
| 99 | + color = MutedGrey, |
| 100 | + fontSize = 11.sp, |
| 101 | + ) |
| 102 | + } |
| 103 | + }, |
| 104 | + colors = TopAppBarDefaults.topAppBarColors( |
| 105 | + containerColor = BgDark, |
| 106 | + ) |
| 107 | + ) |
| 108 | + }, |
| 109 | + bottomBar = { |
| 110 | + NavigationBar(containerColor = BgDark) { |
| 111 | + tabs.forEachIndexed { i, tab -> |
| 112 | + NavigationBarItem( |
| 113 | + selected = current == i, |
| 114 | + onClick = { current = i }, |
| 115 | + icon = { Icon(tab.icon, contentDescription = tab.label) }, |
| 116 | + label = { Text(tab.label, fontSize = 11.sp) }, |
| 117 | + colors = NavigationBarItemDefaults.colors( |
| 118 | + selectedIconColor = AccentRed, |
| 119 | + selectedTextColor = AccentRed, |
| 120 | + unselectedIconColor = MutedGrey, |
| 121 | + unselectedTextColor = MutedGrey, |
| 122 | + indicatorColor = LineDark, |
| 123 | + ) |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + ) { pad -> |
| 129 | + Box( |
| 130 | + Modifier |
| 131 | + .fillMaxSize() |
| 132 | + .background(BgDark) |
| 133 | + .padding(pad) |
| 134 | + ) { |
| 135 | + when (current) { |
| 136 | + 0 -> HomeScreen( |
| 137 | + onRequestOverlay = onRequestOverlay, |
| 138 | + onToggleOverlay = onToggleOverlay, |
| 139 | + ) |
| 140 | + 1 -> GamesScreen() |
| 141 | + 2 -> SettingsScreen(onRequestOverlay = onRequestOverlay) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments