home_screen.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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: 'Settings',
  89. subtitle: 'Theme & PIN',
  90. iconAsset: 'assets/icons/settings.svg',
  91. assigned: true,
  92. onTap: () => _openSettings(context),
  93. ),
  94. ShortcutSpec(
  95. title: 'All apps',
  96. subtitle: 'Installed apps',
  97. iconAsset: 'assets/icons/layout-grid.svg',
  98. assigned: true,
  99. onTap: () => _openAllApps(context),
  100. ),
  101. ];
  102. return PopScope(
  103. canPop: false,
  104. child: Scaffold(
  105. backgroundColor: colors.canvas,
  106. body: Stack(
  107. children: [
  108. Positioned.fill(
  109. child: DecoratedBox(
  110. decoration: homeBackground(
  111. colors,
  112. Theme.of(context).brightness,
  113. ),
  114. ),
  115. ),
  116. const _FirstRunLauncherTip(),
  117. Positioned(
  118. top: -190,
  119. right: -120,
  120. child: IgnorePointer(
  121. child: Container(
  122. width: 560,
  123. height: 560,
  124. decoration: BoxDecoration(
  125. shape: BoxShape.circle,
  126. gradient: RadialGradient(
  127. colors: [
  128. colors.accent.withValues(
  129. alpha: Theme.of(context).brightness == Brightness.dark
  130. ? 0.16
  131. : 0.10,
  132. ),
  133. colors.accent.withValues(alpha: 0),
  134. ],
  135. ),
  136. ),
  137. ),
  138. ),
  139. ),
  140. SafeArea(
  141. minimum: EdgeInsets.fromLTRB(
  142. horizontalPadding,
  143. compact ? 18 : 24,
  144. horizontalPadding,
  145. compact ? 14 : 18,
  146. ),
  147. child: SizedBox.expand(
  148. child: Column(
  149. crossAxisAlignment: CrossAxisAlignment.stretch,
  150. children: [
  151. Row(
  152. crossAxisAlignment: CrossAxisAlignment.start,
  153. children: [
  154. ClockBlock(
  155. compact: compact,
  156. temperatureText: temperatureText,
  157. temperatureAlert: temperatureAlert,
  158. ),
  159. const Spacer(),
  160. StatusCluster(
  161. status: system.status,
  162. compact: compact,
  163. showBattery: appearance.showBattery,
  164. ),
  165. ],
  166. ),
  167. SizedBox(height: compact ? 16 : 24),
  168. Expanded(
  169. child: Row(
  170. crossAxisAlignment: CrossAxisAlignment.stretch,
  171. children: [
  172. SizedBox(
  173. width: leftWidth,
  174. child: Column(
  175. crossAxisAlignment: CrossAxisAlignment.stretch,
  176. mainAxisAlignment: MainAxisAlignment.end,
  177. children: [
  178. if (obd.hasTelemetry) ...[
  179. ObdCard(
  180. speedKmh: obd.speedKmh,
  181. throttlePercent: obd.throttlePercent,
  182. economyL100: obd.economyL100,
  183. compact: compact,
  184. ),
  185. SizedBox(height: compact ? 12 : 14),
  186. ],
  187. VolumeCard(
  188. volume: system.volume,
  189. compact: compact,
  190. onSetVolume: system.setVolume,
  191. onToggleMute: system.toggleMute,
  192. ),
  193. ],
  194. ),
  195. ),
  196. SizedBox(width: gap),
  197. Expanded(
  198. child: Column(
  199. children: [
  200. Expanded(
  201. child: ShortcutGrid(
  202. shortcuts: shortcuts,
  203. compact: compact,
  204. ),
  205. ),
  206. if (!compact) ...[
  207. const SizedBox(height: 4),
  208. _HomeHint(
  209. text: launcher.pinnedApps.isEmpty
  210. ? 'Hold a shortcut to change the app'
  211. : 'Hold a slot to change · hold an added app for options',
  212. style: theme.textTheme.bodySmall?.copyWith(
  213. color: colors.inkFaint,
  214. ),
  215. ),
  216. ],
  217. ],
  218. ),
  219. ),
  220. ],
  221. ),
  222. ),
  223. ],
  224. ),
  225. ),
  226. ),
  227. ],
  228. ),
  229. ),
  230. );
  231. }
  232. Future<void> _openPinned(
  233. BuildContext context,
  234. SlotAssignment assignment,
  235. ) async {
  236. final error = await context.read<LauncherController>().launchAssignment(
  237. assignment,
  238. );
  239. if (error != null && context.mounted) {
  240. _showMessage(context, error);
  241. }
  242. }
  243. Future<void> _openSlot(BuildContext context, SlotType slot) async {
  244. final launcher = context.read<LauncherController>();
  245. if (launcher.assignmentFor(slot) == null) {
  246. await showAppPicker(context, slot);
  247. return;
  248. }
  249. final error = await launcher.launchSlot(slot);
  250. if (error != null && context.mounted) {
  251. _showMessage(context, error);
  252. }
  253. }
  254. void _openSettings(BuildContext context) {
  255. Navigator.of(context)
  256. .push(MaterialPageRoute<void>(builder: (_) => const SettingsScreen()));
  257. }
  258. void _openAllApps(BuildContext context) {
  259. Navigator.of(context)
  260. .push(MaterialPageRoute<void>(builder: (_) => const AllAppsScreen()));
  261. }
  262. void _showMessage(BuildContext context, String message) {
  263. final messenger = ScaffoldMessenger.of(context);
  264. messenger.hideCurrentSnackBar();
  265. messenger.showSnackBar(SnackBar(content: Text(message)));
  266. }
  267. }
  268. class _FirstRunLauncherTip extends StatefulWidget {
  269. const _FirstRunLauncherTip();
  270. @override
  271. State<_FirstRunLauncherTip> createState() => _FirstRunLauncherTipState();
  272. }
  273. class _FirstRunLauncherTipState extends State<_FirstRunLauncherTip> {
  274. @override
  275. void initState() {
  276. super.initState();
  277. WidgetsBinding.instance.addPostFrameCallback((_) async {
  278. if (!mounted) return;
  279. final launcher = context.read<LauncherController>();
  280. if (launcher.homeRoleHintShown) return;
  281. await launcher.markHomeRoleHintShown();
  282. if (!mounted) return;
  283. final messenger = ScaffoldMessenger.of(context);
  284. messenger.hideCurrentSnackBar();
  285. messenger.showSnackBar(
  286. const SnackBar(
  287. content: Text(
  288. 'You can set CarDash as your default launcher in Settings.',
  289. ),
  290. duration: Duration(seconds: 5),
  291. ),
  292. );
  293. });
  294. }
  295. @override
  296. Widget build(BuildContext context) => const SizedBox.shrink();
  297. }
  298. class _HomeHint extends StatefulWidget {
  299. const _HomeHint({required this.text, this.style});
  300. final String text;
  301. final TextStyle? style;
  302. @override
  303. State<_HomeHint> createState() => _HomeHintState();
  304. }
  305. class _HomeHintState extends State<_HomeHint> {
  306. Timer? _timer;
  307. bool _visible = true;
  308. @override
  309. void initState() {
  310. super.initState();
  311. final elapsed = DateTime.now().difference(AppClock.startedAt);
  312. final remaining = const Duration(minutes: 2) - elapsed;
  313. if (remaining <= Duration.zero) {
  314. _visible = false;
  315. } else {
  316. _timer = Timer(remaining, () {
  317. if (mounted) setState(() => _visible = false);
  318. });
  319. }
  320. }
  321. @override
  322. void dispose() {
  323. _timer?.cancel();
  324. super.dispose();
  325. }
  326. @override
  327. Widget build(BuildContext context) {
  328. return AnimatedSwitcher(
  329. duration: const Duration(milliseconds: 350),
  330. switchInCurve: Curves.easeOut,
  331. switchOutCurve: Curves.easeIn,
  332. child: _visible
  333. ? Text(
  334. widget.text,
  335. key: const ValueKey('home-hint'),
  336. textAlign: TextAlign.center,
  337. style: widget.style,
  338. )
  339. : const SizedBox.shrink(key: ValueKey('home-hint-gone')),
  340. );
  341. }
  342. }