optimized
This commit is contained in:
@ -6,7 +6,7 @@ import 'package:path/path.dart';
|
||||
|
||||
import '../model/rog.dart';
|
||||
|
||||
class DatabaseHelper{
|
||||
class DatabaseHelper {
|
||||
DatabaseHelper._privateConstructor();
|
||||
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
|
||||
|
||||
@ -21,7 +21,13 @@ class DatabaseHelper{
|
||||
// version: 1,
|
||||
// onCreate: _onCreate,
|
||||
// );
|
||||
return openDatabase(join(await getDatabasesPath(), 'rog.db',), version: 1, onCreate: _onCreate);
|
||||
return openDatabase(
|
||||
join(
|
||||
await getDatabasesPath(),
|
||||
'rog.db',
|
||||
),
|
||||
version: 1,
|
||||
onCreate: _onCreate);
|
||||
}
|
||||
|
||||
Future _onCreate(Database db, int version) async {
|
||||
@ -56,7 +62,7 @@ class DatabaseHelper{
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
await db.execute('''
|
||||
CREATE TABLE rogaining(
|
||||
rog_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
course_id INTEGER,
|
||||
@ -69,7 +75,7 @@ class DatabaseHelper{
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
await db.execute('''
|
||||
CREATE TABLE rog(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
team_name TEXT,
|
||||
@ -81,86 +87,72 @@ class DatabaseHelper{
|
||||
rog_action_type INTEGER
|
||||
)
|
||||
''');
|
||||
|
||||
}
|
||||
|
||||
Future<List<Rog>> allRogianing() async {
|
||||
Database db = await instance.database;
|
||||
var rog = await db.query('rog');
|
||||
List<Rog> roglist = rog.isNotEmpty ?
|
||||
rog.map((e) => Rog.fromMap(e)).toList() : [];
|
||||
print("--------- $rog");
|
||||
List<Rog> roglist =
|
||||
rog.isNotEmpty ? rog.map((e) => Rog.fromMap(e)).toList() : [];
|
||||
//print("--------- $rog");
|
||||
return roglist;
|
||||
}
|
||||
|
||||
|
||||
Future<List<Rog>> getRogainingByLatLon(double lat, double lon) async {
|
||||
Database db = await instance.database;
|
||||
var rog = await db.query('rog', where: "lat = $lat and lon= $lon");
|
||||
List<Rog> roglist = rog.isNotEmpty
|
||||
? rog.map((e) => Rog.fromMap(e)).toList() : [];
|
||||
List<Rog> roglist =
|
||||
rog.isNotEmpty ? rog.map((e) => Rog.fromMap(e)).toList() : [];
|
||||
return roglist;
|
||||
}
|
||||
|
||||
Future clearSelection() async {
|
||||
Database db = await instance.database;
|
||||
Map<String, dynamic> rowClear = {
|
||||
"selected": false
|
||||
};
|
||||
return await db.update(
|
||||
"destination",
|
||||
rowClear
|
||||
);
|
||||
Map<String, dynamic> rowClear = {"selected": false};
|
||||
return await db.update("destination", rowClear);
|
||||
}
|
||||
|
||||
Future<int> toggleSelecttion(Destination dest) async {
|
||||
Database db = await instance.database;
|
||||
|
||||
|
||||
bool val = !dest.selected!;
|
||||
Map<String, dynamic> rowTarget = {
|
||||
"selected": val
|
||||
};
|
||||
Map<String, dynamic> rowTarget = {"selected": val};
|
||||
|
||||
await clearSelection();
|
||||
|
||||
return await db.update(
|
||||
"destination",
|
||||
rowTarget,
|
||||
where: 'location_id = ?',
|
||||
whereArgs: [dest.location_id!]
|
||||
);
|
||||
return await db.update("destination", rowTarget,
|
||||
where: 'location_id = ?', whereArgs: [dest.location_id!]);
|
||||
}
|
||||
|
||||
Future<int> deleteRogaining(int id) async {
|
||||
Database db = await instance.database;
|
||||
var rog = await db.delete('rog', where: "id = $id");
|
||||
int ret = rog > 0 ? rog : -1;
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Future<void> deleteAllRogaining() async {
|
||||
Database db = await instance.database;
|
||||
await db.delete('rog');
|
||||
}
|
||||
|
||||
|
||||
Future<bool>isRogAlreadyAvailable(int id) async{
|
||||
Future<bool> isRogAlreadyAvailable(int id) async {
|
||||
Database db = await instance.database;
|
||||
var rog = await db.query('rog', where: "id = $id");
|
||||
return rog.isNotEmpty ? true : false;
|
||||
}
|
||||
|
||||
Future<int?>latestGoal() async{
|
||||
Future<int?> latestGoal() async {
|
||||
Database db = await instance.database;
|
||||
return Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(checkintime) FROM rog'));
|
||||
|
||||
return Sqflite.firstIntValue(
|
||||
await db.rawQuery('SELECT MAX(checkintime) FROM rog'));
|
||||
}
|
||||
|
||||
Future<int> insertRogaining(Rog rog) async {
|
||||
Database db = await instance.database;
|
||||
int? nextOrder = Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(id) FROM rog'));
|
||||
int? nextOrder =
|
||||
Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(id) FROM rog'));
|
||||
nextOrder = nextOrder ?? 0;
|
||||
nextOrder = nextOrder + 1;
|
||||
rog.id = nextOrder;
|
||||
@ -169,68 +161,63 @@ class DatabaseHelper{
|
||||
rog.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
print("------ database helper insert $res-----------::::::::");
|
||||
//print("------ database helper insert $res-----------::::::::");
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
Future<List<Destination>> getDestinations() async {
|
||||
Database db = await instance.database;
|
||||
var dest = await db.query('destination', orderBy: 'list_order');
|
||||
List<Destination> destList = dest.isNotEmpty ?
|
||||
dest.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
print("--------- $destList");
|
||||
List<Destination> destList =
|
||||
dest.isNotEmpty ? dest.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
//print("--------- $destList");
|
||||
return destList;
|
||||
}
|
||||
|
||||
Future<List<Destination>> getDestinationById(int id) async {
|
||||
Database db = await instance.database;
|
||||
var rog = await db.query('destination', where: "location_id = $id");
|
||||
List<Destination> deslist = rog.isNotEmpty
|
||||
? rog.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
List<Destination> deslist =
|
||||
rog.isNotEmpty ? rog.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
return deslist;
|
||||
}
|
||||
|
||||
|
||||
Future<List<Destination>> getDestinationByLatLon(double lat, double lon) async {
|
||||
Future<List<Destination>> getDestinationByLatLon(
|
||||
double lat, double lon) async {
|
||||
Database db = await instance.database;
|
||||
var dest = await db.query('destination', where: "lat = $lat and lon= $lon", orderBy: 'list_order');
|
||||
List<Destination> destList = dest.isNotEmpty
|
||||
? dest.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
var dest = await db.query('destination',
|
||||
where: "lat = $lat and lon= $lon", orderBy: 'list_order');
|
||||
List<Destination> destList =
|
||||
dest.isNotEmpty ? dest.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
return destList;
|
||||
}
|
||||
|
||||
Future<int> deleteDestination(int locationId) async {
|
||||
Database db = await instance.database;
|
||||
var dest = await db.delete('destination', where: "location_id = $locationId");
|
||||
var dest =
|
||||
await db.delete('destination', where: "location_id = $locationId");
|
||||
int ret = dest > 0 ? dest : -1;
|
||||
|
||||
//after deleting set correct order
|
||||
await setOrder();
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Future<void>setOrder() async {
|
||||
Future<void> setOrder() async {
|
||||
Database db = await instance.database;
|
||||
var byOrder = await db.query('destination', orderBy: 'list_order');
|
||||
|
||||
List<Destination> desDb = byOrder.isNotEmpty
|
||||
? byOrder.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
? byOrder.map((e) => Destination.fromMap(e)).toList()
|
||||
: [];
|
||||
|
||||
int order = 1;
|
||||
for( Destination d in desDb){
|
||||
for (Destination d in desDb) {
|
||||
Map<String, dynamic> rowTarget = {"list_order": order};
|
||||
|
||||
Map<String, dynamic> rowTarget = {
|
||||
"list_order": order
|
||||
};
|
||||
|
||||
await db.update(
|
||||
"destination",
|
||||
rowTarget,
|
||||
where: 'location_id = ?',
|
||||
whereArgs: [d.location_id]
|
||||
);
|
||||
await db.update("destination", rowTarget,
|
||||
where: 'location_id = ?', whereArgs: [d.location_id]);
|
||||
|
||||
order += 1;
|
||||
}
|
||||
@ -241,15 +228,17 @@ class DatabaseHelper{
|
||||
await db.delete('destination');
|
||||
}
|
||||
|
||||
Future<bool>isAlreadyAvailable(int locationId) async{
|
||||
Future<bool> isAlreadyAvailable(int locationId) async {
|
||||
Database db = await instance.database;
|
||||
var dest = await db.query('destination', where: "location_id = $locationId");
|
||||
var dest =
|
||||
await db.query('destination', where: "location_id = $locationId");
|
||||
return dest.isNotEmpty ? true : false;
|
||||
}
|
||||
|
||||
Future<int> insertDestination(Destination dest) async {
|
||||
Database db = await instance.database;
|
||||
int? nextOrder = Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(list_order) FROM destination'));
|
||||
int? nextOrder = Sqflite.firstIntValue(
|
||||
await db.rawQuery('SELECT MAX(list_order) FROM destination'));
|
||||
nextOrder = nextOrder ?? 0;
|
||||
nextOrder = nextOrder + 1;
|
||||
dest.list_order = nextOrder;
|
||||
@ -262,67 +251,50 @@ class DatabaseHelper{
|
||||
return res;
|
||||
}
|
||||
|
||||
Future<int> updateCancelBuyPoint(Destination destination)async {
|
||||
print("---- updating puypint image in db -----");
|
||||
Future<int> updateCancelBuyPoint(Destination destination) async {
|
||||
//print("---- updating puypint image in db -----");
|
||||
Database db = await instance.database;
|
||||
Map<String, dynamic> row = {
|
||||
"buy_point": 0
|
||||
};
|
||||
return await db.update(
|
||||
"destination",
|
||||
row,
|
||||
where: 'location_id = ?',
|
||||
whereArgs: [destination.location_id!]
|
||||
);
|
||||
Map<String, dynamic> row = {"buy_point": 0};
|
||||
return await db.update("destination", row,
|
||||
where: 'location_id = ?', whereArgs: [destination.location_id!]);
|
||||
}
|
||||
|
||||
Future<int> updateBuyPoint(Destination destination, String imageUrl)async {
|
||||
print("---- updating puypint image in db -----");
|
||||
Future<int> updateBuyPoint(Destination destination, String imageUrl) async {
|
||||
//print("---- updating puypint image in db -----");
|
||||
Database db = await instance.database;
|
||||
Map<String, dynamic> row = {
|
||||
"buypoint_image": imageUrl
|
||||
};
|
||||
return await db.update(
|
||||
"destination",
|
||||
row,
|
||||
where: 'location_id = ?',
|
||||
whereArgs: [destination.location_id!]
|
||||
);
|
||||
Map<String, dynamic> row = {"buypoint_image": imageUrl};
|
||||
return await db.update("destination", row,
|
||||
where: 'location_id = ?', whereArgs: [destination.location_id!]);
|
||||
}
|
||||
|
||||
Future<int> updateAction(Destination destination, bool checkin)async {
|
||||
Future<int> updateAction(Destination destination, bool checkin) async {
|
||||
Database db = await instance.database;
|
||||
int act = checkin == false ? 0 : 1;
|
||||
Map<String, dynamic> row = {
|
||||
"checkedin": act
|
||||
};
|
||||
return await db.update(
|
||||
"destination",
|
||||
row,
|
||||
where: 'location_id = ?',
|
||||
whereArgs: [destination.location_id!]
|
||||
);
|
||||
Map<String, dynamic> row = {"checkedin": act};
|
||||
return await db.update("destination", row,
|
||||
where: 'location_id = ?', whereArgs: [destination.location_id!]);
|
||||
}
|
||||
|
||||
Future<void> updateOrder(Destination d, int dir)async {
|
||||
Future<void> updateOrder(Destination d, int dir) async {
|
||||
Database db = await instance.database;
|
||||
var target = await db.query('destination', where: "list_order = ${d.list_order! + dir}");
|
||||
var dest = await db.query('destination', where: "location_id = ${d.location_id}");
|
||||
var target = await db.query('destination',
|
||||
where: "list_order = ${d.list_order! + dir}");
|
||||
var dest =
|
||||
await db.query('destination', where: "location_id = ${d.location_id}");
|
||||
|
||||
print("--- target in db is $target");
|
||||
print("--- destine in db is $dest");
|
||||
|
||||
if(target.isNotEmpty){
|
||||
// print("--- target in db is $target");
|
||||
// print("--- destine in db is $dest");
|
||||
|
||||
if (target.isNotEmpty) {
|
||||
List<Destination> targetIndb = target.isNotEmpty
|
||||
? target.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
? target.map((e) => Destination.fromMap(e)).toList()
|
||||
: [];
|
||||
|
||||
List<Destination> destIndb = dest.isNotEmpty
|
||||
? dest.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
? dest.map((e) => Destination.fromMap(e)).toList()
|
||||
: [];
|
||||
|
||||
Map<String, dynamic> rowTarget = {
|
||||
"list_order": d.list_order
|
||||
};
|
||||
Map<String, dynamic> rowTarget = {"list_order": d.list_order};
|
||||
|
||||
Map<String, dynamic> rowDes = {
|
||||
"list_order": destIndb[0].list_order! + dir
|
||||
@ -331,19 +303,11 @@ class DatabaseHelper{
|
||||
// print("--- target destination is ${target_indb[0].location_id}");
|
||||
// print("--- destine destination is is ${dest_indb[0].location_id}");
|
||||
|
||||
await db.update(
|
||||
"destination",
|
||||
rowTarget,
|
||||
where: 'location_id = ?',
|
||||
whereArgs: [targetIndb[0].location_id]
|
||||
);
|
||||
await db.update("destination", rowTarget,
|
||||
where: 'location_id = ?', whereArgs: [targetIndb[0].location_id]);
|
||||
|
||||
await db.update(
|
||||
"destination",
|
||||
rowDes,
|
||||
where: 'location_id = ?',
|
||||
whereArgs: [destIndb[0].location_id]
|
||||
);
|
||||
await db.update("destination", rowDes,
|
||||
where: 'location_id = ?', whereArgs: [destIndb[0].location_id]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -351,6 +315,4 @@ class DatabaseHelper{
|
||||
// Database db = await instance.database;
|
||||
// return await Sqflite.firstIntValue(await db.rawQuery("SELECT COUNT(*) FROM incidents"));
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user