62 lines
1.8 KiB
Dart
62 lines
1.8 KiB
Dart
//ignore_for_file: constant_identifier_names
|
|
import 'dart:io';
|
|
|
|
import 'package:sk_base_mobile/models/task_model.dart';
|
|
import 'package:sk_base_mobile/util/util.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
import 'package:path/path.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
class DbHelper {
|
|
Database? _db;
|
|
Future<Database?> get db async {
|
|
if (_db != null) {
|
|
return _db;
|
|
}
|
|
final directory = Platform.isAndroid
|
|
? await getExternalStorageDirectory()
|
|
: await getApplicationSupportDirectory();
|
|
String path = join(directory!.path, 'db');
|
|
var db = await openDatabase(
|
|
path,
|
|
version: 1,
|
|
onCreate: (db, version) {
|
|
db.execute(
|
|
"CREATE TABLE Tasks(key TEXT PRIMARY KEY,title TEXT,category TEXT,description TEXT,image TEXT,date TEXT,startTime TEXT,periority TEXT,show TEXT,endTime TEXT,status,TEXT)");
|
|
},
|
|
);
|
|
return db;
|
|
}
|
|
|
|
Future<TaskModel> insert(TaskModel model) async {
|
|
var dbClient = await db;
|
|
dbClient!.insert('Tasks', model.toMap()).then((value) {});
|
|
return model;
|
|
}
|
|
|
|
Future<int> delete(String id, String table) async {
|
|
var dbClient = await db;
|
|
return await dbClient!
|
|
.delete(table, where: 'key = ?', whereArgs: [id]).then((value) {
|
|
SnackBarUtil().success(
|
|
'Deleted',
|
|
message: 'Task is removed successfully',
|
|
);
|
|
return value;
|
|
});
|
|
}
|
|
|
|
Future<int> update(String id, String key, String value) async {
|
|
var dbClient = await db;
|
|
return await dbClient!
|
|
.update('Tasks', {key: value}, where: 'key = ?', whereArgs: [id]);
|
|
}
|
|
|
|
Future<List<TaskModel>> getData() async {
|
|
var dbClient = await db;
|
|
final List<Map<String, Object?>> queryResult =
|
|
await dbClient!.query('Tasks');
|
|
return queryResult.map((e) => TaskModel.fromMap(e)).toList();
|
|
}
|
|
}
|