home_screen.dart 14 KB

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