feat(umgebung): Server-Umgebung im Login-Screen waehlbar (Produktion/Test)
- Umgebungen (Name, API-URL, Keycloak-Issuer) kommen aus assets/hl_server_config.json statt hartcodiertem BackendConfig.prod; Testumgebung 192.168.1.9:3001 ist dort hinterlegt - AppBloc laedt die Config, aktiviert die gespeicherte Wahl (Fallback Default) und registriert das Networking; Wechsel persistiert per SharedPreferences und registriert neu - Bloc-Baum ist pro Umgebung gekeyt: beim Wechsel entstehen alle Blocs mit dem neuen API-Client neu - Login-Screen: Zahnrad oben rechts oeffnet Einstellungen mit Umgebungs- Cards (Name + IP:Port); Hinweis auf aktive Umgebung unter dem Button - Fahrzeugauswahl pro Umgebung getrennt (eigene DB = eigene Car-IDs) - Tests fuer Config-Parsing, Auswahl-Fallback, Card-Wechsel, Re-Registrierung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'package:hl_lieferservice/bloc/app_bloc.dart';
|
||||
import 'package:hl_lieferservice/bloc/app_states.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_event.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_state.dart';
|
||||
import 'package:hl_lieferservice/feature/settings/presentation/environment_settings_page.dart';
|
||||
|
||||
/// Login-Page nach der Migration auf Keycloak OIDC (Phase B).
|
||||
///
|
||||
@ -28,7 +31,25 @@ class LoginPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
appBar: AppBar(
|
||||
actions: [
|
||||
// Während eines laufenden Logins gesperrt: Ein Umgebungswechsel
|
||||
// würde den Token-Provider mitten im Browser-Flow austauschen.
|
||||
BlocBuilder<AuthBloc, AuthState>(
|
||||
builder: (context, state) => IconButton(
|
||||
tooltip: 'Einstellungen',
|
||||
icon: const Icon(Icons.settings),
|
||||
onPressed: state is Authenticating
|
||||
? null
|
||||
: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => const EnvironmentSettingsPage(),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
if (sessionExpired)
|
||||
@ -111,6 +132,7 @@ class LoginPage extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
const _ActiveEnvironmentHint(),
|
||||
],
|
||||
),
|
||||
),
|
||||
@ -120,3 +142,23 @@ class LoginPage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Zeigt, gegen welchen Server die Anmeldung läuft — damit niemand
|
||||
/// versehentlich in der Testumgebung ausliefert.
|
||||
class _ActiveEnvironmentHint extends StatelessWidget {
|
||||
const _ActiveEnvironmentHint();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final state = context.watch<AppBloc>().state;
|
||||
if (state is! AppConfigLoaded) return const SizedBox.shrink();
|
||||
final env = state.active;
|
||||
return Text(
|
||||
'Umgebung: ${env.name} (${env.hostAndPort})',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,11 +6,22 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
/// (Strings). Alte Pre-Phase-D-Installations können noch int-Werte
|
||||
/// unter `_car_id` liegen haben — die werden beim Lesen
|
||||
/// stillschweigend ignoriert (Migration durch "neu auswählen").
|
||||
///
|
||||
/// Die Keys sind pro Server-Umgebung ([environmentId]) getrennt: Jede
|
||||
/// Umgebung hat ihre eigene Datenbank und damit eigene Fahrzeug-IDs.
|
||||
/// Eine Auswahl aus der Produktion darf in der Testumgebung nicht
|
||||
/// wiederhergestellt werden (und umgekehrt).
|
||||
class CarSelectionRepository {
|
||||
static String _keyDate(String userId) => 'car_selection_${userId}_date';
|
||||
static String _keyCarId(String userId) => 'car_selection_${userId}_car_id';
|
||||
static String _keyCarPlate(String userId) =>
|
||||
'car_selection_${userId}_car_plate';
|
||||
CarSelectionRepository({required this.environmentId});
|
||||
|
||||
final String environmentId;
|
||||
|
||||
String _keyDate(String userId) =>
|
||||
'car_selection_${environmentId}_${userId}_date';
|
||||
String _keyCarId(String userId) =>
|
||||
'car_selection_${environmentId}_${userId}_car_id';
|
||||
String _keyCarPlate(String userId) =>
|
||||
'car_selection_${environmentId}_${userId}_car_plate';
|
||||
|
||||
Future<CarSelection?> getSelection(String userId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
148
lib/feature/settings/presentation/environment_settings_page.dart
Normal file
148
lib/feature/settings/presentation/environment_settings_page.dart
Normal file
@ -0,0 +1,148 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'package:hl_lieferservice/bloc/app_bloc.dart';
|
||||
import 'package:hl_lieferservice/bloc/app_events.dart';
|
||||
import 'package:hl_lieferservice/bloc/app_states.dart';
|
||||
import 'package:hl_lieferservice/data/network/backend_environment.dart';
|
||||
|
||||
/// Einstellungen vor dem Login: Auswahl der Server-Umgebung.
|
||||
///
|
||||
/// Erreichbar über das Zahnrad im Login-Screen. Die Umgebungen stammen
|
||||
/// aus `assets/hl_server_config.json`; die Wahl persistiert der
|
||||
/// [AppBloc]. Nach dem Antippen einer anderen Umgebung schließt sich die
|
||||
/// Seite und die App baut sich gegen den neuen Server neu auf.
|
||||
class EnvironmentSettingsPage extends StatelessWidget {
|
||||
const EnvironmentSettingsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Einstellungen')),
|
||||
body: BlocBuilder<AppBloc, AppState>(
|
||||
builder: (context, state) {
|
||||
if (state is! AppConfigLoaded) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
final catalog = state.catalog;
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
Text('Umgebung', style: Theme.of(context).textTheme.titleMedium),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Mit welchem Server sich die App verbindet.',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
for (final env in catalog.environments)
|
||||
_EnvironmentCard(
|
||||
environment: env,
|
||||
selected: env.id == state.active.id,
|
||||
isDefault: env.id == catalog.defaultEnvironmentId,
|
||||
onTap: () => _select(context, state.active, env),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _select(
|
||||
BuildContext context,
|
||||
BackendEnvironment active,
|
||||
BackendEnvironment picked,
|
||||
) {
|
||||
final appBloc = context.read<AppBloc>();
|
||||
Navigator.of(context).pop();
|
||||
if (picked.id != active.id) {
|
||||
appBloc.add(AppSwitchEnvironment(environmentId: picked.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _EnvironmentCard extends StatelessWidget {
|
||||
const _EnvironmentCard({
|
||||
required this.environment,
|
||||
required this.selected,
|
||||
required this.isDefault,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
final BackendEnvironment environment;
|
||||
final bool selected;
|
||||
final bool isDefault;
|
||||
final VoidCallback onTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
elevation: selected ? 2 : 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: selected ? scheme.primary : scheme.outlineVariant,
|
||||
width: selected ? 2 : 1,
|
||||
),
|
||||
),
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.dns_outlined,
|
||||
color: selected ? scheme.primary : scheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
environment.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (isDefault) ...[
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'Standard',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: scheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
environment.hostAndPort,
|
||||
style: TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
color: scheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (selected) Icon(Icons.check_circle, color: scheme.primary),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import 'package:hl_lieferservice/data/network/backend_config.dart';
|
||||
import 'package:hl_lieferservice/main.dart' show locator;
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_event.dart';
|
||||
import 'package:hl_lieferservice/feature/settings/bloc/settings_bloc.dart';
|
||||
@ -64,7 +65,7 @@ class _SettingsPage extends State<SettingsPage> {
|
||||
/// Externer Browser statt In-App-WebView, damit ggf. im Browser
|
||||
/// gespeicherte Credentials / Authenticator-Apps weiter funktionieren.
|
||||
Future<void> _openAccountConsole() async {
|
||||
final issuer = BackendConfig.fromEnvironment.keycloakIssuerUrl;
|
||||
final issuer = locator<BackendConfig>().keycloakIssuerUrl;
|
||||
final uri = Uri.parse('$issuer/account/');
|
||||
final ok = await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
if (!ok && mounted) {
|
||||
|
||||
Reference in New Issue
Block a user