Add Docker build workflow and mobile application

This commit is contained in:
Sven laptop
2026-05-05 17:39:02 +02:00
parent 39278dc857
commit 00758561f9
159 changed files with 8424 additions and 3 deletions
View File
@@ -0,0 +1,79 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import '../models/user.dart';
/// Authentication Service
class AuthService {
// TODO: Implement API connector - configure base URL from env/config
static const String _apiBaseUrl = 'http://localhost:5000';
static const String _tokenKey = 'auth_token';
late SharedPreferences _prefs;
Future<void> _initPrefs() async {
_prefs = await SharedPreferences.getInstance();
}
/// Login with userCode and password
Future<AuthResponse> login({
required String userCode,
required String password,
}) async {
await _initPrefs();
try {
final response = await http.post(
Uri.parse('$_apiBaseUrl/login'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'userCode': userCode,
'password': password,
}),
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
final authResponse = AuthResponse.fromJson(data);
// Store token
await _prefs.setString(_tokenKey, authResponse.token);
return authResponse;
} else if (response.statusCode == 401) {
throw Exception('Invalid credentials');
} else {
throw Exception('Login failed: ${response.statusCode}');
}
} catch (e) {
throw Exception('Login error: $e');
}
}
/// Get stored token
Future<String?> getToken() async {
await _initPrefs();
return _prefs.getString(_tokenKey);
}
/// Check if user is logged in
Future<bool> isLoggedIn() async {
final token = await getToken();
return token != null;
}
/// Logout
Future<void> logout() async {
await _initPrefs();
await _prefs.remove(_tokenKey);
}
/// Get Authorization header
Future<Map<String, String>> getAuthHeaders() async {
final token = await getToken();
return {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
};
}
}
@@ -0,0 +1,128 @@
import 'dart:convert';
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart' as http;
import 'package:latlong2/latlong.dart';
import '../models/navigation.dart';
/// Navigation Service using OpenStreetMap
class NavigationService {
// OSRM (OpenStreetMap Routing Machine) - free public service
static const String _osrmBaseUrl = 'https://router.project-osrm.org/route/v1/car';
/// Get current device location
Future<NavigationPoint?> getCurrentLocation() async {
try {
final permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
await Geolocator.requestPermission();
}
final position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
);
return NavigationPoint(
latitude: position.latitude,
longitude: position.longitude,
name: 'Current Location',
);
} catch (e) {
throw Exception('Failed to get location: $e');
}
}
/// Get route between two points
/// Uses OSRM for open routing without API key
Future<NavigationRoute> getRoute({
required NavigationPoint start,
required NavigationPoint end,
}) async {
try {
// OSRM format: /route/v1/car/lon1,lat1;lon2,lat2
final coordinates =
'${start.longitude},${start.latitude};${end.longitude},${end.latitude}';
final url =
'$_osrmBaseUrl/$coordinates?overview=full&steps=true&geometries=geojson';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
if (data['code'] != 'Ok') {
throw Exception('Route not found: ${data['message']}');
}
final route = NavigationRoute.fromJson(data);
// Set start and end points properly
return NavigationRoute(
start: start,
end: end,
legs: route.legs,
totalDistance: route.totalDistance,
totalDuration: route.totalDuration,
);
} else {
throw Exception('Failed to get route: ${response.statusCode}');
}
} catch (e) {
throw Exception('Navigation error: $e');
}
}
/// Get multiple routes (alternatives)
Future<List<NavigationRoute>> getMultipleRoutes({
required NavigationPoint start,
required NavigationPoint end,
int alternatives = 2,
}) async {
try {
final coordinates =
'${start.longitude},${start.latitude};${end.longitude},${end.latitude}';
final url =
'$_osrmBaseUrl/$coordinates?overview=full&steps=true&geometries=geojson&alternatives=$alternatives';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
if (data['code'] != 'Ok') {
throw Exception('Routes not found: ${data['message']}');
}
final routes = (data['routes'] as List)
.map((routeData) {
return NavigationRoute(
start: start,
end: end,
legs: (routeData['legs'] as List)
.map((leg) =>
RouteLeg.fromJson(leg as Map<String, dynamic>))
.toList(),
totalDistance:
(routeData['distance'] as num).toDouble(),
totalDuration: (routeData['duration'] as num).toInt(),
);
})
.toList();
return routes;
} else {
throw Exception('Failed to get routes: ${response.statusCode}');
}
} catch (e) {
throw Exception('Navigation error: $e');
}
}
/// Calculate straight-line distance between two points
double calculateDistance(NavigationPoint p1, NavigationPoint p2) {
const distance = Distance();
return distance(
LatLng(p1.latitude, p1.longitude),
LatLng(p2.latitude, p2.longitude),
);
}
}
@@ -0,0 +1,82 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../models/schedule.dart';
import 'auth_service.dart';
/// Schedule/Route Service
class ScheduleService {
// TODO: Implement API connector - configure base URL from env/config
static const String _apiBaseUrl = 'http://localhost:5000';
final _authService = AuthService();
/// Get daily schedule for a specific date
Future<DailySchedule> getDailySchedule(DateTime date) async {
try {
final headers = await _authService.getAuthHeaders();
final dateStr = '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
final response = await http.get(
Uri.parse('$_apiBaseUrl/schedule/daily?date=$dateStr'),
headers: headers,
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return DailySchedule.fromJson(data);
} else if (response.statusCode == 401) {
throw Exception('Unauthorized - Please login again');
} else {
throw Exception('Failed to load schedule: ${response.statusCode}');
}
} catch (e) {
throw Exception('Schedule error: $e');
}
}
/// Get today's schedule
Future<DailySchedule> getTodaySchedule() async {
return getDailySchedule(DateTime.now());
}
/// Get route details
Future<Route> getRouteDetails(int routeId) async {
try {
final headers = await _authService.getAuthHeaders();
final response = await http.get(
Uri.parse('$_apiBaseUrl/schedule/route/$routeId'),
headers: headers,
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return Route.fromJson(data);
} else if (response.statusCode == 401) {
throw Exception('Unauthorized - Please login again');
} else {
throw Exception('Failed to load route: ${response.statusCode}');
}
} catch (e) {
throw Exception('Route error: $e');
}
}
/// Update route status
Future<void> updateRouteStatus(int routeId, String status) async {
try {
final headers = await _authService.getAuthHeaders();
final response = await http.patch(
Uri.parse('$_apiBaseUrl/schedule/route/$routeId/status'),
headers: headers,
body: jsonEncode({'status': status}),
);
if (response.statusCode != 200) {
throw Exception('Failed to update status: ${response.statusCode}');
}
} catch (e) {
throw Exception('Status update error: $e');
}
}
}