Skip to content

Commit 9fdbb07

Browse files
committed
Added quick action to direclty launch app for adding a new weight, #468
1 parent a1b5952 commit 9fdbb07

9 files changed

Lines changed: 161 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1414

1515
## [Unreleased]
1616

17+
### Added Features and Improvements 🙌:
18+
- Added quick action to direclty launch app for adding a new weight
19+
1720
## [1.1.0] - 2026-06-14
1821

1922
### Added Features and Improvements 🙌:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<!-- Pink circle matching the app icon background; the launcher clips the
7+
shortcut icon to a circle anyway. -->
8+
<path
9+
android:fillColor="#FFFFF1EF"
10+
android:pathData="M12,0a12,12 0 1,0 0,24a12,12 0 1,0 0,-24z" />
11+
<!-- "+" glyph (Material add icon) in the icon's dark foreground tone,
12+
centred in the circle. -->
13+
<path
14+
android:fillColor="#FF03202E"
15+
android:pathData="M18,13h-5v5h-2v-5H6v-2h5V6h2v5h5z" />
16+
</vector>

app/lib/core/changelog.g.dart

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'package:quick_actions/quick_actions.dart';
2+
3+
/// Shortcut type identifier for the "add weight" home-screen app shortcut.
4+
const String addWeightShortcutType = 'action_add_weight';
5+
6+
/// Manages Android home-screen app shortcuts (long-press on the launcher icon).
7+
///
8+
/// Bridges the native shortcut callback to the Flutter UI: when the app is
9+
/// cold-started via a shortcut it stores a pending request that the home page
10+
/// consumes once it is ready; when the app is already running it forwards the
11+
/// tap to a registered live [handler].
12+
class QuickActionsService {
13+
/// Returns the singleton instance.
14+
factory QuickActionsService() => _instance;
15+
QuickActionsService._();
16+
static final QuickActionsService _instance = QuickActionsService._();
17+
18+
final QuickActions _quickActions = const QuickActions();
19+
20+
/// Whether an "add weight" shortcut was triggered but not yet handled.
21+
bool pendingAddWeight = false;
22+
23+
/// Live handler invoked when a shortcut is tapped while the app is running.
24+
void Function()? _handler;
25+
26+
/// Initialise the platform channel and register the shortcut callback.
27+
///
28+
/// Call once during app start-up. On cold start the callback fires before
29+
/// any UI exists, so the request is stored in [pendingAddWeight].
30+
void init() {
31+
_quickActions.initialize((String type) {
32+
if (type != addWeightShortcutType) {
33+
return;
34+
}
35+
final void Function()? handler = _handler;
36+
if (handler != null) {
37+
handler();
38+
} else {
39+
pendingAddWeight = true;
40+
}
41+
});
42+
}
43+
44+
/// Register the home-screen shortcut items.
45+
///
46+
/// Call from a context where a localized [title] is available so the entry
47+
/// in the launcher menu is translated.
48+
Future<void> setShortcuts({required String title}) async {
49+
await _quickActions.setShortcutItems(<ShortcutItem>[
50+
ShortcutItem(
51+
type: addWeightShortcutType,
52+
localizedTitle: title,
53+
icon: 'ic_shortcut_add',
54+
),
55+
]);
56+
}
57+
58+
/// Register a live [handler] for shortcut taps and flush any pending request.
59+
void registerHandler(void Function() handler) {
60+
_handler = handler;
61+
if (pendingAddWeight) {
62+
pendingAddWeight = false;
63+
handler();
64+
}
65+
}
66+
67+
/// Remove the live handler, e.g. when the consuming widget is disposed.
68+
void unregisterHandler() {
69+
_handler = null;
70+
}
71+
}

app/lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:trale/core/l10n_extension.dart';
66
import 'package:trale/core/language.dart';
77
import 'package:trale/core/measurement.dart';
88
import 'package:trale/core/notification_service.dart';
9+
import 'package:trale/core/quick_actions_service.dart';
910
import 'package:trale/core/trale_notifier.dart';
1011
import 'package:trale/pages/splash.dart';
1112

@@ -18,6 +19,9 @@ Future<void> main() async {
1819
// Populate QPLanguage.supportedLanguages from AppLocalizations.
1920
initLanguages();
2021

22+
// Register the home-screen app shortcut callback (long-press launcher icon).
23+
QuickActionsService().init();
24+
2125
runApp(
2226
QPApp<TraleNotifier>(
2327
notifier: TraleNotifier(),

app/lib/pages/home.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:trale/core/l10n_extension.dart';
66
import 'package:trale/core/measurement.dart';
77
import 'package:trale/core/measurement_database.dart';
88
import 'package:trale/core/preferences.dart';
9+
import 'package:trale/core/quick_actions_service.dart';
910
import 'package:trale/pages/measurement_screen.dart';
1011
import 'package:trale/pages/overview.dart';
1112
import 'package:trale/pages/settings_overview.dart';
@@ -25,6 +26,31 @@ class Home extends StatefulWidget {
2526
class _HomeState extends State<Home> {
2627
bool _popupShown = false;
2728

29+
@override
30+
void initState() {
31+
super.initState();
32+
// Open the add-weight dialog when launched via the home-screen shortcut.
33+
// Handles the warm-start case (app already running) and flushes any
34+
// pending request stored during cold start.
35+
QuickActionsService().registerHandler(_onShortcut);
36+
}
37+
38+
@override
39+
void dispose() {
40+
QuickActionsService().unregisterHandler();
41+
super.dispose();
42+
}
43+
44+
void _onShortcut() {
45+
// Defer to the next frame so the navigator/overlay is ready to host the
46+
// dialog (the handler may fire during initState on cold start).
47+
WidgetsBinding.instance.addPostFrameCallback((_) {
48+
if (mounted && !_popupShown) {
49+
_onFABPressed();
50+
}
51+
});
52+
}
53+
2854
Future<void> _onFABPressed() async {
2955
final MeasurementDatabase database = MeasurementDatabase();
3056
final List<SortedMeasurement> measurements = database.sortedMeasurements;

app/lib/pages/splash.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:quantumphysique/quantumphysique.dart';
33

44
import 'package:trale/core/measurement_database.dart';
55
import 'package:trale/core/notification_service.dart';
6+
import 'package:trale/core/quick_actions_service.dart';
67
import 'package:trale/l10n-gen/app_localizations.dart';
78
import 'package:trale/pages/home.dart';
89

@@ -28,6 +29,8 @@ class Splash extends StatelessWidget {
2829
title: l10n.reminderNotificationTitle,
2930
body: l10n.reminderNotificationBody,
3031
);
32+
// Register the (translated) home-screen shortcut entry.
33+
QuickActionsService().setShortcuts(title: l10n.addWeight);
3134
}
3235
},
3336
homeBuilder: (_) => const Home(),

app/pubspec.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,38 @@ packages:
788788
relative: true
789789
source: path
790790
version: "0.1.0"
791+
quick_actions:
792+
dependency: "direct main"
793+
description:
794+
name: quick_actions
795+
sha256: "7e35dd6a21f5bbd21acf6899039eaf85001a5ac26d52cbd6a8a2814505b90798"
796+
url: "https://pub.dev"
797+
source: hosted
798+
version: "1.1.0"
799+
quick_actions_android:
800+
dependency: transitive
801+
description:
802+
name: quick_actions_android
803+
sha256: af50d52d882a0f520c4be082636b5177b3de239bd468857d5da0b5a6eec61e07
804+
url: "https://pub.dev"
805+
source: hosted
806+
version: "1.0.32"
807+
quick_actions_ios:
808+
dependency: transitive
809+
description:
810+
name: quick_actions_ios
811+
sha256: be1496e7ca1debc86d9ea08e56325649fbc5abb2b6930690c97ba0dae59992b1
812+
url: "https://pub.dev"
813+
source: hosted
814+
version: "1.2.4"
815+
quick_actions_platform_interface:
816+
dependency: transitive
817+
description:
818+
name: quick_actions_platform_interface
819+
sha256: "1fec7068db5122cd019e9340d3d7be5d36eab099695ef3402c7059ee058329a4"
820+
url: "https://pub.dev"
821+
source: hosted
822+
version: "1.1.0"
791823
quiver:
792824
dependency: transitive
793825
description:

app/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies:
4242
provider: ^6.1.5+1
4343
quantumphysique:
4444
path: ../quantumphysique
45+
quick_actions: ^1.1.0
4546
share_plus: ^12.0.2
4647
shared_preferences: ^2.5.5
4748
timezone: ^0.11.0

0 commit comments

Comments
 (0)