wake-alarm/phone_app/lib/screens/github_mirror_screen.dart
Krzysztof kuhy Rudnicki 0059d44571
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Audit icons: add 1-word hover tooltips
Todo: fe70b845
2026-11-22 19:20:38 +01:00

144 lines
4.3 KiB
Dart

import 'dart:async';
import 'package:github_device_auth/github_device_auth.dart';
import 'package:material_ui/material_ui.dart';
import 'package:wake_alarm_sync/services/sync_settings.dart';
import 'package:wake_alarm_sync/ui/cues.dart';
/// The GitHub mirror screen: cutover-only sync transport, not a peer of
/// Firebase.
///
/// Kept app-local rather than folded into the shared `sync_settings_ui`
/// package since that package carries no GitHub surface at all. Logic moved
/// verbatim from the old combined settings screen.
class GitHubMirrorScreen extends StatefulWidget {
/// Creates a [GitHubMirrorScreen].
const GitHubMirrorScreen({super.key});
@override
State<GitHubMirrorScreen> createState() => _GitHubMirrorScreenState();
}
class _GitHubMirrorScreenState extends State<GitHubMirrorScreen> {
SyncSettings _settings = const SyncSettings(token: '');
bool _busy = false;
String _status = '';
@override
void initState() {
super.initState();
unawaited(_load());
}
Future<void> _load() async {
final s = await SyncSettings.load();
if (!mounted) return;
setState(() => _settings = s);
}
Future<void> _connectGitHub() async {
final auth = GitHubDeviceAuth(clientId: SyncSettings.defaultClientId);
try {
final device = await auth.requestDeviceCode();
if (!mounted) return;
final token = await showDialog<String>(
context: context,
barrierDismissible: false,
builder: (_) => DeviceCodeDialog(device: device, auth: auth),
);
if (token == null || token.isEmpty) return;
setState(() {
_busy = true;
_status = 'Connecting…';
});
final updated = SyncSettings(token: token);
await updated.save();
if (mounted) {
setState(() {
_settings = updated;
_busy = false;
_status = 'Connected! Alarm will sync to kuhyx/syncs.';
});
}
} on Exception catch (e) {
if (mounted) {
setState(() {
_busy = false;
_status = 'Error: $e';
});
}
} finally {
auth.close();
}
}
Future<void> _disconnect() async {
await SyncSettings.clear();
if (mounted) {
setState(() {
_settings = const SyncSettings(token: '');
_status = 'Disconnected.';
});
}
}
@override
Widget build(BuildContext context) {
final isConnected = _settings.isConfigured;
return Scaffold(
appBar: AppBar(),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Tooltip(
message: isConnected ? 'Connected' : 'Disconnected',
child: Icon(
isConnected ? Icons.link : Icons.link_off,
color: isConnected
? Theme.of(context).colorScheme.tertiary
: Theme.of(context).colorScheme.onSurfaceVariant,
size: 32,
semanticLabel: isConnected
? 'Connected to GitHub.'
: 'Not connected to GitHub.',
),
),
const SizedBox(height: 12),
if (!isConnected)
Tooltip(
message: 'Connect',
child: ElevatedButton(
key: const ValueKey<String>('github-connect'),
onPressed: _busy ? null : _connectGitHub,
child: const Icon(
Icons.link,
semanticLabel: 'Connect GitHub',
),
),
)
else
Tooltip(
message: 'Disconnect',
child: OutlinedButton(
key: const ValueKey<String>('github-disconnect'),
onPressed: _busy ? null : _disconnect,
child: const Icon(
Icons.link_off,
semanticLabel: 'Disconnect GitHub',
),
),
),
const SizedBox(height: 16),
StatusCue(semanticLabel: _status),
],
),
),
),
);
}
}