fix: server page empty by default, https prefix non-editable, StateProvider for instant redirect

This commit is contained in:
Bernd Steckmeister
2026-04-19 20:22:30 +02:00
parent 0f6dda7bb9
commit 85cf7cdf13
5 changed files with 50 additions and 53 deletions
+1 -2
View File
@@ -10,8 +10,7 @@ import 'package:pyramid/layout/shell_page.dart';
final routerProvider = Provider<GoRouter>((ref) { final routerProvider = Provider<GoRouter>((ref) {
final isLoggedIn = ref.watch(isLoggedInProvider); final isLoggedIn = ref.watch(isLoggedInProvider);
final homeserverAsync = ref.watch(homeserverProvider); final hasServer = ref.watch(homeserverProvider) != null;
final hasServer = homeserverAsync.valueOrNull != null;
return GoRouter( return GoRouter(
initialLocation: '/rooms', initialLocation: '/rooms',
+9 -6
View File
@@ -5,15 +5,18 @@ import 'package:shared_preferences/shared_preferences.dart';
const _prefHomeserver = 'homeserver_url'; const _prefHomeserver = 'homeserver_url';
// Gespeicherter Homeserver aus SharedPreferences final homeserverProvider = StateProvider<String?>((ref) => null);
final homeserverProvider = FutureProvider<String?>((ref) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_prefHomeserver);
});
Future<void> saveHomeserver(String url) async { Future<void> initHomeserver(WidgetRef ref) async {
final prefs = await SharedPreferences.getInstance();
final saved = prefs.getString(_prefHomeserver);
ref.read(homeserverProvider.notifier).state = saved;
}
Future<void> saveHomeserver(WidgetRef ref, String url) async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
await prefs.setString(_prefHomeserver, url); await prefs.setString(_prefHomeserver, url);
ref.read(homeserverProvider.notifier).state = url;
} }
enum LoginStatus { idle, loading, error } enum LoginStatus { idle, loading, error }
+2 -2
View File
@@ -26,7 +26,7 @@ class _LoginPageState extends ConsumerState<LoginPage> {
Future<void> _submit() async { Future<void> _submit() async {
if (!_formKey.currentState!.validate()) return; if (!_formKey.currentState!.validate()) return;
final homeserver = ref.read(homeserverProvider).valueOrNull ?? 'matrix.org'; final homeserver = ref.read(homeserverProvider) ?? 'matrix.org';
await ref.read(loginNotifierProvider.notifier).login( await ref.read(loginNotifierProvider.notifier).login(
username: _userCtrl.text.trim(), username: _userCtrl.text.trim(),
@@ -45,7 +45,7 @@ class _LoginPageState extends ConsumerState<LoginPage> {
final theme = Theme.of(context); final theme = Theme.of(context);
final loginState = ref.watch(loginNotifierProvider); final loginState = ref.watch(loginNotifierProvider);
final loading = loginState.status == LoginStatus.loading; final loading = loginState.status == LoginStatus.loading;
final homeserver = ref.watch(homeserverProvider).valueOrNull ?? 'matrix.org'; final homeserver = ref.watch(homeserverProvider) ?? 'matrix.org';
return Scaffold( return Scaffold(
body: Center( body: Center(
+30 -36
View File
@@ -12,18 +12,18 @@ class ServerPage extends ConsumerStatefulWidget {
class _ServerPageState extends ConsumerState<ServerPage> { class _ServerPageState extends ConsumerState<ServerPage> {
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
final _ctrl = TextEditingController(text: 'matrix.org'); final _ctrl = TextEditingController();
bool _loading = false; bool _loading = false;
String? _error; String? _error;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
ref.read(homeserverProvider.future).then((saved) { // Gespeicherten Server vorausfüllen (falls vorhanden)
if (saved != null && mounted) { final saved = ref.read(homeserverProvider);
_ctrl.text = saved.replaceAll(RegExp(r'^https?://'), ''); if (saved != null) {
} _ctrl.text = saved.replaceAll(RegExp(r'^https?://'), '');
}); }
} }
@override @override
@@ -38,7 +38,7 @@ class _ServerPageState extends ConsumerState<ServerPage> {
final input = _ctrl.text.trim().replaceAll(RegExp(r'^https?://'), ''); final input = _ctrl.text.trim().replaceAll(RegExp(r'^https?://'), '');
try { try {
await saveHomeserver(input); await saveHomeserver(ref, input);
if (mounted) context.go('/login'); if (mounted) context.go('/login');
} catch (e) { } catch (e) {
setState(() => _error = e.toString()); setState(() => _error = e.toString());
@@ -63,11 +63,8 @@ class _ServerPageState extends ConsumerState<ServerPage> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch, crossAxisAlignment: CrossAxisAlignment.stretch,
children: [ children: [
Icon( Icon(Icons.change_history_rounded,
Icons.change_history_rounded, size: 72, color: theme.colorScheme.primary),
size: 72,
color: theme.colorScheme.primary,
),
const SizedBox(height: 8), const SizedBox(height: 8),
Text( Text(
'Pyramid', 'Pyramid',
@@ -76,45 +73,42 @@ class _ServerPageState extends ConsumerState<ServerPage> {
?.copyWith(fontWeight: FontWeight.bold), ?.copyWith(fontWeight: FontWeight.bold),
), ),
const SizedBox(height: 32), const SizedBox(height: 32),
Text( Text('Server', style: theme.textTheme.titleMedium),
'Server',
style: theme.textTheme.titleMedium,
),
const SizedBox(height: 8), const SizedBox(height: 8),
TextFormField( TextFormField(
controller: _ctrl, controller: _ctrl,
decoration: const InputDecoration( autofocus: true,
hintText: 'matrix.org', decoration: InputDecoration(
prefixText: 'https://', hintText: 'dein-server.de',
prefixIcon: Icon(Icons.dns_outlined), prefixIcon: const Icon(Icons.dns_outlined),
border: OutlineInputBorder(), border: const OutlineInputBorder(),
// https:// als nicht-editierbares Prefix
prefix: Text(
'https://',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
), ),
keyboardType: TextInputType.url, keyboardType: TextInputType.url,
validator: (v) { validator: (v) => (v == null || v.trim().isEmpty)
if (v == null || v.trim().isEmpty) { ? 'Server-Adresse eingeben'
return 'Server-Adresse eingeben'; : null,
}
return null;
},
onFieldSubmitted: (_) => _continue(), onFieldSubmitted: (_) => _continue(),
), ),
if (_error != null) ...[ if (_error != null) ...[
const SizedBox(height: 12), const SizedBox(height: 12),
Text( Text(_error!,
_error!, style: TextStyle(color: theme.colorScheme.error),
style: TextStyle(color: theme.colorScheme.error), textAlign: TextAlign.center),
textAlign: TextAlign.center,
),
], ],
const SizedBox(height: 24), const SizedBox(height: 24),
FilledButton( FilledButton(
onPressed: _loading ? null : _continue, onPressed: _loading ? null : _continue,
child: _loading child: _loading
? const SizedBox( ? const SizedBox(
height: 20, height: 20, width: 20,
width: 20, child: CircularProgressIndicator(strokeWidth: 2))
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Weiter'), : const Text('Weiter'),
), ),
], ],
+8 -7
View File
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pyramid/core/app.dart'; import 'package:pyramid/core/app.dart';
import 'package:pyramid/core/matrix_client.dart'; import 'package:pyramid/core/matrix_client.dart';
import 'package:pyramid/features/auth/login_notifier.dart';
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
@@ -17,16 +18,16 @@ class PyramidBootstrap extends ConsumerWidget {
return clientAsync.when( return clientAsync.when(
loading: () => const MaterialApp( loading: () => const MaterialApp(
home: Scaffold( home: Scaffold(body: Center(child: CircularProgressIndicator())),
body: Center(child: CircularProgressIndicator()),
),
), ),
error: (e, _) => MaterialApp( error: (e, _) => MaterialApp(
home: Scaffold( home: Scaffold(body: Center(child: Text('Fehler: $e'))),
body: Center(child: Text('Fehler: $e')),
),
), ),
data: (_) => const PyramidApp(), data: (_) {
// Homeserver einmalig aus SharedPreferences laden
initHomeserver(ref);
return const PyramidApp();
},
); );
} }
} }