mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-03 10:33:49 +01:00
Replace ~250 Material, 19 Bootstrap and 11 Radix icons across 65 files with their LucideIcons equivalents (e.g. more_vert -> ellipsisVertical, add/remove -> plus/minus, workspace_premium -> crown, patchPlus/Minus -> badgePlus/Minus, GitHub/Facebook -> Lucide's own logos). The only glyph left outside Lucide is the Reddit logo, which Lucide doesn't have. A source guard test fails on any new non-Lucide icon outside that allowlist. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
325 lines
12 KiB
Dart
325 lines
12 KiB
Dart
import 'dart:async';
|
||
import 'dart:io';
|
||
import 'dart:isolate';
|
||
import 'dart:ui';
|
||
|
||
import 'package:bike_control/gen/l10n.dart';
|
||
import 'package:bike_control/utils/actions/android.dart';
|
||
import 'package:bike_control/utils/core.dart';
|
||
import 'package:bike_control/utils/requirements/platform.dart';
|
||
import 'package:bike_control/widgets/accessibility_disclosure_dialog.dart';
|
||
import 'package:bike_control/widgets/ui/toast.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
import 'package:shadcn_flutter/shadcn_flutter.dart';
|
||
import 'package:universal_ble/universal_ble.dart';
|
||
import 'package:url_launcher/url_launcher_string.dart';
|
||
|
||
class AccessibilityRequirement extends PlatformRequirement {
|
||
AccessibilityRequirement()
|
||
: super(
|
||
AppLocalizations.current.allowAccessibilityService,
|
||
description: AppLocalizations.current.accessibilityDescription,
|
||
icon: LucideIcons.personStanding,
|
||
);
|
||
|
||
@override
|
||
Future<void> call(BuildContext context, VoidCallback onUpdate) async {
|
||
// Only meaningful while the local (AndroidActions) handler is active. When
|
||
// connected to a trainer app on another device the handler is a
|
||
// RemoteActions, and the accessibility service does not apply.
|
||
if (core.actionHandler is! AndroidActions) return;
|
||
await _showDisclosureDialog(context, onUpdate);
|
||
await getStatus();
|
||
}
|
||
|
||
@override
|
||
Future<bool> getStatus() async {
|
||
// Guard the cast: on Android, connecting to a trainer app on another device
|
||
// makes actionHandler a RemoteActions, so a blind `as AndroidActions` here
|
||
// threw a TypeError (e.g. from the Network Troubleshooting "enable Local"
|
||
// fix, which checks this requirement regardless of the active handler).
|
||
final handler = core.actionHandler;
|
||
if (handler is! AndroidActions) {
|
||
status = false;
|
||
return status;
|
||
}
|
||
status = await handler.accessibilityHandler.hasPermission();
|
||
return status;
|
||
}
|
||
|
||
Future<void> _showDisclosureDialog(BuildContext context, VoidCallback onUpdate) async {
|
||
return showDialog<void>(
|
||
context: context,
|
||
barrierDismissible: false, // Prevent dismissing by tapping outside
|
||
builder: (BuildContext context) {
|
||
return AccessibilityDisclosureDialog(
|
||
onAccept: () {
|
||
Navigator.of(context).pop();
|
||
// Open accessibility settings after user consents
|
||
(core.actionHandler as AndroidActions).accessibilityHandler.openPermissions().then((_) async {
|
||
await getStatus();
|
||
onUpdate();
|
||
});
|
||
},
|
||
onDeny: () async {
|
||
await getStatus();
|
||
Navigator.of(context).pop();
|
||
// User denied, no action taken
|
||
},
|
||
);
|
||
},
|
||
);
|
||
}
|
||
}
|
||
|
||
class BluetoothScanRequirement extends PlatformRequirement {
|
||
BluetoothScanRequirement() : super(AppLocalizations.current.allowBluetoothScan, icon: LucideIcons.bluetoothSearching);
|
||
|
||
@override
|
||
Future<void> call(BuildContext context, VoidCallback onUpdate) async {
|
||
await Permission.bluetoothScan.request();
|
||
await getStatus();
|
||
}
|
||
|
||
@override
|
||
Future<bool> getStatus() async {
|
||
final state = await Permission.bluetoothScan.status;
|
||
status = state.isGranted || state.isLimited;
|
||
return status;
|
||
}
|
||
}
|
||
|
||
class LocationRequirement extends PlatformRequirement {
|
||
LocationRequirement() : super(AppLocalizations.current.allowLocationForBluetooth, icon: LucideIcons.mapPin);
|
||
|
||
@override
|
||
Future<void> call(BuildContext context, VoidCallback onUpdate) async {
|
||
await Permission.locationWhenInUse.request();
|
||
await getStatus();
|
||
}
|
||
|
||
@override
|
||
Future<bool> getStatus() async {
|
||
final state = await Permission.locationWhenInUse.status;
|
||
status = state.isGranted || state.isLimited;
|
||
return status;
|
||
}
|
||
}
|
||
|
||
class BluetoothConnectRequirement extends PlatformRequirement {
|
||
BluetoothConnectRequirement()
|
||
: super(AppLocalizations.current.allowBluetoothConnections, icon: LucideIcons.bluetoothConnected);
|
||
|
||
@override
|
||
Future<void> call(BuildContext context, VoidCallback onUpdate) async {
|
||
await Permission.bluetoothConnect.request();
|
||
await getStatus();
|
||
}
|
||
|
||
@override
|
||
Future<bool> getStatus() async {
|
||
final state = await Permission.bluetoothConnect.status;
|
||
status = state.isGranted || state.isLimited;
|
||
return status;
|
||
}
|
||
}
|
||
|
||
ReceivePort? _receivePort;
|
||
StreamSubscription? _sub;
|
||
|
||
class NotificationRequirement extends PlatformRequirement {
|
||
NotificationRequirement()
|
||
: super(
|
||
AppLocalizations.current.allowPersistentNotification,
|
||
description: AppLocalizations.current.notificationDescription,
|
||
icon: LucideIcons.bellRing,
|
||
);
|
||
@override
|
||
Future<void> call(BuildContext context, VoidCallback onUpdate) async {
|
||
if (Platform.isAndroid) {
|
||
final result = await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
||
?.requestNotificationsPermission();
|
||
if (result == false) {
|
||
buildToast(
|
||
title: AppLocalizations.current.enableNotificationsAndroid,
|
||
);
|
||
}
|
||
} else if (Platform.isIOS) {
|
||
final result = await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
|
||
?.requestPermissions(
|
||
alert: true,
|
||
badge: false,
|
||
sound: false,
|
||
);
|
||
core.settings.setHasAskedPermissions(true);
|
||
if (result == false) {
|
||
buildToast(
|
||
title: AppLocalizations.current.enableNotificationsApple,
|
||
);
|
||
launchUrlString('x-apple.systempreferences:com.apple.preference.notifications');
|
||
}
|
||
} else if (Platform.isMacOS) {
|
||
final result = await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<MacOSFlutterLocalNotificationsPlugin>()
|
||
?.requestPermissions(
|
||
alert: true,
|
||
badge: false,
|
||
sound: false,
|
||
);
|
||
core.settings.setHasAskedPermissions(true);
|
||
if (result == false) {
|
||
buildToast(
|
||
title: AppLocalizations.current.enableNotificationsApple,
|
||
);
|
||
launchUrlString('x-apple.systempreferences:com.apple.preference.notifications');
|
||
}
|
||
}
|
||
await getStatus();
|
||
return;
|
||
}
|
||
|
||
@override
|
||
Future<bool> getStatus() async {
|
||
if (Platform.isAndroid) {
|
||
final bool granted =
|
||
await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
||
?.areNotificationsEnabled() ??
|
||
false;
|
||
status = granted;
|
||
} else if (Platform.isIOS) {
|
||
final permissions = await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
|
||
?.checkPermissions();
|
||
status = permissions?.isEnabled == true || core.settings.hasAskedPermissions();
|
||
} else if (Platform.isMacOS) {
|
||
final permissions = await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<MacOSFlutterLocalNotificationsPlugin>()
|
||
?.checkPermissions();
|
||
status = permissions?.isEnabled == true || core.settings.hasAskedPermissions();
|
||
} else {
|
||
status = true;
|
||
}
|
||
return status;
|
||
}
|
||
|
||
static Future<void> setup() async {
|
||
print('NOTIFICATION SETUP');
|
||
// macOS has no background-isolate support for notification responses, and
|
||
// registering that callback there is the prime suspect for the startup
|
||
// hang some macOS users hit — so only wire the background handler off
|
||
// macOS. Foreground responses still route through the same handler.
|
||
await core.flutterLocalNotificationsPlugin.initialize(
|
||
settings: InitializationSettings(
|
||
android: AndroidInitializationSettings(
|
||
'@drawable/ic_notification',
|
||
),
|
||
iOS: DarwinInitializationSettings(
|
||
requestAlertPermission: false,
|
||
requestBadgePermission: false,
|
||
requestSoundPermission: false,
|
||
),
|
||
macOS: DarwinInitializationSettings(
|
||
requestAlertPermission: false,
|
||
),
|
||
windows: WindowsInitializationSettings(
|
||
appName: 'BikeControl',
|
||
appUserModelId: 'OpenBikeControl.BikeControl',
|
||
guid: '00000012-0000-1000-8000-00805f9b34fb',
|
||
),
|
||
),
|
||
onDidReceiveBackgroundNotificationResponse: Platform.isMacOS ? null : notificationTapBackground,
|
||
onDidReceiveNotificationResponse: (n) {
|
||
notificationTapBackground(n);
|
||
},
|
||
);
|
||
}
|
||
|
||
static Future<void> addPersistentNotification() async {
|
||
const String channelGroupId = 'BikeControl';
|
||
// create the group first
|
||
await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
|
||
.createNotificationChannelGroup(
|
||
AndroidNotificationChannelGroup(channelGroupId, channelGroupId, description: 'Keep Alive'),
|
||
);
|
||
|
||
// create channels associated with the group
|
||
await core.flutterLocalNotificationsPlugin
|
||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
|
||
.createNotificationChannel(
|
||
const AndroidNotificationChannel(
|
||
channelGroupId,
|
||
channelGroupId,
|
||
description: 'Keep Alive',
|
||
groupId: channelGroupId,
|
||
),
|
||
);
|
||
|
||
await AndroidFlutterLocalNotificationsPlugin().startForegroundService(
|
||
id: 1,
|
||
title: channelGroupId,
|
||
body: AppLocalizations.current.allowsRunningInBackground,
|
||
foregroundServiceTypes: {AndroidServiceForegroundType.foregroundServiceTypeConnectedDevice},
|
||
startType: AndroidServiceStartType.startRedeliverIntent,
|
||
notificationDetails: AndroidNotificationDetails(
|
||
channelGroupId,
|
||
'Keep Alive',
|
||
actions: [
|
||
AndroidNotificationAction(
|
||
'disconnect',
|
||
AppLocalizations.current.disconnectDevices,
|
||
cancelNotification: true,
|
||
showsUserInterface: false,
|
||
),
|
||
AndroidNotificationAction(
|
||
'close',
|
||
AppLocalizations.current.close,
|
||
cancelNotification: true,
|
||
showsUserInterface: false,
|
||
),
|
||
],
|
||
),
|
||
);
|
||
|
||
_receivePort = ReceivePort();
|
||
// If already registered, remove and re-register
|
||
IsolateNameServer.removePortNameMapping('_backgroundChannelKey');
|
||
final ok = IsolateNameServer.registerPortWithName(_receivePort!.sendPort, '_backgroundChannelKey');
|
||
if (!ok) {
|
||
// If this happens, something else re-registered immediately or you’re in a weird state.
|
||
throw StateError('Failed to register port name');
|
||
}
|
||
final backgroundMessagePort = _receivePort!.asBroadcastStream();
|
||
_sub = backgroundMessagePort.listen((message) {
|
||
print('Background isolate received message: $message');
|
||
if (message == 'disconnect' || message == 'close') {
|
||
UniversalBle.onAvailabilityChange = null;
|
||
core.connection.disconnectAll();
|
||
}
|
||
if (message == 'close') {
|
||
core.actionHandler.cleanup();
|
||
core.connection.stop();
|
||
SystemNavigator.pop();
|
||
AndroidFlutterLocalNotificationsPlugin().stopForegroundService();
|
||
AndroidFlutterLocalNotificationsPlugin().cancelAll();
|
||
}
|
||
|
||
//exit(0);
|
||
});
|
||
}
|
||
}
|
||
|
||
@pragma('vm:entry-point')
|
||
void notificationTapBackground(NotificationResponse notificationResponse) {
|
||
if (notificationResponse.actionId != null) {
|
||
final sendPort = IsolateNameServer.lookupPortByName('_backgroundChannelKey');
|
||
sendPort?.send(notificationResponse.actionId);
|
||
//exit(0);
|
||
}
|
||
}
|