Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drift Example #60

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.g.dart linguist-generated
6 changes: 6 additions & 0 deletions demos/supabase-todolist/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
targets:
$default:
builders:
drift_dev:
options:
store_date_time_values_as_text: true
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:powersync/powersync.dart' as powersync;
import 'package:powersync_flutter_demo/attachments/queue.dart';
import 'package:powersync_flutter_demo/models/todo_item.dart';
import 'package:powersync_flutter_demo/powersync.dart';

class TakePhotoWidget extends StatefulWidget {
Expand Down Expand Up @@ -43,7 +42,7 @@ class _TakePhotoWidgetState extends State<TakePhotoWidget> {
super.dispose();
}

Future<void> _takePhoto(context) async {
Future<void> _takePhoto(BuildContext context) async {
try {
// Ensure the camera is initialized before taking a photo
await _initializeControllerFuture;
Expand All @@ -57,13 +56,14 @@ class _TakePhotoWidgetState extends State<TakePhotoWidget> {

int photoSize = await photo.length();

TodoItem.addPhoto(photoId, widget.todoId);
attachmentQueue.savePhoto(photoId, photoSize);
await appDb.addTodoPhoto(widget.todoId, photoId);
await attachmentQueue.savePhoto(photoId, photoSize);
} catch (e) {
log.info('Error taking photo: $e');
}

// After taking the photo, navigate back to the previous screen
if (!context.mounted) return;
Navigator.pop(context);
}

Expand Down
3 changes: 1 addition & 2 deletions demos/supabase-todolist/lib/attachments/photo_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import 'package:flutter/material.dart';
import 'package:powersync_flutter_demo/attachments/camera_helpers.dart';
import 'package:powersync_flutter_demo/attachments/photo_capture_widget.dart';
import 'package:powersync_flutter_demo/attachments/queue.dart';

import '../models/todo_item.dart';
import 'package:powersync_flutter_demo/database.dart';

class PhotoWidget extends StatefulWidget {
final TodoItem todo;
Expand Down
116 changes: 116 additions & 0 deletions demos/supabase-todolist/lib/database.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import 'package:drift/drift.dart';
import 'package:powersync/powersync.dart' show uuid, PowerSyncDatabase;
import 'package:drift_sqlite_async/drift_sqlite_async.dart';
import 'package:powersync_flutter_demo/powersync.dart';

part 'database.g.dart';

class TodoItems extends Table {
@override
String get tableName => 'todos';

TextColumn get id => text().clientDefault(() => uuid.v4())();
TextColumn get listId => text().named('list_id').references(ListItems, #id)();
TextColumn get photoId => text().nullable().named('photo_id')();
DateTimeColumn get createdAt => dateTime().nullable().named('created_at')();
DateTimeColumn get completedAt =>
dateTime().nullable().named('completed_at')();
BoolColumn get completed => boolean().nullable()();
TextColumn get description => text()();
TextColumn get createdBy => text().nullable().named('created_by')();
TextColumn get completedBy => text().nullable().named('completed_by')();
}

class ListItems extends Table {
@override
String get tableName => 'lists';

TextColumn get id => text().clientDefault(() => uuid.v4())();
DateTimeColumn get createdAt =>
dateTime().named('created_at').clientDefault(() => DateTime.now())();
TextColumn get name => text()();
TextColumn get ownerId => text().nullable().named('owner_id')();
}

class ListItemWithStats {
late ListItem self;
int completedCount;
int pendingCount;

ListItemWithStats(
this.self,
this.completedCount,
this.pendingCount,
);
}

@DriftDatabase(tables: [TodoItems, ListItems], include: {'queries.drift'})
class AppDatabase extends _$AppDatabase {
AppDatabase(PowerSyncDatabase db) : super(SqliteAsyncDriftConnection(db));

@override
int get schemaVersion => 1;

Stream<List<ListItem>> watchLists() {
return (select(listItems)
..orderBy([(l) => OrderingTerm(expression: l.createdAt)]))
.watch();
}

Stream<List<ListItemWithStats>> watchListsWithStats() {
return listsWithStats().watch();
}

Future<ListItem> createList(String name) async {
return into(listItems).insertReturning(
ListItemsCompanion.insert(name: name, ownerId: Value(getUserId())));
}

Future<void> deleteList(ListItem list) async {
await (delete(listItems)..where((t) => t.id.equals(list.id))).go();
}

Stream<List<TodoItem>> watchTodoItems(ListItem list) {
return (select(todoItems)
..where((t) => t.listId.equals(list.id))
..orderBy([(t) => OrderingTerm(expression: t.createdAt)]))
.watch();
}

Future<void> deleteTodo(TodoItem todo) async {
await (delete(todoItems)..where((t) => t.id.equals(todo.id))).go();
}

Future<TodoItem> addTodo(ListItem list, String description) async {
return into(todoItems).insertReturning(TodoItemsCompanion.insert(
listId: list.id,
description: description,
completed: const Value(false),
createdBy: Value(getUserId())));
}

Future<void> toggleTodo(TodoItem todo) async {
if (todo.completed != true) {
await (update(todoItems)..where((t) => t.id.equals(todo.id))).write(
TodoItemsCompanion(
completed: const Value(true),
completedAt: Value(DateTime.now()),
completedBy: Value(getUserId())));
} else {
await (update(todoItems)..where((t) => t.id.equals(todo.id))).write(
const TodoItemsCompanion(
completed: Value(false),
completedAt: Value.absent(),
completedBy: Value.absent()));
}
}

Future<void> addTodoPhoto(String todoId, String photoId) async {
await (update(todoItems)..where((t) => t.id.equals(todoId)))
.write(TodoItemsCompanion(photoId: Value(photoId)));
}

Future<ListItem> findList(String id) {
return (select(listItems)..where((t) => t.id.equals(id))).getSingle();
}
}
Loading
Loading