home_screen.dart 17 KB

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