todo-app/lib/ui/github_mirror_screen.dart
Krzysztof kuhy Rudnicki 3a747c2e51
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
Report errors and slow waits to the board
crdt_sync_flutter 0.9.0's installAppErrors in main(): uncaught errors and
every spinner shown over 1 s (WaitWatch: sync.run, mirror.test) upload to
RTDB /todo/errors/<device> with the versionCode, where the PC app-errors
ingest files board tickets (kuhyx/utils DOCS-app-errors.md).

The app moves to material_ui first: sync_settings_ui 0.8.x and
design_system 0.4.x sit on it, and Flutter 3.50 deprecates
flutter/material. Pins: crdt_sync 0.14.1, csf 0.9.0, ssui 0.8.3,
gda 0.5.1, design_system 0.4.1, image 4.13.1, mcp_dart 2.4.4.
file_picker stays 13.3.3, held by data_transfer_flutter 0.2.2
(allowlisted, self-clearing).

appRoute takes a route name so each report names its screen; the
release build passes APP_BUILD/APP_VERSION. Nothing new on screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 23:07:58 +01:00

255 lines
7.3 KiB
Dart

import 'dart:async';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' show WaitWatch;
import 'package:github_device_auth/github_device_auth.dart';
import 'package:http/http.dart' as http;
import 'package:material_ui/material_ui.dart';
import 'package:todo/analytics/analytics_service.dart';
import 'package:todo/data/app_settings.dart';
import 'package:todo/data/note_repository.dart';
import 'package:todo/sync/run_sync.dart';
import 'package:todo/sync/sync_settings.dart';
import 'package:todo/ui/status_cue.dart';
part 'github_mirror_screen_widget.dart';
class _GitHubMirrorScreenState extends State<GitHubMirrorScreen> {
late final TextEditingController _owner = TextEditingController(
text: widget.initial.owner,
);
late final TextEditingController _repo = TextEditingController(
text: widget.initial.repo,
);
late final TextEditingController _token = TextEditingController(
text: widget.initial.token,
);
late final TextEditingController _clientId = TextEditingController(
text: widget.initial.clientId,
);
bool _testing = false;
StatusCue? _status;
@override
void dispose() {
_owner.dispose();
_repo.dispose();
_token.dispose();
_clientId.dispose();
super.dispose();
}
SyncSettings get _current => SyncSettings(
owner: _owner.text.trim(),
repo: _repo.text.trim(),
token: _token.text.trim(),
clientId: _clientId.text.trim(),
);
/// Runs the OAuth device flow and, on success, fills in the token field.
Future<void> _connectGitHub() async {
final clientId = _clientId.text.trim();
if (clientId.isEmpty) {
return _show(
const StatusCue(
Icons.key_off,
'Enter the OAuth App client id first.',
error: true,
),
);
}
final auth = GitHubDeviceAuth(
clientId: clientId,
httpClient: widget.httpClient,
);
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.isNotEmpty) {
setState(() {
_token.text = token;
_status = const StatusCue(Icons.sync, 'Connected — syncing…');
});
await _current.save();
await _syncAfterConnect();
}
} on Exception catch (e) {
_show(StatusCue.failed('Could not start device flow: $e'));
} finally {
auth.close();
}
}
/// Syncs right after connecting so notes download at once, with a cue.
Future<void> _syncAfterConnect() async {
final s = _current;
try {
final run = await runSync(
widget.repository,
s,
appSettings: widget.appSettings.value,
analytics: widget.analytics,
httpClient: widget.httpClient,
firebaseFactory: widget.firebaseFactory,
stateStore: widget.stateStore,
);
widget.appSettings.value = widget.appSettings.value.adopt(
run.appSettings,
);
_show(
StatusCue.ok(
'Connected and synced (merged '
'${run.syncResult.mergedDevices} device(s)). Your notes are '
'up to date.',
),
);
} on Exception catch (e) {
_show(
StatusCue(
Icons.sync_problem,
'Connected, but sync failed: $e',
error: true,
),
);
}
}
/// Shows [cue] as the screen's latest result, if still mounted.
void _show(StatusCue cue) {
if (mounted) setState(() => _status = cue);
}
Future<void> _test() async {
setState(() {
_testing = true;
_status = null;
});
final s = _current;
final client = GitHubClient(
owner: s.owner,
repo: s.repo,
token: s.token,
httpClient: widget.httpClient,
);
try {
final ok = await client.canAccessRepo();
_show(
ok
? const StatusCue.ok('Connected — repo is reachable.')
: StatusCue(
Icons.lock,
'Could not access ${s.owner}/${s.repo}. Check token scope.',
error: true,
),
);
} on Exception catch (e) {
_show(StatusCue.failed('Error: $e'));
} finally {
client.close();
if (mounted) setState(() => _testing = false);
}
}
Future<void> _save() async {
final s = _current;
await s.save();
if (mounted) Navigator.of(context).pop(s);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Icon(
Icons.cloud_sync,
semanticLabel: 'Advanced sync (GitHub)',
),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Align(
alignment: Alignment.centerLeft,
child: IconButton.filled(
onPressed: _connectGitHub,
icon: const Icon(Icons.login, semanticLabel: 'Connect GitHub'),
),
),
const SizedBox(height: 16),
TextField(
controller: _owner,
decoration: const InputDecoration(
prefixIcon: Icon(Icons.person, semanticLabel: 'GitHub owner'),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
TextField(
controller: _repo,
decoration: const InputDecoration(
prefixIcon: Icon(Icons.folder, semanticLabel: 'Repository name'),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
TextField(
controller: _clientId,
decoration: const InputDecoration(
prefixIcon: Icon(Icons.key, semanticLabel: 'OAuth App client id'),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
TextField(
controller: _token,
obscureText: true,
decoration: const InputDecoration(
prefixIcon: Icon(
Icons.vpn_key,
semanticLabel: 'Access token (fallback)',
),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
Row(
children: [
IconButton.outlined(
onPressed: _testing ? null : _test,
icon: _testing
? const WaitWatch(
operation: 'mirror.test',
child: SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
),
)
: const Icon(
Icons.wifi_tethering,
semanticLabel: 'Test connection',
),
),
const SizedBox(width: 12),
IconButton.filled(
onPressed: _save,
icon: const Icon(Icons.save, semanticLabel: 'Save'),
),
],
),
if (_status != null) ...[
const SizedBox(height: 16),
StatusCueView(_status!),
],
],
),
);
}
}