ionic-planb-logistic-app-fl.../lib/providers/providers.dart
Claude Code 4b03e9aba5 Initial commit: Plan B Logistics Flutter app with dark mode and responsive design
Implements complete refactor of Ionic Angular logistics app to Flutter/Dart with:
- Svrnty dark mode console theme (Material Design 3)
- Responsive layouts (mobile, tablet, desktop) following FRONTEND standards
- CQRS API integration with Result<T> error handling
- OAuth2/OIDC authentication support (mocked for initial testing)
- Delivery route and delivery management features
- Multi-language support (EN/FR) with i18n
- Native integrations (camera, phone calls, maps)
- Strict typing throughout codebase
- Mock data for UI testing without backend

Follows all FRONTEND style guides, design patterns, and conventions.
App is running in dark mode and fully responsive across all device sizes.

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-31 04:58:10 -04:00

205 lines
6.2 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../api/types.dart';
import '../api/client.dart';
import '../api/openapi_config.dart';
import '../services/auth_service.dart';
import '../models/user_profile.dart';
import '../models/delivery_route.dart';
import '../models/delivery.dart';
import '../models/delivery_order.dart';
import '../models/delivery_address.dart';
import '../models/delivery_contact.dart';
final authServiceProvider = Provider<AuthService>((ref) {
return AuthService();
});
final apiClientProvider = Provider<CqrsApiClient>((ref) {
return CqrsApiClient(config: ApiClientConfig.production);
});
final isAuthenticatedProvider = FutureProvider<bool>((ref) async {
final authService = ref.watch(authServiceProvider);
return await authService.isAuthenticated();
});
final userProfileProvider = FutureProvider<UserProfile?>((ref) async {
final authService = ref.watch(authServiceProvider);
final token = await authService.getToken();
if (token == null) return null;
return authService.decodeToken(token);
});
final authTokenProvider = FutureProvider<String?>((ref) async {
final authService = ref.watch(authServiceProvider);
return await authService.getToken();
});
final deliveryRoutesProvider = FutureProvider<List<DeliveryRoute>>((ref) async {
// ignore: unused_local_variable
final client = ref.watch(apiClientProvider);
final token = ref.watch(authTokenProvider).valueOrNull;
// TODO: Remove mock data when Keycloak is configured
if (token == null) {
return [
DeliveryRoute(
id: 1,
name: 'Route A - Downtown',
routeFragmentId: 1,
totalDeliveries: 12,
completedDeliveries: 5,
skippedDeliveries: 0,
createdAt: DateTime.now().subtract(const Duration(days: 1)).toIso8601String(),
),
DeliveryRoute(
id: 2,
name: 'Route B - Suburbs',
routeFragmentId: 2,
totalDeliveries: 8,
completedDeliveries: 8,
skippedDeliveries: 0,
createdAt: DateTime.now().subtract(const Duration(days: 2)).toIso8601String(),
),
DeliveryRoute(
id: 3,
name: 'Route C - Industrial Zone',
routeFragmentId: 3,
totalDeliveries: 15,
completedDeliveries: 3,
skippedDeliveries: 2,
createdAt: DateTime.now().subtract(const Duration(days: 3)).toIso8601String(),
),
];
}
// Create a new client with auth token
final authClient = CqrsApiClient(
config: ApiClientConfig(
baseUrl: ApiClientConfig.production.baseUrl,
defaultHeaders: {'Authorization': 'Bearer $token'},
),
);
final result = await authClient.executeQuery<List<DeliveryRoute>>(
endpoint: 'simpleDeliveryRouteQueryItems',
query: _EmptyQuery(),
fromJson: (json) {
final routes = json['items'] as List?;
return routes?.map((r) => DeliveryRoute.fromJson(r as Map<String, dynamic>)).toList() ?? [];
},
);
return result.whenSuccess((routes) => routes) ?? [];
});
final deliveriesProvider = FutureProvider.family<List<Delivery>, int>((ref, routeFragmentId) async {
// ignore: unused_local_variable
final client = ref.watch(apiClientProvider);
final token = ref.watch(authTokenProvider).valueOrNull;
// TODO: Remove mock data when Keycloak is configured
if (token == null) {
return _getMockDeliveries(routeFragmentId);
}
final authClient = CqrsApiClient(
config: ApiClientConfig(
baseUrl: ApiClientConfig.production.baseUrl,
defaultHeaders: {'Authorization': 'Bearer $token'},
),
);
final result = await authClient.executeQuery<List<Delivery>>(
endpoint: 'simpleDeliveriesQueryItems',
query: _DeliveriesQuery(routeFragmentId: routeFragmentId),
fromJson: (json) {
final items = json['items'] as List?;
return items?.map((d) => Delivery.fromJson(d as Map<String, dynamic>)).toList() ?? [];
},
);
return result.whenSuccess((deliveries) => deliveries) ?? [];
});
final languageProvider = StateProvider<String>((ref) {
return 'fr';
});
// Mock data generator for testing without authentication
List<Delivery> _getMockDeliveries(int routeFragmentId) {
final mockDeliveries = <Delivery>[];
for (int i = 1; i <= 6; i++) {
final isDelivered = i <= 2;
mockDeliveries.add(
Delivery(
id: i,
routeFragmentId: routeFragmentId,
deliveryIndex: i,
orders: [
DeliveryOrder(
id: i * 100,
isNewCustomer: i == 3,
totalAmount: 150.0 + (i * 10),
totalItems: 3 + i,
contacts: [
DeliveryContact(
firstName: 'Client',
lastName: 'Name$i',
fullName: 'Client Name $i',
phoneNumber: '+212${i}23456789',
),
],
contact: DeliveryContact(
firstName: 'Client',
lastName: 'Name$i',
fullName: 'Client Name $i',
phoneNumber: '+212${i}23456789',
),
),
],
deliveryAddress: DeliveryAddress(
id: i,
line1: 'Street $i',
line2: 'Building ${i * 10}',
postalCode: '3000${i.toString().padLeft(2, '0')}',
city: 'Casablanca',
subdivision: 'Casablanca-Settat',
countryCode: 'MA',
latitude: 33.5731 + (i * 0.01),
longitude: -7.5898 + (i * 0.01),
formattedAddress: 'Street $i, Building ${i * 10}, Casablanca, Morocco',
),
delivered: isDelivered,
isSkipped: false,
hasBeenSkipped: false,
deliveredAt: isDelivered ? DateTime.now().subtract(Duration(hours: i)).toIso8601String() : null,
name: 'Delivery #${routeFragmentId}-$i',
createdAt: DateTime.now().subtract(Duration(days: 1)).toIso8601String(),
updatedAt: DateTime.now().toIso8601String(),
),
);
}
return mockDeliveries;
}
class _EmptyQuery implements Serializable {
@override
Map<String, Object?> toJson() => {};
}
class _DeliveriesQuery implements Serializable {
final int routeFragmentId;
_DeliveriesQuery({required this.routeFragmentId});
@override
Map<String, Object?> toJson() => {
'params': {
'routeFragmentId': routeFragmentId,
},
};
}