import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:rogapp/model/Rogaining.dart'; import 'package:rogapp/model/destination.dart'; import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; import '../model/rog.dart'; class DatabaseHelper{ DatabaseHelper._privateConstructor(); static final DatabaseHelper instance = DatabaseHelper._privateConstructor(); static Database? _database; Future get database async => _database ??= await _initDatabase(); Future _initDatabase() async { Directory documentDirectory = await getApplicationDocumentsDirectory(); String path = join(documentDirectory.path, 'rog.db'); // return await openDatabase( // path, // version: 1, // onCreate: _onCreate, // ); return openDatabase(join(await getDatabasesPath(), 'rog.db',), version: 1, onCreate: _onCreate); } Future _onCreate(Database db, int version) async { await db.execute(''' CREATE TABLE destination( location_id INTEGER PRIMARY KEY, name TEXT, address TEXT, phone TEXT, email TEXT, webcontents TEXT, videos TEXT, category TEXT, series INTEGER, lat REAL, lon REAL, list_order INTEGER, photos TEXT, checkin_radious REAL, sub_loc_id TEXT, auto_checkin INTEGER, selected INTEGER, checkedin INTEGER, cp REAL, checkin_point REAL, buy_point REAL, hidden_location INTEGER ) '''); await db.execute(''' CREATE TABLE rogaining( rog_id INTEGER PRIMARY KEY AUTOINCREMENT, course_id INTEGER, location_id INTEGER, user_id INTEGER, lat REAL, lon REAL, time_stamp INTEGER, image TEXT ) '''); await db.execute(''' CREATE TABLE rog( id INTEGER PRIMARY KEY AUTOINCREMENT, team_name TEXT, event_code TEXT, user_id INTEGER, cp_number INTEGER, checkintime INTEGER, image TEXT, rog_action_type INTEGER ) '''); } Future> allRogianing() async { Database db = await instance.database; var rog = await db.query('rog'); List roglist = rog.isNotEmpty ? rog.map((e) => Rog.fromMap(e)).toList() : []; print("--------- ${rog}"); return roglist; } Future> getRogainingByLatLon(double lat, double lon) async { Database db = await instance.database; var rog = await db.query('rog', where: "lat = ${lat} and lon= ${lon}"); List roglist = rog.isNotEmpty ? rog.map((e) => Rog.fromMap(e)).toList() : []; return roglist; } Future clearSelection() async { Database db = await instance.database; Map row_clear = { "selected": false }; return await db.update( "destination", row_clear ); } Future toggleSelecttion(Destination dest) async { Database db = await instance.database; bool val = !dest.selected!; Map row_target = { "selected": val }; await clearSelection(); return await db.update( "destination", row_target, where: 'location_id = ?', whereArgs: [dest.location_id!] ); } Future 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 deleteAllRogaining() async { Database db = await instance.database; await db.delete('rog'); } FutureisRogAlreadyAvailable(int id) async{ Database db = await instance.database; var rog = await db.query('rog', where: "id = ${id}"); return rog.length > 0 ? true : false; } Future insertRogaining(Rog rog) async { Database db = await instance.database; int? next_order = Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(id) FROM rog')); next_order = next_order==null ? 0 : next_order; next_order = next_order + 1; rog.id = next_order; int res = await db.insert( 'rog', rog.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); print("------ database helper insert ${res}-----------::::::::"); return res; } Future> getDestinations() async { Database db = await instance.database; var dest = await db.query('destination', orderBy: 'list_order'); List destList = dest.isNotEmpty ? dest.map((e) => Destination.fromMap(e)).toList() : []; print("--------- ${destList}"); return destList; } Future> getDestinationById(int id) async { Database db = await instance.database; var rog = await db.query('destination', where: "location_id = ${id}"); List deslist = rog.isNotEmpty ? rog.map((e) => Destination.fromMap(e)).toList() : []; return deslist; } Future> 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 destList = dest.isNotEmpty ? dest.map((e) => Destination.fromMap(e)).toList() : []; return destList; } Future deleteDestination(int location_id) async { Database db = await instance.database; var dest = await db.delete('destination', where: "location_id = ${location_id}"); int ret = dest > 0 ? dest : -1; //after deleting set correct order await setOrder(); return ret; } FuturesetOrder() async { Database db = await instance.database; var byOrder = await db.query('destination', orderBy: 'list_order'); List des_db = byOrder.isNotEmpty ? byOrder.map((e) => Destination.fromMap(e)).toList() : []; int order = 1; for( Destination d in des_db){ Map row_target = { "list_order": order }; await db.update( "destination", row_target, where: 'location_id = ?', whereArgs: [d.location_id] ); order += 1; } } Future deleteAllDestinations() async { Database db = await instance.database; await db.delete('destination'); } FutureisAlreadyAvailable(int location_id) async{ Database db = await instance.database; var dest = await db.query('destination', where: "location_id = ${location_id}"); return dest.length > 0 ? true : false; } Future insertDestination(Destination dest) async { Database db = await instance.database; int? next_order = Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(list_order) FROM destination')); next_order = next_order==null ? 0 : next_order; next_order = next_order + 1; dest.list_order = next_order; int res = await db.insert( 'destination', dest.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); //print("------ database helper insert ${dest.toMap()}-----------::::::::"); return res; } Future updateAction(Destination destination, bool checkin)async { Database db = await instance.database; int act = checkin == false ? 0 : 1; Map row = { "checkedin": act }; return await db.update( "destination", row, where: 'location_id = ?', whereArgs: [destination.location_id!] ); } Future 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}"); print("--- target in db is ${target}"); print("--- destine in db is ${dest}"); if(target.isNotEmpty){ List target_indb = target.isNotEmpty ? target.map((e) => Destination.fromMap(e)).toList() : []; List dest_indb = dest.isNotEmpty ? dest.map((e) => Destination.fromMap(e)).toList() : []; Map row_target = { "list_order": d.list_order }; Map row_des = { "list_order": dest_indb[0].list_order! + dir }; // print("--- target destination is ${target_indb[0].location_id}"); // print("--- destine destination is is ${dest_indb[0].location_id}"); await db.update( "destination", row_target, where: 'location_id = ?', whereArgs: [target_indb[0]!.location_id] ); await db.update( "destination", row_des, where: 'location_id = ?', whereArgs: [dest_indb[0]!.location_id] ); } } // Future getPending() async{ // Database db = await instance.database; // return await Sqflite.firstIntValue(await db.rawQuery("SELECT COUNT(*) FROM incidents")); // } }