65 lines
2.1 KiB
Dart
65 lines
2.1 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.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/trip/bloc/driver_trip_bloc.dart';
|
|
import '../features/trip/view/driver_trip_screen.dart';
|
|
import '../features/trip/view/driver_chat_screen.dart';
|
|
import '../core/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: (_, __) => const HomeScreen()),
|
|
GoRoute(
|
|
path: '/trip',
|
|
builder: (_, __) => BlocProvider.value(
|
|
value: getIt<DriverTripBloc>(),
|
|
child: const DriverTripScreen(),
|
|
),
|
|
),
|
|
GoRoute(
|
|
path: '/chat/:tripId',
|
|
builder: (context, state) {
|
|
final tripId = state.pathParameters['tripId']!;
|
|
return DriverChatScreen(tripId: tripId);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|