diff options
author | Marvin Borner | 2018-07-20 18:14:18 +0200 |
---|---|---|
committer | Marvin Borner | 2018-07-20 18:14:18 +0200 |
commit | 4263997c3e419ef1cf7447d447c5582a322acf74 (patch) | |
tree | d6b5a7c836ba74d1a57f3e6b1a8f372064864c13 /lib/data/database_helper.dart | |
parent | 18b8bd888a13c6dadfb7961cc88214332b6f5d38 (diff) |
Began basic login screen feature (mvp architecture)
Diffstat (limited to 'lib/data/database_helper.dart')
-rw-r--r-- | lib/data/database_helper.dart | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/data/database_helper.dart b/lib/data/database_helper.dart new file mode 100644 index 0000000..da65dcc --- /dev/null +++ b/lib/data/database_helper.dart @@ -0,0 +1,54 @@ +import 'dart:async'; +import 'dart:io' as io; + +import 'package:path/path.dart'; +import 'package:beam_messenger/models/user.dart'; +import 'package:sqflite/sqflite.dart'; +import 'package:path_provider/path_provider.dart'; + +class DatabaseHelper { + static final DatabaseHelper _instance = new DatabaseHelper.internal(); + factory DatabaseHelper() => _instance; + + static Database _db; + + Future<Database> get db async { + if (_db != null) return _db; + _db = await initDb(); + return _db; + } + + DatabaseHelper.internal(); + + initDb() async { + io.Directory documentsDirectory = await getApplicationDocumentsDirectory(); + String path = join(documentsDirectory.path, "main.db"); + var theDb = await openDatabase(path, version: 1, onCreate: _onCreate); + return theDb; + } + + void _onCreate(Database db, int version) async { + // When creating the db, create the table + await db.execute( + "CREATE TABLE User(id INTEGER PRIMARY KEY, email TEXT, password TEXT)"); + print("Created tables"); + } + + Future<int> saveUser(User user) async { + var dbClient = await db; + int res = await dbClient.insert("User", user.toMap()); + return res; + } + + Future<int> deleteUsers() async { + var dbClient = await db; + int res = await dbClient.delete("User"); + return res; + } + + Future<bool> isLoggedIn() async { + var dbClient = await db; + var res = await dbClient.query("User"); + return res.length > 0 ? true : false; + } +} |