Skip to content

Commit

Permalink
Use riverpod in supabase-todolist-drift
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Feb 13, 2025
1 parent c0d44cb commit 5b25aaa
Show file tree
Hide file tree
Showing 33 changed files with 1,747 additions and 743 deletions.
2 changes: 2 additions & 0 deletions demos/supabase-todolist-drift/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/

# IntelliJ related
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ import 'dart:async';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:powersync/powersync.dart' as powersync;
import 'package:supabase_todolist_drift/attachments/queue.dart';
import 'package:supabase_todolist_drift/powersync.dart';

class TakePhotoWidget extends StatefulWidget {
class TakePhotoWidget extends ConsumerStatefulWidget {
final String todoId;
final CameraDescription camera;

const TakePhotoWidget(
{super.key, required this.todoId, required this.camera});

@override
State<StatefulWidget> createState() {
ConsumerState<TakePhotoWidget> createState() {
return _TakePhotoWidgetState();
}
}

class _TakePhotoWidgetState extends State<TakePhotoWidget> {
class _TakePhotoWidgetState extends ConsumerState<TakePhotoWidget> {
late CameraController _cameraController;
late Future<void> _initializeControllerFuture;

Expand Down Expand Up @@ -56,7 +57,7 @@ class _TakePhotoWidgetState extends State<TakePhotoWidget> {

int photoSize = await photo.length();

await appDb.addTodoPhoto(widget.todoId, photoId);
await ref.read(driftDatabase).addTodoPhoto(widget.todoId, photoId);
await attachmentQueue.saveFile(photoId, photoSize);
} catch (e) {
log.info('Error taking photo: $e');
Expand Down
42 changes: 19 additions & 23 deletions demos/supabase-todolist-drift/lib/database.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:drift/drift.dart';
import 'package:powersync/powersync.dart' show PowerSyncDatabase, uuid;
import 'package:drift_sqlite_async/drift_sqlite_async.dart';
import 'package:supabase_todolist_drift/powersync.dart';
import 'package:powersync/powersync.dart' show uuid;

part 'database.g.dart';

Expand Down Expand Up @@ -32,21 +30,21 @@ class ListItems extends Table {
TextColumn get ownerId => text().nullable().named('owner_id')();
}

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

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

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

@override
int get schemaVersion => 1;
Expand All @@ -57,13 +55,9 @@ class AppDatabase extends _$AppDatabase {
.watch();
}

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

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

Future<void> deleteList(ListItem list) async {
Expand All @@ -81,21 +75,23 @@ class AppDatabase extends _$AppDatabase {
await (delete(todoItems)..where((t) => t.id.equals(todo.id))).go();
}

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

Future<void> toggleTodo(TodoItem todo) async {
Future<void> toggleTodo(TodoItem todo, String? userId) 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())));
completedBy: Value(userId)));
} else {
await (update(todoItems)..where((t) => t.id.equals(todo.id))).write(
const TodoItemsCompanion(
Expand Down
Loading

0 comments on commit 5b25aaa

Please sign in to comment.