92 lines
3.4 KiB
Dart
92 lines
3.4 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../features/auth/cubit/auth_cubit.dart';
|
|
import '../features/auth/cubit/auth_state.dart';
|
|
import '../features/auth/view/phone_screen.dart';
|
|
import '../features/auth/view/otp_screen.dart';
|
|
import '../features/home/view/home_screen.dart';
|
|
import '../features/home/view/destination_screen.dart';
|
|
import '../features/home/view/route_preview_screen.dart';
|
|
import '../features/trip/view/trip_tracking_screen.dart';
|
|
import '../features/trip/view/trip_invoice_screen.dart';
|
|
import '../features/trip/view/rating_screen.dart';
|
|
import '../features/home/cubit/home_cubit.dart';
|
|
import '../features/home/cubit/destination_cubit.dart';
|
|
import '../features/home/cubit/quote_cubit.dart';
|
|
import '../features/home/data/models/place_model.dart';
|
|
import '../features/home/data/maps_repository.dart';
|
|
import '../features/home/data/tariff_repository.dart';
|
|
import 'di.dart';
|
|
|
|
GoRouter buildRouter(AuthCubit auth) {
|
|
return GoRouter(
|
|
initialLocation: '/phone',
|
|
refreshListenable: _StreamRefresh(auth.stream),
|
|
redirect: (context, state) {
|
|
final status = auth.state.status;
|
|
final loc = state.matchedLocation;
|
|
final onAuthPages = loc == '/phone' || loc == '/otp';
|
|
|
|
if (status == AuthStatus.authenticated) {
|
|
return onAuthPages ? '/home' : null;
|
|
}
|
|
if (status == AuthStatus.codeSent && loc == '/phone') return '/otp';
|
|
if (status != AuthStatus.authenticated && !onAuthPages) return '/phone';
|
|
return null;
|
|
},
|
|
routes: [
|
|
GoRoute(path: '/phone', builder: (_, __) => const PhoneScreen()),
|
|
GoRoute(path: '/otp', builder: (_, __) => const OtpScreen()),
|
|
GoRoute(
|
|
path: '/home',
|
|
builder: (_, __) => MultiBlocProvider(
|
|
providers: [
|
|
BlocProvider(create: (_) => HomeCubit(getIt<MapsRepository>())),
|
|
],
|
|
child: const HomeScreen(),
|
|
),
|
|
),
|
|
GoRoute(
|
|
path: '/destination',
|
|
builder: (_, __) => BlocProvider(
|
|
create: (_) => DestinationCubit(getIt<MapsRepository>()),
|
|
child: const DestinationScreen(),
|
|
),
|
|
),
|
|
GoRoute(
|
|
path: '/route-preview',
|
|
builder: (_, state) {
|
|
final extra = state.extra as Map<String, dynamic>?;
|
|
final pickup = extra?['pickup'] as PlaceModel?;
|
|
final destination = extra?['destination'] as PlaceModel?;
|
|
if (pickup == null || destination == null) {
|
|
return const Scaffold(body: Center(child: Text('Missing data')));
|
|
}
|
|
return BlocProvider(
|
|
create: (_) => QuoteCubit(getIt<TariffRepository>()),
|
|
child: RoutePreviewScreen(pickup: pickup, destination: destination),
|
|
);
|
|
},
|
|
),
|
|
GoRoute(path: '/trip-tracking', builder: (_, __) => const TripTrackingScreen()),
|
|
GoRoute(path: '/trip-invoice', builder: (_, __) => const TripInvoiceScreen()),
|
|
GoRoute(path: '/trip-rating', builder: (_, __) => const RatingScreen()),
|
|
],
|
|
);
|
|
}
|
|
|
|
class _StreamRefresh extends ChangeNotifier {
|
|
late final StreamSubscription<dynamic> _sub;
|
|
_StreamRefresh(Stream<dynamic> stream) {
|
|
notifyListeners();
|
|
_sub = stream.asBroadcastStream().listen((_) => notifyListeners());
|
|
}
|
|
@override
|
|
void dispose() {
|
|
_sub.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|