integrated with previous
This commit is contained in:
82
lib/utils/checkin_db_helper.dart
Normal file
82
lib/utils/checkin_db_helper.dart
Normal file
@ -0,0 +1,82 @@
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart';
|
||||
|
||||
class CheckinDBHelper {
|
||||
static final _databaseName = "EventDatabase.db";
|
||||
static final _databaseVersion = 1;
|
||||
|
||||
static final table = 'event_table';
|
||||
|
||||
static final columnLocationId = 'location_id';
|
||||
static final columnOrderId = 'order_id';
|
||||
static final columnCheckinTime = 'checkin_time';
|
||||
static final columnEventName = 'event_name';
|
||||
|
||||
// make this a singleton class
|
||||
CheckinDBHelper._privateConstructor();
|
||||
static final CheckinDBHelper instance = CheckinDBHelper._privateConstructor();
|
||||
|
||||
// only have a single app-wide reference to the database
|
||||
static Database? _database;
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database!;
|
||||
_database = await _initDatabase();
|
||||
return _database!;
|
||||
}
|
||||
|
||||
// this opens the database (and creates it if it doesn't exist)
|
||||
_initDatabase() async {
|
||||
String path = join(await getDatabasesPath(), _databaseName);
|
||||
return await openDatabase(path,
|
||||
version: _databaseVersion,
|
||||
onCreate: _onCreate);
|
||||
}
|
||||
|
||||
// SQL code to create the database table
|
||||
Future _onCreate(Database db, int version) async {
|
||||
await db.execute('''
|
||||
CREATE TABLE $table (
|
||||
$columnLocationId INTEGER PRIMARY KEY,
|
||||
$columnOrderId INTEGER NOT NULL,
|
||||
$columnCheckinTime TEXT NOT NULL,
|
||||
$columnEventName TEXT NOT NULL
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
Future<int> insert(Map<String, dynamic> row) async {
|
||||
Database db = await instance.database;
|
||||
return await db.insert(table, row);
|
||||
}
|
||||
|
||||
Future<bool> exists(int locationId) async {
|
||||
Database db = await instance.database;
|
||||
List<Map<String, dynamic>> rows = await db.query(
|
||||
table,
|
||||
where: '$columnLocationId = ?',
|
||||
whereArgs: [locationId],
|
||||
);
|
||||
return rows.isNotEmpty;
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> queryAllRows() async {
|
||||
Database db = await instance.database;
|
||||
return await db.query(table);
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> queryRowsByLocation(int locationId) async {
|
||||
Database db = await instance.database;
|
||||
return await db.query(table,
|
||||
where: '$columnLocationId = ?',
|
||||
whereArgs: [locationId],
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> update(Map<String, dynamic> row) async {
|
||||
Database db = await instance.database;
|
||||
int id = row[columnLocationId];
|
||||
return await db.update(table, row, where: '$columnLocationId = ?', whereArgs: [id]);
|
||||
}
|
||||
}
|
||||
170
lib/utils/db_helper.dart
Normal file
170
lib/utils/db_helper.dart
Normal file
@ -0,0 +1,170 @@
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:rogapp/model/location_response.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'dart:io';
|
||||
import 'dart:convert'; // for jsonEncode
|
||||
|
||||
class DatabaseHelper {
|
||||
static const _databaseName = "FeatureDatabase.db";
|
||||
static const _databaseVersion = 1;
|
||||
|
||||
// Singleton class
|
||||
DatabaseHelper._();
|
||||
static final DatabaseHelper instance = DatabaseHelper._();
|
||||
|
||||
Database? _database;
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database!;
|
||||
_database = await _initDatabase();
|
||||
return _database!;
|
||||
}
|
||||
|
||||
Future<Database> _initDatabase() async {
|
||||
Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
||||
String path = join(documentsDirectory.path, _databaseName);
|
||||
return await openDatabase(
|
||||
path,
|
||||
version: _databaseVersion,
|
||||
onCreate: _onCreate,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onCreate(Database db, int version) async {
|
||||
await db.execute('''
|
||||
CREATE TABLE features (
|
||||
id INTEGER PRIMARY KEY,
|
||||
geometry TEXT NOT NULL,
|
||||
properties TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
location_id,
|
||||
latitude REAL NOT NULL,
|
||||
longitude REAL NOT NULL
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
// Database insert operation
|
||||
Future<void> insertFeature(Map<String, dynamic> feature) async {
|
||||
// Get a reference to the database.
|
||||
final db = await database;
|
||||
|
||||
// Insert the feature into the correct table.
|
||||
await db.insert(
|
||||
'features',
|
||||
feature,
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
}
|
||||
|
||||
// Database get operation
|
||||
Future<List<Feature>> getFeatures() async {
|
||||
Database db = await database;
|
||||
final List<Map<String, dynamic>> maps = await db.query('features');
|
||||
|
||||
return List.generate(maps.length, (i) {
|
||||
return Feature(
|
||||
id: maps[i]['id'],
|
||||
geometry: Geometry.fromMap(jsonDecode(maps[i]['geometry'])),
|
||||
properties: Properties.fromMap(jsonDecode(maps[i]['properties'])),
|
||||
type: maps[i]['type'],
|
||||
location_id: Properties.fromMap(jsonDecode(maps[i]['properties'])).location_id,
|
||||
latitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][1],
|
||||
longitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][0]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Database delete operation
|
||||
Future<void> deleteFeature(int id) async {
|
||||
Database db = await database;
|
||||
await db.delete(
|
||||
'features',
|
||||
where: "id = ?",
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> deleteAll() async {
|
||||
// Get a reference to the database.
|
||||
final db = await database;
|
||||
|
||||
// Delete all rows.
|
||||
await db.rawDelete('DELETE FROM features');
|
||||
}
|
||||
|
||||
// Database search by location_id
|
||||
Future<List<Feature>> getFeatureByLocationId(int locationId) async {
|
||||
Database db = await database;
|
||||
final List<Map<String, dynamic>> maps = await db.query(
|
||||
'features',
|
||||
where: "location_id = ?",
|
||||
whereArgs: [locationId],
|
||||
);
|
||||
|
||||
return List.generate(maps.length, (i) {
|
||||
return Feature(
|
||||
id: maps[i]['id'],
|
||||
geometry: Geometry.fromMap(jsonDecode(maps[i]['geometry'])),
|
||||
properties: Properties.fromMap(jsonDecode(maps[i]['properties'])),
|
||||
type: maps[i]['type'],
|
||||
location_id: Properties.fromMap(jsonDecode(maps[i]['properties'])).location_id,
|
||||
latitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][1],
|
||||
longitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][0]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Database search by latitude and longitude
|
||||
Future<List<Feature>> getFeatureByLatLon(double latitude, double longitude) async {
|
||||
Database db = await database;
|
||||
final List<Map<String, dynamic>> maps = await db.query(
|
||||
'features',
|
||||
where: "latitude = ? AND longitude = ?",
|
||||
whereArgs: [latitude, longitude],
|
||||
);
|
||||
|
||||
return List.generate(maps.length, (i) {
|
||||
return Feature(
|
||||
id: maps[i]['id'],
|
||||
geometry: Geometry.fromMap(jsonDecode(maps[i]['geometry'])),
|
||||
properties: Properties.fromMap(jsonDecode(maps[i]['properties'])),
|
||||
type: maps[i]['type'],
|
||||
location_id: Properties.fromMap(jsonDecode(maps[i]['properties'])).location_id,
|
||||
latitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][1],
|
||||
longitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][0]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<Feature>> getFeaturesWithinBounds(LatLngBounds bounds) async {
|
||||
final db = await database;
|
||||
|
||||
final List<Map<String, dynamic>> maps = await db.query(
|
||||
'features',
|
||||
where: 'latitude BETWEEN ? AND ? AND longitude BETWEEN ? AND ?',
|
||||
whereArgs: [
|
||||
bounds.southWest!.latitude,
|
||||
bounds.northEast!.latitude,
|
||||
bounds.southWest!.longitude,
|
||||
bounds.northEast!.longitude,
|
||||
],
|
||||
);
|
||||
|
||||
return List.generate(maps.length, (i) {
|
||||
return Feature(
|
||||
id: maps[i]['id'],
|
||||
geometry: Geometry.fromMap(jsonDecode(maps[i]['geometry'])),
|
||||
properties: Properties.fromMap(jsonDecode(maps[i]['properties'])),
|
||||
type: maps[i]['type'],
|
||||
location_id: Properties.fromMap(jsonDecode(maps[i]['properties'])).location_id,
|
||||
latitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][1],
|
||||
longitude: Geometry.fromMap(jsonDecode(maps[i]['geometry'])).coordinates![0][0]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
|
||||
import 'package:geojson/geojson.dart';
|
||||
import 'package:rogapp/model/destination.dart';
|
||||
import 'package:rogapp/model/location_response.dart';
|
||||
|
||||
class TextUtils{
|
||||
|
||||
@ -18,22 +19,25 @@ class TextUtils{
|
||||
}
|
||||
|
||||
|
||||
static String getDisplayText(Destination dp){
|
||||
static String getDisplayText(Feature dp){
|
||||
RegExp regex = RegExp(r'([.]*0)(?!.*\d)');
|
||||
String txt = "";
|
||||
if(dp.cp! > 0){
|
||||
txt = "${dp.cp.toString().replaceAll(regex, '')}";
|
||||
if(dp.checkin_point != null && dp.checkin_point! > 0){
|
||||
txt = txt + "{${dp.checkin_point.toString().replaceAll(regex, '')}}";
|
||||
if(dp.properties!.cp! > 0){
|
||||
txt = "${dp.properties!.cp.toString().replaceAll(regex, '')}";
|
||||
if(dp.properties!.checkin_point != null && dp.properties!.checkin_point! > 0){
|
||||
txt = txt + "{${dp.properties!.checkin_point.toString().replaceAll(regex, '')}}";
|
||||
}
|
||||
if(dp.buy_point != null && dp.buy_point! > 0){
|
||||
print("^^^^^^^^^ ${dp.sub_loc_id}^^^^^^^^^^");
|
||||
txt = "#${dp.cp.toString().replaceAll(regex, '')}(${dp.checkin_point.toString().replaceAll(regex, '')}+${dp.buy_point.toString().replaceAll(regex, '')})";
|
||||
if(dp.properties!.buy_point != null && dp.properties!.buy_point! > 0){
|
||||
txt = "#${dp.properties!.cp.toString().replaceAll(regex, '')}(${dp.properties!.checkin_point.toString().replaceAll(regex, '')}+${dp.properties!.buy_point.toString().replaceAll(regex, '')})";
|
||||
}
|
||||
}
|
||||
return txt;
|
||||
}
|
||||
|
||||
static String getDisplayTextForFeature(Feature f){
|
||||
return "${f.properties!.sub_loc_id}";
|
||||
}
|
||||
|
||||
// static String getDisplayText(String num){
|
||||
// RegExp regex = RegExp(r'([.]*0)(?!.*\d)');
|
||||
// return "${num.replaceAll(regex, '')}";
|
||||
|
||||
Reference in New Issue
Block a user