home_screen.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:provider/provider.dart';
  4. import '../../core/theme/app_colors.dart';
  5. import '../../core/theme/theme_controller.dart';
  6. import '../../data/models/slot_assignment.dart';
  7. import '../../data/models/slot_type.dart';
  8. import '../apps/all_apps_screen.dart';
  9. import '../apps/app_picker_sheet.dart';
  10. import '../settings/settings_screen.dart';
  11. import 'gps_speed_controller.dart';
  12. import 'launcher_controller.dart';
  13. import 'obd_controller.dart';
  14. import 'pinned_app_actions.dart';
  15. import 'system_controller.dart';
  16. import 'telemetry_settings_controller.dart';
  17. import 'weather_controller.dart';
  18. import '../settings/update_controller.dart';
  19. import 'widgets/clock_block.dart';
  20. import 'widgets/gps_speed_card.dart';
  21. import 'widgets/home_hint.dart';
  22. import 'widgets/night_mode_overlay.dart';
  23. import 'widgets/obd_card.dart';
  24. import 'widgets/shortcut_grid.dart';
  25. import 'widgets/status_cluster.dart';
  26. import 'widgets/volume_card.dart';
  27. /// The only home page. No app bar, no dock, no pages: information, the main
  28. /// volume control, and up to 16 configurable shortcuts.
  29. class HomeScreen extends StatefulWidget {
  30. const HomeScreen({super.key});
  31. @override
  32. State<HomeScreen> createState() => _HomeScreenState();
  33. }
  34. class _HomeScreenState extends State<HomeScreen> {
  35. bool _nightMode = false;
  36. void _enterNightMode() {
  37. setState(() => _nightMode = true);
  38. }
  39. void _leaveNightMode() {
  40. setState(() => _nightMode = false);
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. final colors = context.colors;
  45. final theme = Theme.of(context);
  46. final launcher = context.watch<LauncherController>();
  47. final system = context.watch<SystemController>();
  48. final appearance = context.watch<ThemeController>();
  49. final obd = context.watch<ObdController>();
  50. final gps = context.watch<GpsSpeedController>();
  51. final weather = context.watch<WeatherController>();
  52. final telemetrySettings = context.watch<TelemetrySettingsController>();
  53. final update = context.watch<UpdateController>();
  54. final screen = MediaQuery.sizeOf(context);
  55. final compact = screen.height < 700 || screen.width < 1100;
  56. final horizontalPadding = compact ? 30.0 : 44.0;
  57. final denseHome = launcher.homeButtonCount > 8;
  58. final leftWidth = denseHome
  59. ? (screen.width * 0.31).clamp(300.0, 400.0)
  60. : (screen.width * 0.37).clamp(300.0, 440.0);
  61. final gap = compact ? 26.0 : 40.0;
  62. final obdTemperature = obd.ambientCelsius;
  63. final useObdTemperature =
  64. telemetrySettings.temperatureUseObd && obdTemperature != null;
  65. final temperature = !telemetrySettings.temperatureEnabled
  66. ? null
  67. : useObdTemperature
  68. ? obdTemperature
  69. : (weather.hasTemperature ? weather.temperatureCelsius : null);
  70. final temperatureText = temperature == null
  71. ? null
  72. : useObdTemperature
  73. ? 'Outside, ${temperature.round()}°C'
  74. : '${weather.locationName}, ${temperature.round()}°C';
  75. final temperatureAlert = temperature != null && temperature < 3;
  76. final showObdSpeed =
  77. telemetrySettings.speedEnabled &&
  78. telemetrySettings.speedUseObd &&
  79. obd.hasTelemetry;
  80. final showGpsSpeed =
  81. telemetrySettings.speedEnabled && !showObdSpeed && gps.hasSpeed;
  82. ShortcutSpec slotSpec(SlotType slot) {
  83. final assignment = launcher.assignmentFor(slot);
  84. final playing =
  85. slot == SlotType.music && system.isMusicPlaying && assignment != null;
  86. return ShortcutSpec(
  87. title: slot.label,
  88. subtitle: assignment?.label ?? 'Select app',
  89. iconAsset: slot.iconAsset,
  90. showEqualizer: playing,
  91. assigned: assignment != null,
  92. onTap: () => _openSlot(context, slot),
  93. onLongPress: () => showAppPicker(context, slot),
  94. longPressHint: 'change the app',
  95. );
  96. }
  97. ShortcutSpec pinnedSpec(SlotAssignment pinned) {
  98. final app = launcher.appForAssignment(pinned);
  99. final customIcon = pinned.iconAsset;
  100. return ShortcutSpec(
  101. title: pinned.label.isEmpty ? pinned.packageName : pinned.label,
  102. subtitle: null,
  103. iconAsset: customIcon ?? 'assets/icons/app-window.svg',
  104. iconBytes: customIcon == null ? app?.icon : null,
  105. assigned: true,
  106. onTap: () => _openPinned(context, pinned),
  107. onLongPress: () => showPinnedAppMenu(context, pinned),
  108. longPressHint: 'open app options',
  109. );
  110. }
  111. final shortcuts = <ShortcutSpec>[
  112. for (final slot in SlotType.values)
  113. if (launcher.isSlotEnabled(slot)) slotSpec(slot),
  114. for (final pinned in launcher.pinnedApps) pinnedSpec(pinned),
  115. ShortcutSpec(
  116. title: 'All apps',
  117. subtitle: 'Installed apps',
  118. iconAsset: 'assets/icons/layout-grid.svg',
  119. assigned: true,
  120. onTap: () => _openAllApps(context),
  121. ),
  122. ];
  123. return PopScope(
  124. canPop: false,
  125. child: Scaffold(
  126. backgroundColor: colors.canvas,
  127. body: Stack(
  128. children: [
  129. Positioned.fill(
  130. child: DecoratedBox(
  131. decoration: homeBackground(
  132. colors,
  133. Theme.of(context).brightness,
  134. ),
  135. ),
  136. ),
  137. if (!_nightMode)
  138. Positioned.fill(
  139. child: GestureDetector(
  140. behavior: HitTestBehavior.translucent,
  141. onLongPress: _enterNightMode,
  142. ),
  143. ),
  144. const _FirstRunLauncherTip(),
  145. Positioned(
  146. top: -190,
  147. right: -120,
  148. child: IgnorePointer(
  149. child: Container(
  150. width: 560,
  151. height: 560,
  152. decoration: BoxDecoration(
  153. shape: BoxShape.circle,
  154. gradient: RadialGradient(
  155. colors: [
  156. colors.accent.withValues(
  157. alpha: Theme.of(context).brightness == Brightness.dark
  158. ? 0.16
  159. : 0.10,
  160. ),
  161. colors.accent.withValues(alpha: 0),
  162. ],
  163. ),
  164. ),
  165. ),
  166. ),
  167. ),
  168. SafeArea(
  169. minimum: EdgeInsets.fromLTRB(
  170. horizontalPadding,
  171. compact ? 18 : 24,
  172. horizontalPadding,
  173. compact ? 14 : 18,
  174. ),
  175. child: SizedBox.expand(
  176. child: Column(
  177. crossAxisAlignment: CrossAxisAlignment.stretch,
  178. children: [
  179. Row(
  180. crossAxisAlignment: CrossAxisAlignment.start,
  181. children: [
  182. ClockBlock(
  183. compact: compact,
  184. temperatureText: temperatureText,
  185. temperatureAlert: temperatureAlert,
  186. ),
  187. const Spacer(),
  188. StatusCluster(
  189. status: system.status,
  190. compact: compact,
  191. showBattery: appearance.showBattery,
  192. onOpenSettings: () => _openSettings(context),
  193. showUpdateAvailable: update.updateAvailable,
  194. ),
  195. ],
  196. ),
  197. SizedBox(height: compact ? 16 : 24),
  198. Expanded(
  199. child: Row(
  200. crossAxisAlignment: CrossAxisAlignment.stretch,
  201. children: [
  202. SizedBox(
  203. width: leftWidth,
  204. child: Column(
  205. crossAxisAlignment: CrossAxisAlignment.stretch,
  206. mainAxisAlignment: MainAxisAlignment.end,
  207. children: [
  208. if (showObdSpeed) ...[
  209. ObdCard(
  210. speedKmh: obd.speedKmh,
  211. throttlePercent: obd.throttlePercent,
  212. economyL100: obd.economyL100,
  213. compact: compact,
  214. ),
  215. SizedBox(height: compact ? 12 : 14),
  216. ] else if (showGpsSpeed) ...[
  217. GpsSpeedCard(
  218. speedKmh: gps.speedKmh ?? 0,
  219. compact: compact,
  220. ),
  221. SizedBox(height: compact ? 12 : 14),
  222. ],
  223. VolumeCard(
  224. volume: system.volume,
  225. compact: compact,
  226. onSetVolume: system.setVolume,
  227. onToggleMute: system.toggleMute,
  228. isMusicPlaying: system.isMusicPlaying,
  229. onPlayPause: () =>
  230. unawaited(_toggleMusic(context)),
  231. media: system.media,
  232. diagnostics: system.volumeDiagnostics,
  233. onToggleDiagnostics:
  234. system.toggleVolumeDiagnostics,
  235. ),
  236. ],
  237. ),
  238. ),
  239. SizedBox(width: gap),
  240. Expanded(
  241. child: Column(
  242. children: [
  243. Expanded(
  244. child: ShortcutGrid(
  245. shortcuts: shortcuts,
  246. compact: compact,
  247. ),
  248. ),
  249. if (!compact &&
  250. launcher.firstLaunchAt != null) ...[
  251. const SizedBox(height: 4),
  252. HomeHint(
  253. firstLaunchAt: launcher.firstLaunchAt!,
  254. text: launcher.pinnedApps.isEmpty
  255. ? 'Hold a shortcut to change the app'
  256. : 'Hold a slot to change · hold an added app for options',
  257. style: theme.textTheme.bodySmall?.copyWith(
  258. color: colors.inkFaint,
  259. ),
  260. ),
  261. ],
  262. ],
  263. ),
  264. ),
  265. ],
  266. ),
  267. ),
  268. ],
  269. ),
  270. ),
  271. ),
  272. if (_nightMode)
  273. Positioned.fill(
  274. child: NightModeOverlay(onDismiss: _leaveNightMode),
  275. ),
  276. ],
  277. ),
  278. ),
  279. );
  280. }
  281. Future<void> _openPinned(
  282. BuildContext context,
  283. SlotAssignment assignment,
  284. ) async {
  285. final error = await context.read<LauncherController>().launchAssignment(
  286. assignment,
  287. );
  288. if (error != null && context.mounted) {
  289. _showMessage(context, error);
  290. }
  291. }
  292. Future<void> _toggleMusic(BuildContext context) async {
  293. final system = context.read<SystemController>();
  294. final launcher = context.read<LauncherController>();
  295. if (system.isMusicPlaying) {
  296. await system.pauseMusic();
  297. return;
  298. }
  299. // A media play key resumes a paused session. If nothing responds, open the
  300. // app assigned to the Music slot (or ask the user to choose one).
  301. final started = await system.playMusic();
  302. if (started || !context.mounted) return;
  303. final assignment = launcher.assignmentFor(SlotType.music);
  304. if (assignment == null) {
  305. await showAppPicker(context, SlotType.music);
  306. return;
  307. }
  308. final error = await launcher.launchSlot(SlotType.music);
  309. if (error != null && context.mounted) {
  310. _showMessage(context, error);
  311. }
  312. }
  313. Future<void> _openSlot(BuildContext context, SlotType slot) async {
  314. final launcher = context.read<LauncherController>();
  315. if (launcher.assignmentFor(slot) == null) {
  316. await showAppPicker(context, slot);
  317. return;
  318. }
  319. final error = await launcher.launchSlot(slot);
  320. if (error != null && context.mounted) {
  321. _showMessage(context, error);
  322. }
  323. }
  324. void _openSettings(BuildContext context) {
  325. Navigator.of(context)
  326. .push(MaterialPageRoute<void>(builder: (_) => const SettingsScreen()));
  327. }
  328. void _openAllApps(BuildContext context) {
  329. Navigator.of(context)
  330. .push(MaterialPageRoute<void>(builder: (_) => const AllAppsScreen()));
  331. }
  332. void _showMessage(BuildContext context, String message) {
  333. final messenger = ScaffoldMessenger.of(context);
  334. messenger.hideCurrentSnackBar();
  335. messenger.showSnackBar(SnackBar(content: Text(message)));
  336. }
  337. }
  338. class _FirstRunLauncherTip extends StatefulWidget {
  339. const _FirstRunLauncherTip();
  340. @override
  341. State<_FirstRunLauncherTip> createState() => _FirstRunLauncherTipState();
  342. }
  343. class _FirstRunLauncherTipState extends State<_FirstRunLauncherTip> {
  344. @override
  345. void initState() {
  346. super.initState();
  347. WidgetsBinding.instance.addPostFrameCallback((_) async {
  348. if (!mounted) return;
  349. final launcher = context.read<LauncherController>();
  350. if (launcher.homeRoleHintShown) return;
  351. await launcher.markHomeRoleHintShown();
  352. if (!mounted) return;
  353. final messenger = ScaffoldMessenger.of(context);
  354. messenger.hideCurrentSnackBar();
  355. messenger.showSnackBar(
  356. const SnackBar(
  357. content: Text(
  358. 'You can set CarDash as your default launcher in Settings.',
  359. ),
  360. duration: Duration(seconds: 5),
  361. ),
  362. );
  363. });
  364. }
  365. @override
  366. Widget build(BuildContext context) => const SizedBox.shrink();
  367. }