implemented RMV public transit module including trip search, station departures, and nearby stop lookup, added "Marianum Connect" API integration with bearer token authentication and auto-refresh logic, integrated geolocator for location-based station search, added persistent storage for favorite stations and recent trip queries, and implemented comprehensive UI for journey details, trip results, and disruption alerts
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
|
||||
import '../../../../api/connect/rmv/rmv_models.dart';
|
||||
import '../../../../api/errors/error_mapper.dart';
|
||||
import '../../../../routing/app_routes.dart';
|
||||
import '../../../../state/app/modules/rmv/repository/rmv_repository.dart';
|
||||
import '../../../../widget/app_progress_indicator.dart';
|
||||
import '../../../../widget/centered_leading.dart';
|
||||
|
||||
class NearbyStationsView extends StatefulWidget {
|
||||
const NearbyStationsView({super.key});
|
||||
|
||||
@override
|
||||
State<NearbyStationsView> createState() => _NearbyStationsViewState();
|
||||
}
|
||||
|
||||
class _NearbyStationsViewState extends State<NearbyStationsView> {
|
||||
final RmvRepository _repo = RmvRepository();
|
||||
List<StopLocation>? _stops;
|
||||
bool _loading = true;
|
||||
String? _userError;
|
||||
Object? _apiError;
|
||||
int _radiusMeters = 1000;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_userError = null;
|
||||
_apiError = null;
|
||||
});
|
||||
final position = await _resolvePosition();
|
||||
if (!mounted) return;
|
||||
if (position == null) {
|
||||
// _userError is set by _resolvePosition
|
||||
setState(() => _loading = false);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final stops = await _repo.nearbyStops(
|
||||
lat: position.latitude,
|
||||
lon: position.longitude,
|
||||
radiusMeters: _radiusMeters,
|
||||
max: 30,
|
||||
);
|
||||
if (!mounted) return;
|
||||
stops.sort((a, b) =>
|
||||
(a.distanceMeters ?? 0).compareTo(b.distanceMeters ?? 0));
|
||||
setState(() {
|
||||
_stops = stops;
|
||||
_loading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_apiError = e;
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<Position?> _resolvePosition() async {
|
||||
if (!await Geolocator.isLocationServiceEnabled()) {
|
||||
_userError =
|
||||
'Bitte aktiviere die Standortdienste in den System-Einstellungen.';
|
||||
return null;
|
||||
}
|
||||
var permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
}
|
||||
if (permission == LocationPermission.deniedForever) {
|
||||
_userError =
|
||||
'Standortzugriff dauerhaft verweigert. Bitte in den App-Einstellungen aktivieren.';
|
||||
return null;
|
||||
}
|
||||
if (permission == LocationPermission.denied) {
|
||||
_userError = 'Ohne Standortzugriff können keine Stationen in der Nähe gefunden werden.';
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return await Geolocator.getCurrentPosition(
|
||||
locationSettings: const LocationSettings(
|
||||
accuracy: LocationAccuracy.medium,
|
||||
timeLimit: Duration(seconds: 10),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
_userError = 'Standort konnte nicht ermittelt werden: $e';
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('In meiner Nähe'),
|
||||
actions: [
|
||||
PopupMenuButton<int>(
|
||||
tooltip: 'Suchradius',
|
||||
icon: const Icon(Icons.tune),
|
||||
onSelected: (r) {
|
||||
setState(() => _radiusMeters = r);
|
||||
_load();
|
||||
},
|
||||
itemBuilder: (_) => [500, 1000, 2000, 5000]
|
||||
.map(
|
||||
(r) => CheckedPopupMenuItem<int>(
|
||||
value: r,
|
||||
checked: r == _radiusMeters,
|
||||
child: Text(r >= 1000 ? '${r ~/ 1000} km' : '$r m'),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _body(),
|
||||
);
|
||||
|
||||
Widget _body() {
|
||||
if (_loading) return const Center(child: AppProgressIndicator.large());
|
||||
if (_userError != null) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(_userError!, textAlign: TextAlign.center),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _load,
|
||||
label: const Text('Erneut versuchen'),
|
||||
),
|
||||
OutlinedButton.icon(
|
||||
icon: const Icon(Icons.settings),
|
||||
onPressed: Geolocator.openAppSettings,
|
||||
label: const Text('Einstellungen'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (_apiError != null) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
errorToUserMessage(_apiError),
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
FilledButton.icon(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _load,
|
||||
label: const Text('Erneut versuchen'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
final list = _stops ?? const <StopLocation>[];
|
||||
if (list.isEmpty) {
|
||||
return const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(24),
|
||||
child: Text(
|
||||
'Keine Stationen im gewählten Umkreis gefunden.',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return RefreshIndicator(
|
||||
onRefresh: _load,
|
||||
child: ListView.separated(
|
||||
itemCount: list.length,
|
||||
separatorBuilder: (_, _) => const Divider(height: 1),
|
||||
itemBuilder: (_, i) => _tile(list[i]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _tile(StopLocation stop) => ListTile(
|
||||
leading: const CenteredLeading(Icon(Icons.directions_transit)),
|
||||
title: Text(stop.name),
|
||||
subtitle: stop.distanceMeters == null
|
||||
? null
|
||||
: Text('${stop.distanceMeters} m entfernt'),
|
||||
onTap: () => AppRoutes.openRmvStationDetail(context, stop),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../api/connect/rmv/rmv_models.dart';
|
||||
import '../../../../api/errors/error_mapper.dart';
|
||||
import '../../../../routing/app_routes.dart';
|
||||
import '../../../../state/app/modules/rmv/repository/rmv_repository.dart';
|
||||
import '../../../../state/app/modules/settings/bloc/settings_cubit.dart';
|
||||
import '../../../../widget/app_progress_indicator.dart';
|
||||
import '../favorites_controller.dart';
|
||||
import '../widgets/departure_arrival_tile.dart';
|
||||
|
||||
enum _Direction { departures, arrivals }
|
||||
|
||||
class StationDetailView extends StatefulWidget {
|
||||
final StopLocation station;
|
||||
const StationDetailView({super.key, required this.station});
|
||||
|
||||
@override
|
||||
State<StationDetailView> createState() => _StationDetailViewState();
|
||||
}
|
||||
|
||||
class _StationDetailViewState extends State<StationDetailView> {
|
||||
final RmvRepository _repo = RmvRepository();
|
||||
_Direction _direction = _Direction.departures;
|
||||
List<Departure>? _departures;
|
||||
List<Arrival>? _arrivals;
|
||||
bool _loading = false;
|
||||
Object? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
if (_direction == _Direction.departures) {
|
||||
final result = await _repo.departures(widget.station.id);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_departures = result;
|
||||
_loading = false;
|
||||
});
|
||||
} else {
|
||||
final result = await _repo.arrivals(widget.station.id);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_arrivals = result;
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_error = e;
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _switch(_Direction d) {
|
||||
if (d == _direction) return;
|
||||
setState(() {
|
||||
_direction = d;
|
||||
_departures = null;
|
||||
_arrivals = null;
|
||||
});
|
||||
_load();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final settings = context.watch<SettingsCubit>();
|
||||
final favCtrl = RmvFavoritesController(settings);
|
||||
final isFav = favCtrl.isFavorite(widget.station);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.station.name),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: isFav ? 'Favorit entfernen' : 'Als Favorit speichern',
|
||||
icon: Icon(isFav ? Icons.star : Icons.star_border),
|
||||
onPressed: () => favCtrl.toggleFavorite(widget.station),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
|
||||
child: SegmentedButton<_Direction>(
|
||||
segments: const [
|
||||
ButtonSegment(
|
||||
value: _Direction.departures,
|
||||
icon: Icon(Icons.north_east),
|
||||
label: Text('Abfahrten'),
|
||||
),
|
||||
ButtonSegment(
|
||||
value: _Direction.arrivals,
|
||||
icon: Icon(Icons.south_west),
|
||||
label: Text('Ankünfte'),
|
||||
),
|
||||
],
|
||||
selected: {_direction},
|
||||
onSelectionChanged: (s) => _switch(s.first),
|
||||
),
|
||||
),
|
||||
Expanded(child: _body()),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body() {
|
||||
if (_loading) {
|
||||
return const Center(child: AppProgressIndicator.large());
|
||||
}
|
||||
final err = _error;
|
||||
if (err != null) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
errorToUserMessage(err),
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
FilledButton.icon(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _load,
|
||||
label: const Text('Erneut versuchen'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (_direction == _Direction.departures) {
|
||||
final list = _departures ?? const <Departure>[];
|
||||
if (list.isEmpty) return _emptyState('Keine Abfahrten gefunden.');
|
||||
return RefreshIndicator(
|
||||
onRefresh: _load,
|
||||
child: ListView.separated(
|
||||
itemCount: list.length,
|
||||
separatorBuilder: (_, _) => const Divider(height: 1),
|
||||
itemBuilder: (_, i) => DepartureArrivalTile.fromDeparture(
|
||||
list[i],
|
||||
onTap: () => _openJourney(list[i].journeyRef),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
final list = _arrivals ?? const <Arrival>[];
|
||||
if (list.isEmpty) return _emptyState('Keine Ankünfte gefunden.');
|
||||
return RefreshIndicator(
|
||||
onRefresh: _load,
|
||||
child: ListView.separated(
|
||||
itemCount: list.length,
|
||||
separatorBuilder: (_, _) => const Divider(height: 1),
|
||||
itemBuilder: (_, i) => DepartureArrivalTile.fromArrival(
|
||||
list[i],
|
||||
onTap: () => _openJourney(list[i].journeyRef),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _emptyState(String text) => RefreshIndicator(
|
||||
onRefresh: _load,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 80, horizontal: 24),
|
||||
child: Center(
|
||||
child: Text(text, style: Theme.of(context).textTheme.bodyLarge),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
void _openJourney(String? ref) {
|
||||
if (ref == null) return;
|
||||
AppRoutes.openRmvJourneyDetail(context, ref);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../api/connect/rmv/rmv_models.dart';
|
||||
import '../../../../routing/app_routes.dart';
|
||||
import '../../../../state/app/modules/settings/bloc/settings_cubit.dart';
|
||||
import '../../../../storage/settings.dart';
|
||||
import '../../../../widget/centered_leading.dart';
|
||||
import '../../../../widget/confirm_dialog.dart';
|
||||
import '../favorites_controller.dart';
|
||||
import '../widgets/station_picker_sheet.dart';
|
||||
import 'nearby_stations_view.dart';
|
||||
|
||||
class StationOverviewTab extends StatelessWidget {
|
||||
const StationOverviewTab({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
BlocBuilder<SettingsCubit, Settings>(builder: _buildBody);
|
||||
|
||||
Widget _buildBody(BuildContext context, Settings settings) {
|
||||
final rmv = settings.rmvSettings;
|
||||
final favorites = rmv.favoriteStations;
|
||||
final recents = rmv.recentStations;
|
||||
final favCtrl = RmvFavoritesController(context.read<SettingsCubit>());
|
||||
|
||||
final children = <Widget>[
|
||||
_searchBar(context),
|
||||
_nearbyButton(context),
|
||||
if (favorites.isEmpty && recents.isEmpty) _emptyState(context),
|
||||
if (favorites.isNotEmpty) ...[
|
||||
_sectionHeader(context, 'Favoriten', null),
|
||||
...favorites.map((s) => _stationTile(context, s, favCtrl, isFavorite: true)),
|
||||
],
|
||||
if (recents.isNotEmpty) ...[
|
||||
_sectionHeader(
|
||||
context,
|
||||
'Zuletzt verwendet',
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_sweep_outlined),
|
||||
tooltip: 'Alle löschen',
|
||||
onPressed: () => _confirmClearRecents(context, favCtrl),
|
||||
),
|
||||
),
|
||||
...recents.map((s) => _stationTile(context, s, favCtrl, isFavorite: false)),
|
||||
],
|
||||
];
|
||||
|
||||
return ListView(children: children);
|
||||
}
|
||||
|
||||
Widget _searchBar(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
|
||||
child: FilledButton.tonalIcon(
|
||||
icon: const Icon(Icons.search),
|
||||
label: const Text('Station suchen…'),
|
||||
onPressed: () async {
|
||||
final picked = await showStationPickerSheet(context);
|
||||
if (picked != null && context.mounted) {
|
||||
AppRoutes.openRmvStationDetail(context, picked);
|
||||
}
|
||||
},
|
||||
style: FilledButton.styleFrom(
|
||||
minimumSize: const Size.fromHeight(48),
|
||||
alignment: Alignment.centerLeft,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Widget _nearbyButton(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: OutlinedButton.icon(
|
||||
icon: const Icon(Icons.my_location),
|
||||
label: const Text('In meiner Nähe'),
|
||||
onPressed: () => Navigator.of(context).push<void>(
|
||||
MaterialPageRoute(builder: (_) => const NearbyStationsView()),
|
||||
),
|
||||
style: OutlinedButton.styleFrom(
|
||||
minimumSize: const Size.fromHeight(40),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Widget _sectionHeader(BuildContext context, String title, Widget? trailing) =>
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 16, 8, 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.titleSmall?.copyWith(
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
),
|
||||
),
|
||||
?trailing,
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Widget _emptyState(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.fromLTRB(24, 40, 24, 16),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Noch keine Stationen gespeichert. Suche eine Station, um sie zu öffnen oder als Favorit zu markieren.',
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Widget _stationTile(
|
||||
BuildContext context,
|
||||
StopLocation station,
|
||||
RmvFavoritesController favCtrl, {
|
||||
required bool isFavorite,
|
||||
}) => ListTile(
|
||||
leading: CenteredLeading(
|
||||
Icon(isFavorite ? Icons.star : Icons.directions_transit),
|
||||
),
|
||||
title: Text(station.name),
|
||||
subtitle: station.description == null ? null : Text(station.description!),
|
||||
trailing: IconButton(
|
||||
icon: Icon(
|
||||
favCtrl.isFavorite(station) ? Icons.star : Icons.star_border,
|
||||
),
|
||||
tooltip: favCtrl.isFavorite(station)
|
||||
? 'Favorit entfernen'
|
||||
: 'Als Favorit speichern',
|
||||
onPressed: () => favCtrl.toggleFavorite(station),
|
||||
),
|
||||
onTap: () => AppRoutes.openRmvStationDetail(context, station),
|
||||
);
|
||||
|
||||
Future<void> _confirmClearRecents(
|
||||
BuildContext context,
|
||||
RmvFavoritesController favCtrl,
|
||||
) async {
|
||||
ConfirmDialog(
|
||||
title: 'Verlauf leeren?',
|
||||
content:
|
||||
'Die zuletzt verwendeten Stationen werden aus der Übersicht entfernt. Favoriten bleiben bestehen.',
|
||||
confirmButton: 'Leeren',
|
||||
onConfirm: () => favCtrl.clearRecents(),
|
||||
).asDialog(context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user