feat: introduce FixMate Flutter app and React dashboard
- Add Flutter app shell (FixMateApp/MainScreen) with tabs: Report, Map, My Reports, Settings - Implement capture and review flow (image_picker, geolocator, deterministic mock AI), and local storage (SharedPreferences + photo files on mobile) - Build Map screen with flutter_map, marker clustering, filters, legend, marker details, and external maps deeplink - Add My Reports list (view details, cycle status, delete) and Settings (language toggle via Provider, diagnostics, clear all data) - Introduce JSON i18n loader and LocaleProvider; add EN/BM assets - Define models (Report, enums) and UI badges (severity, status) - Add static React dashboard (Leaflet map with clustering, heatmap toggle, filters incl. date range, queue, detail drawer), i18n (EN/BM), and demo data - Update build/config and platform setup: - Extend pubspec with required packages and register i18n assets - Android: add CAMERA and location permissions; pin NDK version - iOS: add usage descriptions for camera, photo library, location - Gradle properties tuned for Windows/UNC stability - Register desktop plugins (Linux/macOS/Windows) - .gitignore: ignore .kilocode - Overhaul README and replace sample widget test
This commit is contained in:
202
lib/services/location_service.dart
Normal file
202
lib/services/location_service.dart
Normal file
@@ -0,0 +1,202 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import '../models/report.dart';
|
||||
|
||||
/// Service for handling location operations and permissions
|
||||
class LocationService {
|
||||
/// Check if location services are enabled
|
||||
static Future<bool> isLocationServiceEnabled() async {
|
||||
try {
|
||||
return await Geolocator.isLocationServiceEnabled();
|
||||
} catch (e) {
|
||||
print('Error checking location service: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Check location permissions
|
||||
static Future<LocationPermission> checkPermission() async {
|
||||
try {
|
||||
return await Geolocator.checkPermission();
|
||||
} catch (e) {
|
||||
print('Error checking location permission: $e');
|
||||
return LocationPermission.denied;
|
||||
}
|
||||
}
|
||||
|
||||
/// Request location permissions
|
||||
static Future<LocationPermission> requestPermission() async {
|
||||
try {
|
||||
return await Geolocator.requestPermission();
|
||||
} catch (e) {
|
||||
print('Error requesting location permission: $e');
|
||||
return LocationPermission.denied;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current position with high accuracy
|
||||
static Future<Position?> getCurrentPosition() async {
|
||||
try {
|
||||
// Check if location services are enabled
|
||||
final serviceEnabled = await isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
print('Location services are disabled');
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check permissions
|
||||
var permission = await checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
print('Location permission denied');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (permission == LocationPermission.deniedForever) {
|
||||
print('Location permission permanently denied');
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get current position
|
||||
return await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high,
|
||||
timeLimit: const Duration(seconds: 30),
|
||||
);
|
||||
} catch (e) {
|
||||
print('Error getting current position: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current position with best available accuracy
|
||||
static Future<Position?> getBestAvailablePosition() async {
|
||||
try {
|
||||
// Check if location services are enabled
|
||||
final serviceEnabled = await isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
print('Location services are disabled');
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check permissions
|
||||
var permission = await checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
print('Location permission denied');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (permission == LocationPermission.deniedForever) {
|
||||
print('Location permission permanently denied');
|
||||
return null;
|
||||
}
|
||||
|
||||
// Try high accuracy first, fallback to medium if it takes too long
|
||||
try {
|
||||
return await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high,
|
||||
timeLimit: const Duration(seconds: 15),
|
||||
);
|
||||
} catch (e) {
|
||||
print('High accuracy failed, trying medium accuracy: $e');
|
||||
return await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.medium,
|
||||
timeLimit: const Duration(seconds: 10),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error getting best available position: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert Position to LocationData
|
||||
static LocationData positionToLocationData(Position position) {
|
||||
return LocationData(
|
||||
lat: position.latitude,
|
||||
lng: position.longitude,
|
||||
accuracy: position.accuracy,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get location accuracy description
|
||||
static String getAccuracyDescription(double? accuracy) {
|
||||
if (accuracy == null) return 'Unknown';
|
||||
|
||||
if (accuracy <= 3) {
|
||||
return 'Very High';
|
||||
} else if (accuracy <= 10) {
|
||||
return 'High';
|
||||
} else if (accuracy <= 50) {
|
||||
return 'Medium';
|
||||
} else if (accuracy <= 100) {
|
||||
return 'Low';
|
||||
} else {
|
||||
return 'Very Low';
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if location accuracy is good enough for reporting
|
||||
static bool isAccuracyGoodForReporting(double? accuracy) {
|
||||
if (accuracy == null) return false;
|
||||
return accuracy <= 50; // Within 50 meters is acceptable
|
||||
}
|
||||
|
||||
/// Get user-friendly location permission status message
|
||||
static String getPermissionStatusMessage(LocationPermission permission) {
|
||||
switch (permission) {
|
||||
case LocationPermission.denied:
|
||||
return 'Location permission denied. Please enable location access to attach GPS coordinates to your reports.';
|
||||
case LocationPermission.deniedForever:
|
||||
return 'Location permission permanently denied. Please enable location access in your device settings to use this feature.';
|
||||
case LocationPermission.whileInUse:
|
||||
case LocationPermission.always:
|
||||
return 'Location permission granted.';
|
||||
case LocationPermission.unableToDetermine:
|
||||
return 'Unable to determine location permission status.';
|
||||
}
|
||||
}
|
||||
|
||||
/// Open device location settings
|
||||
static Future<bool> openLocationSettings() async {
|
||||
try {
|
||||
return await Geolocator.openLocationSettings();
|
||||
} catch (e) {
|
||||
print('Error opening location settings: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate distance between two points in meters
|
||||
static double calculateDistance(LocationData point1, LocationData point2) {
|
||||
return Geolocator.distanceBetween(
|
||||
point1.lat,
|
||||
point1.lng,
|
||||
point2.lat,
|
||||
point2.lng,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get address from coordinates (placeholder - would need geocoding service)
|
||||
static Future<String?> getAddressFromCoordinates(double lat, double lng) async {
|
||||
// This is a placeholder implementation
|
||||
// In a real app, you would use a geocoding service like Google Maps API
|
||||
// or OpenStreetMap Nominatim API
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Validate location data
|
||||
static bool isValidLocation(LocationData location) {
|
||||
final acc = location.accuracy;
|
||||
return location.lat >= -90 &&
|
||||
location.lat <= 90 &&
|
||||
location.lng >= -180 &&
|
||||
location.lng <= 180 &&
|
||||
acc != null &&
|
||||
acc >= 0;
|
||||
}
|
||||
}
|
||||
116
lib/services/mock_ai.dart
Normal file
116
lib/services/mock_ai.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
import 'dart:math';
|
||||
import 'package:flutter/foundation.dart' hide Category;
|
||||
import '../models/enums.dart';
|
||||
import '../models/report.dart';
|
||||
|
||||
/// Service for generating deterministic AI suggestions for reports
|
||||
class MockAIService {
|
||||
/// Generate a deterministic seed based on report parameters
|
||||
static int _generateSeed(String id, String createdAt, double lat, double lng, int? photoSizeBytes) {
|
||||
final combined = '$id$createdAt$lat$lng${photoSizeBytes ?? 0}';
|
||||
var hash = 0;
|
||||
for (var i = 0; i < combined.length; i++) {
|
||||
hash = ((hash << 5) - hash) + combined.codeUnitAt(i);
|
||||
hash = hash & hash; // Convert to 32-bit integer
|
||||
}
|
||||
return hash.abs();
|
||||
}
|
||||
|
||||
/// Generate AI suggestion for a report
|
||||
static AISuggestion generateSuggestion({
|
||||
required String id,
|
||||
required String createdAt,
|
||||
required double lat,
|
||||
required double lng,
|
||||
int? photoSizeBytes,
|
||||
}) {
|
||||
final seed = _generateSeed(id, createdAt, lat, lng, photoSizeBytes);
|
||||
final random = Random(seed);
|
||||
|
||||
// Category selection with weighted probabilities
|
||||
final categoryWeights = {
|
||||
Category.pothole: 0.35,
|
||||
Category.trash: 0.25,
|
||||
Category.streetlight: 0.15,
|
||||
Category.signage: 0.10,
|
||||
Category.drainage: 0.10,
|
||||
Category.other: 0.05,
|
||||
};
|
||||
|
||||
// Apply heuristics based on image dimensions (if available)
|
||||
final aspectRatio = photoSizeBytes != null ? (random.nextDouble() * 2) : 1.0;
|
||||
if (aspectRatio > 1.2) {
|
||||
// Wide image - likely signage
|
||||
categoryWeights[Category.signage] = categoryWeights[Category.signage]! * 2;
|
||||
categoryWeights[Category.pothole] = categoryWeights[Category.pothole]! * 0.5;
|
||||
}
|
||||
|
||||
// Select category based on weights
|
||||
final categoryRand = random.nextDouble();
|
||||
double cumulative = 0.0;
|
||||
Category selectedCategory = Category.pothole;
|
||||
|
||||
for (final entry in categoryWeights.entries) {
|
||||
cumulative += entry.value;
|
||||
if (categoryRand <= cumulative) {
|
||||
selectedCategory = entry.key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Severity selection with weighted probabilities
|
||||
final severityWeights = {
|
||||
Severity.medium: 0.45,
|
||||
Severity.high: 0.30,
|
||||
Severity.low: 0.25,
|
||||
};
|
||||
|
||||
// Apply location accuracy heuristic
|
||||
final accuracy = random.nextDouble() * 50; // Simulate accuracy 0-50m
|
||||
final isNight = random.nextBool(); // Simulate night time
|
||||
|
||||
if (accuracy <= 10 && isNight) {
|
||||
// High accuracy at night - bump high severity
|
||||
severityWeights[Severity.high] = severityWeights[Severity.high]! * 1.5;
|
||||
severityWeights[Severity.medium] = severityWeights[Severity.medium]! * 0.8;
|
||||
}
|
||||
|
||||
// Select severity based on weights
|
||||
final severityRand = random.nextDouble();
|
||||
cumulative = 0.0;
|
||||
Severity selectedSeverity = Severity.medium;
|
||||
|
||||
for (final entry in severityWeights.entries) {
|
||||
cumulative += entry.value;
|
||||
if (severityRand <= cumulative) {
|
||||
selectedSeverity = entry.key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate confidence score (0.6 - 0.9)
|
||||
final confidence = 0.6 + (random.nextDouble() * 0.3);
|
||||
|
||||
return AISuggestion(
|
||||
category: selectedCategory,
|
||||
severity: selectedSeverity,
|
||||
confidence: confidence,
|
||||
);
|
||||
}
|
||||
|
||||
/// Check if the AI suggestion is reliable enough to use
|
||||
static bool isSuggestionReliable(AISuggestion suggestion) {
|
||||
return suggestion.confidence >= 0.7;
|
||||
}
|
||||
|
||||
/// Get confidence level description
|
||||
static String getConfidenceDescription(double confidence) {
|
||||
if (confidence >= 0.8) {
|
||||
return 'High confidence';
|
||||
} else if (confidence >= 0.7) {
|
||||
return 'Medium confidence';
|
||||
} else {
|
||||
return 'Low confidence';
|
||||
}
|
||||
}
|
||||
}
|
||||
231
lib/services/storage.dart
Normal file
231
lib/services/storage.dart
Normal file
@@ -0,0 +1,231 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import '../models/report.dart';
|
||||
|
||||
/// Service for persisting reports and managing local storage
|
||||
class StorageService {
|
||||
static const String _reportsKey = 'reports_v1';
|
||||
|
||||
/// Get all reports from storage
|
||||
static Future<List<Report>> getReports() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final reportsJson = prefs.getString(_reportsKey);
|
||||
|
||||
if (reportsJson == null || reportsJson.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final List<dynamic> reportsList = json.decode(reportsJson);
|
||||
return reportsList.map((json) => Report.fromJson(json)).toList();
|
||||
} catch (e) {
|
||||
print('Error loading reports: $e');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Save a single report to storage
|
||||
static Future<bool> saveReport(Report report) async {
|
||||
try {
|
||||
final reports = await getReports();
|
||||
final existingIndex = reports.indexWhere((r) => r.id == report.id);
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
reports[existingIndex] = report;
|
||||
} else {
|
||||
reports.add(report);
|
||||
}
|
||||
|
||||
return await _saveReportsList(reports);
|
||||
} catch (e) {
|
||||
print('Error saving report: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a report from storage
|
||||
static Future<bool> deleteReport(String reportId) async {
|
||||
try {
|
||||
final reports = await getReports();
|
||||
final updatedReports = reports.where((r) => r.id != reportId).toList();
|
||||
|
||||
// Delete photo file if it exists
|
||||
if (kIsWeb) {
|
||||
// On web, base64 is stored in memory, no file to delete
|
||||
} else {
|
||||
await _deletePhotoFile(reportId);
|
||||
}
|
||||
|
||||
return await _saveReportsList(updatedReports);
|
||||
} catch (e) {
|
||||
print('Error deleting report: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all reports from storage
|
||||
static Future<bool> clearAllReports() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_reportsKey);
|
||||
|
||||
// Delete all photo files
|
||||
if (!kIsWeb) {
|
||||
await _deleteAllPhotoFiles();
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
print('Error clearing reports: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Save reports list to SharedPreferences
|
||||
static Future<bool> _saveReportsList(List<Report> reports) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
// Convert reports to JSON, excluding photo data for mobile
|
||||
final reportsForStorage = reports.map((report) {
|
||||
final json = report.toJson();
|
||||
if (!kIsWeb) {
|
||||
// On mobile, remove base64Photo to save space
|
||||
json.remove('base64Photo');
|
||||
}
|
||||
return json;
|
||||
}).toList();
|
||||
|
||||
final reportsJson = json.encode(reportsForStorage);
|
||||
return await prefs.setString(_reportsKey, reportsJson);
|
||||
} catch (e) {
|
||||
print('Error saving reports list: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the photo file for a report
|
||||
static Future<File?> getPhotoFile(String reportId) async {
|
||||
if (kIsWeb) return null;
|
||||
|
||||
try {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final photoFile = File('${appDir.path}/$reportId.jpg');
|
||||
|
||||
if (await photoFile.exists()) {
|
||||
return photoFile;
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
print('Error getting photo file: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Save photo file for a report
|
||||
static Future<bool> savePhotoFile(String reportId, List<int> photoBytes) async {
|
||||
if (kIsWeb) return false;
|
||||
|
||||
try {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final photoFile = File('${appDir.path}/$reportId.jpg');
|
||||
await photoFile.writeAsBytes(photoBytes);
|
||||
return true;
|
||||
} catch (e) {
|
||||
print('Error saving photo file: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete photo file for a report
|
||||
static Future<bool> _deletePhotoFile(String reportId) async {
|
||||
if (kIsWeb) return true;
|
||||
|
||||
try {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final photoFile = File('${appDir.path}/$reportId.jpg');
|
||||
|
||||
if (await photoFile.exists()) {
|
||||
await photoFile.delete();
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
print('Error deleting photo file: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete all photo files
|
||||
static Future<void> _deleteAllPhotoFiles() async {
|
||||
if (kIsWeb) return;
|
||||
|
||||
try {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final reports = await getReports();
|
||||
|
||||
for (final report in reports) {
|
||||
final photoFile = File('${appDir.path}/${report.id}.jpg');
|
||||
if (await photoFile.exists()) {
|
||||
await photoFile.delete();
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error deleting all photo files: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Get storage statistics
|
||||
static Future<StorageStats> getStorageStats() async {
|
||||
try {
|
||||
final reports = await getReports();
|
||||
int photoFilesSize = 0;
|
||||
|
||||
if (!kIsWeb) {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final reportsDir = Directory(appDir.path);
|
||||
|
||||
if (await reportsDir.exists()) {
|
||||
final files = reportsDir.listSync().whereType<File>();
|
||||
photoFilesSize = files
|
||||
.where((file) => file.path.endsWith('.jpg'))
|
||||
.fold(0, (sum, file) => sum + file.lengthSync());
|
||||
}
|
||||
}
|
||||
|
||||
return StorageStats(
|
||||
reportCount: reports.length,
|
||||
photoFilesSize: photoFilesSize,
|
||||
);
|
||||
} catch (e) {
|
||||
print('Error getting storage stats: $e');
|
||||
return StorageStats(reportCount: 0, photoFilesSize: 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Storage statistics model
|
||||
class StorageStats {
|
||||
final int reportCount;
|
||||
final int photoFilesSize; // in bytes
|
||||
|
||||
const StorageStats({
|
||||
required this.reportCount,
|
||||
required this.photoFilesSize,
|
||||
});
|
||||
|
||||
String get formattedPhotoSize {
|
||||
const units = ['B', 'KB', 'MB', 'GB'];
|
||||
double size = photoFilesSize.toDouble();
|
||||
int unitIndex = 0;
|
||||
|
||||
while (size >= 1024 && unitIndex < units.length - 1) {
|
||||
size /= 1024;
|
||||
unitIndex++;
|
||||
}
|
||||
|
||||
return '${size.toStringAsFixed(1)} ${units[unitIndex]}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user