From 18d0206c44016ef8a40a2e6a2afde2e65f0d7d9b Mon Sep 17 00:00:00 2001 From: dab246 Date: Wed, 8 Sep 2021 15:08:48 +0700 Subject: [PATCH] TF-48 Setup database manager for local data --- core/lib/core.dart | 11 +- .../data/local/config/database_config.dart | 5 + .../local/config/email_address_table.dart | 12 ++ core/lib/data/local/database_client.dart | 109 ++++++++++++++++++ core/lib/data/local/database_manager.dart | 16 +++ core/lib/data/model/data_source_type.dart | 5 + .../extensions/color_extension.dart | 3 + core/lib/presentation/utils/app_toast.dart | 35 ++++++ core/pubspec.yaml | 8 +- 9 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 core/lib/data/local/config/database_config.dart create mode 100644 core/lib/data/local/config/email_address_table.dart create mode 100644 core/lib/data/local/database_client.dart create mode 100644 core/lib/data/local/database_manager.dart create mode 100644 core/lib/data/model/data_source_type.dart create mode 100644 core/lib/presentation/utils/app_toast.dart diff --git a/core/lib/core.dart b/core/lib/core.dart index 4a170eb8e..d45341d83 100644 --- a/core/lib/core.dart +++ b/core/lib/core.dart @@ -13,7 +13,7 @@ export 'presentation/utils/theme_utils.dart'; export 'presentation/utils/responsive_utils.dart'; export 'presentation/utils/keyboard_utils.dart'; export 'presentation/utils/style_utils.dart'; -export 'presentation/extensions/list_extensions.dart'; +export 'presentation/utils/app_toast.dart'; // Views export 'presentation/views/text/slogan_builder.dart'; @@ -49,3 +49,12 @@ export 'presentation/state/app_state.dart'; // Validator 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'; \ No newline at end of file diff --git a/core/lib/data/local/config/database_config.dart b/core/lib/data/local/config/database_config.dart new file mode 100644 index 000000000..68e857ab2 --- /dev/null +++ b/core/lib/data/local/config/database_config.dart @@ -0,0 +1,5 @@ + +class DatabaseConfig { + static final String databaseName = 'TMailDatabase.db'; + static final int databaseVersion = 1; +} \ No newline at end of file diff --git a/core/lib/data/local/config/email_address_table.dart b/core/lib/data/local/config/email_address_table.dart new file mode 100644 index 000000000..84d13af90 --- /dev/null +++ b/core/lib/data/local/config/email_address_table.dart @@ -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 + )'''; +} \ No newline at end of file diff --git a/core/lib/data/local/database_client.dart b/core/lib/data/local/database_client.dart new file mode 100644 index 000000000..d640a9470 --- /dev/null +++ b/core/lib/data/local/database_client.dart @@ -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 get database async { + return _database ?? await _initDatabase(); + } + + Future get batch async { + return _batch ?? (await database).batch(); + } + + Future _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 insertData(String tableName, Map mapObject) async { + final db = await database; + return await db.insert(tableName, mapObject); + } + + Future deleteData(String tableName, String key, String value) async { + final db = await database; + return await db.delete( + tableName, + where: '$key = ?', + whereArgs: [value]); + } + + Future updateData(String tableName, String key, String value, Map mapObject) async { + final db = await database; + return await db.update( + tableName, + mapObject, + where: '$key = ?', + whereArgs: [value]); + } + + Future>> getData(String tableName, String key, String value) async { + final db = await database; + return await db.query( + tableName, + where: '$key = ?', + whereArgs: [value]); + } + + Future>> 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>> getListDataWithCondition(String tableName, String condition, List values, {int? limit, String? orderBy}) async { + final db = await database; + return await db.query( + tableName, + where: condition, + whereArgs: values, + limit: limit, + orderBy: orderBy); + } + + Future>> getDataBySql(String sql) async { + final db = await database; + return await db.rawQuery(sql); + } + + Future> insertMultipleData(String tableName, List?> mapObjects) async { + final batchInsert = await batch; + mapObjects.forEach((element) { + if (element != null) { + batchInsert.insert(tableName, element); + } + }); + return await batchInsert.commit(noResult: false, continueOnError: true); + } +} \ No newline at end of file diff --git a/core/lib/data/local/database_manager.dart b/core/lib/data/local/database_manager.dart new file mode 100644 index 000000000..1c2e6c2f7 --- /dev/null +++ b/core/lib/data/local/database_manager.dart @@ -0,0 +1,16 @@ + +abstract class DatabaseManager { + Future insertData(T newObject); + + Future insertMultipleData(List listObject); + + Future deleteData(String key); + + Future updateData(T newObject); + + Future getData(String key); + + Future> getListData({String? word, int? limit, String? orderBy}); + + Future> getListDataWithCondition(String condition, {String? word, int? limit, String? orderBy}); +} \ No newline at end of file diff --git a/core/lib/data/model/data_source_type.dart b/core/lib/data/model/data_source_type.dart new file mode 100644 index 000000000..1d273da48 --- /dev/null +++ b/core/lib/data/model/data_source_type.dart @@ -0,0 +1,5 @@ + +enum DataSourceType { + network, + local +} \ No newline at end of file diff --git a/core/lib/presentation/extensions/color_extension.dart b/core/lib/presentation/extensions/color_extension.dart index 8d5686439..47ce8140d 100644 --- a/core/lib/presentation/extensions/color_extension.dart +++ b/core/lib/presentation/extensions/color_extension.dart @@ -52,4 +52,7 @@ extension AppColor on Color { static const enableSendEmailButtonColor = Color(0xFF3840F7); static const disableSendEmailButtonColor = Color(0xFFACAFFF); static const borderLeftEmailContentColor = Color(0xFFEFEFEF); + static const toastBackgroundColor = Color(0xFFACAFFF); + static const toastSuccessBackgroundColor = Color(0xFF04AA11); + static const toastErrorBackgroundColor = Color(0xFFFF5858); } diff --git a/core/lib/presentation/utils/app_toast.dart b/core/lib/presentation/utils/app_toast.dart new file mode 100644 index 000000000..0dabca478 --- /dev/null +++ b/core/lib/presentation/utils/app_toast.dart @@ -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); + } +} diff --git a/core/pubspec.yaml b/core/pubspec.yaml index 53fd0ca38..a37ae8ed6 100644 --- a/core/pubspec.yaml +++ b/core/pubspec.yaml @@ -44,11 +44,15 @@ dependencies: html: 0.15.0 + # fluttertoast + fluttertoast: 8.0.8 + + # sqflite + sqflite: 2.0.0+4 + dev_dependencies: flutter_test: sdk: flutter - - build_runner: 2.0.5 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec