mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-10 19:07:02 +01:00
todo was the donor for both halves of the GitHub device flow -- its
`lib/sync/github_device_auth.dart` was byte-identical to home_inventory's,
and `lib/ui/device_code_dialog.dart` was the public version of the modal
diet-guard and wake-alarm each carried privately. All of it now lives in
`github_device_auth` in the kuhyx/utils monorepo.
The package is not a straight copy of what left here: it takes the more
capable variant of each divergence found across the four apps, so this app
gains two fixes it never had --
* `pollForToken` retries a transient `ClientException` instead of
failing, because GitHub can close the connection at the moment the
user authorizes (wake-alarm's fix)
* the dialog catches `on Object`, not `on Exception`, so an
`ArgumentError` surfaces as an error message instead of leaving the
dialog spinning forever
The local `test/github_device_auth_test.dart` is deleted; its cases live in
the package now, where they gate all four consumers. 300 tests pass and
`test/github_mirror_device_flow_test.dart` -- which drives the shared
dialog end to end -- is among them.
Deployed to the phone as versionCode 17 via phone_deploy.sh (signer
verified, `adb install -r`, no uninstall): launches with all 109 notes
intact.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U4Rj3s7K2qzrLNgJqqPhno
232 lines
6.9 KiB
Dart
232 lines
6.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:github_device_auth/github_device_auth.dart';
|
|
import 'package:http/http.dart' as http;
|
|
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';
|
|
|
|
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;
|
|
String? _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) {
|
|
setState(() => _status = 'Enter the OAuth App client id first.');
|
|
return;
|
|
}
|
|
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 = 'Connected — syncing…';
|
|
});
|
|
await _current.save();
|
|
await _syncAfterConnect();
|
|
}
|
|
} on Exception catch (e) {
|
|
if (mounted) setState(() => _status = 'Could not start device flow: $e');
|
|
} finally {
|
|
auth.close();
|
|
}
|
|
}
|
|
|
|
/// Runs a sync right after connecting so the user's notes download
|
|
/// immediately and they get clear confirmation it worked.
|
|
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,
|
|
);
|
|
if (mounted) {
|
|
setState(
|
|
() => _status =
|
|
'Connected and synced (merged '
|
|
'${run.syncResult.mergedDevices} device(s)). Your notes are '
|
|
'up to date.',
|
|
);
|
|
}
|
|
} on Exception catch (e) {
|
|
if (mounted) setState(() => _status = 'Connected, but sync failed: $e');
|
|
}
|
|
}
|
|
|
|
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();
|
|
setState(
|
|
() => _status = ok
|
|
? 'Connected — repo is reachable.'
|
|
: 'Could not access ${s.owner}/${s.repo}. Check token scope.',
|
|
);
|
|
} on Exception catch (e) {
|
|
setState(() => _status = '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 Text('Advanced sync (GitHub)')),
|
|
body: ListView(
|
|
padding: const EdgeInsets.all(16),
|
|
children: [
|
|
Text(
|
|
'Authorize in your browser — no token to paste. Syncs to '
|
|
'kuhyx/syncs by default.',
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
const SizedBox(height: 12),
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: FilledButton.icon(
|
|
onPressed: _connectGitHub,
|
|
icon: const Icon(Icons.login),
|
|
label: const Text('Connect GitHub'),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: _owner,
|
|
decoration: const InputDecoration(
|
|
labelText: 'GitHub owner',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _repo,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Repository name',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _clientId,
|
|
decoration: const InputDecoration(
|
|
labelText: 'OAuth App client id',
|
|
helperText: 'Leave as the baked-in default unless self-hosting',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _token,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Access token (fallback)',
|
|
helperText: 'Contents: read/write on the sync repo only',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
children: [
|
|
OutlinedButton.icon(
|
|
onPressed: _testing ? null : _test,
|
|
icon: _testing
|
|
? const SizedBox(
|
|
width: 16,
|
|
height: 16,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.wifi_tethering),
|
|
label: const Text('Test connection'),
|
|
),
|
|
const SizedBox(width: 12),
|
|
FilledButton.icon(
|
|
onPressed: _save,
|
|
icon: const Icon(Icons.save),
|
|
label: const Text('Save'),
|
|
),
|
|
],
|
|
),
|
|
if (_status != null) ...[
|
|
const SizedBox(height: 16),
|
|
Text(_status!, style: Theme.of(context).textTheme.bodyMedium),
|
|
],
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|