mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-28 22:49:15 +01:00
Compare commits
No commits in common. "2cdb642e849f8bcf69d262eb77a3379ee0bdfc3b" and "66bc3a9e5535fda8f94d8cbe7d8263f15d91e30a" have entirely different histories.
2cdb642e84
...
66bc3a9e55
@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
|
|||||||
|
|
||||||
import 'package:wake_alarm_sync/screens/settings_screen.dart';
|
import 'package:wake_alarm_sync/screens/settings_screen.dart';
|
||||||
import 'package:wake_alarm_sync/services/alarm_sync_service.dart';
|
import 'package:wake_alarm_sync/services/alarm_sync_service.dart';
|
||||||
import 'package:wake_alarm_sync/services/firebase_backend.dart';
|
|
||||||
import 'package:wake_alarm_sync/services/sync_settings.dart';
|
import 'package:wake_alarm_sync/services/sync_settings.dart';
|
||||||
import 'package:wake_alarm_sync/ui/theme.dart';
|
import 'package:wake_alarm_sync/ui/theme.dart';
|
||||||
|
|
||||||
@ -44,15 +43,9 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
// hiccup): keep the on-screen defaults rather than crashing.
|
// hiccup): keep the on-screen defaults rather than crashing.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Either backend counts: a device connected only to Firebase is fully
|
|
||||||
// configured, and gating on the GitHub token alone would lock it out of
|
|
||||||
// syncing entirely once the mirror is retired.
|
|
||||||
final firebaseAccount = await loadAccount();
|
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
_service = service;
|
_service = service;
|
||||||
setState(
|
setState(() => _configured = settings.isConfigured);
|
||||||
() => _configured = settings.isConfigured || firebaseAccount != null,
|
|
||||||
);
|
|
||||||
|
|
||||||
final alarm = service.currentAlarm;
|
final alarm = service.currentAlarm;
|
||||||
if (alarm != null && mounted) {
|
if (alarm != null && mounted) {
|
||||||
@ -98,11 +91,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
Future<void> _syncAndSet() async {
|
Future<void> _syncAndSet() async {
|
||||||
final settings = await SyncSettings.load();
|
final settings = await SyncSettings.load();
|
||||||
final service = _service;
|
final service = _service;
|
||||||
// syncBackend() falls back to GitHub alone, or pushes to Firebase alone
|
if (!settings.isConfigured || service == null) {
|
||||||
// via the mirror, so either credential is enough to proceed.
|
setState(() => _status = 'Configure GitHub in Settings first.');
|
||||||
final hasFirebase = await loadAccount() != null;
|
|
||||||
if ((!settings.isConfigured && !hasFirebase) || service == null) {
|
|
||||||
setState(() => _status = 'Connect a sync backend in Settings first.');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,7 +209,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
if (!_configured) ...[
|
if (!_configured) ...[
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
Text(
|
Text(
|
||||||
'Open Settings (gear icon) to connect Firebase.',
|
'Open Settings (gear icon) to connect GitHub.',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: Theme.of(
|
color: Theme.of(
|
||||||
|
|||||||
@ -1,21 +1,17 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:crdt_sync/crdt_sync.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
import 'package:wake_alarm_sync/services/firebase_backend.dart';
|
|
||||||
import 'package:wake_alarm_sync/services/github_device_auth.dart';
|
import 'package:wake_alarm_sync/services/github_device_auth.dart';
|
||||||
import 'package:wake_alarm_sync/services/sync_settings.dart';
|
import 'package:wake_alarm_sync/services/sync_settings.dart';
|
||||||
import 'package:wake_alarm_sync/ui/theme.dart';
|
import 'package:wake_alarm_sync/ui/theme.dart';
|
||||||
|
|
||||||
/// Settings screen for the backends the alarm syncs through.
|
/// Settings screen for the GitHub connection used to sync the alarm.
|
||||||
///
|
///
|
||||||
/// Firebase is the primary path: sign in once with the shared sync account,
|
/// The "Connect GitHub" button runs the OAuth device flow — the user
|
||||||
/// whose password is kept in the OS keystore. GitHub is only the cutover
|
/// enters a short code on github.com; no token pasting required.
|
||||||
/// mirror, so its device-flow connection sits under "Advanced" rather than
|
|
||||||
/// competing with Firebase as a visible choice.
|
|
||||||
class SettingsScreen extends StatefulWidget {
|
class SettingsScreen extends StatefulWidget {
|
||||||
/// Creates a [SettingsScreen].
|
/// Creates a [SettingsScreen].
|
||||||
const SettingsScreen({super.key});
|
const SettingsScreen({super.key});
|
||||||
@ -26,9 +22,6 @@ class SettingsScreen extends StatefulWidget {
|
|||||||
|
|
||||||
class _SettingsScreenState extends State<SettingsScreen> {
|
class _SettingsScreenState extends State<SettingsScreen> {
|
||||||
SyncSettings _settings = const SyncSettings(token: '');
|
SyncSettings _settings = const SyncSettings(token: '');
|
||||||
final _emailController = TextEditingController();
|
|
||||||
final _passwordController = TextEditingController();
|
|
||||||
bool _firebaseConnected = false;
|
|
||||||
bool _busy = false;
|
bool _busy = false;
|
||||||
String _status = '';
|
String _status = '';
|
||||||
|
|
||||||
@ -38,68 +31,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
unawaited(_load());
|
unawaited(_load());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
_emailController.dispose();
|
|
||||||
_passwordController.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _load() async {
|
Future<void> _load() async {
|
||||||
final s = await SyncSettings.load();
|
final s = await SyncSettings.load();
|
||||||
// Reflect a previously-stored account, so a returning user sees the real
|
if (mounted) setState(() => _settings = s);
|
||||||
// state instead of an empty form that looks unconfigured.
|
|
||||||
final account = await loadAccount();
|
|
||||||
if (!mounted) return;
|
|
||||||
if (account != null) _emailController.text = account.email;
|
|
||||||
setState(() {
|
|
||||||
_settings = s;
|
|
||||||
_firebaseConnected = account != null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Stores the typed account and signs in immediately, so a typo surfaces
|
|
||||||
/// here rather than as a silent background failure at push time.
|
|
||||||
Future<void> _connectFirebase() async {
|
|
||||||
final email = _emailController.text.trim();
|
|
||||||
final password = _passwordController.text;
|
|
||||||
if (email.isEmpty || password.isEmpty) {
|
|
||||||
setState(() => _status = 'Enter the sync account email and password.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setState(() {
|
|
||||||
_busy = true;
|
|
||||||
_status = 'Signing in…';
|
|
||||||
});
|
|
||||||
await saveAccount(FirebaseAccount(email: email, password: password));
|
|
||||||
final client = await openFirebase();
|
|
||||||
if (!mounted) return;
|
|
||||||
if (client == null) {
|
|
||||||
await clearAccount();
|
|
||||||
setState(() {
|
|
||||||
_busy = false;
|
|
||||||
_firebaseConnected = false;
|
|
||||||
_status = 'Error: Firebase rejected that account.';
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_passwordController.clear();
|
|
||||||
setState(() {
|
|
||||||
_busy = false;
|
|
||||||
_firebaseConnected = true;
|
|
||||||
_status = 'Connected to Firebase.';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _disconnectFirebase() async {
|
|
||||||
await clearAccount();
|
|
||||||
if (!mounted) return;
|
|
||||||
_emailController.clear();
|
|
||||||
_passwordController.clear();
|
|
||||||
setState(() {
|
|
||||||
_firebaseConnected = false;
|
|
||||||
_status = 'Firebase disconnected.';
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _connectGitHub() async {
|
Future<void> _connectGitHub() async {
|
||||||
@ -162,74 +96,30 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: [
|
children: [
|
||||||
_StatusCard(connected: _firebaseConnected),
|
_StatusCard(connected: isConnected),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
Text(
|
if (!isConnected)
|
||||||
'Firebase sync',
|
ElevatedButton.icon(
|
||||||
style: Theme.of(context).textTheme.titleMedium,
|
onPressed: _busy ? null : _connectGitHub,
|
||||||
),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
Text(
|
|
||||||
_firebaseConnected
|
|
||||||
? 'Connected. The alarm goes to Firebase first, and still '
|
|
||||||
'mirrors to GitHub until every device has moved.'
|
|
||||||
: 'Not connected — syncing over GitHub only. Enter the '
|
|
||||||
'shared sync account to move this device over. The '
|
|
||||||
'password is kept in the device keystore, never in '
|
|
||||||
'the app or the repo.',
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 16,
|
|
||||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
// Once connected the account is shown as read-only text: an
|
|
||||||
// editable email box beside an empty password box reads as
|
|
||||||
// "you still have to enter this", making a connected device
|
|
||||||
// look unconfigured.
|
|
||||||
if (_firebaseConnected)
|
|
||||||
Row(
|
|
||||||
children: [
|
|
||||||
const Icon(Icons.cloud_done, size: 20),
|
|
||||||
const SizedBox(width: 8),
|
|
||||||
Expanded(child: Text(_emailController.text)),
|
|
||||||
TextButton(
|
|
||||||
onPressed: _busy ? null : _disconnectFirebase,
|
|
||||||
child: const Text('Disconnect'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
else ...[
|
|
||||||
TextField(
|
|
||||||
controller: _emailController,
|
|
||||||
keyboardType: TextInputType.emailAddress,
|
|
||||||
autocorrect: false,
|
|
||||||
decoration: const InputDecoration(
|
|
||||||
labelText: 'Sync account email',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
TextField(
|
|
||||||
controller: _passwordController,
|
|
||||||
obscureText: true,
|
|
||||||
autocorrect: false,
|
|
||||||
decoration: const InputDecoration(
|
|
||||||
labelText: 'Sync account password',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
FilledButton.icon(
|
|
||||||
onPressed: _busy ? null : _connectFirebase,
|
|
||||||
icon: _busy
|
icon: _busy
|
||||||
? const SizedBox(
|
? const SizedBox(
|
||||||
width: 18,
|
width: 18,
|
||||||
height: 18,
|
height: 18,
|
||||||
child: CircularProgressIndicator(strokeWidth: 2),
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
)
|
)
|
||||||
: const Icon(Icons.cloud_done),
|
: const Icon(Icons.link),
|
||||||
label: const Text('Connect Firebase'),
|
label: const Text('Connect GitHub'),
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||||
|
textStyle: const TextStyle(fontSize: 16),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
else
|
||||||
|
OutlinedButton.icon(
|
||||||
|
onPressed: _busy ? null : _disconnect,
|
||||||
|
icon: const Icon(Icons.link_off),
|
||||||
|
label: const Text('Disconnect'),
|
||||||
),
|
),
|
||||||
],
|
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
if (_status.isNotEmpty)
|
if (_status.isNotEmpty)
|
||||||
Text(
|
Text(
|
||||||
@ -243,55 +133,19 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
const Divider(height: 32),
|
const Divider(height: 32),
|
||||||
// GitHub is the cutover mirror, not a choice: everything about
|
Text(
|
||||||
// it lives here so the primary path stays a single decision.
|
'PC config — create this file:\n'
|
||||||
ExpansionTile(
|
'~/.config/wake_alarm/sync.json\n\n'
|
||||||
title: const Text('Advanced (GitHub mirror)'),
|
'{ "token": "<same token>" }\n\n'
|
||||||
tilePadding: EdgeInsets.zero,
|
'The token needs "repo" scope so the PC can read '
|
||||||
childrenPadding: const EdgeInsets.only(bottom: 8),
|
'kuhyx/syncs at shutdown.',
|
||||||
children: [
|
// Was 12px/Colors.grey — below the 16px reading floor (rule
|
||||||
Align(
|
// 20) and off-palette (rule 4); this is real setup
|
||||||
alignment: Alignment.centerLeft,
|
// instructions, not decorative chrome.
|
||||||
child: Text(
|
style: TextStyle(
|
||||||
isConnected
|
fontSize: 16,
|
||||||
? 'Connected to GitHub.'
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||||
: 'Not connected to GitHub.',
|
),
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 16,
|
|
||||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
if (!isConnected)
|
|
||||||
ElevatedButton.icon(
|
|
||||||
onPressed: _busy ? null : _connectGitHub,
|
|
||||||
icon: const Icon(Icons.link),
|
|
||||||
label: const Text('Connect GitHub'),
|
|
||||||
)
|
|
||||||
else
|
|
||||||
OutlinedButton.icon(
|
|
||||||
onPressed: _busy ? null : _disconnect,
|
|
||||||
icon: const Icon(Icons.link_off),
|
|
||||||
label: const Text('Disconnect GitHub'),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 16),
|
|
||||||
Text(
|
|
||||||
'PC config — the PC reads Firebase first and falls back '
|
|
||||||
'to GitHub. For the GitHub fallback, create:\n'
|
|
||||||
'~/.config/wake_alarm/sync.json\n\n'
|
|
||||||
'{ "token": "<same token>" }\n\n'
|
|
||||||
'The token needs "repo" scope so the PC can read '
|
|
||||||
'kuhyx/syncs.',
|
|
||||||
// Was 12px/Colors.grey — below the 16px reading floor
|
|
||||||
// (rule 20) and off-palette (rule 4); this is real setup
|
|
||||||
// instructions, not decorative chrome.
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 16,
|
|
||||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -324,9 +178,7 @@ class _StatusCard extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
const SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(connected ? 'Connected to GitHub' : 'Not connected'),
|
||||||
connected ? 'Connected to Firebase' : 'Not connected',
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import 'package:crdt_sync/crdt_sync_io.dart';
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:wake_alarm_sync/services/firebase_backend.dart';
|
|
||||||
|
|
||||||
/// The alarm time this device holds, as read from or written to the store.
|
/// The alarm time this device holds, as read from or written to the store.
|
||||||
typedef Alarm = ({int hour, int minute});
|
typedef Alarm = ({int hour, int minute});
|
||||||
@ -15,15 +14,12 @@ typedef Alarm = ({int hour, int minute});
|
|||||||
///
|
///
|
||||||
/// The phone is the **sole writer**: it stores the chosen alarm locally in a
|
/// The phone is the **sole writer**: it stores the chosen alarm locally in a
|
||||||
/// [LogStore] (one HLC-stamped [Record], id [_recordId]) and pushes that log
|
/// [LogStore] (one HLC-stamped [Record], id [_recordId]) and pushes that log
|
||||||
/// to `wake-alarm-sync/devices/phone/alarm.json` — Firebase first, mirrored
|
/// to `wake-alarm-sync/devices/phone/alarm.json` in the private `kuhyx/syncs`
|
||||||
/// to the private `kuhyx/syncs` repo during the cutover (see [syncBackend]).
|
/// repo — the same "GitHub as dumb file storage under `devices/<id>/`" layout
|
||||||
/// The path is deliberately identical on both backends, so only the transport
|
/// diet_guard's app and the workout app already use. The PC side
|
||||||
/// differs.
|
/// (`shutdown-wrapper.sh`) is read-only: it fetches that file at shutdown to
|
||||||
///
|
/// set `rtcwake`. There is no pull/merge: the phone is the only writer, so its
|
||||||
/// The PC side is read-only (`wake_alarm/_sync.py`, which tries Firebase then
|
/// local store is always the source of truth.
|
||||||
/// GitHub, caching the result for `shutdown-wrapper.sh`). There is no
|
|
||||||
/// pull/merge here: the phone is the only writer, so its local store is
|
|
||||||
/// always the source of truth.
|
|
||||||
class AlarmSyncService {
|
class AlarmSyncService {
|
||||||
/// Wraps an already-loaded log store.
|
/// Wraps an already-loaded log store.
|
||||||
AlarmSyncService(this._store);
|
AlarmSyncService(this._store);
|
||||||
@ -85,29 +81,20 @@ class AlarmSyncService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pushes the local alarm log using [client].
|
/// Pushes the local alarm log to `kuhyx/syncs` using [client].
|
||||||
///
|
Future<void> pushWith(GitHubClient client) => client.putFileText(
|
||||||
/// Takes a [RemoteStore] rather than a `GitHubClient` so the same call
|
|
||||||
/// serves Firebase, GitHub, or the [MirrorStore] that writes both — the
|
|
||||||
/// backend is chosen once, in [syncBackend].
|
|
||||||
Future<void> pushWith(RemoteStore client) => client.putFileText(
|
|
||||||
_remotePath,
|
_remotePath,
|
||||||
logToJson(_store.snapshot()),
|
logToJson(_store.snapshot()),
|
||||||
message: 'wake_alarm_sync: update alarm',
|
message: 'wake_alarm_sync: update alarm',
|
||||||
);
|
);
|
||||||
|
|
||||||
/// Pushes to Firebase when this device is set up for it, mirroring to
|
/// Pushes using a [token]-authenticated client for the fixed repo.
|
||||||
/// GitHub during the cutover; to GitHub alone when it is not.
|
Future<void> push(String token, {http.Client? httpClient}) => pushWith(
|
||||||
///
|
GitHubClient(
|
||||||
/// [token] still authenticates the GitHub half, so an un-migrated phone
|
|
||||||
/// behaves exactly as before.
|
|
||||||
Future<void> push(String token, {http.Client? httpClient}) async {
|
|
||||||
final github = GitHubClient(
|
|
||||||
owner: owner,
|
owner: owner,
|
||||||
repo: repo,
|
repo: repo,
|
||||||
token: token,
|
token: token,
|
||||||
httpClient: httpClient,
|
httpClient: httpClient,
|
||||||
);
|
),
|
||||||
await pushWith(await syncBackend(github));
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,105 +0,0 @@
|
|||||||
/// Wiring for the Firebase backend, during and after the GitHub cutover.
|
|
||||||
///
|
|
||||||
/// Split by what is safe to publish, because this repo is public:
|
|
||||||
///
|
|
||||||
/// * [kProject] holds the Web API key and database URL. Both are public
|
|
||||||
/// identifiers that already ship inside the APK; the security rules, not
|
|
||||||
/// their secrecy, are what protect the data.
|
|
||||||
/// * The account email and password are entered once per device and kept in
|
|
||||||
/// the OS keystore, next to the GitHub token this app already stores there.
|
|
||||||
///
|
|
||||||
/// Nothing here reads `~/.config/crdt-sync/` — that is the desktop/Python
|
|
||||||
/// half (`wake_alarm/_sync.py`). On Android there is no such file.
|
|
||||||
library;
|
|
||||||
|
|
||||||
import 'package:crdt_sync/crdt_sync.dart';
|
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
||||||
|
|
||||||
/// The shared `kuhy-syncs` project.
|
|
||||||
///
|
|
||||||
/// `databaseUrl` is the **regional** host. The plain `*.firebaseio.com` form
|
|
||||||
/// answers 404 with a `correctUrl` body rather than an obvious error, which
|
|
||||||
/// reads like an auth failure and wastes a debugging session.
|
|
||||||
const kProject = FirebaseProject(
|
|
||||||
apiKey: 'AIzaSyCF_sA3xCMehAYXK8eND-rAygb9NXXW_8E',
|
|
||||||
databaseUrl:
|
|
||||||
'https://kuhy-syncs-default-rtdb.europe-west1.firebasedatabase.app',
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Same options the GitHub token uses: off Android's deprecated
|
|
||||||
/// `encryptedSharedPreferences` path, and libsecret on Linux.
|
|
||||||
const _secure = FlutterSecureStorage();
|
|
||||||
|
|
||||||
// Everything below reaches the OS keystore through a platform channel, which
|
|
||||||
// `flutter test` has no binding for. The logic these wrap (parsing, the
|
|
||||||
// public/private split, sign-in) lives in `crdt_sync` and is covered there at
|
|
||||||
// 100%; what is left here is the two-line adapter.
|
|
||||||
// coverage:ignore-start
|
|
||||||
|
|
||||||
/// The keystore-backed home for the Firebase refresh token.
|
|
||||||
SecureCredentialStore credentialStore() => SecureCredentialStore(
|
|
||||||
read: (key) => _secure.read(key: key),
|
|
||||||
write: (key, value) => _secure.write(key: key, value: value),
|
|
||||||
delete: (key) => _secure.delete(key: key),
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Reads the per-device account, or null when sync has not been set up.
|
|
||||||
Future<FirebaseAccount?> loadAccount() async {
|
|
||||||
try {
|
|
||||||
return FirebaseAccount.tryParse(
|
|
||||||
await _secure.read(key: kFirebaseAccountKey),
|
|
||||||
);
|
|
||||||
} on Object {
|
|
||||||
// No secret service available: behave as "not configured" rather than
|
|
||||||
// crashing the settings screen.
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Stores the per-device account. Keystore only — never prefs, never source.
|
|
||||||
Future<void> saveAccount(FirebaseAccount account) =>
|
|
||||||
_secure.write(key: kFirebaseAccountKey, value: account.toJsonString());
|
|
||||||
|
|
||||||
/// Forgets the account and any cached session.
|
|
||||||
Future<void> clearAccount() async {
|
|
||||||
await _secure.delete(key: kFirebaseAccountKey);
|
|
||||||
await credentialStore().clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a signed-in Firebase client, or null when not configured.
|
|
||||||
///
|
|
||||||
/// Signs in with the stored password only when there is no cached refresh
|
|
||||||
/// token, so the usual path costs no authentication round trip.
|
|
||||||
Future<FirebaseRestClient?> openFirebase() async {
|
|
||||||
final account = await loadAccount();
|
|
||||||
if (account == null) return null;
|
|
||||||
try {
|
|
||||||
return await firebaseClientFor(
|
|
||||||
config: kProject.configFor(account.email),
|
|
||||||
store: credentialStore(),
|
|
||||||
password: account.password,
|
|
||||||
);
|
|
||||||
} on Object {
|
|
||||||
// Unreachable keystore or a rejected sign-in must not fail the push: the
|
|
||||||
// app keeps syncing over GitHub, exactly as before the cutover.
|
|
||||||
// Broader than Exception because an uninitialised platform binding
|
|
||||||
// raises a FlutterError, which is an Error.
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// coverage:ignore-end
|
|
||||||
|
|
||||||
/// Wraps [github] so Firebase is primary and GitHub is kept as a mirror.
|
|
||||||
///
|
|
||||||
/// Returns [github] unchanged when this device has not been set up for
|
|
||||||
/// Firebase — a normal state during the cutover, not an error, and also the
|
|
||||||
/// rollback. The PC reads whichever backend answers first
|
|
||||||
/// (`_fetch_firebase_alarm() or _fetch_github_alarm()`), so a phone that has
|
|
||||||
/// not moved yet still reaches it.
|
|
||||||
Future<RemoteStore> syncBackend(RemoteStore github) async {
|
|
||||||
final firebase = await openFirebase();
|
|
||||||
return firebase == null
|
|
||||||
? github
|
|
||||||
: MirrorStore(primary: firebase, mirror: github);
|
|
||||||
}
|
|
||||||
@ -93,11 +93,11 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: crdt_sync_dart
|
path: crdt_sync_dart
|
||||||
ref: "crdt_sync_dart-v0.6.0"
|
ref: "crdt_sync_dart-v0.2.0"
|
||||||
resolved-ref: fc99eff00599a4c3195e4c1573ba08f15909a10d
|
resolved-ref: "5be99ab11d1657ec04edd65508b8c4ecae4da906"
|
||||||
url: "https://github.com/kuhyx/utils"
|
url: "https://github.com/kuhyx/utils"
|
||||||
source: git
|
source: git
|
||||||
version: "0.6.0"
|
version: "0.2.0"
|
||||||
crypto:
|
crypto:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@ -12,7 +12,7 @@ dependencies:
|
|||||||
crdt_sync:
|
crdt_sync:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/kuhyx/utils
|
url: https://github.com/kuhyx/utils
|
||||||
ref: crdt_sync_dart-v0.6.0
|
ref: crdt_sync_dart-v0.2.0
|
||||||
path: crdt_sync_dart
|
path: crdt_sync_dart
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
flutter:
|
flutter:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user