Skip to content

Commit

Permalink
Updated vibration to version 3.1.1
Browse files Browse the repository at this point in the history
Fixed toast not working on every page because of BuildContext.
  • Loading branch information
jeroen1602 committed Jan 26, 2025
1 parent 1d06b1d commit 9a74450
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 70 deletions.
5 changes: 2 additions & 3 deletions lib/lighthouse_provider/widgets/lighthouse_metadata_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ class _MetadataInkWell extends StatelessWidget {
if (value != null) {
Clipboard.setData(ClipboardData(text: value!));
}
if (await Vibration.hasVibrator() ?? false) {
Vibration.vibrate(duration: 200);
}
Vibration.vibrate(duration: 200);
ToastContext().init(context);
Toast.show('Copied to clipboard',
duration: Toast.lengthShort, gravity: Toast.bottom);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ class UnknownStateHelpOutAlertWidget extends StatelessWidget {
return CustomLongPressGestureRecognizer()
..onLongPress = () async {
Clipboard.setData(ClipboardData(text: _getClipboardString(version)));
if (await Vibration.hasVibrator() ?? false) {
Vibration.vibrate(duration: 200);
}
Vibration.vibrate(duration: 200);
ToastContext().init(context);
Toast.show('Copied to clipboard',
duration: Toast.lengthShort, gravity: Toast.bottom);
};
Expand Down
2 changes: 0 additions & 2 deletions lib/pages/base_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:lighthouse_pm/platform_specific/mobile/android/android_launcher_shortcut/android_launcher_shortcut.dart';
import 'package:lighthouse_pm/widgets/content_container_widget.dart';
import 'package:shared_platform/shared_platform.dart';
import 'package:toast/toast.dart';

/// The same as a [WidgetBuilder] only require it to return a [BasePage].
typedef PageBuilder = BasePage Function(BuildContext context);
Expand All @@ -21,7 +20,6 @@ abstract class BasePage extends StatelessWidget {

@override
Widget build(final BuildContext context) {
ToastContext().init(context);
ContentScrollbar.updateShowScrollbarSubject(context);
return _ShortcutLaunchHandleWidget(
buildPage(context), shortcutHandleArgument, replace);
Expand Down
20 changes: 16 additions & 4 deletions lib/pages/database/group_dao_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ class _GroupConverter extends DaoTableDataConverter<Group> {
final String? value =
(decorators[1] as DaoDataCreateAlertStringDecorator).getNewValue();
if (value == null) {
Toast.show('No name set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No name set!');
}
return;
}
if (id == null) {
Expand Down Expand Up @@ -105,7 +108,10 @@ class _GroupEntryConverter extends DaoTableDataConverter<GroupEntry> {
}
final intValue = int.tryParse(newValue, radix: 10);
if (intValue == null || intValue < 0) {
Toast.show('new group id must be a number and cam\'t be negative');
if (context.mounted) {
ToastContext().init(context);
Toast.show('new group id must be a number and cam\'t be negative');
}
return;
}
await bloc.groups.insertGroupEntry(
Expand All @@ -131,11 +137,17 @@ class _GroupEntryConverter extends DaoTableDataConverter<GroupEntry> {
deviceId = deviceId?.trim().toUpperCase();
}
if (groupId == null) {
Toast.show('No group id set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No group id set!');
}
return;
}
if (deviceId == null) {
Toast.show('No device id set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No device id set!');
}
return;
}
await bloc.groups
Expand Down
20 changes: 16 additions & 4 deletions lib/pages/database/nickname_dao_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ class _NicknameConverter extends DaoTableDataConverter<Nickname> {
final String? value =
(decorators[1] as DaoDataCreateAlertStringDecorator).getNewValue();
if (deviceId == null) {
Toast.show('No device id set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No device id set!');
}
return;
}
if (value == null) {
Toast.show('No nickname set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No nickname set!');
}
return;
}
await bloc.nicknames
Expand Down Expand Up @@ -113,7 +119,10 @@ class _LastSeenConverter extends DaoTableDataConverter<LastSeenDevice> {
await bloc.nicknames.insertLastSeenDevice(LastSeenDevicesCompanion.insert(
deviceId: data.deviceId, lastSeen: drift.Value(newData)));
} on FormatException {
Toast.show('That didn\'t work');
if (context.mounted) {
ToastContext().init(context);
Toast.show('That didn\'t work');
}
}
}

Expand All @@ -138,7 +147,10 @@ class _LastSeenConverter extends DaoTableDataConverter<LastSeenDevice> {
final String? value =
(decorators[1] as DaoDataCreateAlertStringDecorator).getNewValue();
if (deviceId == null) {
Toast.show('No device id set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No device id set!');
}
return;
}
DateTime? date;
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/database/settings_dao_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ class _SimpleSettingConverter extends DaoTableDataConverter<SimpleSetting> {
final String? value =
(decorators[1] as DaoDataCreateAlertStringDecorator).getNewValue();
if (id == null) {
Toast.show('No id set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No id set!');
}
return;
}
await bloc.settings
Expand Down
20 changes: 16 additions & 4 deletions lib/pages/database/vive_base_station_dao_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class _ViveBaseStationIdConverter
final numberValue = int.parse(newValue, radix: 16);
await bloc.viveBaseStation.insertIdNoValidate(data.deviceId, numberValue);
} on FormatException {
Toast.show('Could not convert "$newValue" to a hex number');
if (context.mounted) {
ToastContext().init(context);
Toast.show('Could not convert "$newValue" to a hex number');
}
}
}

Expand All @@ -73,18 +76,27 @@ class _ViveBaseStationIdConverter
final String? newValueString =
(decorators[1] as DaoDataCreateAlertStringDecorator).getNewValue();
if (deviceId == null) {
Toast.show('No device id set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No device id set!');
}
return;
}
if (newValueString == null) {
Toast.show('No base station id set!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('No base station id set!');
}
return;
}
try {
final numberValue = int.parse(newValueString, radix: 16);
await bloc.viveBaseStation.insertIdNoValidate(deviceId, numberValue);
} on FormatException {
Toast.show('Could not convert "$newValueString" to a hex number');
if (context.mounted) {
ToastContext().init(context);
Toast.show('Could not convert "$newValueString" to a hex number');
}
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions lib/pages/database_test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ class DatabaseTestPage extends BasePage with WithBlocStateless {
subtitle: Text(_getTables(bloc)),
onLongPress: () async {
await Clipboard.setData(ClipboardData(text: _getTables(bloc)));
Toast.show('Copied to clipboard',
duration: Toast.lengthShort, gravity: Toast.bottom);
if (context.mounted) {
ToastContext().init(context);
Toast.show('Copied to clipboard',
duration: Toast.lengthShort, gravity: Toast.bottom);
}
},
isThreeLine: true,
),
Expand All @@ -75,8 +78,11 @@ class DatabaseTestPage extends BasePage with WithBlocStateless {
subtitle: Text(data),
onLongPress: () async {
await Clipboard.setData(ClipboardData(text: data));
Toast.show('Copied to clipboard',
duration: Toast.lengthShort, gravity: Toast.bottom);
if (context.mounted) {
ToastContext().init(context);
Toast.show('Copied to clipboard',
duration: Toast.lengthShort, gravity: Toast.bottom);
}
},
);
} else {
Expand All @@ -87,8 +93,12 @@ class DatabaseTestPage extends BasePage with WithBlocStateless {
subtitle: Text(data),
onLongPress: () async {
await Clipboard.setData(ClipboardData(text: data));
Toast.show('Copied to clipboard',
duration: Toast.lengthShort, gravity: Toast.bottom);
if (context.mounted) {
ToastContext().init(context);
Toast.show('Copied to clipboard',
duration: Toast.lengthShort,
gravity: Toast.bottom);
}
},
),
const Divider(),
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/log_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class _LogPageContentState extends State<_LogPageContent> {
});
Clipboard.setData(ClipboardData(text: clipboard))
.then((final _) {
Toast.show("Copied to clipboard");
if (context.mounted) {
ToastContext().init(context);
Toast.show("Copied to clipboard");
}
});
},
icon: const Icon(Icons.copy)),
Expand Down
19 changes: 14 additions & 5 deletions lib/pages/settings/settings_nicknames_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ class _NicknamesPageState extends State<_SettingsNicknamesPageContent> {
setState(() {
selected.clear();
});
Toast.show('Nicknames have been removed');
if (context.mounted) {
ToastContext().init(context);
Toast.show('Nicknames have been removed');
}
},
)
];
Expand Down Expand Up @@ -230,8 +233,11 @@ class _EmptyNicknameState extends State<_EmptyNicknamePage> {
tapCounter++;
}
if (tapCounter < _tapTop && tapCounter > _tapTop - 3) {
Toast.show(
'Just ${_tapTop - tapCounter} left until a fake nicknames are created');
if (context.mounted) {
ToastContext().init(context);
Toast.show(
'Just ${_tapTop - tapCounter} left until a fake nicknames are created');
}
}
if (tapCounter == _tapTop) {
blocWithoutListen.nicknames.insertNickname(Nickname(
Expand All @@ -254,8 +260,11 @@ class _EmptyNicknameState extends State<_EmptyNicknamePage> {
0xFFFFFFFC)
.toString(),
nickname: "This is a test nickname4"));
Toast.show('Fake nickname created!',
duration: Toast.lengthShort);
if (context.mounted) {
ToastContext().init(context);
Toast.show('Fake nickname created!',
duration: Toast.lengthShort);
}
tapCounter++;
}
},
Expand Down
17 changes: 13 additions & 4 deletions lib/pages/settings/settings_vive_base_station_ids_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class _SettingsViveBaseStationIdsPageState
setState(() {
selected.clear();
});
Toast.show('Ids have been removed!');
if (context.mounted) {
ToastContext().init(context);
Toast.show('Ids have been removed!');
}
},
)
];
Expand Down Expand Up @@ -191,8 +194,11 @@ class _EmptyState extends State<_EmptyPage> {
tapCounter++;
}
if (tapCounter < _tapTop && tapCounter > _tapTop - 3) {
Toast.show(
'Just ${_tapTop - tapCounter} left until a fake ids are created');
if (context.mounted) {
ToastContext().init(context);
Toast.show(
'Just ${_tapTop - tapCounter} left until a fake ids are created');
}
}
if (tapCounter == _tapTop) {
blocWithoutListen.viveBaseStation.insertId(
Expand All @@ -211,7 +217,10 @@ class _EmptyState extends State<_EmptyPage> {
FakeDeviceIdentifier.generateDeviceIdentifier(0xFFFFFFFC)
.toString(),
0xFFFFFFFC);
Toast.show('Fake ids created!', duration: Toast.lengthShort);
if (context.mounted) {
ToastContext().init(context);
Toast.show('Fake ids created!', duration: Toast.lengthShort);
}
tapCounter++;
}
},
Expand Down
Loading

0 comments on commit 9a74450

Please sign in to comment.