mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-05 23:43:59 +01:00
add trainer app selection during app start
This commit is contained in:
@@ -14,6 +14,7 @@ import 'package:swift_control/utils/keymap/keymap.dart';
|
||||
import 'package:swift_control/utils/requirements/android.dart';
|
||||
import 'package:universal_ble/universal_ble.dart';
|
||||
|
||||
import '../utils/keymap/apps/my_whoosh.dart';
|
||||
import 'devices/base_device.dart';
|
||||
import 'devices/link/link_device.dart';
|
||||
import 'devices/zwift/constants.dart';
|
||||
@@ -82,7 +83,7 @@ class Connection {
|
||||
final data = manufacturerData
|
||||
.firstOrNullWhere((e) => e.companyId == ZwiftConstants.ZWIFT_MANUFACTURER_ID)
|
||||
?.payload;
|
||||
if (data != null) {
|
||||
if (data != null && kDebugMode) {
|
||||
_actionStreams.add(LogNotification('Found unknown device with identifier: ${data.firstOrNull}'));
|
||||
}
|
||||
}
|
||||
@@ -155,7 +156,9 @@ class Connection {
|
||||
_addDevices(pads);
|
||||
});
|
||||
|
||||
startMyWhooshServer();
|
||||
if (settings.getMyWhooshLinkEnabled() && settings.getTrainerApp() is MyWhoosh) {
|
||||
startMyWhooshServer();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> startMyWhooshServer() {
|
||||
|
||||
@@ -58,6 +58,7 @@ Future<void> initializeActions(ConnectionType connectionType) async {
|
||||
ConnectionType.unknown => StubActions(),
|
||||
};
|
||||
}
|
||||
actionHandler.init(settings.getSupportedApp());
|
||||
}
|
||||
|
||||
class SwiftPlayApp extends StatelessWidget {
|
||||
|
||||
@@ -8,7 +8,9 @@ import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:swift_control/bluetooth/devices/link/link_device.dart';
|
||||
import 'package:swift_control/main.dart';
|
||||
import 'package:swift_control/utils/actions/desktop.dart';
|
||||
import 'package:swift_control/utils/keymap/apps/my_whoosh.dart';
|
||||
import 'package:swift_control/utils/keymap/manager.dart';
|
||||
import 'package:swift_control/widgets/beta_pill.dart';
|
||||
import 'package:swift_control/widgets/keymap_explanation.dart';
|
||||
import 'package:swift_control/widgets/logviewer.dart';
|
||||
import 'package:swift_control/widgets/scan.dart';
|
||||
@@ -227,7 +229,7 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
|
||||
if (connection.remoteDevices.isNotEmpty ||
|
||||
actionHandler is RemoteActions ||
|
||||
!whooshLink.isConnected.value)
|
||||
settings.getTrainerApp() is MyWhoosh)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(bottom: 8.0),
|
||||
width: double.infinity,
|
||||
@@ -250,7 +252,8 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
(device) => device.showInformation(context),
|
||||
),
|
||||
|
||||
if (!whooshLink.isConnected.value) LinkDevice('').showInformation(context),
|
||||
if (settings.getTrainerApp() is MyWhoosh && !whooshLink.isConnected.value)
|
||||
LinkDevice('').showInformation(context),
|
||||
|
||||
if (actionHandler is RemoteActions)
|
||||
Row(
|
||||
@@ -305,7 +308,17 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
controller: controller,
|
||||
dropdownMenuEntries: [
|
||||
..._getAllApps().map(
|
||||
(app) => DropdownMenuEntry<SupportedApp>(value: app, label: app.name),
|
||||
(app) => DropdownMenuEntry<SupportedApp>(
|
||||
value: app,
|
||||
label: app.name,
|
||||
labelWidget: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(app.name),
|
||||
if (app is CustomApp) BetaPill(text: 'CUSTOM'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
DropdownMenuEntry(
|
||||
value: CustomApp(profileName: 'New'),
|
||||
@@ -314,7 +327,7 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
leadingIcon: Icon(Icons.add),
|
||||
),
|
||||
],
|
||||
label: Text('Select Keymap / app'),
|
||||
label: Text('Select Keymap'),
|
||||
onSelected: (app) async {
|
||||
if (app == null) {
|
||||
return;
|
||||
@@ -323,14 +336,14 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
if (profileName != null && profileName.isNotEmpty) {
|
||||
final customApp = CustomApp(profileName: profileName);
|
||||
actionHandler.init(customApp);
|
||||
await settings.setApp(customApp);
|
||||
await settings.setSupportedApp(customApp);
|
||||
controller.text = profileName;
|
||||
setState(() {});
|
||||
}
|
||||
} else {
|
||||
controller.text = app.name ?? '';
|
||||
actionHandler.supportedApp = app;
|
||||
await settings.setApp(app);
|
||||
await settings.setSupportedApp(app);
|
||||
setState(() {});
|
||||
if (app is! CustomApp &&
|
||||
!kIsWeb &&
|
||||
@@ -371,7 +384,7 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
controller.text = actionHandler.supportedApp?.name ?? '';
|
||||
|
||||
if (actionHandler.supportedApp is CustomApp) {
|
||||
settings.setApp(actionHandler.supportedApp!);
|
||||
settings.setSupportedApp(actionHandler.supportedApp!);
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -409,7 +422,7 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
List<SupportedApp> _getAllApps() {
|
||||
final baseApps = SupportedApp.supportedApps.where((app) => app is! CustomApp).toList();
|
||||
final baseApp = settings.getTrainerApp();
|
||||
final customProfiles = settings.getCustomAppProfiles();
|
||||
|
||||
final customApps = customProfiles.map((profile) {
|
||||
@@ -426,7 +439,7 @@ class _DevicePageState extends State<DevicePage> with WidgetsBindingObserver {
|
||||
customApps.add(CustomApp());
|
||||
}
|
||||
|
||||
return [...baseApps, ...customApps];
|
||||
return [if (baseApp != null) baseApp, ...customApps];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,66 +64,90 @@ class _RequirementsPageState extends State<RequirementsPage> with WidgetsBinding
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
actions: buildMenuButtons(),
|
||||
),
|
||||
body: _requirements.isEmpty
|
||||
? Center(child: CircularProgressIndicator())
|
||||
: Card(
|
||||
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
|
||||
child: Stepper(
|
||||
currentStep: _currentStep,
|
||||
connectorColor: WidgetStateProperty.resolveWith<Color>(
|
||||
(Set<WidgetState> states) => Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
onStepContinue: _currentStep < _requirements.length
|
||||
? () {
|
||||
setState(() {
|
||||
_currentStep += 1;
|
||||
});
|
||||
}
|
||||
: null,
|
||||
onStepTapped: (step) {
|
||||
if (_requirements[step].status && _requirements[step] is! TargetRequirement) {
|
||||
return;
|
||||
}
|
||||
final hasEarlierIncomplete =
|
||||
_requirements.indexWhere((req) => !req.status) != -1 &&
|
||||
_requirements.indexWhere((req) => !req.status) < step;
|
||||
if (hasEarlierIncomplete) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_currentStep = step;
|
||||
});
|
||||
},
|
||||
controlsBuilder: (context, details) => Container(),
|
||||
steps: _requirements
|
||||
.mapIndexed(
|
||||
(index, req) => Step(
|
||||
title: Text(req.name, style: TextStyle(fontWeight: FontWeight.w600)),
|
||||
subtitle: req.buildDescription() ?? (req.description != null ? Text(req.description!) : null),
|
||||
content: Container(
|
||||
padding: const EdgeInsets.only(top: 16.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child:
|
||||
(index == _currentStep
|
||||
? req.build(context, () {
|
||||
_reloadRequirements();
|
||||
})
|
||||
: null) ??
|
||||
ElevatedButton(
|
||||
onPressed: req.status
|
||||
? null
|
||||
: () => _callRequirement(req, context, () {
|
||||
_reloadRequirements();
|
||||
}),
|
||||
child: Text(req.name),
|
||||
),
|
||||
),
|
||||
state: req.status ? StepState.complete : StepState.indexed,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
body: Column(
|
||||
spacing: 12,
|
||||
children: [
|
||||
SizedBox(height: 12),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
spacing: 12,
|
||||
children: [
|
||||
Image.asset('icon.png', width: 64, height: 64),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Welcome to SwiftControl!', style: Theme.of(context).textTheme.titleMedium),
|
||||
Container(
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.sizeOf(context).width - 140),
|
||||
child: Text('Get started by completing the following steps:', style: TextStyle(fontSize: 14)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
_requirements.isEmpty
|
||||
? Center(child: CircularProgressIndicator())
|
||||
: Card(
|
||||
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
|
||||
child: Stepper(
|
||||
currentStep: _currentStep,
|
||||
connectorColor: WidgetStateProperty.resolveWith<Color>(
|
||||
(Set<WidgetState> states) => Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
onStepContinue: _currentStep < _requirements.length
|
||||
? () {
|
||||
setState(() {
|
||||
_currentStep += 1;
|
||||
});
|
||||
}
|
||||
: null,
|
||||
onStepTapped: (step) {
|
||||
if (_requirements[step].status && _requirements[step] is! TargetRequirement) {
|
||||
return;
|
||||
}
|
||||
final hasEarlierIncomplete =
|
||||
_requirements.indexWhere((req) => !req.status) != -1 &&
|
||||
_requirements.indexWhere((req) => !req.status) < step;
|
||||
if (hasEarlierIncomplete) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_currentStep = step;
|
||||
});
|
||||
},
|
||||
controlsBuilder: (context, details) => Container(),
|
||||
steps: _requirements
|
||||
.mapIndexed(
|
||||
(index, req) => Step(
|
||||
title: Text(req.name, style: TextStyle(fontWeight: FontWeight.w600)),
|
||||
subtitle:
|
||||
req.buildDescription() ?? (req.description != null ? Text(req.description!) : null),
|
||||
content: Container(
|
||||
padding: const EdgeInsets.only(top: 16.0),
|
||||
alignment: Alignment.centerLeft,
|
||||
child:
|
||||
(index == _currentStep
|
||||
? req.build(context, () {
|
||||
_reloadRequirements();
|
||||
})
|
||||
: null) ??
|
||||
ElevatedButton(
|
||||
onPressed: req.status
|
||||
? null
|
||||
: () => _callRequirement(req, context, () {
|
||||
_reloadRequirements();
|
||||
}),
|
||||
child: Text(req.name),
|
||||
),
|
||||
),
|
||||
state: req.status ? StepState.complete : StepState.indexed,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -139,9 +163,6 @@ class _RequirementsPageState extends State<RequirementsPage> with WidgetsBinding
|
||||
final unresolvedIndex = req.indexWhere((req) => !req.status);
|
||||
if (unresolvedIndex != -1) {
|
||||
_currentStep = unresolvedIndex;
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
} else if (mounted) {
|
||||
String? currentPath;
|
||||
navigatorKey.currentState?.popUntil((route) {
|
||||
@@ -158,6 +179,9 @@ class _RequirementsPageState extends State<RequirementsPage> with WidgetsBinding
|
||||
);
|
||||
}
|
||||
}
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ abstract class BaseActions {
|
||||
|
||||
void init(SupportedApp? supportedApp) {
|
||||
this.supportedApp = supportedApp;
|
||||
print('Supported app: ${supportedApp?.name ?? "None"}');
|
||||
|
||||
if (supportedApp != null) {
|
||||
final allButtons = connection.devices.map((e) => e.availableButtons).flatten().distinct();
|
||||
|
||||
@@ -8,7 +8,7 @@ import '../keymap.dart';
|
||||
class CustomApp extends SupportedApp {
|
||||
final String profileName;
|
||||
|
||||
CustomApp({this.profileName = 'Custom'})
|
||||
CustomApp({this.profileName = 'Other'})
|
||||
: super(
|
||||
name: profileName,
|
||||
packageName: "custom_$profileName",
|
||||
|
||||
@@ -49,7 +49,7 @@ class Keymap {
|
||||
_updateStream.add(null);
|
||||
|
||||
if (actionHandler.supportedApp is CustomApp) {
|
||||
settings.setApp(actionHandler.supportedApp!);
|
||||
settings.setSupportedApp(actionHandler.supportedApp!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class KeymapManager {
|
||||
customApp.decodeKeymap(savedKeymap);
|
||||
}
|
||||
actionHandler.supportedApp = customApp;
|
||||
await settings.setApp(customApp);
|
||||
await settings.setSupportedApp(customApp);
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -236,7 +236,7 @@ class KeymapManager {
|
||||
customApp.decodeKeymap(savedKeymap);
|
||||
}
|
||||
actionHandler.supportedApp = customApp;
|
||||
await settings.setApp(customApp);
|
||||
await settings.setSupportedApp(customApp);
|
||||
return newName;
|
||||
} else {
|
||||
final customApp = CustomApp(profileName: newName);
|
||||
@@ -260,7 +260,7 @@ class KeymapManager {
|
||||
});
|
||||
|
||||
actionHandler.supportedApp = customApp;
|
||||
await settings.setApp(customApp);
|
||||
await settings.setSupportedApp(customApp);
|
||||
return newName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:keypress_simulator/keypress_simulator.dart';
|
||||
import 'package:swift_control/main.dart';
|
||||
import 'package:swift_control/utils/keymap/apps/custom_app.dart';
|
||||
import 'package:swift_control/utils/keymap/apps/my_whoosh.dart';
|
||||
import 'package:swift_control/utils/keymap/apps/supported_app.dart';
|
||||
import 'package:swift_control/utils/requirements/platform.dart';
|
||||
import 'package:swift_control/utils/requirements/remote.dart';
|
||||
import 'package:swift_control/widgets/beta_pill.dart';
|
||||
@@ -151,7 +154,7 @@ enum Target {
|
||||
class TargetRequirement extends PlatformRequirement {
|
||||
TargetRequirement()
|
||||
: super(
|
||||
'Select Target Device',
|
||||
'Select Trainer App & Target Device',
|
||||
description: 'Select your Target Device where you want to run your trainer app on',
|
||||
) {
|
||||
status = false;
|
||||
@@ -162,65 +165,100 @@ class TargetRequirement extends PlatformRequirement {
|
||||
|
||||
@override
|
||||
Future<void> getStatus() async {
|
||||
status = settings.getLastTarget() != null;
|
||||
status = settings.getLastTarget() != null && settings.getTrainerApp() != null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget? build(BuildContext context, VoidCallback onUpdate) {
|
||||
return DropdownMenu<Target>(
|
||||
dropdownMenuEntries: Target.values.map((target) {
|
||||
return DropdownMenuEntry(
|
||||
value: target,
|
||||
label: target.title,
|
||||
enabled: target.isCompatible,
|
||||
trailingIcon: Icon(target.icon),
|
||||
labelWidget: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
return Column(
|
||||
spacing: 8,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Select Trainer App', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
DropdownMenu<SupportedApp>(
|
||||
dropdownMenuEntries: SupportedApp.supportedApps.map((app) {
|
||||
return DropdownMenuEntry(
|
||||
value: app,
|
||||
label: app.name,
|
||||
);
|
||||
}).toList(),
|
||||
hintText: 'Select Trainer app',
|
||||
initialSelection: settings.getTrainerApp(),
|
||||
onSelected: (selectedApp) async {
|
||||
if (settings.getTrainerApp() is MyWhoosh && selectedApp is! MyWhoosh && whooshLink.isStarted.value) {
|
||||
whooshLink.stopServer();
|
||||
}
|
||||
settings.setTrainerApp(selectedApp!);
|
||||
if (actionHandler.supportedApp == null ||
|
||||
(actionHandler.supportedApp is! CustomApp && selectedApp is! CustomApp)) {
|
||||
actionHandler.init(selectedApp);
|
||||
settings.setSupportedApp(selectedApp);
|
||||
}
|
||||
},
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Select Target where ${settings.getTrainerApp()?.name ?? 'the Trainer app'} runs on',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
DropdownMenu<Target>(
|
||||
dropdownMenuEntries: Target.values.map((target) {
|
||||
return DropdownMenuEntry(
|
||||
value: target,
|
||||
label: target.title,
|
||||
enabled: target.isCompatible,
|
||||
trailingIcon: Icon(target.icon),
|
||||
labelWidget: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(target.title, style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
if (target.isBeta || (!Platform.isIOS && target == Target.iPad)) BetaPill(),
|
||||
Row(
|
||||
children: [
|
||||
Text(target.title, style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
if (target.isBeta || (!Platform.isIOS && target == Target.iPad)) BetaPill(),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
target.isCompatible
|
||||
? target.description
|
||||
: 'Due to iOS restrictions only controlling trainer apps on other devices is supported.',
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
target.isCompatible
|
||||
? target.description
|
||||
: 'Due to iOS restrictions only controlling trainer apps on other devices is supported.',
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
hintText: name,
|
||||
initialSelection: settings.getLastTarget(),
|
||||
onSelected: (target) async {
|
||||
if (target != null) {
|
||||
await settings.setLastTarget(target);
|
||||
initializeActions(target.connectionType);
|
||||
if (target.warning != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(target.warning!),
|
||||
duration: Duration(seconds: 10),
|
||||
),
|
||||
);
|
||||
}
|
||||
onUpdate();
|
||||
}
|
||||
},
|
||||
}).toList(),
|
||||
hintText: name,
|
||||
initialSelection: settings.getLastTarget(),
|
||||
onSelected: (target) async {
|
||||
if (target != null) {
|
||||
await settings.setLastTarget(target);
|
||||
initializeActions(target.connectionType);
|
||||
if (target.warning != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(target.warning!),
|
||||
duration: Duration(seconds: 10),
|
||||
),
|
||||
);
|
||||
}
|
||||
onUpdate();
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget? buildDescription() {
|
||||
final trainer = settings.getTrainerApp();
|
||||
final target = settings.getLastTarget();
|
||||
|
||||
if (target != null) {
|
||||
if (target != null && trainer != null) {
|
||||
if (target.warning != null) {
|
||||
return Row(
|
||||
spacing: 8,
|
||||
@@ -235,7 +273,7 @@ class TargetRequirement extends PlatformRequirement {
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return Text(target.title);
|
||||
return Text('${trainer.name} on ${target.title}');
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:swift_control/main.dart';
|
||||
import 'package:swift_control/pages/device.dart';
|
||||
import 'package:swift_control/utils/actions/remote.dart';
|
||||
import 'package:swift_control/utils/keymap/apps/my_whoosh.dart';
|
||||
import 'package:swift_control/utils/requirements/multi.dart';
|
||||
import 'package:swift_control/utils/requirements/platform.dart';
|
||||
import 'package:swift_control/widgets/small_progress_indicator.dart';
|
||||
@@ -323,30 +324,31 @@ class _PairWidgetState extends State<_PairWidget> {
|
||||
if (_isAdvertising || _isLoading) SizedBox(height: 20, width: 20, child: SmallProgressIndicator()),
|
||||
],
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (c) => DevicePage(),
|
||||
settings: RouteSettings(name: '/device'),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Use MyWhoosh Link only'),
|
||||
Text(
|
||||
'No pairing required, connect directly via MyWhoosh Link.',
|
||||
style: TextStyle(fontSize: 10, color: Colors.black87),
|
||||
if (settings.getTrainerApp() is MyWhoosh)
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (c) => DevicePage(),
|
||||
settings: RouteSettings(name: '/device'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Use MyWhoosh Link only'),
|
||||
Text(
|
||||
'No pairing required, connect directly via MyWhoosh Link.',
|
||||
style: TextStyle(fontSize: 10, color: Colors.black87),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_isAdvertising) ...[
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:swift_control/utils/keymap/apps/supported_app.dart';
|
||||
import 'package:swift_control/utils/requirements/multi.dart';
|
||||
@@ -24,47 +23,8 @@ class Settings {
|
||||
}
|
||||
|
||||
try {
|
||||
// Get screen size for migrations
|
||||
Size? screenSize;
|
||||
try {
|
||||
final view = WidgetsBinding.instance.platformDispatcher.views.first;
|
||||
screenSize = view.physicalSize / view.devicePixelRatio;
|
||||
} catch (e) {
|
||||
screenSize = null;
|
||||
}
|
||||
|
||||
// Handle migration from old "customapp" key to new "customapp_Custom" key
|
||||
if (prefs.containsKey('customapp') && !prefs.containsKey('customapp_Custom')) {
|
||||
final oldCustomApp = prefs.getStringList('customapp');
|
||||
if (oldCustomApp != null) {
|
||||
// Migrate pixel-based to percentage-based if screen size available
|
||||
if (screenSize != null) {
|
||||
final migratedData = await _migrateToPercentageBased(oldCustomApp, screenSize);
|
||||
await prefs.setStringList('customapp_Custom', migratedData);
|
||||
} else {
|
||||
await prefs.setStringList('customapp_Custom', oldCustomApp);
|
||||
}
|
||||
await prefs.remove('customapp');
|
||||
}
|
||||
}
|
||||
|
||||
final appName = prefs.getString('app');
|
||||
if (appName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if it's a custom app with a profile name
|
||||
if (appName.startsWith('Custom') || prefs.containsKey('customapp_$appName')) {
|
||||
final customApp = CustomApp(profileName: appName);
|
||||
final appSetting = prefs.getStringList('customapp_$appName');
|
||||
if (appSetting != null) {
|
||||
customApp.decodeKeymap(appSetting);
|
||||
}
|
||||
actionHandler.init(customApp);
|
||||
} else {
|
||||
final app = SupportedApp.supportedApps.firstOrNullWhere((e) => e.name == appName);
|
||||
actionHandler.init(app);
|
||||
}
|
||||
final app = getSupportedApp();
|
||||
actionHandler.init(app);
|
||||
} catch (e) {
|
||||
// couldn't decode, reset
|
||||
await prefs.clear();
|
||||
@@ -77,13 +37,44 @@ class Settings {
|
||||
actionHandler.init(null);
|
||||
}
|
||||
|
||||
Future<void> setApp(SupportedApp app) async {
|
||||
void setTrainerApp(SupportedApp app) {
|
||||
prefs.setString('trainer_app', app.name);
|
||||
}
|
||||
|
||||
SupportedApp? getTrainerApp() {
|
||||
final appName = prefs.getString('trainer_app');
|
||||
if (appName == null) {
|
||||
return null;
|
||||
}
|
||||
return SupportedApp.supportedApps.firstOrNullWhere((e) => e.name == appName);
|
||||
}
|
||||
|
||||
Future<void> setSupportedApp(SupportedApp app) async {
|
||||
if (app is CustomApp) {
|
||||
await prefs.setStringList('customapp_${app.profileName}', app.encodeKeymap());
|
||||
}
|
||||
await prefs.setString('app', app.name);
|
||||
}
|
||||
|
||||
SupportedApp? getSupportedApp() {
|
||||
final appName = prefs.getString('app');
|
||||
if (appName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if it's a custom app with a profile name
|
||||
if (appName.startsWith('Custom') || prefs.containsKey('customapp_$appName')) {
|
||||
final customApp = CustomApp(profileName: appName);
|
||||
final appSetting = prefs.getStringList('customapp_$appName');
|
||||
if (appSetting != null) {
|
||||
customApp.decodeKeymap(appSetting);
|
||||
}
|
||||
return customApp;
|
||||
} else {
|
||||
return SupportedApp.supportedApps.firstOrNullWhere((e) => e.name == appName);
|
||||
}
|
||||
}
|
||||
|
||||
List<String> getCustomAppProfiles() {
|
||||
// Get all keys starting with 'customapp_'
|
||||
final keys = prefs.getKeys().where((key) => key.startsWith('customapp_')).toList();
|
||||
@@ -167,42 +158,6 @@ class Settings {
|
||||
await prefs.setBool('vibration_enabled', enabled);
|
||||
}
|
||||
|
||||
Future<List<String>> _migrateToPercentageBased(List<String> keymapData, Size screenSize) async {
|
||||
final migratedData = <String>[];
|
||||
|
||||
final needMigrations = keymapData.associateWith((encodedKeyPair) {
|
||||
final decoded = jsonDecode(encodedKeyPair);
|
||||
final touchPosData = decoded['touchPosition'];
|
||||
|
||||
// Convert pixel-based to percentage-based
|
||||
final x = (touchPosData['x'] as num).toDouble();
|
||||
final y = (touchPosData['y'] as num).toDouble();
|
||||
return x > 100.0 || y > 100.0;
|
||||
});
|
||||
|
||||
for (final entry in needMigrations.entries) {
|
||||
if (entry.value) {
|
||||
final decoded = jsonDecode(entry.key);
|
||||
final touchPosData = decoded['touchPosition'];
|
||||
|
||||
// Convert pixel-based to percentage-based
|
||||
final x = (touchPosData['x'] as num).toDouble();
|
||||
final y = (touchPosData['y'] as num).toDouble();
|
||||
final newX = (x / screenSize.width).clamp(0.0, 1.0) * 100.0;
|
||||
final newY = (y / screenSize.height).clamp(0.0, 1.0) * 100.0;
|
||||
|
||||
// Update the JSON structure
|
||||
decoded['touchPosition'] = {'x': newX, 'y': newY};
|
||||
|
||||
migratedData.add(jsonEncode(decoded));
|
||||
} else {
|
||||
migratedData.add(entry.key);
|
||||
}
|
||||
}
|
||||
|
||||
return migratedData;
|
||||
}
|
||||
|
||||
bool getMyWhooshLinkEnabled() {
|
||||
return prefs.getBool('mywhoosh_link_enabled') ?? true;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BetaPill extends StatelessWidget {
|
||||
const BetaPill({super.key});
|
||||
final String text;
|
||||
const BetaPill({super.key, this.text = 'BETA'});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -14,7 +15,7 @@ class BetaPill extends StatelessWidget {
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(
|
||||
'BETA',
|
||||
text,
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 10,
|
||||
|
||||
@@ -69,6 +69,7 @@ flutter:
|
||||
- INSTRUCTIONS_WINDOWS.md
|
||||
- INSTRUCTIONS_MACOS.md
|
||||
- shorebird.yaml
|
||||
- icon.png
|
||||
|
||||
msix_config:
|
||||
display_name: SwiftControl
|
||||
|
||||
@@ -28,8 +28,8 @@ void main() {
|
||||
|
||||
test('Should save and retrieve custom profile', () async {
|
||||
final customApp = CustomApp(profileName: 'Race');
|
||||
await settings.setApp(customApp);
|
||||
|
||||
await settings.setSupportedApp(customApp);
|
||||
|
||||
final profiles = settings.getCustomAppProfiles();
|
||||
expect(profiles.contains('Race'), true);
|
||||
});
|
||||
@@ -38,11 +38,11 @@ void main() {
|
||||
final workout = CustomApp(profileName: 'Workout');
|
||||
final race = CustomApp(profileName: 'Race');
|
||||
final event = CustomApp(profileName: 'Event');
|
||||
|
||||
await settings.setApp(workout);
|
||||
await settings.setApp(race);
|
||||
await settings.setApp(event);
|
||||
|
||||
|
||||
await settings.setSupportedApp(workout);
|
||||
await settings.setSupportedApp(race);
|
||||
await settings.setSupportedApp(event);
|
||||
|
||||
final profiles = settings.getCustomAppProfiles();
|
||||
expect(profiles.contains('Workout'), true);
|
||||
expect(profiles.contains('Race'), true);
|
||||
@@ -52,10 +52,10 @@ void main() {
|
||||
|
||||
test('Should duplicate custom profile', () async {
|
||||
final original = CustomApp(profileName: 'Original');
|
||||
await settings.setApp(original);
|
||||
|
||||
await settings.setSupportedApp(original);
|
||||
|
||||
await settings.duplicateCustomAppProfile('Original', 'Copy');
|
||||
|
||||
|
||||
final profiles = settings.getCustomAppProfiles();
|
||||
expect(profiles.contains('Original'), true);
|
||||
expect(profiles.contains('Copy'), true);
|
||||
@@ -64,13 +64,13 @@ void main() {
|
||||
|
||||
test('Should delete custom profile', () async {
|
||||
final customApp = CustomApp(profileName: 'ToDelete');
|
||||
await settings.setApp(customApp);
|
||||
|
||||
await settings.setSupportedApp(customApp);
|
||||
|
||||
var profiles = settings.getCustomAppProfiles();
|
||||
expect(profiles.contains('ToDelete'), true);
|
||||
|
||||
|
||||
await settings.deleteCustomAppProfile('ToDelete');
|
||||
|
||||
|
||||
profiles = settings.getCustomAppProfiles();
|
||||
expect(profiles.contains('ToDelete'), false);
|
||||
});
|
||||
@@ -81,10 +81,10 @@ void main() {
|
||||
'customapp': ['test_data'],
|
||||
'app': 'Custom',
|
||||
});
|
||||
|
||||
|
||||
final newSettings = Settings();
|
||||
await newSettings.init();
|
||||
|
||||
|
||||
// Check that migration happened
|
||||
expect(newSettings.prefs.containsKey('customapp'), false);
|
||||
expect(newSettings.prefs.containsKey('customapp_Custom'), true);
|
||||
@@ -96,10 +96,10 @@ void main() {
|
||||
'customapp_Custom': ['new_data'],
|
||||
'app': 'Custom',
|
||||
});
|
||||
|
||||
|
||||
final newSettings = Settings();
|
||||
await newSettings.init();
|
||||
|
||||
|
||||
// Old key should still exist because new key already existed
|
||||
expect(newSettings.getCustomAppKeymap('customapp'), null);
|
||||
final customKeymap = newSettings.getCustomAppKeymap('Custom');
|
||||
@@ -108,8 +108,8 @@ void main() {
|
||||
|
||||
test('Should export custom profile as JSON', () async {
|
||||
final customApp = CustomApp(profileName: 'TestProfile');
|
||||
await settings.setApp(customApp);
|
||||
|
||||
await settings.setSupportedApp(customApp);
|
||||
|
||||
final jsonData = settings.exportCustomAppProfile('TestProfile');
|
||||
expect(jsonData, isNotNull);
|
||||
expect(jsonData, contains('version'));
|
||||
@@ -120,12 +120,12 @@ void main() {
|
||||
test('Should import custom profile from JSON', () async {
|
||||
// First export a profile
|
||||
final customApp = CustomApp(profileName: 'ExportTest');
|
||||
await settings.setApp(customApp);
|
||||
await settings.setSupportedApp(customApp);
|
||||
final jsonData = settings.exportCustomAppProfile('ExportTest');
|
||||
|
||||
|
||||
// Import with a new name
|
||||
final success = await settings.importCustomAppProfile(jsonData!, newProfileName: 'ImportTest');
|
||||
|
||||
|
||||
expect(success, true);
|
||||
final profiles = settings.getCustomAppProfiles();
|
||||
expect(profiles.contains('ImportTest'), true);
|
||||
|
||||
Reference in New Issue
Block a user