mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-02 10:09:09 +01:00
Connect an account in Settings > Connected accounts (in the browser, through bikecontrol.app/connect/<service>); rides upload by hand from Export or automatically once saved. Tokens stay in the activity-sync function. Garmin has no upload API for us and its app files a shared .fit as a course, so the Garmin row saves the file and opens Connect's import page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
203 lines
7.4 KiB
Dart
203 lines
7.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:bike_control/gen/l10n.dart';
|
|
import 'package:bike_control/main.dart' show recordError;
|
|
import 'package:bike_control/pages/plan/plan_account_page.dart';
|
|
import 'package:bike_control/services/activity_sync/activity_sync_api.dart';
|
|
import 'package:bike_control/services/activity_sync/activity_sync_service.dart';
|
|
import 'package:bike_control/utils/core.dart';
|
|
import 'package:bike_control/widgets/ui/bk_grouped_section.dart';
|
|
import 'package:bike_control/widgets/ui/bk_switch_row.dart';
|
|
import 'package:bike_control/widgets/ui/small_progress_indicator.dart';
|
|
import 'package:bike_control/widgets/ui/toast.dart';
|
|
import 'package:prop/prop.dart' show LogLevel;
|
|
import 'package:shadcn_flutter/shadcn_flutter.dart';
|
|
|
|
/// Strava's own "Connect with Strava" artwork (assets/strava/README.md): the
|
|
/// image carries the wording and logo, so nothing is drawn over it, it keeps
|
|
/// its 48 px height, and it is the only thing that starts Strava's consent.
|
|
class StravaConnectButton extends StatelessWidget {
|
|
const StravaConnectButton({super.key, required this.onPressed});
|
|
|
|
final VoidCallback? onPressed;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Button(
|
|
style: const ButtonStyle.ghost(density: ButtonDensity.compact),
|
|
onPressed: onPressed,
|
|
child: Opacity(
|
|
opacity: onPressed == null ? 0.6 : 1,
|
|
child: Image.asset(
|
|
'assets/strava/btn_strava_connect_with_orange.png',
|
|
height: 48,
|
|
semanticLabel: 'Connect with Strava',
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Opens the account page, where the rider signs in.
|
|
void openSignIn(BuildContext context) {
|
|
unawaited(Navigator.of(context).push(MaterialPageRoute(builder: (_) => const PlanAccountPage())));
|
|
}
|
|
|
|
/// Starts connecting [service]; resolves to whether it ended up connected.
|
|
/// A timeout or a cancel is quiet (the row shows the state); a failure to
|
|
/// even start is said.
|
|
Future<bool> connectActivityService(ActivitySyncService sync, ActivityService service) async {
|
|
try {
|
|
return await sync.connect(service);
|
|
} catch (e, s) {
|
|
await recordError(e, s, context: 'ActivitySync.connect.${service.id}');
|
|
buildToast(
|
|
level: LogLevel.LOGLEVEL_ERROR,
|
|
title: AppLocalizations.current.activitySyncConnectFailed(service.displayName),
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Settings → Connected accounts: Strava and (once its backend is set up)
|
|
/// intervals.icu. Each connected one has its own "every ride automatically".
|
|
class ActivitySyncSection extends StatelessWidget {
|
|
const ActivitySyncSection({super.key, this.sync, this.isSignedIn});
|
|
|
|
/// Test seams; null uses the app's.
|
|
final ActivitySyncService? sync;
|
|
final bool Function()? isSignedIn;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final service = sync ?? core.activitySync;
|
|
return ListenableBuilder(
|
|
listenable: service,
|
|
builder: (context, _) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final signedIn = (isSignedIn ?? service.isSignedIn)();
|
|
// Strava is always offered; intervals.icu only once the backend says
|
|
// it is set up (its OAuth app needs approval first).
|
|
final services = {ActivityService.strava, ...service.available};
|
|
return BkGroupedSection(
|
|
key: const ValueKey('settings-activity-sync'),
|
|
header: l10n.activitySyncSection,
|
|
footer: l10n.activitySyncFooter,
|
|
children: [
|
|
for (final s in ActivityService.values.where(services.contains)) ...[
|
|
_ServiceRow(sync: service, service: s, signedIn: signedIn),
|
|
if (signedIn && service.isConnected(s))
|
|
BkSwitchRow(
|
|
key: ValueKey('activity-sync-auto-${s.id}'),
|
|
icon: LucideIcons.upload,
|
|
title: l10n.activitySyncAutoUpload,
|
|
value: service.autoUpload(s),
|
|
onToggle: () => unawaited(service.setAutoUpload(s, !service.autoUpload(s))),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ServiceRow extends StatelessWidget {
|
|
const _ServiceRow({required this.sync, required this.service, required this.signedIn});
|
|
|
|
final ActivitySyncService sync;
|
|
final ActivityService service;
|
|
final bool signedIn;
|
|
|
|
IconData get _icon => service == ActivityService.strava ? LucideIcons.activity : LucideIcons.chartLine;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final key = ValueKey('activity-sync-${service.id}');
|
|
if (!signedIn) {
|
|
return BkGroupedRow(
|
|
key: key,
|
|
icon: _icon,
|
|
title: service.displayName,
|
|
subtitle: l10n.activitySyncSignInToConnect,
|
|
chevron: true,
|
|
onPressed: () => openSignIn(context),
|
|
);
|
|
}
|
|
final connection = sync.connections[service];
|
|
if (sync.isConnecting(service)) {
|
|
return BkGroupedRow(
|
|
key: key,
|
|
icon: _icon,
|
|
title: service.displayName,
|
|
subtitle: l10n.activitySyncWaiting,
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
spacing: 8,
|
|
children: [
|
|
const SizedBox(width: 16, height: 16, child: SmallProgressIndicator()),
|
|
Button.ghost(onPressed: () => sync.cancelConnect(service), child: Text(l10n.cancel)),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
if (connection != null && !connection.reauthRequired) {
|
|
return BkGroupedRow(
|
|
key: key,
|
|
icon: _icon,
|
|
title: service.displayName,
|
|
subtitle: switch (connection.athleteName) {
|
|
final name? => l10n.activitySyncConnectedAs(name),
|
|
null => l10n.activitySyncConnected,
|
|
},
|
|
trailing: Button.ghost(
|
|
key: ValueKey('activity-sync-disconnect-${service.id}'),
|
|
onPressed: () => unawaited(_disconnect(context)),
|
|
child: Text(l10n.activitySyncDisconnect),
|
|
),
|
|
);
|
|
}
|
|
void connect() => unawaited(connectActivityService(sync, service));
|
|
return BkGroupedRow(
|
|
key: key,
|
|
icon: _icon,
|
|
title: service.displayName,
|
|
subtitle: connection?.reauthRequired == true
|
|
? l10n.activitySyncReconnect(service.displayName)
|
|
: l10n.ridesUploadTo(service.displayName),
|
|
trailing: service == ActivityService.strava
|
|
? StravaConnectButton(key: ValueKey('activity-sync-connect-${service.id}'), onPressed: connect)
|
|
: Button.secondary(
|
|
key: ValueKey('activity-sync-connect-${service.id}'),
|
|
onPressed: connect,
|
|
child: Text(l10n.activitySyncConnect),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _disconnect(BuildContext context) async {
|
|
final l10n = AppLocalizations.of(context);
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (dialogContext) => AlertDialog(
|
|
title: Text(l10n.activitySyncDisconnectTitle(service.displayName)),
|
|
content: Text(l10n.activitySyncDisconnectBody(service.displayName)),
|
|
actions: [
|
|
Button.outline(onPressed: () => Navigator.of(dialogContext).pop(false), child: Text(l10n.cancel)),
|
|
Button.destructive(
|
|
onPressed: () => Navigator.of(dialogContext).pop(true),
|
|
child: Text(l10n.activitySyncDisconnect),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (confirmed != true) return;
|
|
try {
|
|
await sync.disconnect(service);
|
|
} catch (e, s) {
|
|
await recordError(e, s, context: 'ActivitySync.disconnect.${service.id}');
|
|
}
|
|
}
|
|
}
|