fix marker rendering & modernize riding widgets for dark mode - 2026-04-11
This commit is contained in:
@@ -1,146 +1,139 @@
|
||||
import 'dart:convert';
|
||||
import 'package:Intaleq/constant/table_names.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:Intaleq/main.dart';
|
||||
|
||||
class DbSql {
|
||||
static final DbSql instance = DbSql._();
|
||||
|
||||
static Database? _database;
|
||||
|
||||
DbSql._();
|
||||
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database!;
|
||||
_database = await _initDatabase();
|
||||
return _database!;
|
||||
// Helper to read data as a list of maps from GetStorage
|
||||
List<Map<String, dynamic>> _readTable(String table) {
|
||||
String? dataString = box.read(table);
|
||||
if (dataString == null) return [];
|
||||
try {
|
||||
List<dynamic> parsed = jsonDecode(dataString);
|
||||
return parsed.map((e) => Map<String, dynamic>.from(e)).toList();
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Future<Database> _initDatabase() async {
|
||||
String path = join(await getDatabasesPath(), 'my_database.db');
|
||||
return await openDatabase(
|
||||
path,
|
||||
version: 1,
|
||||
onCreate: (db, version) async {
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS ${TableName.carLocations}(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
driver_id TEXT,
|
||||
latitude REAL,
|
||||
longitude REAL,
|
||||
created_at TEXT,
|
||||
updated_at TEXT
|
||||
)
|
||||
''');
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS ${TableName.placesFavorite}(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
latitude REAL,
|
||||
longitude REAL,
|
||||
name TEXT UNIQUE,
|
||||
rate TEXT,
|
||||
createdAt TEXT
|
||||
)
|
||||
''');
|
||||
// await db.execute('DROP TABLE IF EXISTS ${TableName.recentLocations}');
|
||||
await db.execute('''
|
||||
CREATE TABLE ${TableName.recentLocations}(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
latitude REAL,
|
||||
longitude REAL,
|
||||
name TEXT,
|
||||
rate TEXT,
|
||||
createdAt TEXT
|
||||
)
|
||||
''');
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS ${TableName.driverOrdersRefuse}(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
order_id TEXT UNIQUE,
|
||||
created_at TEXT,
|
||||
driver_id TEXT
|
||||
)
|
||||
''');
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS ${TableName.rideLocation}(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
order_id TEXT ,
|
||||
created_at TEXT,
|
||||
lat TEXT,
|
||||
lng TEXT
|
||||
)
|
||||
''');
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS ${TableName.faceDetectTimes}(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
faceDetectTimes INTEGER
|
||||
)
|
||||
''');
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS ${TableName.captainNotification}(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
faceDetectTimes INTEGER
|
||||
)
|
||||
''');
|
||||
},
|
||||
);
|
||||
// Helper to write data back to GetStorage
|
||||
void _writeTable(String table, List<Map<String, dynamic>> data) {
|
||||
box.write(table, jsonEncode(data));
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> getAllData(String table) async {
|
||||
Database db = await instance.database;
|
||||
return await db.query(table);
|
||||
return _readTable(table);
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> getCustomQuery(String query) async {
|
||||
Database db = await instance.database;
|
||||
return await db.rawQuery(query);
|
||||
String q = query.toLowerCase();
|
||||
String targetTable = TableName.recentLocations; // Default target
|
||||
|
||||
// Determine the table
|
||||
if (q.contains(TableName.recentLocations.toLowerCase())) {
|
||||
targetTable = TableName.recentLocations;
|
||||
} else if (q.contains(TableName.carLocations.toLowerCase())) {
|
||||
targetTable = TableName.carLocations;
|
||||
} else if (q.contains(TableName.placesFavorite.toLowerCase())) {
|
||||
targetTable = TableName.placesFavorite;
|
||||
}
|
||||
|
||||
List<Map<String, dynamic>> data = _readTable(targetTable);
|
||||
|
||||
// Apply DISTINCT logic if needed based on query
|
||||
if (q.contains('distinct latitude, longitude, name, rate')) {
|
||||
// Manual distinct by name and location
|
||||
Map<String, Map<String, dynamic>> distinctMap = {};
|
||||
for (var item in data) {
|
||||
String key = '${item['latitude']}_${item['longitude']}_${item['name']}';
|
||||
if (!distinctMap.containsKey(key)) {
|
||||
distinctMap[key] = item;
|
||||
}
|
||||
}
|
||||
data = distinctMap.values.toList();
|
||||
}
|
||||
|
||||
// Apply ORDER BY createdAt DESC logic
|
||||
if (q.contains('order by createdat desc')) {
|
||||
data.sort((a, b) {
|
||||
String dateA = a['createdAt'] ?? '';
|
||||
String dateB = b['createdAt'] ?? '';
|
||||
return dateB.compareTo(dateA);
|
||||
});
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
Future<int> insertData(Map<String, dynamic> map, String table) async {
|
||||
Database db = await instance.database;
|
||||
return await db.insert(table, map);
|
||||
List<Map<String, dynamic>> data = _readTable(table);
|
||||
// Generate simple ID
|
||||
int newId = data.isEmpty ? 1 : (data.last['id'] as int? ?? 0) + 1;
|
||||
Map<String, dynamic> newMap = Map<String, dynamic>.from(map);
|
||||
newMap['id'] = newId;
|
||||
data.add(newMap);
|
||||
_writeTable(table, data);
|
||||
return newId;
|
||||
}
|
||||
|
||||
Future<int> insertMapLocation(Map<String, dynamic> map, String table) async {
|
||||
Database db = await instance.database;
|
||||
|
||||
// Check if the record already exists (based on latitude, longitude, and name)
|
||||
var existing = await db.query(
|
||||
table,
|
||||
where: 'latitude = ? AND longitude = ? AND name = ?',
|
||||
whereArgs: [map['latitude'], map['longitude'], map['name']],
|
||||
);
|
||||
|
||||
if (existing.isNotEmpty) {
|
||||
// If record exists, update the createdAt field with the current timestamp
|
||||
var updatedMap = Map<String, dynamic>.from(map);
|
||||
updatedMap['createdAt'] =
|
||||
DateTime.now().toIso8601String(); // Update timestamp
|
||||
return await db.update(
|
||||
table,
|
||||
updatedMap,
|
||||
where: 'id = ?',
|
||||
whereArgs: [existing.first['id']], // Update the existing row
|
||||
);
|
||||
List<Map<String, dynamic>> data = _readTable(table);
|
||||
|
||||
// Check if exists
|
||||
int existingIndex = data.indexWhere((element) =>
|
||||
element['latitude'] == map['latitude'] &&
|
||||
element['longitude'] == map['longitude'] &&
|
||||
element['name'] == map['name']);
|
||||
|
||||
if (existingIndex != -1) {
|
||||
// Update
|
||||
data[existingIndex] = Map<String, dynamic>.from(data[existingIndex]);
|
||||
data[existingIndex]['createdAt'] = DateTime.now().toIso8601String();
|
||||
for (var key in map.keys) {
|
||||
if (key != 'latitude' && key != 'longitude' && key != 'name') {
|
||||
data[existingIndex][key] = map[key];
|
||||
}
|
||||
}
|
||||
_writeTable(table, data);
|
||||
return data[existingIndex]['id'] as int? ?? 1;
|
||||
} else {
|
||||
// If record doesn't exist, insert new record with the current timestamp
|
||||
map['createdAt'] = DateTime.now().toIso8601String();
|
||||
return await db.insert(table, map);
|
||||
// Insert
|
||||
int newId = data.isEmpty ? 1 : ((data.last['id'] as int?) ?? 0) + 1;
|
||||
Map<String, dynamic> newMap = Map<String, dynamic>.from(map);
|
||||
newMap['id'] = newId;
|
||||
newMap['createdAt'] = DateTime.now().toIso8601String();
|
||||
data.add(newMap);
|
||||
_writeTable(table, data);
|
||||
return newId;
|
||||
}
|
||||
}
|
||||
|
||||
Future<int> updateData(Map<String, dynamic> map, String table, int id) async {
|
||||
Database db = await instance.database;
|
||||
|
||||
return await db.update(table, map, where: 'id = ?', whereArgs: [id]);
|
||||
List<Map<String, dynamic>> data = _readTable(table);
|
||||
int index = data.indexWhere((element) => element['id'] == id);
|
||||
if (index != -1) {
|
||||
data[index] = {...data[index], ...map};
|
||||
_writeTable(table, data);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Future<int> deleteData(String table, int id) async {
|
||||
Database db = await instance.database;
|
||||
return await db.delete(table, where: 'id = ?', whereArgs: [id]);
|
||||
List<Map<String, dynamic>> data = _readTable(table);
|
||||
int initialLength = data.length;
|
||||
data.removeWhere((element) => element['id'] == id);
|
||||
if (data.length < initialLength) {
|
||||
_writeTable(table, data);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Future<int> deleteAllData(String table) async {
|
||||
Database db = await instance.database;
|
||||
return await db.delete(table);
|
||||
_writeTable(table, []);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user