home_screen.dart 16 KB

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