mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-29 09:11:17 +01:00
Picking where Grimpa, Biketerra or Ride Cave runs switched on the OpenBikeControl network method, which these apps can't use, and left Bluetooth off. It switches on their Bluetooth method now. On Android the controller step asks for the advertise permission in the same sheet as the scan ones (a refusal never holds the scan), and the method starts once granted, or on the ride step when it was granted before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
6.1 KiB
Dart
152 lines
6.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:bike_control/main.dart' show ConnectionType;
|
|
import 'package:bike_control/utils/core.dart';
|
|
import 'package:bike_control/utils/keymap/apps/bike_control.dart';
|
|
import 'package:bike_control/utils/keymap/apps/custom_app.dart';
|
|
import 'package:bike_control/utils/keymap/apps/my_whoosh.dart';
|
|
import 'package:bike_control/utils/keymap/apps/supported_app.dart';
|
|
import 'package:bike_control/utils/requirements/multi.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
/// What happens to the rider's own button mapping when they pick another
|
|
/// trainer app.
|
|
enum KeymapSwitchChoice {
|
|
/// Keep using the custom mapping that is active now.
|
|
keepCustom,
|
|
|
|
/// Switch to the picked app's built-in mapping. The custom one stays saved.
|
|
useAppButtons,
|
|
}
|
|
|
|
/// The name of the active custom mapping when picking [selectedApp] should
|
|
/// ask what to do with it — it was copied from a different app, or from none
|
|
/// we know of. Null when there's nothing to ask: the active mapping is a
|
|
/// built-in one, the rider picked a custom "app", or the custom mapping was
|
|
/// made from [selectedApp] itself.
|
|
String? customKeymapToConfirmFor(SupportedApp selectedApp) {
|
|
final active = core.actionHandler.supportedApp;
|
|
if (active is! CustomApp || selectedApp is CustomApp) return null;
|
|
if (core.settings.getCustomKeymapOrigin(active.profileName) == selectedApp.name) return null;
|
|
return active.profileName;
|
|
}
|
|
|
|
/// Applies the trainer app selection, stopping unsupported emulators,
|
|
/// initializing the action handler, and starting the enabled connection method.
|
|
///
|
|
/// [keymapChoice] decides what happens to an active custom mapping (see
|
|
/// [customKeymapToConfirmFor]); the UI asks the rider before calling this.
|
|
///
|
|
/// Shared by [ConfigurationPage] and the onboarding wizard.
|
|
Future<void> applyTrainerAppSelection(
|
|
SupportedApp selectedApp, {
|
|
KeymapSwitchChoice keymapChoice = KeymapSwitchChoice.keepCustom,
|
|
}) async {
|
|
if (selectedApp is! MyWhoosh) {
|
|
if (core.whooshLink.isStarted.value) {
|
|
core.whooshLink.stopServer();
|
|
}
|
|
}
|
|
if (!selectedApp.supports(AppConnectionMethod.zwiftMdns)) {
|
|
if (core.zwiftMdnsEmulator.isStarted.value) {
|
|
core.zwiftMdnsEmulator.stop();
|
|
}
|
|
// TODO restart mDNS when advertisementName changes
|
|
}
|
|
if (!selectedApp.supports(AppConnectionMethod.zwiftBle)) {
|
|
if (core.zwiftEmulator.isStarted.value) {
|
|
core.zwiftEmulator.stopAdvertising();
|
|
}
|
|
}
|
|
if (!selectedApp.supports(AppConnectionMethod.rouvyMdns)) {
|
|
if (core.rouvyMdnsEmulator.isStarted.value) {
|
|
core.rouvyMdnsEmulator.stop();
|
|
}
|
|
}
|
|
if (core.obpMdnsEmulator.isStarted.value) {
|
|
core.obpMdnsEmulator.stopServer();
|
|
}
|
|
if (core.obpBluetoothEmulator.isStarted.value) {
|
|
core.obpBluetoothEmulator.stopServer();
|
|
}
|
|
if (core.bc2BluetoothEmulator.isStarted.value) {
|
|
core.bc2BluetoothEmulator.stopServer();
|
|
}
|
|
|
|
core.settings.setTrainerApp(selectedApp);
|
|
final active = core.actionHandler.supportedApp;
|
|
if (active == null ||
|
|
(selectedApp is! CustomApp &&
|
|
(active is! CustomApp || keymapChoice == KeymapSwitchChoice.useAppButtons))) {
|
|
core.actionHandler.init(selectedApp);
|
|
await core.settings.setKeyMap(selectedApp);
|
|
}
|
|
core.logic.startEnabledConnectionMethod();
|
|
|
|
if (selectedApp is BikeControl) {
|
|
core.settings.setLastTarget(Target.thisDevice);
|
|
} else if (!selectedApp.receivesButtonEvents) {
|
|
// Nothing we can send reaches this app locally, so a stale "this device"
|
|
// would leave the rider on a target that cannot work.
|
|
core.settings.setLastTarget(Target.otherDevice);
|
|
}
|
|
}
|
|
|
|
/// Turns on the Bluetooth method of [app] when it has no network method at
|
|
/// all: OpenBikeControl first, then the BC2 and Zwift ones — the order the
|
|
/// onboarding's Bluetooth tile picks them in.
|
|
void _enableBluetoothOnlyMethod(SupportedApp app) {
|
|
if (!app.connections.every((c) => c.$1.isBluetooth)) return;
|
|
if (app.supports(AppConnectionMethod.obpBle)) {
|
|
core.settings.setObpBleEnabled(true);
|
|
} else if (app.supports(AppConnectionMethod.cycplusBc2Ble)) {
|
|
core.settings.setBc2BleEnabled(true);
|
|
} else if (app.supports(AppConnectionMethod.zwiftBle)) {
|
|
core.settings.setZwiftBleEmulatorEnabled(true);
|
|
}
|
|
}
|
|
|
|
/// Applies the target selection, enabling the recommended network method or local connection
|
|
/// as appropriate, and starting the enabled connection method.
|
|
///
|
|
/// Shared by [ConfigurationPage] and the onboarding wizard.
|
|
Future<void> applyTargetSelection(Target target) async {
|
|
await core.settings.setLastTarget(target);
|
|
|
|
final picked = core.settings.getTrainerApp();
|
|
final supportsObpNetwork =
|
|
picked != null &&
|
|
(picked.supports(AppConnectionMethod.obpMdns) || picked.supports(AppConnectionMethod.obpDirCon));
|
|
if (supportsObpNetwork && !core.logic.emulatorEnabled) {
|
|
core.settings.setObpMdnsEnabled(true);
|
|
} else if (picked != null && target != Target.thisDevice && !core.logic.emulatorEnabled) {
|
|
// An app with no network method (Grimpa, Biketerra) finds BikeControl
|
|
// over Bluetooth alone — that is the method to turn on, not the OBP
|
|
// network one it can't use. It starts once Android's advertise
|
|
// permission is granted (asked for on the controller step).
|
|
_enableBluetoothOnlyMethod(picked);
|
|
}
|
|
|
|
// Zwift and Rouvy find BikeControl over the network too — the method the
|
|
// connection step recommends. Without it on, a first-time rider faces a
|
|
// greyed-out "Finish setup". An app on this device is driven by the Local
|
|
// method instead, where that exists.
|
|
final app = core.settings.getTrainerApp();
|
|
if (app != null &&
|
|
(app.supports(AppConnectionMethod.zwiftMdns) || app.supports(AppConnectionMethod.rouvyMdns)) &&
|
|
target.connectionType != ConnectionType.local &&
|
|
!core.logic.emulatorEnabled) {
|
|
core.settings.setZwiftMdnsEmulatorEnabled(true);
|
|
}
|
|
|
|
// enable local connection on Windows if the app doesn't support OBP
|
|
if (target == Target.thisDevice &&
|
|
!core.settings.getTrainerApp()!.supports(AppConnectionMethod.obpBle) &&
|
|
!core.settings.getTrainerApp()!.supports(AppConnectionMethod.obpMdns) &&
|
|
!kIsWeb &&
|
|
Platform.isWindows) {
|
|
core.settings.setLocalEnabled(true);
|
|
}
|
|
core.logic.startEnabledConnectionMethod();
|
|
}
|