Skip to content

Commit

Permalink
fix: Fix alarm checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Sep 18, 2023
1 parent ceca7aa commit abb57de
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 118 deletions.
65 changes: 39 additions & 26 deletions lib/api/get-locations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ VoidCallback getLocations({
required void Function(LocationPointService) onLocationFetched,
required void Function() onEnd,
final VoidCallback? onEmptyEnd,
final VoidCallback? onError,
int? limit,
DateTime? from,
DateTime? until,
Expand All @@ -26,7 +27,7 @@ VoidCallback getLocations({
authors: [nostrPublicKey],
limit: limit,
until:
until == null ? null : (until.millisecondsSinceEpoch / 1000).floor(),
until == null ? null : (until.millisecondsSinceEpoch / 1000).floor(),
since: from == null ? null : (from.millisecondsSinceEpoch / 1000).floor(),
),
]);
Expand All @@ -44,31 +45,22 @@ VoidCallback getLocations({
"New message. Decrypting...",
);

try {
final location = await LocationPointService.fromEncrypted(
message.message.content,
encryptionPassword,
);

FlutterLogs.logInfo(
LOG_TAG,
"GetLocations",
"New message. Decrypting... Done!",
);

onLocationFetched(location);
} catch (error) {
FlutterLogs.logError(
LOG_TAG,
"GetLocations",
"Error decrypting message: $error",
);

return;
}
final location = await LocationPointService.fromEncrypted(
message.message.content,
encryptionPassword,
);

FlutterLogs.logInfo(
LOG_TAG,
"GetLocations",
"New message. Decrypting... Done!",
);

onLocationFetched(location);
},
onEnd: onEnd,
onEmptyEnd: onEmptyEnd,
onError: onError,
);
}

Expand All @@ -82,8 +74,29 @@ Future<List<LocationPointService>> getLocationsAsFuture({
}) async {
final completer = Completer<List<LocationPointService>>();

int doneAmount = 0;
final List<LocationPointService> locations = [];

final onAnyEnd = () {
doneAmount++;

FlutterLogs.logInfo(
LOG_TAG,
"GetLocations",
"Relay done! $doneAmount / ${relays.length}",
);

if (doneAmount == relays.length) {
FlutterLogs.logInfo(
LOG_TAG,
"GetLocations",
"All relays done!",
);

completer.complete(locations);
}
};

getLocations(
nostrPublicKey: nostrPublicKey,
encryptionPassword: encryptionPassword,
Expand All @@ -94,9 +107,9 @@ Future<List<LocationPointService>> getLocationsAsFuture({
onLocationFetched: (final LocationPointService location) {
locations.add(location);
},
onEnd: () {
completer.complete(locations);
},
onEnd: onAnyEnd,
onEmptyEnd: onAnyEnd,
onError: onAnyEnd,
);

return completer.future;
Expand Down
3 changes: 3 additions & 0 deletions lib/api/nostr-fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class NostrFetch {
required final Future<void> Function(Message message, String relay) onEvent,
required final void Function() onEnd,
final void Function()? onEmptyEnd,
final VoidCallback? onError,
}) {
final List<WebSocket> sockets = [];

Expand Down Expand Up @@ -136,6 +137,8 @@ class NostrFetch {
"Nostr Socket",
"Error for socket: $error",
);

onError?.call();
});
}

Expand Down
27 changes: 23 additions & 4 deletions lib/screens/view_alarm_screen_widgets/ViewAlarmScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import 'package:locus/widgets/ModalSheet.dart';
import 'package:locus/widgets/ModalSheetContent.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:provider/provider.dart';
import 'package:locus/services/manager_service/helpers.dart';
import 'package:locus/services/current_location_service.dart';

import '../../models/log.dart';
import '../../utils/PageRoute.dart';
Expand Down Expand Up @@ -365,10 +367,27 @@ class _ViewAlarmScreenState extends State<ViewAlarmScreen> {
child: Text(l10n.location_manageAlarms_addNewAlarm_actionLabel),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: MEDIUM_SPACE),
child: Text(l10n.location_manageAlarms_lastCheck_description(
widget.view.lastAlarmCheck)),
GestureDetector(
onTap: () async {
final l10n = AppLocalizations.of(context);
final views = context.read<ViewService>();
final currentLocation = context.read<CurrentLocationService>();

checkViewAlarms(
l10n: l10n,
views: views.viewsWithAlarms,
viewService: views,
userLocation: await LocationPointService.fromPosition(
currentLocation.currentPosition!),
);
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: MEDIUM_SPACE),
child: Text(
l10n.location_manageAlarms_lastCheck_description(
widget.view.lastAlarmCheck),
),
),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class _ViewAlarmSelectRadiusBasedScreenState
final zoom = _hasSetInitialPosition
? (16 - log(position.accuracy / 200) / log(2)).toDouble()
: flutterMapController?.zoom ??
(await appleMapController?.getZoomLevel()) ??
16.0;
(await appleMapController?.getZoomLevel()) ??
16.0;

flutterMapController?.move(
LatLng(position.latitude, position.longitude),
Expand All @@ -151,39 +151,38 @@ class _ViewAlarmSelectRadiusBasedScreenState

showHelperSheet(
context: context,
builder: (context) =>
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(l10n.location_addAlarm_radiusBased_help_description),
const SizedBox(height: MEDIUM_SPACE),
if (widget.type == LocationAlarmType.geo) ...[
Row(
children: <Widget>[
const Icon(Icons.touch_app_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_geo_help_tapDescription,
),
),
],
builder: (context) => Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(l10n.location_addAlarm_radiusBased_help_description),
const SizedBox(height: MEDIUM_SPACE),
if (widget.type == LocationAlarmType.geo) ...[
Row(
children: <Widget>[
const Icon(Icons.touch_app_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_geo_help_tapDescription,
),
),
],
const SizedBox(height: MEDIUM_SPACE),
Row(
children: <Widget>[
const Icon(Icons.pinch_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_radiusBased_help_pinchDescription,
),
),
],
),
],
const SizedBox(height: MEDIUM_SPACE),
Row(
children: <Widget>[
const Icon(Icons.pinch_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_radiusBased_help_pinchDescription,
),
),
],
),
],
),
title: l10n.location_addAlarm_radiusBased_help_title,
sheetName: HelperSheet.radiusBasedAlarms,
);
Expand Down Expand Up @@ -224,11 +223,10 @@ class _ViewAlarmSelectRadiusBasedScreenState
isDismissible: true,
isScrollControlled: true,
),
builder: (_) =>
GeoAlarmMetaDataSheet(
center: alarmCenter!,
radius: radius.toDouble(),
),
builder: (_) => GeoAlarmMetaDataSheet(
center: alarmCenter!,
radius: radius.toDouble(),
),
);
break;
case LocationAlarmType.proximity:
Expand All @@ -239,10 +237,9 @@ class _ViewAlarmSelectRadiusBasedScreenState
isDismissible: true,
isScrollControlled: true,
),
builder: (_) =>
ProximityAlarmMetaDataSheet(
radius: radius,
),
builder: (_) => ProximityAlarmMetaDataSheet(
radius: radius,
),
);
}

Expand All @@ -251,7 +248,7 @@ class _ViewAlarmSelectRadiusBasedScreenState
}

final hasGrantedNotificationAccess =
await showNotificationPermissionDialog();
await showNotificationPermissionDialog();

if (!hasGrantedNotificationAccess || !mounted) {
return;
Expand Down Expand Up @@ -281,28 +278,25 @@ class _ViewAlarmSelectRadiusBasedScreenState
};

return PlatformScaffold(
material: (_, __) =>
MaterialScaffoldData(
resizeToAvoidBottomInset: false,
),
material: (_, __) => MaterialScaffoldData(
resizeToAvoidBottomInset: false,
),
appBar: PlatformAppBar(
title: Text(TYPE_TITLE_MAP[widget.type]!),
trailingActions: [
PlatformIconButton(
cupertino: (_, __) =>
CupertinoIconButtonData(
padding: EdgeInsets.zero,
),
cupertino: (_, __) => CupertinoIconButtonData(
padding: EdgeInsets.zero,
),
icon: Icon(context.platformIcons.help),
onPressed: showHelp,
),
],
cupertino: (_, __) =>
CupertinoNavigationBarData(
backgroundColor: isInScaleMode
? null
: getCupertinoAppBarColorForMapScreen(context),
),
cupertino: (_, __) => CupertinoNavigationBarData(
backgroundColor: isInScaleMode
? null
: getCupertinoAppBarColorForMapScreen(context),
),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down
Loading

0 comments on commit abb57de

Please sign in to comment.