requirements rework

This commit is contained in:
Jonas Bark
2025-03-27 14:53:38 +01:00
parent 71e9168d1d
commit 9e34c99aaf
17 changed files with 276 additions and 96 deletions

View File

@@ -5,6 +5,8 @@ import 'package:flutter/material.dart';
import 'package:swift_play/main.dart';
import 'package:swift_play/utils/devices/ble_device.dart';
import '../utils/messages/notification.dart';
class DevicePage extends StatefulWidget {
const DevicePage({super.key});
@@ -17,7 +19,7 @@ class _DevicePageState extends State<DevicePage> {
late StreamSubscription<BleDevice> _connectionStateSubscription;
late StreamSubscription<String> _actionSubscription;
late StreamSubscription<BaseNotification> _actionSubscription;
@override
void initState() {
@@ -49,39 +51,37 @@ class _DevicePageState extends State<DevicePage> {
Widget build(BuildContext context) {
return ScaffoldMessenger(
key: _snackBarMessengerKey,
child: Scaffold(
appBar: AppBar(
title: Text('Swift Play'),
actions: [
TextButton(
onPressed: () {
connection.reset();
Navigator.pop(context);
},
child: Text('Reset'),
),
IconButton(
onPressed: () {
_actions.clear();
setState(() {});
},
icon: Icon(Icons.clear),
),
],
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Devices:\n${connection.devices.joinToString(separator: '\n', transform: (it) {
return "${it.device.platformName}: ${it.device.isConnected ? 'Connected' : 'Not connected'}";
})}',
child: PopScope(
onPopInvokedWithResult: (hello, _) {
connection.reset();
},
child: Scaffold(
appBar: AppBar(
title: Text('Swift Play'),
actions: [
IconButton(
onPressed: () {
_actions.clear();
setState(() {});
},
icon: Icon(Icons.clear),
),
Expanded(child: ListView(children: _actions.map((action) => Text(action)).toList())),
],
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Devices:\n${connection.devices.joinToString(separator: '\n', transform: (it) {
return "${it.device.platformName}: ${it.device.isConnected ? 'Connected' : 'Not connected'}";
})}',
),
Expanded(child: ListView(children: _actions.map((action) => Text(action)).toList())),
],
),
),
),
),

View File

@@ -1,9 +1,7 @@
import 'dart:async';
import 'package:dartx/dartx.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:swift_play/main.dart';
import 'package:swift_play/pages/scan.dart';
import 'package:swift_play/utils/requirements/platform.dart';
import 'device.dart';
@@ -15,23 +13,15 @@ class RequirementsPage extends StatefulWidget {
}
class _RequirementsPageState extends State<RequirementsPage> {
late final StreamSubscription<BluetoothAdapterState> _adapterStateStateSubscription;
StepState _bluetoothStepState = StepState.indexed;
int _currentStep = 0;
List<PlatformRequirement> _requirements = [];
@override
void initState() {
super.initState();
_adapterStateStateSubscription = FlutterBluePlus.adapterState.listen((state) {
_bluetoothStepState = state != BluetoothAdapterState.off ? StepState.complete : StepState.indexed;
if (_bluetoothStepState == StepState.complete) {
_currentStep = 1;
}
if (mounted) {
setState(() {});
}
});
_reloadRequirements();
connection.hasDevices.addListener(() {
if (connection.hasDevices.value) {
@@ -43,48 +33,59 @@ class _RequirementsPageState extends State<RequirementsPage> {
@override
dispose() {
super.dispose();
_adapterStateStateSubscription.cancel();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Swift Play'), backgroundColor: Theme.of(context).colorScheme.inversePrimary),
body: Stepper(
currentStep: _currentStep,
onStepContinue: () {
if (_currentStep <= 2) {
setState(() {
_currentStep += 1;
});
}
},
onStepTapped: (step) {
setState(() {
_currentStep = step;
});
},
steps: [
Step(
title: Text('Bluetooth turned on'),
content: ElevatedButton(
onPressed: () {
FlutterBluePlus.turnOn();
},
child: Text('Turn bluetooth on'),
),
state: _bluetoothStepState,
),
Step(
title: Text('Accessibility service activated'),
content: ElevatedButton(onPressed: () {}, child: Text('Turn Accessibility service on')),
),
Step(
title: Text('Scan for devices'),
content: _currentStep != 2 ? CircularProgressIndicator() : ScanWidget(),
),
],
),
body:
_requirements.isEmpty
? Center(child: CircularProgressIndicator())
: Stepper(
currentStep: _currentStep,
onStepContinue:
_currentStep < _requirements.length
? () {
setState(() {
_currentStep += 1;
});
}
: null,
onStepTapped: (step) {
setState(() {
_currentStep = step;
});
},
steps:
_requirements
.mapIndexed(
(index, req) => Step(
title: Text(req.name),
content:
(index == _currentStep ? req.build(context) : null) ??
ElevatedButton(onPressed: () => _callRequirement(req), child: Text(req.name)),
state: req.status ? StepState.complete : StepState.indexed,
),
)
.toList(),
),
);
}
void _callRequirement(PlatformRequirement req) {
req.call().then((_) {
_reloadRequirements();
});
}
void _reloadRequirements() {
getRequirements().then((req) {
_requirements = req;
_currentStep = req.indexWhere((req) => !req.status);
if (mounted) {
setState(() {});
}
});
}
}

View File

@@ -5,12 +5,14 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:swift_play/utils/devices/ble_device.dart';
import 'messages/notification.dart';
class Connection {
final devices = <BleDevice>[];
final Map<BleDevice, StreamSubscription<String>> _streamSubscriptions = {};
final StreamController<String> _actionStreams = StreamController<String>.broadcast();
Stream<String> get actionStream => _actionStreams.stream;
final Map<BleDevice, StreamSubscription<BaseNotification>> _streamSubscriptions = {};
final StreamController<BaseNotification> _actionStreams = StreamController<BaseNotification>.broadcast();
Stream<BaseNotification> get actionStream => _actionStreams.stream;
final Map<BleDevice, StreamSubscription<BluetoothConnectionState>> _connectionSubscriptions = {};
final StreamController<BleDevice> _connectionStreams = StreamController<BleDevice>.broadcast();
@@ -26,7 +28,7 @@ class Connection {
_addDevices(scanResults);
},
onError: (e) {
_actionStreams.add(e.toString());
_actionStreams.add(LogNotification(e.toString()));
},
);
}
@@ -58,7 +60,7 @@ class Connection {
if (e is FlutterBluePlusException && e.code == FbpErrorCode.connectionCanceled.index) {
// ignore connections canceled by the user
} else {
_actionStreams.add(e.toString());
_actionStreams.add(LogNotification(e.toString()));
if (kDebugMode) {
print(e);
print("backtrace: $backtrace");

View File

@@ -9,6 +9,7 @@ import 'package:swift_play/utils/devices/zwift_click.dart';
import 'package:swift_play/utils/devices/zwift_play.dart';
import '../crypto/zap_crypto.dart';
import '../messages/notification.dart';
abstract class BleDevice {
final ScanResult scanResult;
@@ -47,8 +48,8 @@ abstract class BleDevice {
}
BluetoothDevice get device => scanResult.device;
final StreamController<String> actionStreamInternal = StreamController<String>.broadcast();
Stream<String> get actionStream => actionStreamInternal.stream;
final StreamController<BaseNotification> actionStreamInternal = StreamController<BaseNotification>.broadcast();
Stream<BaseNotification> get actionStream => actionStreamInternal.stream;
Future<void> connect() async {
await device.connect(autoConnect: false).timeout(const Duration(seconds: 3));
@@ -71,7 +72,9 @@ abstract class BleDevice {
}
final services = await device.discoverServices();
await handleServices(services);
if (device.isConnected) {
await handleServices(services);
}
}
Future<void> handleServices(List<BluetoothService> services);

View File

@@ -4,6 +4,7 @@ import 'package:dartx/dartx.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:swift_play/utils/devices/ble_device.dart';
import 'package:swift_play/utils/messages/notification.dart';
import '../ble.dart';
import '../crypto/encryption_utils.dart';
@@ -90,7 +91,7 @@ class ZwiftClick extends BleDevice {
} catch (e, stackTrace) {
print("Error processing data: $e");
print("Stack Trace: $stackTrace");
actionStreamInternal.add(e.toString());
actionStreamInternal.add(LogNotification(e.toString()));
}
}
@@ -136,7 +137,7 @@ class ZwiftClick extends BleDevice {
void processClickNotification(Uint8List message) {
final ClickNotification clickNotification = ClickNotification(message);
if (_lastClickNotification == null || _lastClickNotification != clickNotification) {
actionStreamInternal.add(clickNotification.toString());
actionStreamInternal.add(clickNotification);
}
_lastClickNotification = clickNotification;
}

View File

@@ -12,7 +12,7 @@ class ZwiftPlay extends ZwiftClick {
void processClickNotification(Uint8List message) {
final ControllerNotification clickNotification = ControllerNotification(message);
if (_lastControllerNotification == null || _lastControllerNotification != clickNotification) {
actionStreamInternal.add(clickNotification.toString());
actionStreamInternal.add(clickNotification);
}
_lastControllerNotification = clickNotification;
}

View File

@@ -1,8 +1,10 @@
import 'dart:typed_data';
import 'package:swift_play/utils/messages/notification.dart';
import '../../protocol/zwift.pb.dart';
class ClickNotification {
class ClickNotification extends BaseNotification {
static const int BTN_PRESSED = 0;
bool buttonUp = false;

View File

@@ -1,8 +1,10 @@
import 'dart:typed_data';
import 'package:swift_play/utils/messages/notification.dart';
import '../../protocol/zwift.pb.dart';
class ControllerNotification {
class ControllerNotification extends BaseNotification {
static const int BTN_PRESSED = 0;
late bool rightPad, buttonY, buttonZ, buttonA, buttonB, buttonOn, buttonShift;

View File

@@ -0,0 +1,7 @@
class BaseNotification {}
class LogNotification extends BaseNotification {
final String message;
LogNotification(this.message);
}

View File

@@ -0,0 +1,64 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:keypress_simulator/keypress_simulator.dart';
import 'package:swift_play/pages/scan.dart';
import 'package:swift_play/utils/requirements/platform.dart';
class KeyboardRequirement extends PlatformRequirement {
KeyboardRequirement() : super('Keyboard access');
@override
Future<void> call() async {
return keyPressSimulator.requestAccess(onlyOpenPrefPane: Platform.isMacOS);
}
@override
Future<void> getStatus() async {
status = await keyPressSimulator.isAccessAllowed();
}
}
class BluetoothTurnedOn extends PlatformRequirement {
BluetoothTurnedOn() : super('Bluetooth turned on');
@override
Future<void> call() async {
return FlutterBluePlus.turnOn();
}
@override
Future<void> getStatus() async {
status = FlutterBluePlus.adapterStateNow != BluetoothAdapterState.off;
}
}
class UnsupportedPlatform extends PlatformRequirement {
UnsupportedPlatform() : super('Unsupported platform :(') {
status = false;
}
@override
Future<void> call() async {}
@override
Future<void> getStatus() async {}
}
class BluetoothScanning extends PlatformRequirement {
BluetoothScanning() : super('Bluetooth Scanning') {
status = false;
}
@override
Future<void> call() async {}
@override
Future<void> getStatus() async {}
@override
Widget? build(BuildContext context) {
return ScanWidget();
}
}

View File

@@ -0,0 +1,37 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:swift_play/utils/requirements/multi.dart';
abstract class PlatformRequirement {
String name;
late bool status;
PlatformRequirement(this.name);
Future<void> getStatus();
Future<void> call();
Widget? build(BuildContext context) {
return null;
}
}
Future<List<PlatformRequirement>> getRequirements() async {
List<PlatformRequirement> list;
if (kIsWeb) {
list = [BluetoothTurnedOn(), BluetoothScanning()];
} else if (Platform.isMacOS) {
list = [BluetoothTurnedOn(), KeyboardRequirement(), BluetoothScanning()];
} else if (Platform.isWindows) {
list = [BluetoothTurnedOn(), KeyboardRequirement(), BluetoothScanning()];
} else if (Platform.isAndroid) {
list = [BluetoothTurnedOn(), BluetoothScanning()];
} else {
list = [UnsupportedPlatform()];
}
await Future.wait(list.map((e) => e.getStatus()));
return list;
}

View File

@@ -6,7 +6,9 @@ import FlutterMacOS
import Foundation
import flutter_blue_plus_darwin
import keypress_simulator_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterBluePlusPlugin.register(with: registry.registrar(forPlugin: "FlutterBluePlusPlugin"))
KeypressSimulatorMacosPlugin.register(with: registry.registrar(forPlugin: "KeypressSimulatorMacosPlugin"))
}

View File

@@ -3,20 +3,26 @@ PODS:
- Flutter
- FlutterMacOS
- FlutterMacOS (1.0.0)
- keypress_simulator_macos (0.0.1):
- FlutterMacOS
DEPENDENCIES:
- flutter_blue_plus_darwin (from `Flutter/ephemeral/.symlinks/plugins/flutter_blue_plus_darwin/darwin`)
- FlutterMacOS (from `Flutter/ephemeral`)
- keypress_simulator_macos (from `Flutter/ephemeral/.symlinks/plugins/keypress_simulator_macos/macos`)
EXTERNAL SOURCES:
flutter_blue_plus_darwin:
:path: Flutter/ephemeral/.symlinks/plugins/flutter_blue_plus_darwin/darwin
FlutterMacOS:
:path: Flutter/ephemeral
keypress_simulator_macos:
:path: Flutter/ephemeral/.symlinks/plugins/keypress_simulator_macos/macos
SPEC CHECKSUMS:
flutter_blue_plus_darwin: 3ea4ec9133b377febcc8a70b28cd2d2dc9242bd9
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
keypress_simulator_macos: f8556f9101f9f2f175652e0bceddf0fe82a4c6b2
PODFILE CHECKSUM: 7eb978b976557c8c1cd717d8185ec483fd090a82

View File

@@ -185,6 +185,38 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
keypress_simulator:
dependency: "direct main"
description:
name: keypress_simulator
sha256: d5aa5ed472b6b396f41fd6dcee99f4afb2c7ac6202af622b4cec7955de6ed7f6
url: "https://pub.dev"
source: hosted
version: "0.2.0"
keypress_simulator_macos:
dependency: transitive
description:
name: keypress_simulator_macos
sha256: babb698b1331cff0301de839c7bc6b051d84f98ddb137cf9a6dda6c6caeb78ac
url: "https://pub.dev"
source: hosted
version: "0.2.0"
keypress_simulator_platform_interface:
dependency: transitive
description:
name: keypress_simulator_platform_interface
sha256: "38c35fee6b107ff10cfb6bdb61e32eb0db17545ed64399a357794431212ca4b4"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
keypress_simulator_windows:
dependency: transitive
description:
name: keypress_simulator_windows
sha256: b4ff055131a2e5ea920eb3b6a185e1889fe00749b027df3b83aa726ed590a9b5
url: "https://pub.dev"
source: hosted
version: "0.2.0"
leak_tracker:
dependency: transitive
description:
@@ -257,6 +289,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
pointycastle:
dependency: "direct main"
description:
@@ -350,6 +390,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.4.0"
uni_platform:
dependency: transitive
description:
name: uni_platform
sha256: e02213a7ee5352212412ca026afd41d269eb00d982faa552f419ffc2debfad84
url: "https://pub.dev"
source: hosted
version: "0.1.3"
vector_math:
dependency: transitive
description:

View File

@@ -14,6 +14,7 @@ dependencies:
protobuf: ^3.1.0
dartx: any
pointycastle: any
keypress_simulator: ^0.2.0
dependency_overrides:
flutter_blue_plus_web:

View File

@@ -6,6 +6,9 @@
#include "generated_plugin_registrant.h"
#include <keypress_simulator_windows/keypress_simulator_windows_plugin_c_api.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
KeypressSimulatorWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("KeypressSimulatorWindowsPluginCApi"));
}

View File

@@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
keypress_simulator_windows
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST