home_screen.dart 14 KB

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