TF-48 Setup database manager for local data
This commit is contained in:
+10
-1
@@ -13,7 +13,7 @@ export 'presentation/utils/theme_utils.dart';
|
|||||||
export 'presentation/utils/responsive_utils.dart';
|
export 'presentation/utils/responsive_utils.dart';
|
||||||
export 'presentation/utils/keyboard_utils.dart';
|
export 'presentation/utils/keyboard_utils.dart';
|
||||||
export 'presentation/utils/style_utils.dart';
|
export 'presentation/utils/style_utils.dart';
|
||||||
export 'presentation/extensions/list_extensions.dart';
|
export 'presentation/utils/app_toast.dart';
|
||||||
|
|
||||||
// Views
|
// Views
|
||||||
export 'presentation/views/text/slogan_builder.dart';
|
export 'presentation/views/text/slogan_builder.dart';
|
||||||
@@ -49,3 +49,12 @@ export 'presentation/state/app_state.dart';
|
|||||||
|
|
||||||
// Validator
|
// Validator
|
||||||
export 'presentation/validator/html_message_purifier.dart';
|
export 'presentation/validator/html_message_purifier.dart';
|
||||||
|
|
||||||
|
// Local
|
||||||
|
export 'data/local/config/database_config.dart';
|
||||||
|
export 'data/local/config/email_address_table.dart';
|
||||||
|
export 'data/local/database_client.dart';
|
||||||
|
export 'data/local/database_manager.dart';
|
||||||
|
|
||||||
|
// Model
|
||||||
|
export 'data/model/data_source_type.dart';
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
class DatabaseConfig {
|
||||||
|
static final String databaseName = 'TMailDatabase.db';
|
||||||
|
static final int databaseVersion = 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
class EmailAddressTable {
|
||||||
|
static const String TABLE_NAME = 'EmailAddress';
|
||||||
|
|
||||||
|
static const String EMAIL = 'email';
|
||||||
|
static const String NAME = 'name';
|
||||||
|
|
||||||
|
static const String CREATE = '''CREATE TABLE IF NOT EXISTS $TABLE_NAME (
|
||||||
|
$EMAIL TEXT PRIMARY KEY,
|
||||||
|
$NAME TEXT
|
||||||
|
)''';
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:core/data/local/config/database_config.dart';
|
||||||
|
import 'package:core/data/local/config/email_address_table.dart';
|
||||||
|
import 'package:path/path.dart';
|
||||||
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
|
class DatabaseClient {
|
||||||
|
|
||||||
|
Database? _database;
|
||||||
|
Batch? _batch;
|
||||||
|
|
||||||
|
Future<Database> get database async {
|
||||||
|
return _database ?? await _initDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Batch> get batch async {
|
||||||
|
return _batch ?? (await database).batch();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Database> _initDatabase() async {
|
||||||
|
final databasePath = await getDatabasesPath();
|
||||||
|
final path = join(databasePath, DatabaseConfig.databaseName);
|
||||||
|
return await openDatabase(
|
||||||
|
path,
|
||||||
|
version: DatabaseConfig.databaseVersion,
|
||||||
|
onOpen: (db) {},
|
||||||
|
onCreate: (db, version) async {
|
||||||
|
final batch = db.batch();
|
||||||
|
batch.execute(EmailAddressTable.CREATE);
|
||||||
|
await batch.commit();
|
||||||
|
},
|
||||||
|
onUpgrade: (db, oldVersion, newVersion) async {});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future closeDatabase() async {
|
||||||
|
final db = await database;
|
||||||
|
_database = null;
|
||||||
|
await db.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> insertData(String tableName, Map<String, dynamic> mapObject) async {
|
||||||
|
final db = await database;
|
||||||
|
return await db.insert(tableName, mapObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> deleteData(String tableName, String key, String value) async {
|
||||||
|
final db = await database;
|
||||||
|
return await db.delete(
|
||||||
|
tableName,
|
||||||
|
where: '$key = ?',
|
||||||
|
whereArgs: [value]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> updateData(String tableName, String key, String value, Map<String, dynamic> mapObject) async {
|
||||||
|
final db = await database;
|
||||||
|
return await db.update(
|
||||||
|
tableName,
|
||||||
|
mapObject,
|
||||||
|
where: '$key = ?',
|
||||||
|
whereArgs: [value]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Map<String, dynamic>>> getData(String tableName, String key, String value) async {
|
||||||
|
final db = await database;
|
||||||
|
return await db.query(
|
||||||
|
tableName,
|
||||||
|
where: '$key = ?',
|
||||||
|
whereArgs: [value]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Map<String, dynamic>>> getListData(String tableName, {int? limit, String? orderBy}) async {
|
||||||
|
final db = await database;
|
||||||
|
return await db.query(tableName, limit: limit, orderBy: orderBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future deleteLocalFile(String localPath) async {
|
||||||
|
final fileSaved = File(localPath);
|
||||||
|
final fileExist = await fileSaved.exists();
|
||||||
|
if (fileExist) {
|
||||||
|
await fileSaved.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Map<String, dynamic>>> getListDataWithCondition(String tableName, String condition, List<dynamic> values, {int? limit, String? orderBy}) async {
|
||||||
|
final db = await database;
|
||||||
|
return await db.query(
|
||||||
|
tableName,
|
||||||
|
where: condition,
|
||||||
|
whereArgs: values,
|
||||||
|
limit: limit,
|
||||||
|
orderBy: orderBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Map<String, dynamic>>> getDataBySql(String sql) async {
|
||||||
|
final db = await database;
|
||||||
|
return await db.rawQuery(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Object?>> insertMultipleData(String tableName, List<Map<String, dynamic>?> mapObjects) async {
|
||||||
|
final batchInsert = await batch;
|
||||||
|
mapObjects.forEach((element) {
|
||||||
|
if (element != null) {
|
||||||
|
batchInsert.insert(tableName, element);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return await batchInsert.commit(noResult: false, continueOnError: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
abstract class DatabaseManager<T> {
|
||||||
|
Future<bool> insertData(T newObject);
|
||||||
|
|
||||||
|
Future<bool> insertMultipleData(List<T> listObject);
|
||||||
|
|
||||||
|
Future<bool> deleteData(String key);
|
||||||
|
|
||||||
|
Future<bool> updateData(T newObject);
|
||||||
|
|
||||||
|
Future<T?> getData(String key);
|
||||||
|
|
||||||
|
Future<List<T>> getListData({String? word, int? limit, String? orderBy});
|
||||||
|
|
||||||
|
Future<List<T>> getListDataWithCondition(String condition, {String? word, int? limit, String? orderBy});
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
enum DataSourceType {
|
||||||
|
network,
|
||||||
|
local
|
||||||
|
}
|
||||||
@@ -52,4 +52,7 @@ extension AppColor on Color {
|
|||||||
static const enableSendEmailButtonColor = Color(0xFF3840F7);
|
static const enableSendEmailButtonColor = Color(0xFF3840F7);
|
||||||
static const disableSendEmailButtonColor = Color(0xFFACAFFF);
|
static const disableSendEmailButtonColor = Color(0xFFACAFFF);
|
||||||
static const borderLeftEmailContentColor = Color(0xFFEFEFEF);
|
static const borderLeftEmailContentColor = Color(0xFFEFEFEF);
|
||||||
|
static const toastBackgroundColor = Color(0xFFACAFFF);
|
||||||
|
static const toastSuccessBackgroundColor = Color(0xFF04AA11);
|
||||||
|
static const toastErrorBackgroundColor = Color(0xFFFF5858);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import 'package:core/presentation/extensions/color_extension.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:fluttertoast/fluttertoast.dart';
|
||||||
|
|
||||||
|
class AppToast {
|
||||||
|
void showToast(String message) {
|
||||||
|
Fluttertoast.showToast(
|
||||||
|
msg: message,
|
||||||
|
fontSize: 16,
|
||||||
|
textColor: Colors.white,
|
||||||
|
backgroundColor: AppColor.toastBackgroundColor,
|
||||||
|
toastLength: Toast.LENGTH_SHORT,
|
||||||
|
gravity: ToastGravity.BOTTOM);
|
||||||
|
}
|
||||||
|
|
||||||
|
void showSuccessToast(String message) {
|
||||||
|
Fluttertoast.showToast(
|
||||||
|
msg: message,
|
||||||
|
fontSize: 16,
|
||||||
|
textColor: Colors.white,
|
||||||
|
backgroundColor: AppColor.toastSuccessBackgroundColor,
|
||||||
|
toastLength: Toast.LENGTH_SHORT,
|
||||||
|
gravity: ToastGravity.BOTTOM);
|
||||||
|
}
|
||||||
|
|
||||||
|
void showErrorToast(String message) {
|
||||||
|
Fluttertoast.showToast(
|
||||||
|
msg: message,
|
||||||
|
fontSize: 16,
|
||||||
|
textColor: Colors.white,
|
||||||
|
backgroundColor: AppColor.toastErrorBackgroundColor,
|
||||||
|
toastLength: Toast.LENGTH_SHORT,
|
||||||
|
gravity: ToastGravity.BOTTOM);
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-2
@@ -44,11 +44,15 @@ dependencies:
|
|||||||
|
|
||||||
html: 0.15.0
|
html: 0.15.0
|
||||||
|
|
||||||
|
# fluttertoast
|
||||||
|
fluttertoast: 8.0.8
|
||||||
|
|
||||||
|
# sqflite
|
||||||
|
sqflite: 2.0.0+4
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
|
||||||
build_runner: 2.0.5
|
|
||||||
# For information on the generic Dart part of this file, see the
|
# For information on the generic Dart part of this file, see the
|
||||||
# following page: https://dart.dev/tools/pub/pubspec
|
# following page: https://dart.dev/tools/pub/pubspec
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user