mirror of https://github.com/AMT-Cheif/drift.git
Finish web prototype
This commit is contained in:
parent
311a47c704
commit
97c068f0ba
|
@ -24,6 +24,22 @@ class Database extends _$Database {
|
||||||
@override
|
@override
|
||||||
final int schemaVersion = 1;
|
final int schemaVersion = 1;
|
||||||
|
|
||||||
|
@override
|
||||||
|
MigrationStrategy get migration {
|
||||||
|
return MigrationStrategy(
|
||||||
|
onCreate: (m) async => await m.createAllTables(),
|
||||||
|
beforeOpen: (engine, details) async {
|
||||||
|
if (details.wasCreated) {
|
||||||
|
// populate default data
|
||||||
|
await createTodoEntry('A simple todo list using moor web',
|
||||||
|
engine: engine);
|
||||||
|
await createTodoEntry('It even supports prepopulated data!',
|
||||||
|
engine: engine);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Stream<List<Entry>> incompleteEntries() {
|
Stream<List<Entry>> incompleteEntries() {
|
||||||
return (select(todoEntries)..where((e) => not(e.done))).watch();
|
return (select(todoEntries)..where((e) => not(e.done))).watch();
|
||||||
}
|
}
|
||||||
|
@ -37,11 +53,14 @@ class Database extends _$Database {
|
||||||
.watch();
|
.watch();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future createTodoEntry(String desc) {
|
Future createTodoEntry(String desc, {QueryEngine engine}) {
|
||||||
return into(todoEntries).insert(TodoEntriesCompanion(content: Value(desc)));
|
final resolved = engine ?? this;
|
||||||
|
return resolved
|
||||||
|
.into(todoEntries)
|
||||||
|
.insert(TodoEntriesCompanion(content: Value(desc)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future setCompleted(Entry entry, bool done) {
|
Future setCompleted(Entry entry, bool done) {
|
||||||
return update(todoEntries).write(entry.copyWith(done: done));
|
return update(todoEntries).replace(entry.copyWith(done: done));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,7 +123,7 @@ class $TodoEntriesTable extends TodoEntries
|
||||||
GeneratedBoolColumn get done => _done ??= _constructDone();
|
GeneratedBoolColumn get done => _done ??= _constructDone();
|
||||||
GeneratedBoolColumn _constructDone() {
|
GeneratedBoolColumn _constructDone() {
|
||||||
return GeneratedBoolColumn('done', $tableName, false,
|
return GeneratedBoolColumn('done', $tableName, false,
|
||||||
defaultValue: const Constant(true));
|
defaultValue: const Constant(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
|
@ -23,6 +23,7 @@ class _CreateEntryBarState extends State<CreateEntryBar> {
|
||||||
Expanded(
|
Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: _controller,
|
controller: _controller,
|
||||||
|
onSubmitted: (_) => _submit(),
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
),
|
),
|
||||||
|
@ -30,14 +31,18 @@ class _CreateEntryBarState extends State<CreateEntryBar> {
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(Icons.add),
|
icon: Icon(Icons.add),
|
||||||
onPressed: () {
|
onPressed: _submit,
|
||||||
final text = _controller.text;
|
|
||||||
_controller.clear();
|
|
||||||
|
|
||||||
DatabaseProvider.provide(context).createTodoEntry(text);
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _submit() {
|
||||||
|
final text = _controller.text.trim();
|
||||||
|
_controller.clear();
|
||||||
|
|
||||||
|
if (text.isNotEmpty) {
|
||||||
|
DatabaseProvider.provide(context).createTodoEntry(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:html';
|
||||||
|
|
||||||
import 'package:example_web/main.dart';
|
import 'package:example_web/main.dart';
|
||||||
import 'package:example_web/widgets/entry_list.dart';
|
import 'package:example_web/widgets/entry_list.dart';
|
||||||
import 'package:flutter_web/material.dart';
|
import 'package:flutter_web/material.dart';
|
||||||
|
@ -13,6 +15,40 @@ class HomeScreen extends StatelessWidget {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Moor Web'),
|
title: const Text('Moor Web'),
|
||||||
|
actions: [
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(Icons.info_outline),
|
||||||
|
onPressed: () {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('Moor web'),
|
||||||
|
content: const Text(
|
||||||
|
'Simple demonstration of moor web: This whole page '
|
||||||
|
'is a offline-capable Flutter app using sql.js to '
|
||||||
|
'store data!'),
|
||||||
|
actions: <Widget>[
|
||||||
|
FlatButton(
|
||||||
|
child: const Text('Moor'),
|
||||||
|
onPressed: () {
|
||||||
|
window.open('https://moor.simonbinder.eu/', 'Moor');
|
||||||
|
},
|
||||||
|
),
|
||||||
|
FlatButton(
|
||||||
|
child: const Text('sql.js'),
|
||||||
|
onPressed: () {
|
||||||
|
window.open(
|
||||||
|
'https://github.com/kripken/sql.js/', 'sql.js');
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 3.5 KiB |
|
@ -204,12 +204,19 @@ class WebDatabase extends _DatabaseUser {
|
||||||
);
|
);
|
||||||
db.close();
|
db.close();
|
||||||
|
|
||||||
|
if (version < 1) {
|
||||||
|
// assume version code 0 (default) to be null. Other parts of moor
|
||||||
|
// interpret a null version code as "the database was just created", so it
|
||||||
|
// fits.
|
||||||
|
version = null;
|
||||||
|
}
|
||||||
|
|
||||||
final module = await initSqlJs();
|
final module = await initSqlJs();
|
||||||
final restored = _restoreDb();
|
final restored = _restoreDb();
|
||||||
_state.db = module.createDatabase(restored);
|
_state.db = module.createDatabase(restored);
|
||||||
|
|
||||||
if (upgradeNeeded) {
|
if (upgradeNeeded) {
|
||||||
if (version == null || version < 1) {
|
if (version == null) {
|
||||||
await databaseInfo.handleDatabaseCreation(executor: _runWithoutArgs);
|
await databaseInfo.handleDatabaseCreation(executor: _runWithoutArgs);
|
||||||
} else {
|
} else {
|
||||||
await databaseInfo.handleDatabaseVersionChange(
|
await databaseInfo.handleDatabaseVersionChange(
|
||||||
|
|
Loading…
Reference in New Issue