home_screen.dart 16 KB

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