Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
check_in_service.dart
Go to the documentation of this file.
1import 'dart:developer';
2
3import '../api/api_client.dart';
4import '../api/api_endpoints.dart';
5import '../common/models/check_in_status_model.dart';
6import 'service_locator.dart';
7import 'package:shared_preferences/shared_preferences.dart';
8import 'dart:convert';
9import 'package:geolocator/geolocator.dart';
10
13 static const String _checkInStatusKey = 'check_in_status';
14
16
17 Future<CheckInStatusModel?> getCheckInStatus({required int userId}) async {
18 try {
19 final url = ApiEndpoints.getCheckInStatus(userId: userId);
20
21 final response = await _apiClient.get(path: url);
22
23 if (response is Map && response.isEmpty) {
24 return null;
25 }
26
27 return CheckInStatusModel.fromJson(response);
28 } catch (e) {
29 return null;
30 }
31 }
32
33 // Save check-in status locally and send location to server
34 Future<bool> saveCheckInStatus({
35 required int vehicleId,
36 required String immatriculation,
37 required int driverId,
38 }) async {
39 try {
40 // Get current location
41 final position = await _getCurrentLocation();
42 if (position == null) {
43 return false;
44 }
45
46 // Send location to server with 'start' status
47 final locationSent = await _sendLocationToServer(
49 latitude: position.latitude,
50 longitude: position.longitude,
51 poId: null,
52 poStatus: 'start',
53 );
54
55 if (!locationSent) {
56 return false;
57 }
58
59 // Save check-in status locally only if server call succeeded
60 final prefs = await SharedPreferences.getInstance();
61 final checkInStatus = CheckInStatusModel(
63 vehicleId: vehicleId,
64 immatriculation: immatriculation,
65 );
66
67 await prefs.setString(
68 _checkInStatusKey, jsonEncode(checkInStatus.toJson()));
69 return true;
70 } catch (e) {
71 log('Error saving check-in status: $e');
72 return false;
73 }
74 }
75
76 Future<Position?> _getCurrentLocation() async {
77 try {
78 bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
79 if (!serviceEnabled) {
80 return null;
81 }
82
83 LocationPermission permission = await Geolocator.checkPermission();
84 if (permission == LocationPermission.denied) {
85 permission = await Geolocator.requestPermission();
86 if (permission == LocationPermission.denied) {
87 return null;
88 }
89 }
90
91 if (permission == LocationPermission.deniedForever) {
92 return null;
93 }
94
95 return await Geolocator.getCurrentPosition(
96 locationSettings: const LocationSettings(
97 accuracy: LocationAccuracy.high,
98 ),
99 );
100 } catch (e) {
101 log('Error getting current location: $e');
102 return null;
103 }
104 }
105
107 required int driverId,
108 required double latitude,
109 required double longitude,
110 int? poId,
111 String? poStatus,
112 }) async {
113 try {
114 final data = {
115 "driverId": driverId,
116 "lag": latitude.toString(),
117 "longitude": longitude.toString(),
118 "poId": poId,
119 "poStatus": poStatus,
120 };
121
122 final response = await _apiClient.post(
123 url: ApiEndpoints.sendGeoLocation,
124 data: data,
125 );
126
127 return response.isNotEmpty;
128 } catch (e) {
129 log('Error sending location to server: $e');
130 return false;
131 }
132 }
133
134 // Check if user is already checked in
135 Future<bool> isUserCheckedIn() async {
136 try {
137 final prefs = await SharedPreferences.getInstance();
138 final statusJson = prefs.getString(_checkInStatusKey);
139
140 return statusJson != null;
141 } catch (e) {
142 log('Error checking check-in status: $e');
143 return false;
144 }
145 }
146
147 // Clear check-in status (checkout)
148 Future<void> clearCheckInStatus() async {
149 try {
150 final prefs = await SharedPreferences.getInstance();
151 await prefs.remove(_checkInStatusKey);
152 } catch (e) {
153 // Handle error silently
154 log('Error clearing check-in status: $e');
155 }
156 }
157}
sealed class CheckInOutEvent extends Equatable userId
Future< Position?> _getCurrentLocation() async
Future< bool > isUserCheckedIn() async
Future< void > clearCheckInStatus() async
Future< bool > saveCheckInStatus({ required int vehicleId, required String immatriculation, required int driverId, }) async
static const String _checkInStatusKey
Future< bool > _sendLocationToServer({ required int driverId, required double latitude, required double longitude, int? poId, String? poStatus, }) async
final ApiClient _apiClient
Future< CheckInStatusModel?> getCheckInStatus({required int userId}) async
final String path
class Partner String
final sl
final ApiClient _apiClient