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