Initial Commit, finally got some time to start this
This commit is contained in:
21
lib/main.dart
Normal file
21
lib/main.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MainApp());
|
||||
}
|
||||
|
||||
class MainApp extends StatelessWidget {
|
||||
const MainApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: Text('Hello World!'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
10
lib/models/constants.dart
Normal file
10
lib/models/constants.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
const String dbName = 'notes.db';
|
||||
|
||||
const int dbVersionNumber = 1;
|
||||
|
||||
String tableNotes = 'Notes';
|
||||
|
||||
String columnId = '_id';
|
||||
String columnTitle = 'title';
|
||||
String columnContent = 'content';
|
||||
|
||||
11
lib/models/db/dbnote.dart
Normal file
11
lib/models/db/dbnote.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'package:cv/cv.dart';
|
||||
import 'package:simple_notes/models/db/dbrecord.dart';
|
||||
import 'package:simple_notes/models/constants.dart';
|
||||
|
||||
class DBNote extends DBRecord {
|
||||
final title = CvField<String>(columnTitle);
|
||||
final content = CvField<String>(columnContent);
|
||||
|
||||
@override
|
||||
List<CvField> get fields => [id, title, content];
|
||||
}
|
||||
6
lib/models/db/dbrecord.dart
Normal file
6
lib/models/db/dbrecord.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
import 'package:cv/cv.dart';
|
||||
import 'package:simple_notes/models/constants.dart';
|
||||
|
||||
abstract class DBRecord extends CvModelBase {
|
||||
final id = CvField<int>(columnId);
|
||||
}
|
||||
5
lib/providers/DatabaseProvider.dart
Normal file
5
lib/providers/DatabaseProvider.dart
Normal file
@@ -0,0 +1,5 @@
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:simple_notes/repositories/databaserepository.dart';
|
||||
|
||||
|
||||
|
||||
102
lib/repositories/databaserepository.dart
Normal file
102
lib/repositories/databaserepository.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'package:sqflite_common/sqlite_api.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
import 'package:simple_notes/models/db/dbnote.dart';
|
||||
import 'package:simple_notes/models/constants.dart';
|
||||
|
||||
class DatabaseRepository {
|
||||
final DatabaseFactory factory;
|
||||
Database? db;
|
||||
|
||||
DatabaseRepository(this.factory);
|
||||
|
||||
Future<void> open(String path) async {
|
||||
return _openPath(path);
|
||||
}
|
||||
|
||||
Future<void> saveNote(DatabaseExecutor? db, DBNote note) async {
|
||||
if (note.id.v != null) {
|
||||
await db!.update(
|
||||
tableNotes,
|
||||
note.toMap(),
|
||||
where: '$columnId = ?',
|
||||
whereArgs: <Object?>[note.id.v]
|
||||
);
|
||||
} else {
|
||||
note.id.v = await db!.insert(tableNotes, note.toMap());
|
||||
}
|
||||
}
|
||||
|
||||
Future<DBNote?> getNote(int? id) async {
|
||||
var list = (await db!.query(
|
||||
tableNotes,
|
||||
columns: [columnId, columnTitle, columnContent],
|
||||
where: "$columnId = ?",
|
||||
whereArgs: <Object?>[id]
|
||||
));
|
||||
|
||||
if(list.isNotEmpty) {
|
||||
return DBNote()..fromMap(list.first);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<List<DBNote>?> getNotes() async {
|
||||
var list = (await db!.query(
|
||||
tableNotes,
|
||||
columns: [columnId, columnTitle, columnContent]
|
||||
));
|
||||
|
||||
if(list.isNotEmpty) {
|
||||
return list.map((e) => DBNote()..fromMap(e)).toList();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> deleteNote(int? id) async {
|
||||
await db!.delete(
|
||||
tableNotes,
|
||||
where: "$columnId = ?",
|
||||
whereArgs: <Object?>[id]
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openPath(String path) async {
|
||||
db = await factory.openDatabase(
|
||||
path,
|
||||
options: OpenDatabaseOptions(
|
||||
version: dbVersionNumber,
|
||||
onCreate: (db, version) async {
|
||||
await _createDB(db);
|
||||
},
|
||||
onUpgrade: (db, oldVersion, newVersion) async {
|
||||
if (oldVersion < dbVersionNumber) {
|
||||
await _createDB(db);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createDB(Database db) async {
|
||||
await db.execute("DROP TABLE IF EXISTS $tableNotes");
|
||||
await db.execute(
|
||||
"CREATE TABLE $tableNotes($columnId INTEGER PRIMARY KEY, $columnTitle TEXT, $columnContent TEXT)"
|
||||
);
|
||||
|
||||
await saveNote(
|
||||
db,
|
||||
DBNote()
|
||||
..title.v = "Simple Title"
|
||||
..content.v = "Simple Content"
|
||||
);
|
||||
|
||||
await saveNote(
|
||||
db,
|
||||
DBNote()
|
||||
..title.v = "Welcome to the terrible notes app"
|
||||
..content.v = "Enter some notes\nSee if it actually loads\nwill this even work?"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user