A cross platform Audiobookshelf client for Android and iOS
Dart
Let you play your downloads from the sign-in screen when your session expires, and sync that listening once you sign back in
Barnabasdb35e946d7b0
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arbindex f090001b..7e4bf720 100644--- a/lib/l10n/app_en.arb+++ b/lib/l10n/app_en.arb@@ -131,6 +131,13 @@},"setupLinkInvalid": "This sign-in link is invalid or incomplete","setupLinkSigningIn": "Checking sign-in link...",+ "loginPlayDownloads": "Play your downloads",+ "signedOutDownloadsNote": "You're signed out. Anything you listen to here syncs to {username} when you sign back in.",+ "@signedOutDownloadsNote": {+ "placeholders": {+ "username": {"type": "String"}+ }+ },"loginPasteLink": "Paste login link","loginPasteLinkHelp": "Paste the complete sign-in link you received. Treat it like a password.","loginFailed": "Login failed",diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dartindex dfb07bc3..1912ba79 100644--- a/lib/l10n/app_localizations.dart+++ b/lib/l10n/app_localizations.dart@@ -694,6 +694,18 @@ abstract class AppLocalizations {/// **'Checking sign-in link...'**String get setupLinkSigningIn;+ /// No description provided for @loginPlayDownloads.+ ///+ /// In en, this message translates to:+ /// **'Play your downloads'**+ String get loginPlayDownloads;++ /// No description provided for @signedOutDownloadsNote.+ ///+ /// In en, this message translates to:+ /// **'You\'re signed out. Anything you listen to here syncs to {username} when you sign back in.'**+ String signedOutDownloadsNote(String username);+/// No description provided for @loginPasteLink.////// In en, this message translates to:diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dartindex f35b6fa5..817686a7 100644--- a/lib/l10n/app_localizations_de.dart+++ b/lib/l10n/app_localizations_de.dart@@ -331,6 +331,14 @@ class AppLocalizationsDe extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Anmelde-Link wird überprüft...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Anmelde-Link einfügen';diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dartindex a251fcad..7e3ef977 100644--- a/lib/l10n/app_localizations_el.dart+++ b/lib/l10n/app_localizations_el.dart@@ -326,6 +326,14 @@ class AppLocalizationsEl extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dartindex e934ffed..fb1f8b16 100644--- a/lib/l10n/app_localizations_en.dart+++ b/lib/l10n/app_localizations_en.dart@@ -326,6 +326,14 @@ class AppLocalizationsEn extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dartindex 41daaea3..7ff70e06 100644--- a/lib/l10n/app_localizations_es.dart+++ b/lib/l10n/app_localizations_es.dart@@ -328,6 +328,14 @@ class AppLocalizationsEs extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dartindex e9bf47dd..b126c176 100644--- a/lib/l10n/app_localizations_fr.dart+++ b/lib/l10n/app_localizations_fr.dart@@ -332,6 +332,14 @@ class AppLocalizationsFr extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Vérification du lien de connexion...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Coller le lien de connexion';diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dartindex 76f82b13..bf4d0166 100644--- a/lib/l10n/app_localizations_it.dart+++ b/lib/l10n/app_localizations_it.dart@@ -326,6 +326,14 @@ class AppLocalizationsIt extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dartindex abdcbc36..20187b07 100644--- a/lib/l10n/app_localizations_ja.dart+++ b/lib/l10n/app_localizations_ja.dart@@ -326,6 +326,14 @@ class AppLocalizationsJa extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dartindex 43481727..03016eef 100644--- a/lib/l10n/app_localizations_no.dart+++ b/lib/l10n/app_localizations_no.dart@@ -326,6 +326,14 @@ class AppLocalizationsNo extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dartindex 890ad654..ea514889 100644--- a/lib/l10n/app_localizations_pt.dart+++ b/lib/l10n/app_localizations_pt.dart@@ -326,6 +326,14 @@ class AppLocalizationsPt extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dartindex de2de514..44d6303c 100644--- a/lib/l10n/app_localizations_ro.dart+++ b/lib/l10n/app_localizations_ro.dart@@ -326,6 +326,14 @@ class AppLocalizationsRo extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dartindex 475c83e7..b522df94 100644--- a/lib/l10n/app_localizations_ru.dart+++ b/lib/l10n/app_localizations_ru.dart@@ -327,6 +327,14 @@ class AppLocalizationsRu extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dartindex 04e4b571..eb9107e5 100644--- a/lib/l10n/app_localizations_zh.dart+++ b/lib/l10n/app_localizations_zh.dart@@ -324,6 +324,14 @@ class AppLocalizationsZh extends AppLocalizations {@overrideString get setupLinkSigningIn => 'Checking sign-in link...';+ @override+ String get loginPlayDownloads => 'Play your downloads';++ @override+ String signedOutDownloadsNote(String username) {+ return 'You\'re signed out. Anything you listen to here syncs to $username when you sign back in.';+ }+@overrideString get loginPasteLink => 'Paste login link';diff --git a/lib/screens/downloads_screen.dart b/lib/screens/downloads_screen.dartindex fc0cba86..98c61409 100644--- a/lib/screens/downloads_screen.dart+++ b/lib/screens/downloads_screen.dart@@ -7,6 +7,7 @@ import '../providers/auth_provider.dart';import '../providers/library_provider.dart';import '../services/audio_player_service.dart';import '../services/download_service.dart';+import '../services/signed_out_playback.dart';import '../services/wording.dart';import '../widgets/absorb_page_header.dart';import '../widgets/card_buttons.dart' show showErrorToast;@@ -15,7 +16,11 @@ import '../widgets/overlay_toast.dart';import '../l10n/app_localizations.dart';class DownloadsScreen extends StatefulWidget {- const DownloadsScreen({super.key});+ /// Opened from the sign-in screen after the session expired: plays without+ /// a server and keeps the user here instead of heading into the app.+ final bool signedOut;++ const DownloadsScreen({super.key, this.signedOut = false});@overrideState<DownloadsScreen> createState() => _DownloadsScreenState();}@@ -172,8 +177,13 @@ class _DownloadsScreenState extends State<DownloadsScreen> {/// any library - the one way to reach a downloaded podcast when the app/// started offline stuck in a book library.Future<void> _play(DownloadInfo info) async {- final api = context.read<AuthProvider>().apiService;+ final api = widget.signedOut+ ? SignedOutPlayback.api()+ : context.read<AuthProvider>().apiService;if (api == null || info.localPaths.isEmpty) return;+ if (widget.signedOut) SignedOutPlayback.started();+ debugPrint(+ '[Downloads] Play ${info.itemId} from the downloads list (signedOut=${widget.signedOut} library=${info.libraryId})');final isEpisode = info.itemId.length > 36;final itemId = isEpisode ? info.itemId.substring(0, 36) : info.itemId;final episodeId = isEpisode ? info.itemId.substring(37) : null;@@ -208,6 +218,7 @@ class _DownloadsScreenState extends State<DownloadsScreen> {showErrorToast(context, error);return;}+ if (widget.signedOut) return;context.read<LibraryProvider>().addToAbsorbing(info.itemId);Navigator.of(context).popUntil((route) => route.isFirst);AppShell.goToAbsorbingGlobal();@@ -287,6 +298,16 @@ class _DownloadsScreenState extends State<DownloadsScreen> {],]),),+ if (widget.signedOut)+ Padding(+ padding: const EdgeInsets.fromLTRB(20, 8, 20, 0),+ child: Text(+ l.signedOutDownloadsNote(+ SignedOutPlayback.expiredAccount()?.username ?? ''),+ style: tt.bodySmall+ ?.copyWith(color: cs.onSurfaceVariant),+ ),+ ),const SizedBox(height: 12),// Content@@ -399,6 +420,9 @@ class _DownloadsScreenState extends State<DownloadsScreen> {),),+ if (widget.signedOut && !_selecting)+ const _SignedOutPlayerBar(),+// Bottom delete barif (_selecting && _selected.isNotEmpty)Container(@@ -746,3 +770,98 @@ class _ActiveDownloadCard extends StatelessWidget {);}}++/// A small player for signed-out listening, where the main app with its+/// cards isn't reachable. The notification and lock screen controls work as+/// usual on top of this.+class _SignedOutPlayerBar extends StatelessWidget {+ const _SignedOutPlayerBar();++ @override+ Widget build(BuildContext context) {+ final cs = Theme.of(context).colorScheme;+ final tt = Theme.of(context).textTheme;+ final player = AudioPlayerService();+ return ListenableBuilder(+ listenable: player,+ builder: (context, _) {+ if (!player.hasBook) return const SizedBox.shrink();+ final total = player.totalDuration;+ return Container(+ padding: const EdgeInsets.fromLTRB(16, 8, 8, 8),+ decoration: BoxDecoration(+ color: cs.surfaceContainerHigh,+ border: Border(+ top: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.3)),+ ),+ ),+ child: SafeArea(+ top: false,+ child: Column(+ mainAxisSize: MainAxisSize.min,+ children: [+ Row(children: [+ Expanded(+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ Text(+ player.currentEpisodeTitle ?? player.currentTitle ?? '',+ style: tt.titleSmall+ ?.copyWith(fontWeight: FontWeight.w600),+ maxLines: 1,+ overflow: TextOverflow.ellipsis,+ ),+ Text(+ player.currentAuthor ?? '',+ style: tt.bodySmall+ ?.copyWith(color: cs.onSurfaceVariant),+ maxLines: 1,+ overflow: TextOverflow.ellipsis,+ ),+ ],+ ),+ ),+ IconButton(+ icon: const Icon(Icons.fast_rewind_rounded),+ onPressed: () async => player+ .skipBackward(await PlayerSettings.getBackSkip()),+ ),+ IconButton.filled(+ icon: Icon(player.isPlaying+ ? Icons.pause_rounded+ : Icons.play_arrow_rounded),+ onPressed: () => player.togglePlayPause(fromUi: true),+ ),+ IconButton(+ icon: const Icon(Icons.fast_forward_rounded),+ onPressed: () async => player+ .skipForward(await PlayerSettings.getForwardSkip()),+ ),+ ]),+ if (total > 0)+ StreamBuilder<Duration>(+ stream: player.absolutePositionStream,+ builder: (context, _) {+ final pos = player.position.inMilliseconds / 1000;+ return Padding(+ padding: const EdgeInsets.only(top: 6, right: 8),+ child: ClipRRect(+ borderRadius: BorderRadius.circular(4),+ child: LinearProgressIndicator(+ value: (pos / total).clamp(0.0, 1.0),+ minHeight: 4,+ backgroundColor: cs.surfaceContainerHighest,+ ),+ ),+ );+ },+ ),+ ],+ ),+ ),+ );+ },+ );+ }+}diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dartindex 74a930a9..a87f3a65 100644--- a/lib/screens/login_screen.dart+++ b/lib/screens/login_screen.dart@@ -1,1354 +1,1380 @@-import 'dart:async';-import 'dart:convert';-import 'dart:io';-import 'dart:ui';-import 'package:flutter/material.dart';-import 'package:flutter/services.dart';-import 'package:file_picker/file_picker.dart';-import 'package:package_info_plus/package_info_plus.dart';-import 'package:provider/provider.dart';-import '../providers/auth_provider.dart';-import '../services/api_service.dart';-import '../services/backup_service.dart';-import '../services/oidc_service.dart';-import '../services/settings_sync_service.dart';-import '../services/setup_link_service.dart';-import '../services/user_account_service.dart';-import '../widgets/absorb_wave_icon.dart';-import '../widgets/overlay_toast.dart';-import '../widgets/setup_link_login.dart';-import '../services/audio_player_service.dart';-import '../main.dart' show applyTrustAllCerts, flatNotifier;-import '../l10n/app_localizations.dart';-import '../services/wording.dart';--class LoginScreen extends StatefulWidget {- final SavedAccount? prefillAccount;- final String? prefillServerUrl;-- const LoginScreen({- super.key,- this.prefillAccount,- this.prefillServerUrl,- });-- @override- State<LoginScreen> createState() => _LoginScreenState();-}-class _LoginScreenState extends State<LoginScreen>- with SingleTickerProviderStateMixin {- final _formKey = GlobalKey<FormState>();- final _serverController = TextEditingController();- final _usernameController = TextEditingController();- final _passwordController = TextEditingController();- final _apiKeyController = TextEditingController();- final _usernameFocus = FocusNode();-- bool _obscurePassword = true;- bool _obscureApiKey = true;- bool _isConnecting = false;- String _protocol = 'https://';-- // Server validation state- bool _serverValid = false;- bool _serverChecking = false;- String? _serverError;- // Technical reason the last check failed (TLS, timeout, HTTP status, etc.),- // shown beneath the field so unreachable-in-app-but-fine-in-browser servers- // are diagnosable instead of just "could not reach server".- String? _serverErrorDetail;- Timer? _debounce;-- // Login error- String? _loginError;-- // OIDC state- OidcConfig? _oidcConfig;- bool _isOidcLoading = false;-- // Custom headers (advanced)- bool _showAdvanced = false;- bool _trustAllCerts = false;- final List<(TextEditingController, TextEditingController)> _headerControllers = [];-- // App version- String _appVersion = '';-- late AnimationController _animController;- late Animation<double> _fadeAnim;- late Animation<double> _slideAnim;-- @override- void initState() {- super.initState();- _animController = AnimationController(- vsync: this,- duration: const Duration(milliseconds: 1200),- );- _fadeAnim = CurvedAnimation(- parent: _animController,- curve: const Interval(0.0, 0.5, curve: Curves.easeOut),- );- _slideAnim = CurvedAnimation(- parent: _animController,- curve: const Interval(0.3, 1.0, curve: Curves.easeOutCubic),- );- _animController.forward();- _serverController.addListener(_onServerChanged);- _loadVersion();- WidgetsBinding.instance.addPostFrameCallback((_) {- if (!mounted) return;- final auth = context.read<AuthProvider>();- final account = widget.prefillAccount;- if (account != null) {- _setInitialHeaders(account.customHeaders);- _prefillLogin(account.serverUrl, account.username);- return;- }- final requestedServer = widget.prefillServerUrl;- if (requestedServer != null && requestedServer.isNotEmpty) {- _setInitialHeaders(auth.customHeaders);- _prefillLogin(requestedServer, '');- return;- }- final serverUrl = auth.serverUrl;- if (!auth.isAuthenticated && serverUrl != null && serverUrl.isNotEmpty) {- _prefillLogin(serverUrl, auth.username ?? '');- }- });- }-- void _prefillLogin(String serverUrl, String username) {- final uri = Uri.tryParse(serverUrl);- final protocol = uri?.scheme == 'http' || uri?.scheme == 'https'- ? '${uri!.scheme}://'- : _protocol;- setState(() => _protocol = protocol);- _serverController.text = serverUrl.startsWith(protocol)- ? serverUrl.substring(protocol.length)- : serverUrl;- _usernameController.text = username;- }-- void _setInitialHeaders(Map<String, String> headers) {- for (final (key, value) in _headerControllers) {- key.dispose();- value.dispose();- }- _headerControllers- ..clear()- ..addAll(headers.entries.map(- (entry) => (- TextEditingController(text: entry.key),- TextEditingController(text: entry.value),- ),- ));- if (headers.isNotEmpty) _showAdvanced = true;- }-- Future<void> _loadVersion() async {- try {- final info = await PackageInfo.fromPlatform();- if (mounted) setState(() => _appVersion = 'v${info.version}');- } catch (_) {}- try {- final trust = await PlayerSettings.getTrustAllCerts();- if (mounted) setState(() => _trustAllCerts = trust);- } catch (_) {}- }-- @override- void dispose() {- _debounce?.cancel();- _animController.dispose();- _serverController.dispose();- _usernameController.dispose();- _passwordController.dispose();- _apiKeyController.dispose();- _usernameFocus.dispose();- for (final (k, v) in _headerControllers) { k.dispose(); v.dispose(); }- OidcService().cancel();- super.dispose();- }-- String _lastValidatedServer = '';-- /// Re-trigger server validation when custom headers change.- void _revalidateServer() {- if (_serverController.text.trim().isEmpty) return;- _lastValidatedServer = ''; // Force re-check- _debounce?.cancel();- _debounce = Timer(const Duration(milliseconds: 800), () => _checkServer());- }-- void _onServerChanged() {- final text = _serverController.text.trim();- if (text.isEmpty) {- setState(() {- _serverValid = false;- _serverChecking = false;- _serverError = null;- _serverErrorDetail = null;- _lastValidatedServer = '';- });- _debounce?.cancel();- return;- }-- // Only invalidate if the server text actually changed from what we validated- final cleanUrl = text.replaceAll(RegExp(r'^https?://'), '');- final fullUrl = '$_protocol$cleanUrl';- if (fullUrl != _lastValidatedServer) {- setState(() {- _serverValid = false;- _serverChecking = true;- _serverError = null;- });- } else {- // Same server, just re-checking — keep fields visible- setState(() {- _serverChecking = true;- _serverError = null;- });- }-- _debounce?.cancel();- _debounce = Timer(const Duration(milliseconds: 800), () => _checkServer());- }-- Map<String, String> _collectHeaders() {- final headers = <String, String>{};- for (final (keyCtrl, valCtrl) in _headerControllers) {- final k = keyCtrl.text.trim();- final v = valCtrl.text.trim();- if (k.isNotEmpty && v.isNotEmpty) headers[k] = v;- }- return headers;- }-- Future<void> _checkServer() async {- final text = _serverController.text.trim();- if (text.isEmpty) return;-- final cleanUrl = text.replaceAll(RegExp(r'^https?://'), '');- final fullUrl = '$_protocol$cleanUrl';-- try {- final headers = _collectHeaders();- final result = await ApiService.pingServerDetailed(fullUrl, customHeaders: headers);- final ok = result.ok;- if (!mounted) return;- if (_serverController.text.trim() != text) return;-- setState(() {- _serverChecking = false;- _serverValid = ok;- _serverError = ok ? null : AppLocalizations.of(context)!.loginCouldNotReachServer;- _serverErrorDetail = ok ? null : result.detail;- if (ok) {- _lastValidatedServer = fullUrl;- } else {- _oidcConfig = null;- _lastValidatedServer = '';- }- });-- if (ok) {- // Also check if OIDC is available- OidcService.checkOidcEnabled(fullUrl, customHeaders: headers).then((config) {- if (mounted && _serverController.text.trim() == text) {- setState(() => _oidcConfig = config);- }- });-- // Don't auto-focus — user may still be typing IP:port- }- } catch (e) {- if (!mounted) return;- setState(() {- _serverChecking = false;- _serverValid = false;- _serverError = AppLocalizations.of(context)!.loginCouldNotReachServer;- _serverErrorDetail = e.toString();- _oidcConfig = null;- });- }- }-- Future<void> _handleLogin() async {- final apiKey = _apiKeyController.text.trim();-- // Skip form validation when an API key is supplied — the username- // field's required-validator would otherwise block the submit.- if (apiKey.isEmpty && !_formKey.currentState!.validate()) return;-- // Validate server first if not yet checked- if (!_serverValid) {- await _checkServer();- if (!_serverValid) {- setState(() => _loginError = _serverError ?? AppLocalizations.of(context)!.loginCouldNotReachServer);- return;- }- }-- setState(() {- _isConnecting = true;- _loginError = null;- });-- final auth = context.read<AuthProvider>();- final serverText = _serverController.text.trim();- final cleanUrl = serverText.replaceAll(RegExp(r'^https?://'), '');- final fullUrl = '$_protocol$cleanUrl';-- final headers = _collectHeaders();-- final success = apiKey.isNotEmpty- ? await auth.loginWithApiKey(- serverUrl: fullUrl,- apiKey: apiKey,- customHeaders: headers,- l: AppLocalizations.of(context),- )- : await auth.login(- serverUrl: fullUrl,- username: _usernameController.text.trim(),- password: _passwordController.text,- customHeaders: headers,- l: AppLocalizations.of(context),- );-- if (mounted) {- setState(() => _isConnecting = false);-- if (!success) {- setState(() {- _loginError = auth.errorMessage ?? AppLocalizations.of(context)!.loginFailed;- });- } else {- TextInput.finishAutofillContext();- FocusManager.instance.primaryFocus?.unfocus();- if (Navigator.of(context).canPop()) {- // If pushed as a route (e.g. Add Account), pop back- Navigator.of(context).pop();- }- }- }- }-- Future<void> _handleOidcLogin() async {- if (!_serverValid) return;-- setState(() {- _isOidcLoading = true;- _loginError = null;- });-- final serverText = _serverController.text.trim();- final cleanUrl = serverText.replaceAll(RegExp(r'^https?://'), '');- final fullUrl = '$_protocol$cleanUrl';-- final headers = _collectHeaders();- final oidc = OidcService();- final callbackUri = await oidc.startLogin(fullUrl, customHeaders: headers);- if (callbackUri == null) {- if (!mounted) return;- // Quiet path: user just dismissed the popup. Don't surface an error.- if (oidc.lastWasUserCancel) {- setState(() => _isOidcLoading = false);- return;- }- final base = AppLocalizations.of(context)!.loginSsoFailed;- final detail = oidc.lastError;- setState(() {- _isOidcLoading = false;- _loginError = detail != null ? '$base\n\n$detail' : base;- });- return;- }-- final result = await oidc.handleCallback(callbackUri);- if (result != null && mounted) {- FocusManager.instance.primaryFocus?.unfocus();- final auth = context.read<AuthProvider>();- final success = await auth.loginWithOidc(- serverUrl: fullUrl,- result: result,- customHeaders: headers,- l: AppLocalizations.of(context),- );- if (mounted) {- setState(() => _isOidcLoading = false);- if (!success) {- setState(() => _loginError = auth.errorMessage ?? AppLocalizations.of(context)!.loginSsoFailed);- } else if (Navigator.of(context).canPop()) {- Navigator.of(context).pop();- }- }- } else if (mounted) {- final base = AppLocalizations.of(context)!.loginSsoAuthFailed;- final detail = oidc.lastError;- setState(() {- _isOidcLoading = false;- _loginError = detail != null ? '$base\n\n$detail' : base;- });- }- }-- @override- Widget build(BuildContext context) {- final cs = Theme.of(context).colorScheme;- final tt = Theme.of(context).textTheme;- final l = AppLocalizations.of(context)!;-- return Scaffold(- resizeToAvoidBottomInset: true,- body: Container(- decoration: flatNotifier.value ? null : BoxDecoration(- gradient: LinearGradient(- begin: Alignment.topLeft,- end: Alignment.bottomRight,- stops: const [0.0, 0.4, 0.7, 1.0],- colors: [- cs.primary.withValues(alpha: 0.15),- cs.primary.withValues(alpha: 0.05),- cs.surface,- cs.surface,- ],- ),- ),- child: SafeArea(- child: LayoutBuilder(- builder: (context, constraints) => SingleChildScrollView(- padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 24),- child: ConstrainedBox(- constraints: BoxConstraints(minHeight: constraints.maxHeight - 48),- child: Column(- mainAxisAlignment: MainAxisAlignment.center,- mainAxisSize: MainAxisSize.min,- children: [- // ── Logo + Tagline ──- FadeTransition(- opacity: _fadeAnim,- child: Column(- children: [- // Wave icon with glow- Container(- width: 100,- height: 100,- decoration: BoxDecoration(- shape: BoxShape.circle,- boxShadow: [- BoxShadow(- color: cs.primary.withValues(alpha: 0.3),- blurRadius: 40,- spreadRadius: 8,- ),- ],- ),- child: Container(- decoration: BoxDecoration(- shape: BoxShape.circle,- color: cs.primary.withValues(alpha: 0.1),- border: Border.all(- color: cs.primary.withValues(alpha: 0.15),- width: 1.5,- ),- ),- child: Center(- child: AbsorbWaveIcon(- size: 44,- color: cs.primary,- ),- ),- ),- ),- const SizedBox(height: 28),- Text(- l.appTitle,- style: tt.headlineMedium?.copyWith(- fontWeight: FontWeight.w200,- color: cs.onSurface,- letterSpacing: 10,- ),- ),- const SizedBox(height: 10),- Text(- Wording.of(context).loginTagline,- style: tt.bodyLarge?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.6),- fontWeight: FontWeight.w400,- letterSpacing: 0.5,- ),- ),- ],- ),- ),- const SizedBox(height: 40),-- // ── Glass form card ──- SlideTransition(- position: Tween<Offset>(- begin: const Offset(0, 0.12),- end: Offset.zero,- ).animate(_slideAnim),- child: FadeTransition(- opacity: _slideAnim,- child: ClipRRect(- borderRadius: BorderRadius.circular(24),- child: BackdropFilter(- filter: ImageFilter.blur(sigmaX: 20, sigmaY: 20),- child: Container(- padding: const EdgeInsets.all(24),- decoration: BoxDecoration(- color: cs.surfaceContainerHighest.withValues(alpha: 0.35),- borderRadius: BorderRadius.circular(24),- border: Border.all(- color: cs.outlineVariant.withValues(alpha: 0.15),- ),- ),- child: Form(- key: _formKey,- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- // Section label- Padding(- padding: const EdgeInsets.only(left: 4, bottom: 16),- child: Text(- l.loginConnectToServer,- style: tt.titleSmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.7),- fontWeight: FontWeight.w500,- ),- ),- ),-- // Server URL- _buildInputField(- controller: _serverController,- label: l.loginServerAddress,- hint: l.loginServerHint,- helperText: l.loginServerHelper,- keyboardType: TextInputType.url,- textInputAction: TextInputAction.next,- onFieldSubmitted: (_) {- if (!_serverValid && !_serverChecking) {- _checkServer().then((_) {- if (mounted && _serverValid) _usernameFocus.requestFocus();- });- } else if (_serverValid) {- _usernameFocus.requestFocus();- }- },- cs: cs,- prefixIcon: Padding(- padding: const EdgeInsets.only(left: 8),- child: DropdownButtonHideUnderline(- child: DropdownButton<String>(- value: _protocol,- isDense: true,- style: TextStyle(- color: cs.primary,- fontSize: 13,- fontWeight: FontWeight.w600,- ),- items: const [- DropdownMenuItem(- value: 'https://',- child: Text('https://'),- ),- DropdownMenuItem(- value: 'http://',- child: Text('http://'),- ),- ],- onChanged: (v) {- if (v != null) {- setState(() {- _protocol = v;- _serverValid = false;- });- _onServerChanged();- }- },- ),- ),- ),- suffixIcon: _serverChecking- ? const Padding(- padding: EdgeInsets.all(14),- child: SizedBox(- width: 18, height: 18,- child: CircularProgressIndicator(strokeWidth: 2),- ),- )- : _serverValid- ? Icon(Icons.check_circle_rounded,- color: Colors.green.shade400, size: 22)- : _serverError != null- ? Icon(Icons.error_outline_rounded,- color: cs.error, size: 22)- : null,- borderColor: _serverValid- ? Colors.green.shade400.withValues(alpha: 0.4)- : _serverError != null- ? cs.error.withValues(alpha: 0.4)- : null,- errorText: _serverError,- ),-- if (_serverErrorDetail != null) ...[- const SizedBox(height: 6),- Padding(- padding: const EdgeInsets.symmetric(horizontal: 4),- child: Text(- _serverErrorDetail!,- style: TextStyle(- fontSize: 11,- color: cs.onSurfaceVariant.withValues(alpha: 0.6),- ),- ),- ),- ],-- // Advanced: Custom Headers — must be before server validation- const SizedBox(height: 8),- GestureDetector(- onTap: () => setState(() => _showAdvanced = !_showAdvanced),- child: Row(- children: [- Icon(_showAdvanced ? Icons.expand_less_rounded : Icons.expand_more_rounded,- size: 18, color: cs.onSurfaceVariant.withValues(alpha: 0.5)),- const SizedBox(width: 4),- Text(l.loginAdvanced, style: TextStyle(fontSize: 13, color: cs.onSurfaceVariant.withValues(alpha: 0.5))),- ],- ),- ),- if (_showAdvanced) ...[- const SizedBox(height: 8),- Text(l.loginCustomHttpHeaders, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: cs.onSurfaceVariant.withValues(alpha: 0.7))),- const SizedBox(height: 4),- Text(l.loginCustomHeadersDescription,- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4))),- const SizedBox(height: 8),- ..._headerControllers.asMap().entries.map((entry) {- final i = entry.key;- final (keyCtrl, valCtrl) = entry.value;- return Padding(- padding: const EdgeInsets.only(bottom: 8),- child: Row(- children: [- Expanded(- flex: 2,- child: TextField(- controller: keyCtrl,- style: TextStyle(fontSize: 13, color: cs.onSurface),- onChanged: (_) => _revalidateServer(),- decoration: InputDecoration(- hintText: l.loginHeaderName,- hintStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.3), fontSize: 13),- filled: true,- fillColor: cs.surfaceContainerHighest.withValues(alpha: 0.5),- border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none),- contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),- ),- ),- ),- const SizedBox(width: 8),- Expanded(- flex: 3,- child: TextField(- controller: valCtrl,- style: TextStyle(fontSize: 13, color: cs.onSurface),- onChanged: (_) => _revalidateServer(),- decoration: InputDecoration(- hintText: l.loginHeaderValue,- hintStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.3), fontSize: 13),- filled: true,- fillColor: cs.surfaceContainerHighest.withValues(alpha: 0.5),- border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none),- contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),- ),- ),- ),- const SizedBox(width: 4),- GestureDetector(- onTap: () {- setState(() {- _headerControllers[i].$1.dispose();- _headerControllers[i].$2.dispose();- _headerControllers.removeAt(i);- });- _revalidateServer();- },- child: Icon(Icons.close_rounded, size: 18, color: cs.error.withValues(alpha: 0.6)),- ),- ],- ),- );- }),- GestureDetector(- onTap: () => setState(() {- _headerControllers.add((TextEditingController(), TextEditingController()));- }),- child: Container(- padding: const EdgeInsets.symmetric(vertical: 8),- child: Row(- mainAxisAlignment: MainAxisAlignment.center,- children: [- Icon(Icons.add_rounded, size: 16, color: cs.primary),- const SizedBox(width: 4),- Text(l.loginAddHeader, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: cs.primary)),- ],- ),- ),- ),- const SizedBox(height: 8),- const Divider(height: 1),- const SizedBox(height: 4),- Text(l.loginSelfSignedCertificates, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: cs.onSurfaceVariant.withValues(alpha: 0.7))),- Row(- children: [- Expanded(- child: Text(- l.loginTrustAllCertificates,- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4)),- ),- ),- Transform.scale(- scale: 0.8,- child: Switch(- value: _trustAllCerts,- onChanged: (v) async {- setState(() => _trustAllCerts = v);- await PlayerSettings.setTrustAllCerts(v);- applyTrustAllCerts(v);- _revalidateServer();- },- ),- ),- ],- ),- const SizedBox(height: 8),- const Divider(height: 1),- const SizedBox(height: 8),- Text(l.loginApiKey, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: cs.onSurfaceVariant.withValues(alpha: 0.7))),- const SizedBox(height: 4),- Text(l.loginApiKeyDescription,- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4))),- const SizedBox(height: 8),- TextField(- controller: _apiKeyController,- obscureText: _obscureApiKey,- autocorrect: false,- enableSuggestions: false,- style: TextStyle(fontSize: 13, color: cs.onSurface),- onChanged: (_) => setState(() {}),- decoration: InputDecoration(- hintText: l.loginApiKey,- hintStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.3), fontSize: 13),- filled: true,- fillColor: cs.surfaceContainerHighest.withValues(alpha: 0.5),- border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none),- contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),- prefixIcon: Icon(Icons.vpn_key_rounded, size: 18, color: cs.onSurfaceVariant.withValues(alpha: 0.5)),- suffixIcon: IconButton(- icon: Icon(- _obscureApiKey ? Icons.visibility_outlined : Icons.visibility_off_outlined,- size: 18,- color: cs.onSurfaceVariant.withValues(alpha: 0.5),- ),- onPressed: () => setState(() => _obscureApiKey = !_obscureApiKey),- ),- ),- ),- ],-- // Credentials — always visible- _buildCredentialFields(cs, tt),- ],- ),- ),- ),- ),- ),- ),- ),-- // ── Version + Restore pill ──- const SizedBox(height: 32),- FadeTransition(- opacity: _fadeAnim,- child: Column(- children: [- Text(- _appVersion,- style: tt.labelSmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.3),- letterSpacing: 1,- ),- ),- const SizedBox(height: 12),- Wrap(- alignment: WrapAlignment.center,- spacing: 8,- runSpacing: 8,- children: [- ActionChip(- avatar: Icon(Icons.restore_rounded, size: 16,- color: cs.onSurfaceVariant.withValues(alpha: 0.6)),- label: Text(l.loginRestoreFromBackup,- style: tt.labelSmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.6),- )),- backgroundColor: cs.surfaceContainerHighest.withValues(alpha: 0.3),- side: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.15)),- shape: RoundedRectangleBorder(- borderRadius: BorderRadius.circular(20),- ),- onPressed: _restoreFromBackup,- ),- ActionChip(- key: const Key('paste-login-link'),- avatar: Icon(Icons.content_paste_rounded, size: 16,- color: cs.onSurfaceVariant.withValues(alpha: 0.6)),- label: Text(l.loginPasteLink,- style: tt.labelSmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.6),- )),- backgroundColor: cs.surfaceContainerHighest.withValues(alpha: 0.3),- side: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.15)),- shape: RoundedRectangleBorder(- borderRadius: BorderRadius.circular(20),- ),- onPressed: _pasteLoginLink,- ),- ],- )- ],- ),- ),-- // ── Saved accounts quick-switch ──- _buildSavedAccounts(cs, tt),- ],- ),- ),- ),- ),- ),- ),- );- }-- Widget _buildSavedAccounts(ColorScheme cs, TextTheme tt) {- final accounts = UserAccountService().accounts;- if (accounts.isEmpty) return const SizedBox.shrink();- final l = AppLocalizations.of(context)!;-- return FadeTransition(- opacity: _fadeAnim,- child: Padding(- padding: const EdgeInsets.only(top: 32),- child: Column(- children: [- Row(- children: [- Expanded(child: Divider(color: cs.outlineVariant.withValues(alpha: 0.15))),- Padding(- padding: const EdgeInsets.symmetric(horizontal: 14),- child: Text(l.loginSavedAccounts,- style: tt.bodySmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.4))),- ),- Expanded(child: Divider(color: cs.outlineVariant.withValues(alpha: 0.15))),- ],- ),- const SizedBox(height: 12),- ...accounts.map((account) {- final shortUrl = account.serverUrl- .replaceAll(RegExp(r'^https?://'), '')- .replaceAll(RegExp(r'/+$'), '');- return Padding(- padding: const EdgeInsets.only(bottom: 8),- child: Material(- color: cs.surface.withValues(alpha: 0.3),- borderRadius: BorderRadius.circular(14),- child: InkWell(- borderRadius: BorderRadius.circular(14),- onTap: () => _quickSwitch(account),- child: Padding(- padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),- child: Row(- children: [- CircleAvatar(- radius: 18,- backgroundColor: cs.primary.withValues(alpha: 0.15),- child: Text(- account.username.isNotEmpty- ? account.username[0].toUpperCase()- : '?',- style: TextStyle(- color: cs.primary,- fontWeight: FontWeight.w600,- ),- ),- ),- const SizedBox(width: 12),- Expanded(- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- Text(account.username,- style: tt.bodyMedium?.copyWith(- fontWeight: FontWeight.w600,- color: cs.onSurface)),- Text(shortUrl,- style: tt.labelSmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.5)),- maxLines: 1,- overflow: TextOverflow.ellipsis),- ],- ),- ),- Icon(Icons.arrow_forward_ios_rounded,- size: 14, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),- ],- ),- ),- ),- ),- );- }),- ],- ),- ),- );- }-- Future<void> _restoreFromBackup() async {- try {- final result = await FilePicker.platform.pickFiles(type: FileType.any);- if (result == null || result.files.isEmpty) return;-- final file = File(result.files.single.path!);- final jsonStr = await file.readAsString();- final data = jsonDecode(jsonStr) as Map<String, dynamic>;-- if (!data.containsKey('version')) {- if (mounted) {- final l = AppLocalizations.of(context)!;- showOverlayToast(context, l.loginInvalidBackupFile,- icon: Icons.error_outline_rounded);- }- return;- }-- final accounts = data['accounts'] as List<dynamic>?;- final accountCount = accounts?.length ?? 0;- final hasAccounts = accountCount > 0;- // A setup/login file carries an account but no settings payload, so it- // should read as a sign-in rather than a backup restore.- final isSetup = data['setup'] == true || (hasAccounts && data['settings'] == null);- final firstUsername = hasAccounts ? ((accounts!.first as Map)['username'] as String? ?? '') : '';-- if (!mounted) return;- final l = AppLocalizations.of(context)!;- final confirm = await showDialog<bool>(- context: context,- builder: (ctx) => AlertDialog(- title: Text(isSetup ? l.loginSignIn : l.loginRestoreBackupTitle),- content: Text(isSetup- ? (firstUsername.isNotEmpty ? l.loginSignInAs(firstUsername) : l.loginSignInToServer)- : (hasAccounts- ? l.loginRestoreBackupWithAccounts(accountCount)- : l.loginRestoreBackupNoAccounts)),- actions: [- TextButton(- onPressed: () => Navigator.pop(ctx, false),- child: Text(l.cancel),- ),- FilledButton(- onPressed: () => Navigator.pop(ctx, true),- child: Text(isSetup ? l.loginSignIn : l.loginRestore),- ),- ],- ),- );-- if (confirm != true) return;-- await BackupService.importSettings(data);-- // Auto-login with the first restored account- if (hasAccounts && mounted) {- final restoredAccounts = UserAccountService().accounts;- if (restoredAccounts.isNotEmpty) {- final auth = context.read<AuthProvider>();- await auth.switchToAccount(restoredAccounts.first);-- if (mounted) {- final l2 = AppLocalizations.of(context)!;- if (auth.isAuthenticated && auth.serverReachable) {- showOverlayToast(- context,- isSetup- ? l2.loginSignedInAs(restoredAccounts.first.username)- : l2.loginRestoredAndSignedIn(restoredAccounts.first.username),- icon: Icons.check_circle_outline_rounded,- );- } else {- // Token expired — accounts are saved but need re-auth- showOverlayToast(context, l2.loginSessionExpired, icon: Icons.error_outline_rounded);- setState(() {}); // Refresh to show saved accounts list- }- }- }- } else if (mounted) {- final l3 = AppLocalizations.of(context)!;- showOverlayToast(context, l3.loginSettingsRestored, icon: Icons.check_circle_outline_rounded);- }-- await SettingsSyncService().resolveSourceAfterRestore();- } catch (e) {- if (mounted) {- final l4 = AppLocalizations.of(context)!;- showOverlayToast(context, l4.loginRestoreFailed(e.toString()), icon: Icons.error_outline_rounded);- }- }- }-- Future<void> _pasteLoginLink() async {- final l = AppLocalizations.of(context)!;- final controller = TextEditingController();- String? errorText;- final link = await showDialog<Uri>(- context: context,- builder: (dialogContext) => StatefulBuilder(- builder: (context, setDialogState) {- void submit() {- final uri = Uri.tryParse(controller.text.trim());- try {- if (uri == null) throw const SetupLinkException('Invalid link');- SetupLinkService.parseLink(uri);- Navigator.pop(dialogContext, uri);- } on SetupLinkException {- setDialogState(() => errorText = l.setupLinkInvalid);- }- }-- return AlertDialog(- title: Text(l.loginPasteLink),- content: Column(- mainAxisSize: MainAxisSize.min,- crossAxisAlignment: CrossAxisAlignment.start,- children: [- Text(l.loginPasteLinkHelp),- const SizedBox(height: 16),- TextField(- key: const Key('login-link-field'),- controller: controller,- autofocus: true,- autocorrect: false,- enableSuggestions: false,- keyboardType: TextInputType.url,- minLines: 2,- maxLines: 4,- decoration: InputDecoration(- hintText: 'absorb://setup/...',- errorText: errorText,- suffixIcon: IconButton(- tooltip: l.loginPasteLink,- icon: const Icon(Icons.content_paste_rounded),- onPressed: () async {- final data = await Clipboard.getData('text/plain');- controller.text = data?.text ?? '';- setDialogState(() => errorText = null);- },- ),- ),- ),- ],- ),- actions: [- TextButton(- onPressed: () => Navigator.pop(dialogContext),- child: Text(l.cancel),- ),- FilledButton(- key: const Key('submit-login-link'),- onPressed: submit,- child: Text(l.loginSignIn),- ),- ],- );- },- ),- );- controller.dispose();- if (link == null || !mounted) return;- await handleSetupLinkLogin(context, link);- }-- Future<void> _quickSwitch(SavedAccount account) async {- if (account.token.isEmpty) {- _setInitialHeaders(account.customHeaders);- _prefillLogin(account.serverUrl, account.username);- _passwordController.clear();- _usernameFocus.unfocus();- return;- }- setState(() => _isConnecting = true);-- final auth = context.read<AuthProvider>();- await auth.switchToAccount(account);-- if (mounted) {- setState(() => _isConnecting = false);- if (auth.isAuthenticated && Navigator.of(context).canPop()) {- Navigator.of(context).pop();- }- // If this is the root login screen, AuthGate will react to the state change- }- }-- Widget _buildInputField({- required TextEditingController controller,- required String label,- required ColorScheme cs,- String? hint,- String? helperText,- TextInputType? keyboardType,- TextInputAction? textInputAction,- FocusNode? focusNode,- Widget? prefixIcon,- Widget? suffixIcon,- Color? borderColor,- String? errorText,- bool obscureText = false,- void Function(String)? onFieldSubmitted,- String? Function(String?)? validator,- Iterable<String>? autofillHints,- }) {- final isUrl = keyboardType == TextInputType.url;- return TextFormField(- controller: controller,- focusNode: focusNode,- keyboardType: keyboardType,- textInputAction: textInputAction,- obscureText: obscureText,- autocorrect: !isUrl,- enableSuggestions: !isUrl,- autofillHints: autofillHints,- onFieldSubmitted: onFieldSubmitted,- validator: validator,- style: TextStyle(color: cs.onSurface),- decoration: InputDecoration(- labelText: label,- hintText: hint,- helperText: helperText,- helperStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.5), fontSize: 11),- helperMaxLines: 2,- prefixIcon: prefixIcon,- suffixIcon: suffixIcon,- errorText: errorText,- border: OutlineInputBorder(- borderRadius: BorderRadius.circular(14),- borderSide: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.2)),- ),- enabledBorder: OutlineInputBorder(- borderRadius: BorderRadius.circular(14),- borderSide: BorderSide(- color: borderColor ?? cs.outlineVariant.withValues(alpha: 0.15),- ),- ),- focusedBorder: OutlineInputBorder(- borderRadius: BorderRadius.circular(14),- borderSide: BorderSide(color: cs.primary.withValues(alpha: 0.6), width: 1.5),- ),- filled: true,- fillColor: cs.surface.withValues(alpha: 0.4),- contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),- ),- );- }-- Widget _buildOidcButton(ColorScheme cs, TextTheme tt) {- final l = AppLocalizations.of(context)!;- return Column(- children: [- const SizedBox(height: 12),- SizedBox(- width: double.infinity,- height: 44,- child: OutlinedButton.icon(- onPressed: _isConnecting || _isOidcLoading ? null : _handleOidcLogin,- icon: _isOidcLoading- ? SizedBox(- width: 18, height: 18,- child: CircularProgressIndicator(strokeWidth: 2, color: cs.primary),- )- : Icon(Icons.login_rounded, size: 20, color: cs.primary),- label: Text(- _isOidcLoading ? l.loginWaitingForSso : _oidcConfig!.buttonText,- style: tt.titleMedium?.copyWith(- fontWeight: FontWeight.w600,- color: cs.primary,- ),- ),- style: OutlinedButton.styleFrom(- side: BorderSide(color: cs.primary.withValues(alpha: 0.3)),- shape: RoundedRectangleBorder(- borderRadius: BorderRadius.circular(12),- ),- ),- ),- ),- const SizedBox(height: 6),- Text(- l.loginRedirectUri,- style: tt.labelSmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.4),- letterSpacing: 0.3,- ),- ),- ],- );- }-- Widget _buildCredentialFields(ColorScheme cs, TextTheme tt) {- final l = AppLocalizations.of(context)!;- final usingApiKey = _apiKeyController.text.trim().isNotEmpty;- return AutofillGroup(- child: Column(- children: [- const SizedBox(height: 16),-- // Login error message- if (_loginError != null)- Padding(- padding: const EdgeInsets.only(bottom: 12),- child: Container(- width: double.infinity,- padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),- decoration: BoxDecoration(- color: cs.error.withValues(alpha: 0.1),- borderRadius: BorderRadius.circular(12),- border: Border.all(color: cs.error.withValues(alpha: 0.3)),- ),- child: Row(- children: [- Icon(Icons.warning_amber_rounded, size: 18, color: cs.error),- const SizedBox(width: 10),- Expanded(- child: Text(- _loginError!,- style: tt.bodySmall?.copyWith(color: cs.error),- ),- ),- ],- ),- ),- ),-- if (!usingApiKey) ...[- // Username- _buildInputField(- controller: _usernameController,- focusNode: _usernameFocus,- label: l.loginUsername,- cs: cs,- textInputAction: TextInputAction.next,- autofillHints: const [AutofillHints.username],- prefixIcon: Icon(Icons.person_outline_rounded, size: 20,- color: cs.onSurfaceVariant.withValues(alpha: 0.5)),- validator: (v) {- if (v == null || v.trim().isEmpty) {- return l.loginUsernameRequired;- }- return null;- },- ),- const SizedBox(height: 14),-- // Password- _buildInputField(- controller: _passwordController,- label: l.loginPassword,- cs: cs,- obscureText: _obscurePassword,- textInputAction: TextInputAction.done,- autofillHints: const [AutofillHints.password],- onFieldSubmitted: (_) => _handleLogin(),- prefixIcon: Icon(Icons.lock_outline_rounded, size: 20,- color: cs.onSurfaceVariant.withValues(alpha: 0.5)),- suffixIcon: IconButton(- icon: Icon(- _obscurePassword- ? Icons.visibility_outlined- : Icons.visibility_off_outlined,- size: 20,- color: cs.onSurfaceVariant.withValues(alpha: 0.5),- ),- onPressed: () {- setState(() => _obscurePassword = !_obscurePassword);- },- ),- // No validator — ABS allows passwordless accounts- ),- ],- const SizedBox(height: 24),-- // Sign In button- SizedBox(- width: double.infinity,- height: 52,- child: FilledButton(- onPressed: _isConnecting || _isOidcLoading ? null : _handleLogin,- style: FilledButton.styleFrom(- backgroundColor: cs.primary,- foregroundColor: cs.onPrimary,- shape: RoundedRectangleBorder(- borderRadius: BorderRadius.circular(14),- ),- elevation: 0,- ),- child: AnimatedSwitcher(- duration: const Duration(milliseconds: 200),- child: _isConnecting- ? SizedBox(- key: const ValueKey('loading'),- width: 22,- height: 22,- child: CircularProgressIndicator(- strokeWidth: 2.5,- color: cs.onPrimary,- ),- )- : Text(- l.loginSignIn,- key: const ValueKey('text'),- style: tt.titleMedium?.copyWith(- fontWeight: FontWeight.w600,- color: cs.onPrimary,- ),- ),- ),- ),- ),- AnimatedSize(- duration: const Duration(milliseconds: 350),- curve: Curves.easeOutCubic,- alignment: Alignment.topCenter,- child: _serverValid && _oidcConfig != null && _oidcConfig!.enabled- ? _buildOidcButton(cs, tt)- : const SizedBox.shrink(),- ),--- ],- ),- );- }-}+import 'dart:async';+import 'dart:convert';+import 'dart:io';+import 'dart:ui';+import 'package:flutter/material.dart';+import 'package:flutter/services.dart';+import 'package:file_picker/file_picker.dart';+import 'package:package_info_plus/package_info_plus.dart';+import 'package:provider/provider.dart';+import '../providers/auth_provider.dart';+import '../services/api_service.dart';+import '../services/backup_service.dart';+import '../services/oidc_service.dart';+import '../services/settings_sync_service.dart';+import '../services/setup_link_service.dart';+import '../services/user_account_service.dart';+import '../widgets/absorb_wave_icon.dart';+import '../widgets/overlay_toast.dart';+import '../widgets/setup_link_login.dart';+import '../services/signed_out_playback.dart';+import 'downloads_screen.dart';+import '../services/audio_player_service.dart';+import '../main.dart' show applyTrustAllCerts, flatNotifier;+import '../l10n/app_localizations.dart';+import '../services/wording.dart';++class LoginScreen extends StatefulWidget {+ final SavedAccount? prefillAccount;+ final String? prefillServerUrl;++ const LoginScreen({+ super.key,+ this.prefillAccount,+ this.prefillServerUrl,+ });++ @override+ State<LoginScreen> createState() => _LoginScreenState();+}+class _LoginScreenState extends State<LoginScreen>+ with SingleTickerProviderStateMixin {+ final _formKey = GlobalKey<FormState>();+ final _serverController = TextEditingController();+ final _usernameController = TextEditingController();+ final _passwordController = TextEditingController();+ final _apiKeyController = TextEditingController();+ final _usernameFocus = FocusNode();++ bool _obscurePassword = true;+ bool _obscureApiKey = true;+ bool _isConnecting = false;+ String _protocol = 'https://';++ // Server validation state+ bool _serverValid = false;+ bool _serverChecking = false;+ String? _serverError;+ // Technical reason the last check failed (TLS, timeout, HTTP status, etc.),+ // shown beneath the field so unreachable-in-app-but-fine-in-browser servers+ // are diagnosable instead of just "could not reach server".+ String? _serverErrorDetail;+ Timer? _debounce;++ // Login error+ String? _loginError;++ // OIDC state+ OidcConfig? _oidcConfig;+ bool _isOidcLoading = false;++ // Custom headers (advanced)+ bool _showAdvanced = false;+ bool _trustAllCerts = false;+ final List<(TextEditingController, TextEditingController)> _headerControllers = [];++ // App version+ String _appVersion = '';++ late AnimationController _animController;+ late Animation<double> _fadeAnim;+ late Animation<double> _slideAnim;++ @override+ void initState() {+ super.initState();+ _animController = AnimationController(+ vsync: this,+ duration: const Duration(milliseconds: 1200),+ );+ _fadeAnim = CurvedAnimation(+ parent: _animController,+ curve: const Interval(0.0, 0.5, curve: Curves.easeOut),+ );+ _slideAnim = CurvedAnimation(+ parent: _animController,+ curve: const Interval(0.3, 1.0, curve: Curves.easeOutCubic),+ );+ _animController.forward();+ _serverController.addListener(_onServerChanged);+ _loadVersion();+ WidgetsBinding.instance.addPostFrameCallback((_) {+ if (!mounted) return;+ final auth = context.read<AuthProvider>();+ final account = widget.prefillAccount;+ if (account != null) {+ _setInitialHeaders(account.customHeaders);+ _prefillLogin(account.serverUrl, account.username);+ return;+ }+ final requestedServer = widget.prefillServerUrl;+ if (requestedServer != null && requestedServer.isNotEmpty) {+ _setInitialHeaders(auth.customHeaders);+ _prefillLogin(requestedServer, '');+ return;+ }+ final serverUrl = auth.serverUrl;+ if (!auth.isAuthenticated && serverUrl != null && serverUrl.isNotEmpty) {+ _prefillLogin(serverUrl, auth.username ?? '');+ }+ });+ }++ void _prefillLogin(String serverUrl, String username) {+ final uri = Uri.tryParse(serverUrl);+ final protocol = uri?.scheme == 'http' || uri?.scheme == 'https'+ ? '${uri!.scheme}://'+ : _protocol;+ setState(() => _protocol = protocol);+ _serverController.text = serverUrl.startsWith(protocol)+ ? serverUrl.substring(protocol.length)+ : serverUrl;+ _usernameController.text = username;+ }++ void _setInitialHeaders(Map<String, String> headers) {+ for (final (key, value) in _headerControllers) {+ key.dispose();+ value.dispose();+ }+ _headerControllers+ ..clear()+ ..addAll(headers.entries.map(+ (entry) => (+ TextEditingController(text: entry.key),+ TextEditingController(text: entry.value),+ ),+ ));+ if (headers.isNotEmpty) _showAdvanced = true;+ }++ Future<void> _loadVersion() async {+ try {+ final info = await PackageInfo.fromPlatform();+ if (mounted) setState(() => _appVersion = 'v${info.version}');+ } catch (_) {}+ try {+ final trust = await PlayerSettings.getTrustAllCerts();+ if (mounted) setState(() => _trustAllCerts = trust);+ } catch (_) {}+ }++ @override+ void dispose() {+ _debounce?.cancel();+ _animController.dispose();+ _serverController.dispose();+ _usernameController.dispose();+ _passwordController.dispose();+ _apiKeyController.dispose();+ _usernameFocus.dispose();+ for (final (k, v) in _headerControllers) { k.dispose(); v.dispose(); }+ OidcService().cancel();+ super.dispose();+ }++ String _lastValidatedServer = '';++ /// Re-trigger server validation when custom headers change.+ void _revalidateServer() {+ if (_serverController.text.trim().isEmpty) return;+ _lastValidatedServer = ''; // Force re-check+ _debounce?.cancel();+ _debounce = Timer(const Duration(milliseconds: 800), () => _checkServer());+ }++ void _onServerChanged() {+ final text = _serverController.text.trim();+ if (text.isEmpty) {+ setState(() {+ _serverValid = false;+ _serverChecking = false;+ _serverError = null;+ _serverErrorDetail = null;+ _lastValidatedServer = '';+ });+ _debounce?.cancel();+ return;+ }++ // Only invalidate if the server text actually changed from what we validated+ final cleanUrl = text.replaceAll(RegExp(r'^https?://'), '');+ final fullUrl = '$_protocol$cleanUrl';+ if (fullUrl != _lastValidatedServer) {+ setState(() {+ _serverValid = false;+ _serverChecking = true;+ _serverError = null;+ });+ } else {+ // Same server, just re-checking — keep fields visible+ setState(() {+ _serverChecking = true;+ _serverError = null;+ });+ }++ _debounce?.cancel();+ _debounce = Timer(const Duration(milliseconds: 800), () => _checkServer());+ }++ Map<String, String> _collectHeaders() {+ final headers = <String, String>{};+ for (final (keyCtrl, valCtrl) in _headerControllers) {+ final k = keyCtrl.text.trim();+ final v = valCtrl.text.trim();+ if (k.isNotEmpty && v.isNotEmpty) headers[k] = v;+ }+ return headers;+ }++ Future<void> _checkServer() async {+ final text = _serverController.text.trim();+ if (text.isEmpty) return;++ final cleanUrl = text.replaceAll(RegExp(r'^https?://'), '');+ final fullUrl = '$_protocol$cleanUrl';++ try {+ final headers = _collectHeaders();+ final result = await ApiService.pingServerDetailed(fullUrl, customHeaders: headers);+ final ok = result.ok;+ if (!mounted) return;+ if (_serverController.text.trim() != text) return;++ setState(() {+ _serverChecking = false;+ _serverValid = ok;+ _serverError = ok ? null : AppLocalizations.of(context)!.loginCouldNotReachServer;+ _serverErrorDetail = ok ? null : result.detail;+ if (ok) {+ _lastValidatedServer = fullUrl;+ } else {+ _oidcConfig = null;+ _lastValidatedServer = '';+ }+ });++ if (ok) {+ // Also check if OIDC is available+ OidcService.checkOidcEnabled(fullUrl, customHeaders: headers).then((config) {+ if (mounted && _serverController.text.trim() == text) {+ setState(() => _oidcConfig = config);+ }+ });++ // Don't auto-focus — user may still be typing IP:port+ }+ } catch (e) {+ if (!mounted) return;+ setState(() {+ _serverChecking = false;+ _serverValid = false;+ _serverError = AppLocalizations.of(context)!.loginCouldNotReachServer;+ _serverErrorDetail = e.toString();+ _oidcConfig = null;+ });+ }+ }++ Future<void> _handleLogin() async {+ final apiKey = _apiKeyController.text.trim();++ // Skip form validation when an API key is supplied — the username+ // field's required-validator would otherwise block the submit.+ if (apiKey.isEmpty && !_formKey.currentState!.validate()) return;++ // Validate server first if not yet checked+ if (!_serverValid) {+ await _checkServer();+ if (!_serverValid) {+ setState(() => _loginError = _serverError ?? AppLocalizations.of(context)!.loginCouldNotReachServer);+ return;+ }+ }++ setState(() {+ _isConnecting = true;+ _loginError = null;+ });++ final auth = context.read<AuthProvider>();+ final serverText = _serverController.text.trim();+ final cleanUrl = serverText.replaceAll(RegExp(r'^https?://'), '');+ final fullUrl = '$_protocol$cleanUrl';++ final headers = _collectHeaders();++ final success = apiKey.isNotEmpty+ ? await auth.loginWithApiKey(+ serverUrl: fullUrl,+ apiKey: apiKey,+ customHeaders: headers,+ l: AppLocalizations.of(context),+ )+ : await auth.login(+ serverUrl: fullUrl,+ username: _usernameController.text.trim(),+ password: _passwordController.text,+ customHeaders: headers,+ l: AppLocalizations.of(context),+ );++ if (mounted) {+ setState(() => _isConnecting = false);++ if (!success) {+ setState(() {+ _loginError = auth.errorMessage ?? AppLocalizations.of(context)!.loginFailed;+ });+ } else {+ TextInput.finishAutofillContext();+ FocusManager.instance.primaryFocus?.unfocus();+ if (Navigator.of(context).canPop()) {+ // If pushed as a route (e.g. Add Account), pop back+ Navigator.of(context).pop();+ }+ }+ }+ }++ Future<void> _handleOidcLogin() async {+ if (!_serverValid) return;++ setState(() {+ _isOidcLoading = true;+ _loginError = null;+ });++ final serverText = _serverController.text.trim();+ final cleanUrl = serverText.replaceAll(RegExp(r'^https?://'), '');+ final fullUrl = '$_protocol$cleanUrl';++ final headers = _collectHeaders();+ final oidc = OidcService();+ final callbackUri = await oidc.startLogin(fullUrl, customHeaders: headers);+ if (callbackUri == null) {+ if (!mounted) return;+ // Quiet path: user just dismissed the popup. Don't surface an error.+ if (oidc.lastWasUserCancel) {+ setState(() => _isOidcLoading = false);+ return;+ }+ final base = AppLocalizations.of(context)!.loginSsoFailed;+ final detail = oidc.lastError;+ setState(() {+ _isOidcLoading = false;+ _loginError = detail != null ? '$base\n\n$detail' : base;+ });+ return;+ }++ final result = await oidc.handleCallback(callbackUri);+ if (result != null && mounted) {+ FocusManager.instance.primaryFocus?.unfocus();+ final auth = context.read<AuthProvider>();+ final success = await auth.loginWithOidc(+ serverUrl: fullUrl,+ result: result,+ customHeaders: headers,+ l: AppLocalizations.of(context),+ );+ if (mounted) {+ setState(() => _isOidcLoading = false);+ if (!success) {+ setState(() => _loginError = auth.errorMessage ?? AppLocalizations.of(context)!.loginSsoFailed);+ } else if (Navigator.of(context).canPop()) {+ Navigator.of(context).pop();+ }+ }+ } else if (mounted) {+ final base = AppLocalizations.of(context)!.loginSsoAuthFailed;+ final detail = oidc.lastError;+ setState(() {+ _isOidcLoading = false;+ _loginError = detail != null ? '$base\n\n$detail' : base;+ });+ }+ }++ @override+ Widget build(BuildContext context) {+ final cs = Theme.of(context).colorScheme;+ final tt = Theme.of(context).textTheme;+ final l = AppLocalizations.of(context)!;++ return Scaffold(+ resizeToAvoidBottomInset: true,+ body: Container(+ decoration: flatNotifier.value ? null : BoxDecoration(+ gradient: LinearGradient(+ begin: Alignment.topLeft,+ end: Alignment.bottomRight,+ stops: const [0.0, 0.4, 0.7, 1.0],+ colors: [+ cs.primary.withValues(alpha: 0.15),+ cs.primary.withValues(alpha: 0.05),+ cs.surface,+ cs.surface,+ ],+ ),+ ),+ child: SafeArea(+ child: LayoutBuilder(+ builder: (context, constraints) => SingleChildScrollView(+ padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 24),+ child: ConstrainedBox(+ constraints: BoxConstraints(minHeight: constraints.maxHeight - 48),+ child: Column(+ mainAxisAlignment: MainAxisAlignment.center,+ mainAxisSize: MainAxisSize.min,+ children: [+ // ── Logo + Tagline ──+ FadeTransition(+ opacity: _fadeAnim,+ child: Column(+ children: [+ // Wave icon with glow+ Container(+ width: 100,+ height: 100,+ decoration: BoxDecoration(+ shape: BoxShape.circle,+ boxShadow: [+ BoxShadow(+ color: cs.primary.withValues(alpha: 0.3),+ blurRadius: 40,+ spreadRadius: 8,+ ),+ ],+ ),+ child: Container(+ decoration: BoxDecoration(+ shape: BoxShape.circle,+ color: cs.primary.withValues(alpha: 0.1),+ border: Border.all(+ color: cs.primary.withValues(alpha: 0.15),+ width: 1.5,+ ),+ ),+ child: Center(+ child: AbsorbWaveIcon(+ size: 44,+ color: cs.primary,+ ),+ ),+ ),+ ),+ const SizedBox(height: 28),+ Text(+ l.appTitle,+ style: tt.headlineMedium?.copyWith(+ fontWeight: FontWeight.w200,+ color: cs.onSurface,+ letterSpacing: 10,+ ),+ ),+ const SizedBox(height: 10),+ Text(+ Wording.of(context).loginTagline,+ style: tt.bodyLarge?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.6),+ fontWeight: FontWeight.w400,+ letterSpacing: 0.5,+ ),+ ),+ ],+ ),+ ),+ const SizedBox(height: 40),++ // ── Glass form card ──+ SlideTransition(+ position: Tween<Offset>(+ begin: const Offset(0, 0.12),+ end: Offset.zero,+ ).animate(_slideAnim),+ child: FadeTransition(+ opacity: _slideAnim,+ child: ClipRRect(+ borderRadius: BorderRadius.circular(24),+ child: BackdropFilter(+ filter: ImageFilter.blur(sigmaX: 20, sigmaY: 20),+ child: Container(+ padding: const EdgeInsets.all(24),+ decoration: BoxDecoration(+ color: cs.surfaceContainerHighest.withValues(alpha: 0.35),+ borderRadius: BorderRadius.circular(24),+ border: Border.all(+ color: cs.outlineVariant.withValues(alpha: 0.15),+ ),+ ),+ child: Form(+ key: _formKey,+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ // Section label+ Padding(+ padding: const EdgeInsets.only(left: 4, bottom: 16),+ child: Text(+ l.loginConnectToServer,+ style: tt.titleSmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.7),+ fontWeight: FontWeight.w500,+ ),+ ),+ ),++ // Server URL+ _buildInputField(+ controller: _serverController,+ label: l.loginServerAddress,+ hint: l.loginServerHint,+ helperText: l.loginServerHelper,+ keyboardType: TextInputType.url,+ textInputAction: TextInputAction.next,+ onFieldSubmitted: (_) {+ if (!_serverValid && !_serverChecking) {+ _checkServer().then((_) {+ if (mounted && _serverValid) _usernameFocus.requestFocus();+ });+ } else if (_serverValid) {+ _usernameFocus.requestFocus();+ }+ },+ cs: cs,+ prefixIcon: Padding(+ padding: const EdgeInsets.only(left: 8),+ child: DropdownButtonHideUnderline(+ child: DropdownButton<String>(+ value: _protocol,+ isDense: true,+ style: TextStyle(+ color: cs.primary,+ fontSize: 13,+ fontWeight: FontWeight.w600,+ ),+ items: const [+ DropdownMenuItem(+ value: 'https://',+ child: Text('https://'),+ ),+ DropdownMenuItem(+ value: 'http://',+ child: Text('http://'),+ ),+ ],+ onChanged: (v) {+ if (v != null) {+ setState(() {+ _protocol = v;+ _serverValid = false;+ });+ _onServerChanged();+ }+ },+ ),+ ),+ ),+ suffixIcon: _serverChecking+ ? const Padding(+ padding: EdgeInsets.all(14),+ child: SizedBox(+ width: 18, height: 18,+ child: CircularProgressIndicator(strokeWidth: 2),+ ),+ )+ : _serverValid+ ? Icon(Icons.check_circle_rounded,+ color: Colors.green.shade400, size: 22)+ : _serverError != null+ ? Icon(Icons.error_outline_rounded,+ color: cs.error, size: 22)+ : null,+ borderColor: _serverValid+ ? Colors.green.shade400.withValues(alpha: 0.4)+ : _serverError != null+ ? cs.error.withValues(alpha: 0.4)+ : null,+ errorText: _serverError,+ ),++ if (_serverErrorDetail != null) ...[+ const SizedBox(height: 6),+ Padding(+ padding: const EdgeInsets.symmetric(horizontal: 4),+ child: Text(+ _serverErrorDetail!,+ style: TextStyle(+ fontSize: 11,+ color: cs.onSurfaceVariant.withValues(alpha: 0.6),+ ),+ ),+ ),+ ],++ // Advanced: Custom Headers — must be before server validation+ const SizedBox(height: 8),+ GestureDetector(+ onTap: () => setState(() => _showAdvanced = !_showAdvanced),+ child: Row(+ children: [+ Icon(_showAdvanced ? Icons.expand_less_rounded : Icons.expand_more_rounded,+ size: 18, color: cs.onSurfaceVariant.withValues(alpha: 0.5)),+ const SizedBox(width: 4),+ Text(l.loginAdvanced, style: TextStyle(fontSize: 13, color: cs.onSurfaceVariant.withValues(alpha: 0.5))),+ ],+ ),+ ),+ if (_showAdvanced) ...[+ const SizedBox(height: 8),+ Text(l.loginCustomHttpHeaders, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: cs.onSurfaceVariant.withValues(alpha: 0.7))),+ const SizedBox(height: 4),+ Text(l.loginCustomHeadersDescription,+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4))),+ const SizedBox(height: 8),+ ..._headerControllers.asMap().entries.map((entry) {+ final i = entry.key;+ final (keyCtrl, valCtrl) = entry.value;+ return Padding(+ padding: const EdgeInsets.only(bottom: 8),+ child: Row(+ children: [+ Expanded(+ flex: 2,+ child: TextField(+ controller: keyCtrl,+ style: TextStyle(fontSize: 13, color: cs.onSurface),+ onChanged: (_) => _revalidateServer(),+ decoration: InputDecoration(+ hintText: l.loginHeaderName,+ hintStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.3), fontSize: 13),+ filled: true,+ fillColor: cs.surfaceContainerHighest.withValues(alpha: 0.5),+ border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none),+ contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),+ ),+ ),+ ),+ const SizedBox(width: 8),+ Expanded(+ flex: 3,+ child: TextField(+ controller: valCtrl,+ style: TextStyle(fontSize: 13, color: cs.onSurface),+ onChanged: (_) => _revalidateServer(),+ decoration: InputDecoration(+ hintText: l.loginHeaderValue,+ hintStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.3), fontSize: 13),+ filled: true,+ fillColor: cs.surfaceContainerHighest.withValues(alpha: 0.5),+ border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none),+ contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),+ ),+ ),+ ),+ const SizedBox(width: 4),+ GestureDetector(+ onTap: () {+ setState(() {+ _headerControllers[i].$1.dispose();+ _headerControllers[i].$2.dispose();+ _headerControllers.removeAt(i);+ });+ _revalidateServer();+ },+ child: Icon(Icons.close_rounded, size: 18, color: cs.error.withValues(alpha: 0.6)),+ ),+ ],+ ),+ );+ }),+ GestureDetector(+ onTap: () => setState(() {+ _headerControllers.add((TextEditingController(), TextEditingController()));+ }),+ child: Container(+ padding: const EdgeInsets.symmetric(vertical: 8),+ child: Row(+ mainAxisAlignment: MainAxisAlignment.center,+ children: [+ Icon(Icons.add_rounded, size: 16, color: cs.primary),+ const SizedBox(width: 4),+ Text(l.loginAddHeader, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: cs.primary)),+ ],+ ),+ ),+ ),+ const SizedBox(height: 8),+ const Divider(height: 1),+ const SizedBox(height: 4),+ Text(l.loginSelfSignedCertificates, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: cs.onSurfaceVariant.withValues(alpha: 0.7))),+ Row(+ children: [+ Expanded(+ child: Text(+ l.loginTrustAllCertificates,+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4)),+ ),+ ),+ Transform.scale(+ scale: 0.8,+ child: Switch(+ value: _trustAllCerts,+ onChanged: (v) async {+ setState(() => _trustAllCerts = v);+ await PlayerSettings.setTrustAllCerts(v);+ applyTrustAllCerts(v);+ _revalidateServer();+ },+ ),+ ),+ ],+ ),+ const SizedBox(height: 8),+ const Divider(height: 1),+ const SizedBox(height: 8),+ Text(l.loginApiKey, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: cs.onSurfaceVariant.withValues(alpha: 0.7))),+ const SizedBox(height: 4),+ Text(l.loginApiKeyDescription,+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4))),+ const SizedBox(height: 8),+ TextField(+ controller: _apiKeyController,+ obscureText: _obscureApiKey,+ autocorrect: false,+ enableSuggestions: false,+ style: TextStyle(fontSize: 13, color: cs.onSurface),+ onChanged: (_) => setState(() {}),+ decoration: InputDecoration(+ hintText: l.loginApiKey,+ hintStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.3), fontSize: 13),+ filled: true,+ fillColor: cs.surfaceContainerHighest.withValues(alpha: 0.5),+ border: OutlineInputBorder(borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none),+ contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),+ prefixIcon: Icon(Icons.vpn_key_rounded, size: 18, color: cs.onSurfaceVariant.withValues(alpha: 0.5)),+ suffixIcon: IconButton(+ icon: Icon(+ _obscureApiKey ? Icons.visibility_outlined : Icons.visibility_off_outlined,+ size: 18,+ color: cs.onSurfaceVariant.withValues(alpha: 0.5),+ ),+ onPressed: () => setState(() => _obscureApiKey = !_obscureApiKey),+ ),+ ),+ ),+ ],++ // Credentials — always visible+ _buildCredentialFields(cs, tt),+ ],+ ),+ ),+ ),+ ),+ ),+ ),+ ),++ if (SignedOutPlayback.available) ...[+ const SizedBox(height: 16),+ SizedBox(+ width: double.infinity,+ height: 48,+ child: FilledButton.tonalIcon(+ key: const Key('play-downloads-signed-out'),+ icon: const Icon(Icons.download_done_rounded),+ label: Text(l.loginPlayDownloads),+ style: FilledButton.styleFrom(+ shape: RoundedRectangleBorder(+ borderRadius: BorderRadius.circular(14),+ ),+ ),+ onPressed: () => Navigator.of(context).push(+ MaterialPageRoute(+ builder: (_) =>+ const DownloadsScreen(signedOut: true),+ ),+ ),+ ),+ ),+ ],++ // ── Version + Restore pill ──+ const SizedBox(height: 32),+ FadeTransition(+ opacity: _fadeAnim,+ child: Column(+ children: [+ Text(+ _appVersion,+ style: tt.labelSmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.3),+ letterSpacing: 1,+ ),+ ),+ const SizedBox(height: 12),+ Wrap(+ alignment: WrapAlignment.center,+ spacing: 8,+ runSpacing: 8,+ children: [+ ActionChip(+ avatar: Icon(Icons.restore_rounded, size: 16,+ color: cs.onSurfaceVariant.withValues(alpha: 0.6)),+ label: Text(l.loginRestoreFromBackup,+ style: tt.labelSmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.6),+ )),+ backgroundColor: cs.surfaceContainerHighest.withValues(alpha: 0.3),+ side: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.15)),+ shape: RoundedRectangleBorder(+ borderRadius: BorderRadius.circular(20),+ ),+ onPressed: _restoreFromBackup,+ ),+ ActionChip(+ key: const Key('paste-login-link'),+ avatar: Icon(Icons.content_paste_rounded, size: 16,+ color: cs.onSurfaceVariant.withValues(alpha: 0.6)),+ label: Text(l.loginPasteLink,+ style: tt.labelSmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.6),+ )),+ backgroundColor: cs.surfaceContainerHighest.withValues(alpha: 0.3),+ side: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.15)),+ shape: RoundedRectangleBorder(+ borderRadius: BorderRadius.circular(20),+ ),+ onPressed: _pasteLoginLink,+ ),+ ],+ )+ ],+ ),+ ),++ // ── Saved accounts quick-switch ──+ _buildSavedAccounts(cs, tt),+ ],+ ),+ ),+ ),+ ),+ ),+ ),+ );+ }++ Widget _buildSavedAccounts(ColorScheme cs, TextTheme tt) {+ final accounts = UserAccountService().accounts;+ if (accounts.isEmpty) return const SizedBox.shrink();+ final l = AppLocalizations.of(context)!;++ return FadeTransition(+ opacity: _fadeAnim,+ child: Padding(+ padding: const EdgeInsets.only(top: 32),+ child: Column(+ children: [+ Row(+ children: [+ Expanded(child: Divider(color: cs.outlineVariant.withValues(alpha: 0.15))),+ Padding(+ padding: const EdgeInsets.symmetric(horizontal: 14),+ child: Text(l.loginSavedAccounts,+ style: tt.bodySmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.4))),+ ),+ Expanded(child: Divider(color: cs.outlineVariant.withValues(alpha: 0.15))),+ ],+ ),+ const SizedBox(height: 12),+ ...accounts.map((account) {+ final shortUrl = account.serverUrl+ .replaceAll(RegExp(r'^https?://'), '')+ .replaceAll(RegExp(r'/+$'), '');+ return Padding(+ padding: const EdgeInsets.only(bottom: 8),+ child: Material(+ color: cs.surface.withValues(alpha: 0.3),+ borderRadius: BorderRadius.circular(14),+ child: InkWell(+ borderRadius: BorderRadius.circular(14),+ onTap: () => _quickSwitch(account),+ child: Padding(+ padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),+ child: Row(+ children: [+ CircleAvatar(+ radius: 18,+ backgroundColor: cs.primary.withValues(alpha: 0.15),+ child: Text(+ account.username.isNotEmpty+ ? account.username[0].toUpperCase()+ : '?',+ style: TextStyle(+ color: cs.primary,+ fontWeight: FontWeight.w600,+ ),+ ),+ ),+ const SizedBox(width: 12),+ Expanded(+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ Text(account.username,+ style: tt.bodyMedium?.copyWith(+ fontWeight: FontWeight.w600,+ color: cs.onSurface)),+ Text(shortUrl,+ style: tt.labelSmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.5)),+ maxLines: 1,+ overflow: TextOverflow.ellipsis),+ ],+ ),+ ),+ Icon(Icons.arrow_forward_ios_rounded,+ size: 14, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),+ ],+ ),+ ),+ ),+ ),+ );+ }),+ ],+ ),+ ),+ );+ }++ Future<void> _restoreFromBackup() async {+ try {+ final result = await FilePicker.platform.pickFiles(type: FileType.any);+ if (result == null || result.files.isEmpty) return;++ final file = File(result.files.single.path!);+ final jsonStr = await file.readAsString();+ final data = jsonDecode(jsonStr) as Map<String, dynamic>;++ if (!data.containsKey('version')) {+ if (mounted) {+ final l = AppLocalizations.of(context)!;+ showOverlayToast(context, l.loginInvalidBackupFile,+ icon: Icons.error_outline_rounded);+ }+ return;+ }++ final accounts = data['accounts'] as List<dynamic>?;+ final accountCount = accounts?.length ?? 0;+ final hasAccounts = accountCount > 0;+ // A setup/login file carries an account but no settings payload, so it+ // should read as a sign-in rather than a backup restore.+ final isSetup = data['setup'] == true || (hasAccounts && data['settings'] == null);+ final firstUsername = hasAccounts ? ((accounts!.first as Map)['username'] as String? ?? '') : '';++ if (!mounted) return;+ final l = AppLocalizations.of(context)!;+ final confirm = await showDialog<bool>(+ context: context,+ builder: (ctx) => AlertDialog(+ title: Text(isSetup ? l.loginSignIn : l.loginRestoreBackupTitle),+ content: Text(isSetup+ ? (firstUsername.isNotEmpty ? l.loginSignInAs(firstUsername) : l.loginSignInToServer)+ : (hasAccounts+ ? l.loginRestoreBackupWithAccounts(accountCount)+ : l.loginRestoreBackupNoAccounts)),+ actions: [+ TextButton(+ onPressed: () => Navigator.pop(ctx, false),+ child: Text(l.cancel),+ ),+ FilledButton(+ onPressed: () => Navigator.pop(ctx, true),+ child: Text(isSetup ? l.loginSignIn : l.loginRestore),+ ),+ ],+ ),+ );++ if (confirm != true) return;++ await BackupService.importSettings(data);++ // Auto-login with the first restored account+ if (hasAccounts && mounted) {+ final restoredAccounts = UserAccountService().accounts;+ if (restoredAccounts.isNotEmpty) {+ final auth = context.read<AuthProvider>();+ await auth.switchToAccount(restoredAccounts.first);++ if (mounted) {+ final l2 = AppLocalizations.of(context)!;+ if (auth.isAuthenticated && auth.serverReachable) {+ showOverlayToast(+ context,+ isSetup+ ? l2.loginSignedInAs(restoredAccounts.first.username)+ : l2.loginRestoredAndSignedIn(restoredAccounts.first.username),+ icon: Icons.check_circle_outline_rounded,+ );+ } else {+ // Token expired — accounts are saved but need re-auth+ showOverlayToast(context, l2.loginSessionExpired, icon: Icons.error_outline_rounded);+ setState(() {}); // Refresh to show saved accounts list+ }+ }+ }+ } else if (mounted) {+ final l3 = AppLocalizations.of(context)!;+ showOverlayToast(context, l3.loginSettingsRestored, icon: Icons.check_circle_outline_rounded);+ }++ await SettingsSyncService().resolveSourceAfterRestore();+ } catch (e) {+ if (mounted) {+ final l4 = AppLocalizations.of(context)!;+ showOverlayToast(context, l4.loginRestoreFailed(e.toString()), icon: Icons.error_outline_rounded);+ }+ }+ }++ Future<void> _pasteLoginLink() async {+ final l = AppLocalizations.of(context)!;+ final controller = TextEditingController();+ String? errorText;+ final link = await showDialog<Uri>(+ context: context,+ builder: (dialogContext) => StatefulBuilder(+ builder: (context, setDialogState) {+ void submit() {+ final uri = Uri.tryParse(controller.text.trim());+ try {+ if (uri == null) throw const SetupLinkException('Invalid link');+ SetupLinkService.parseLink(uri);+ Navigator.pop(dialogContext, uri);+ } on SetupLinkException {+ setDialogState(() => errorText = l.setupLinkInvalid);+ }+ }++ return AlertDialog(+ title: Text(l.loginPasteLink),+ content: Column(+ mainAxisSize: MainAxisSize.min,+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ Text(l.loginPasteLinkHelp),+ const SizedBox(height: 16),+ TextField(+ key: const Key('login-link-field'),+ controller: controller,+ autofocus: true,+ autocorrect: false,+ enableSuggestions: false,+ keyboardType: TextInputType.url,+ minLines: 2,+ maxLines: 4,+ decoration: InputDecoration(+ hintText: 'absorb://setup/...',+ errorText: errorText,+ suffixIcon: IconButton(+ tooltip: l.loginPasteLink,+ icon: const Icon(Icons.content_paste_rounded),+ onPressed: () async {+ final data = await Clipboard.getData('text/plain');+ controller.text = data?.text ?? '';+ setDialogState(() => errorText = null);+ },+ ),+ ),+ ),+ ],+ ),+ actions: [+ TextButton(+ onPressed: () => Navigator.pop(dialogContext),+ child: Text(l.cancel),+ ),+ FilledButton(+ key: const Key('submit-login-link'),+ onPressed: submit,+ child: Text(l.loginSignIn),+ ),+ ],+ );+ },+ ),+ );+ controller.dispose();+ if (link == null || !mounted) return;+ await handleSetupLinkLogin(context, link);+ }++ Future<void> _quickSwitch(SavedAccount account) async {+ if (account.token.isEmpty) {+ _setInitialHeaders(account.customHeaders);+ _prefillLogin(account.serverUrl, account.username);+ _passwordController.clear();+ _usernameFocus.unfocus();+ return;+ }+ setState(() => _isConnecting = true);++ final auth = context.read<AuthProvider>();+ await auth.switchToAccount(account);++ if (mounted) {+ setState(() => _isConnecting = false);+ if (auth.isAuthenticated && Navigator.of(context).canPop()) {+ Navigator.of(context).pop();+ }+ // If this is the root login screen, AuthGate will react to the state change+ }+ }++ Widget _buildInputField({+ required TextEditingController controller,+ required String label,+ required ColorScheme cs,+ String? hint,+ String? helperText,+ TextInputType? keyboardType,+ TextInputAction? textInputAction,+ FocusNode? focusNode,+ Widget? prefixIcon,+ Widget? suffixIcon,+ Color? borderColor,+ String? errorText,+ bool obscureText = false,+ void Function(String)? onFieldSubmitted,+ String? Function(String?)? validator,+ Iterable<String>? autofillHints,+ }) {+ final isUrl = keyboardType == TextInputType.url;+ return TextFormField(+ controller: controller,+ focusNode: focusNode,+ keyboardType: keyboardType,+ textInputAction: textInputAction,+ obscureText: obscureText,+ autocorrect: !isUrl,+ enableSuggestions: !isUrl,+ autofillHints: autofillHints,+ onFieldSubmitted: onFieldSubmitted,+ validator: validator,+ style: TextStyle(color: cs.onSurface),+ decoration: InputDecoration(+ labelText: label,+ hintText: hint,+ helperText: helperText,+ helperStyle: TextStyle(color: cs.onSurfaceVariant.withValues(alpha: 0.5), fontSize: 11),+ helperMaxLines: 2,+ prefixIcon: prefixIcon,+ suffixIcon: suffixIcon,+ errorText: errorText,+ border: OutlineInputBorder(+ borderRadius: BorderRadius.circular(14),+ borderSide: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.2)),+ ),+ enabledBorder: OutlineInputBorder(+ borderRadius: BorderRadius.circular(14),+ borderSide: BorderSide(+ color: borderColor ?? cs.outlineVariant.withValues(alpha: 0.15),+ ),+ ),+ focusedBorder: OutlineInputBorder(+ borderRadius: BorderRadius.circular(14),+ borderSide: BorderSide(color: cs.primary.withValues(alpha: 0.6), width: 1.5),+ ),+ filled: true,+ fillColor: cs.surface.withValues(alpha: 0.4),+ contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),+ ),+ );+ }++ Widget _buildOidcButton(ColorScheme cs, TextTheme tt) {+ final l = AppLocalizations.of(context)!;+ return Column(+ children: [+ const SizedBox(height: 12),+ SizedBox(+ width: double.infinity,+ height: 44,+ child: OutlinedButton.icon(+ onPressed: _isConnecting || _isOidcLoading ? null : _handleOidcLogin,+ icon: _isOidcLoading+ ? SizedBox(+ width: 18, height: 18,+ child: CircularProgressIndicator(strokeWidth: 2, color: cs.primary),+ )+ : Icon(Icons.login_rounded, size: 20, color: cs.primary),+ label: Text(+ _isOidcLoading ? l.loginWaitingForSso : _oidcConfig!.buttonText,+ style: tt.titleMedium?.copyWith(+ fontWeight: FontWeight.w600,+ color: cs.primary,+ ),+ ),+ style: OutlinedButton.styleFrom(+ side: BorderSide(color: cs.primary.withValues(alpha: 0.3)),+ shape: RoundedRectangleBorder(+ borderRadius: BorderRadius.circular(12),+ ),+ ),+ ),+ ),+ const SizedBox(height: 6),+ Text(+ l.loginRedirectUri,+ style: tt.labelSmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.4),+ letterSpacing: 0.3,+ ),+ ),+ ],+ );+ }++ Widget _buildCredentialFields(ColorScheme cs, TextTheme tt) {+ final l = AppLocalizations.of(context)!;+ final usingApiKey = _apiKeyController.text.trim().isNotEmpty;+ return AutofillGroup(+ child: Column(+ children: [+ const SizedBox(height: 16),++ // Login error message+ if (_loginError != null)+ Padding(+ padding: const EdgeInsets.only(bottom: 12),+ child: Container(+ width: double.infinity,+ padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),+ decoration: BoxDecoration(+ color: cs.error.withValues(alpha: 0.1),+ borderRadius: BorderRadius.circular(12),+ border: Border.all(color: cs.error.withValues(alpha: 0.3)),+ ),+ child: Row(+ children: [+ Icon(Icons.warning_amber_rounded, size: 18, color: cs.error),+ const SizedBox(width: 10),+ Expanded(+ child: Text(+ _loginError!,+ style: tt.bodySmall?.copyWith(color: cs.error),+ ),+ ),+ ],+ ),+ ),+ ),++ if (!usingApiKey) ...[+ // Username+ _buildInputField(+ controller: _usernameController,+ focusNode: _usernameFocus,+ label: l.loginUsername,+ cs: cs,+ textInputAction: TextInputAction.next,+ autofillHints: const [AutofillHints.username],+ prefixIcon: Icon(Icons.person_outline_rounded, size: 20,+ color: cs.onSurfaceVariant.withValues(alpha: 0.5)),+ validator: (v) {+ if (v == null || v.trim().isEmpty) {+ return l.loginUsernameRequired;+ }+ return null;+ },+ ),+ const SizedBox(height: 14),++ // Password+ _buildInputField(+ controller: _passwordController,+ label: l.loginPassword,+ cs: cs,+ obscureText: _obscurePassword,+ textInputAction: TextInputAction.done,+ autofillHints: const [AutofillHints.password],+ onFieldSubmitted: (_) => _handleLogin(),+ prefixIcon: Icon(Icons.lock_outline_rounded, size: 20,+ color: cs.onSurfaceVariant.withValues(alpha: 0.5)),+ suffixIcon: IconButton(+ icon: Icon(+ _obscurePassword+ ? Icons.visibility_outlined+ : Icons.visibility_off_outlined,+ size: 20,+ color: cs.onSurfaceVariant.withValues(alpha: 0.5),+ ),+ onPressed: () {+ setState(() => _obscurePassword = !_obscurePassword);+ },+ ),+ // No validator — ABS allows passwordless accounts+ ),+ ],+ const SizedBox(height: 24),++ // Sign In button+ SizedBox(+ width: double.infinity,+ height: 52,+ child: FilledButton(+ onPressed: _isConnecting || _isOidcLoading ? null : _handleLogin,+ style: FilledButton.styleFrom(+ backgroundColor: cs.primary,+ foregroundColor: cs.onPrimary,+ shape: RoundedRectangleBorder(+ borderRadius: BorderRadius.circular(14),+ ),+ elevation: 0,+ ),+ child: AnimatedSwitcher(+ duration: const Duration(milliseconds: 200),+ child: _isConnecting+ ? SizedBox(+ key: const ValueKey('loading'),+ width: 22,+ height: 22,+ child: CircularProgressIndicator(+ strokeWidth: 2.5,+ color: cs.onPrimary,+ ),+ )+ : Text(+ l.loginSignIn,+ key: const ValueKey('text'),+ style: tt.titleMedium?.copyWith(+ fontWeight: FontWeight.w600,+ color: cs.onPrimary,+ ),+ ),+ ),+ ),+ ),+ AnimatedSize(+ duration: const Duration(milliseconds: 350),+ curve: Curves.easeOutCubic,+ alignment: Alignment.topCenter,+ child: _serverValid && _oidcConfig != null && _oidcConfig!.enabled+ ? _buildOidcButton(cs, tt)+ : const SizedBox.shrink(),+ ),+++ ],+ ),+ );+ }+}diff --git a/lib/services/signed_out_playback.dart b/lib/services/signed_out_playback.dartnew file mode 100644index 00000000..6ddb3573--- /dev/null+++ b/lib/services/signed_out_playback.dart@@ -0,0 +1,59 @@+import 'package:flutter/foundation.dart';+import 'api_service.dart';+import 'audio_player_service.dart';+import 'download_service.dart';+import 'user_account_service.dart';++/// Listening to downloads after the session expired. The account stays saved+/// with its token cleared and remains the active scope, so progress and+/// listening sessions queue under it exactly like offline listening, and+/// signing back into it sends them up.+class SignedOutPlayback {+ SignedOutPlayback._();++ static bool _active = false;+ static bool get active => _active;++ /// The account whose session ran out, or null when there isn't one (a+ /// fresh install, or the user signed out and removed the account).+ static SavedAccount? expiredAccount() {+ final accounts = UserAccountService();+ final scope = accounts.activeScopeKey;+ if (scope.isEmpty) return null;+ for (final account in accounts.accounts) {+ if (account.scopeKey == scope && account.token.isEmpty) return account;+ }+ return null;+ }++ static bool get available =>+ expiredAccount() != null && DownloadService().downloadedItems.isNotEmpty;++ /// Every server call fails straight away as unreachable, which the sync+ /// paths already treat as offline: kept, and sent after the next sign-in.+ static ApiService api() =>+ ApiService(baseUrl: 'http://signed-out.invalid', token: '');++ static void started() {+ if (!_active) {+ debugPrint(+ '[SignedOut] Playing downloads signed out, listening queues for ${expiredAccount()?.username}');+ }+ _active = true;+ UserAccountService.beforeAccountChange = end;+ }++ /// Stops signed-out playback before another sign-in changes the active+ /// account, so its listening is saved under the account it belongs to.+ static Future<void> end() async {+ if (!_active) return;+ _active = false;+ debugPrint('[SignedOut] Signing in, stopping signed-out playback first');+ UserAccountService.beforeAccountChange = null;+ final player = AudioPlayerService();+ if (player.hasBook) {+ await player.pause();+ await player.stop();+ }+ }+}diff --git a/lib/services/user_account_service.dart b/lib/services/user_account_service.dartindex 20c55fd0..f4347af7 100644--- a/lib/services/user_account_service.dart+++ b/lib/services/user_account_service.dart@@ -85,6 +85,10 @@ class UserAccountService {static const _accountsKey = 'saved_accounts';static const _activeKey = 'active_account_scope';+ /// Runs before a sign-in changes the active account, so anything still+ /// being recorded for the old one is saved under it first.+ static Future<void> Function()? beforeAccountChange;+List<SavedAccount> _accounts = [];String? _activeScopeKey;@@ -126,6 +130,7 @@ class UserAccountService {/// Save or update an account after login. Sets it as active.Future<void> saveAccount(SavedAccount account) async {+ await beforeAccountChange?.call();// Remove existing entry for same server+username (update token)_accounts.removeWhere((a) => a.serverUrl == account.serverUrl && a.username == account.username);