added support for multiple accounts with an account switcher in settings
This commit is contained in:
+92
-45
@@ -1,12 +1,13 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../background/widget_background_task.dart';
|
||||
import '../../session/session_lifecycle.dart';
|
||||
import '../../session/session_manager.dart';
|
||||
import '../../state/app/modules/account/bloc/account_bloc.dart';
|
||||
import '../../state/app/modules/account/bloc/account_state.dart';
|
||||
import '../../state/app/modules/settings/bloc/settings_cubit.dart';
|
||||
import '../../storage/dev_tools_settings.dart';
|
||||
import '../../storage/settings.dart' as model;
|
||||
@@ -20,7 +21,10 @@ import 'widgets/login_branding.dart';
|
||||
import 'widgets/login_card.dart';
|
||||
|
||||
class Login extends StatefulWidget {
|
||||
const Login({super.key});
|
||||
/// Signs in another account while one is active; offers to go back.
|
||||
final bool addingAccount;
|
||||
|
||||
const Login({super.key, this.addingAccount = false});
|
||||
|
||||
@override
|
||||
State<Login> createState() => _LoginState();
|
||||
@@ -65,59 +69,100 @@ class _LoginState extends State<Login> with SingleTickerProviderStateMixin {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onLoginSuccess() {
|
||||
Future<void> _onLoginSuccess() async {
|
||||
Haptics.heavyAccent();
|
||||
final accountBloc = context.read<AccountBloc>();
|
||||
// Fade the login content out before handing over to the post-login splash.
|
||||
// Both share the red backdrop, so this reads as one continuous transition
|
||||
// instead of an abrupt swap.
|
||||
final finished = SessionLifecycle.finishLogin();
|
||||
await _fade.reverse().orCancel.onError<TickerCanceled>((_, _) {});
|
||||
String? accountId;
|
||||
try {
|
||||
accountId = await finished;
|
||||
} on Object catch (e) {
|
||||
log('Login: finishing failed: $e');
|
||||
accountId = SessionManager().activeAccount?.id;
|
||||
}
|
||||
// Re-register the periodic refresh (cancelAll runs on logout) and kick
|
||||
// off an immediate one-off so the widget populates within seconds
|
||||
// instead of waiting up to 30 minutes for the next periodic slot.
|
||||
unawaited(WidgetBackgroundTask.initialize());
|
||||
unawaited(WidgetBackgroundTask.requestImmediateRefresh());
|
||||
// Fade the login content out before handing over to the post-login splash.
|
||||
// Both share the red backdrop, so this reads as one continuous transition
|
||||
// instead of an abrupt swap.
|
||||
_fade.reverse().whenComplete(() {
|
||||
if (!mounted) return;
|
||||
context.read<AccountBloc>().setStatus(AccountStatus.loggedIn);
|
||||
});
|
||||
accountBloc.activated(accountId, freshLogin: true);
|
||||
}
|
||||
|
||||
Future<void> _cancelAddAccount() async {
|
||||
final accountBloc = context.read<AccountBloc>();
|
||||
await SessionLifecycle.cancelAddAccount();
|
||||
accountBloc.activated(SessionManager().activeAccount?.id);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
backgroundColor: _marianumRed,
|
||||
body: FadeTransition(
|
||||
opacity: _fade,
|
||||
child: SafeArea(
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) => SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
child: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
minHeight: constraints.maxHeight,
|
||||
maxWidth: 420,
|
||||
Widget build(BuildContext context) => PopScope(
|
||||
canPop: !widget.addingAccount,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop && !_busy) unawaited(_cancelAddAccount());
|
||||
},
|
||||
child: Scaffold(
|
||||
backgroundColor: _marianumRed,
|
||||
appBar: widget.addingAccount
|
||||
? AppBar(
|
||||
backgroundColor: Colors.transparent,
|
||||
foregroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
leading: ListenableBuilder(
|
||||
listenable: _controller,
|
||||
builder: (context, _) => IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: 'Abbrechen',
|
||||
onPressed: _busy ? null : _cancelAddAccount,
|
||||
),
|
||||
// spaceBetween statt Spacer-in-IntrinsicHeight: Letzteres würde
|
||||
// die Column bei Inhaltsänderungen im unteren Block auf die
|
||||
// intrinsic-Höhe pinnen und ein paar Pixel Overflow erzeugen.
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
const LoginHeader(),
|
||||
const SizedBox(height: 28),
|
||||
LoginCard(
|
||||
controller: _controller,
|
||||
onSuccess: _onLoginSuccess,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
),
|
||||
const Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [_EndpointLink(), LoginFooter()],
|
||||
),
|
||||
],
|
||||
),
|
||||
title: const Text('Konto hinzufügen'),
|
||||
)
|
||||
: null,
|
||||
body: FadeTransition(
|
||||
opacity: _fade,
|
||||
child: SafeArea(
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) => SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
child: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
minHeight: constraints.maxHeight,
|
||||
maxWidth: 420,
|
||||
),
|
||||
// spaceBetween statt Spacer-in-IntrinsicHeight: Letzteres würde
|
||||
// die Column bei Inhaltsänderungen im unteren Block auf die
|
||||
// intrinsic-Höhe pinnen und ein paar Pixel Overflow erzeugen.
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
LoginHeader(addingAccount: widget.addingAccount),
|
||||
const SizedBox(height: 28),
|
||||
LoginCard(
|
||||
controller: _controller,
|
||||
onSuccess: _onLoginSuccess,
|
||||
addingAccount: widget.addingAccount,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
),
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// The new account must live on the server the app
|
||||
// already talks to.
|
||||
if (!widget.addingAccount) const _EndpointLink(),
|
||||
const LoginFooter(),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -126,6 +171,8 @@ class _LoginState extends State<Login> with SingleTickerProviderStateMixin {
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
bool get _busy => _controller.loading;
|
||||
}
|
||||
|
||||
/// Subtle text link above the footer that surfaces the currently selected
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LoginHeader extends StatelessWidget {
|
||||
const LoginHeader({super.key});
|
||||
/// Signs in an additional account instead of the first one.
|
||||
final bool addingAccount;
|
||||
|
||||
const LoginHeader({this.addingAccount = false, super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Column(
|
||||
@@ -29,7 +32,10 @@ class LoginHeader extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
'Stundenplan, Talk, Dateien und mehr - alles an einem Ort für deinen Schulalltag am Marianum Fulda.',
|
||||
addingAccount
|
||||
? 'Melde ein weiteres Konto an. In den Einstellungen kannst du '
|
||||
'danach jederzeit zwischen deinen Konten wechseln.'
|
||||
: 'Stundenplan, Talk, Dateien und mehr - alles an einem Ort für deinen Schulalltag am Marianum Fulda.',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.85),
|
||||
|
||||
@@ -12,9 +12,13 @@ class LoginCard extends StatefulWidget {
|
||||
final LoginController controller;
|
||||
final VoidCallback onSuccess;
|
||||
|
||||
/// Signs in an additional account instead of the first one.
|
||||
final bool addingAccount;
|
||||
|
||||
const LoginCard({
|
||||
required this.controller,
|
||||
required this.onSuccess,
|
||||
this.addingAccount = false,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@@ -83,8 +87,11 @@ class _LoginCardState extends State<LoginCard> {
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: LoginCardFrame(
|
||||
title: 'Anmelden',
|
||||
hint: 'Melde dich mit deinen Marianum-Zugangsdaten an.',
|
||||
title: widget.addingAccount ? 'Konto hinzufügen' : 'Anmelden',
|
||||
hint: widget.addingAccount
|
||||
? 'Melde dich mit den Marianum-Zugangsdaten des Kontos an, das '
|
||||
'du hinzufügen möchtest.'
|
||||
: 'Melde dich mit deinen Marianum-Zugangsdaten an.',
|
||||
children: [
|
||||
TextFormField(
|
||||
key: const Key('login-username-field'),
|
||||
@@ -127,7 +134,7 @@ class _LoginCardState extends State<LoginCard> {
|
||||
const SizedBox(height: 20),
|
||||
LoginSubmitButton(
|
||||
key: const Key('login-submit-button'),
|
||||
label: 'Anmelden',
|
||||
label: widget.addingAccount ? 'Hinzufügen' : 'Anmelden',
|
||||
loading: loading,
|
||||
onPressed: _submit,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user