home_screen.dart 13 KB

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