home_screen.dart 15 KB

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