-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2879971
commit c299179
Showing
10 changed files
with
534 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:todo/shared/database/database.dart'; | ||
|
||
class TodoLocalDataSource { | ||
TodoLocalDataSource({required AppDatabase database}) : _database = database; | ||
final AppDatabase _database; | ||
|
||
Stream<List<TodoEntity>> listenAll() { | ||
final query = _database.select(_database.todoEntities); | ||
|
||
return query.watch(); | ||
} | ||
|
||
Future<void> add(String text) async { | ||
final insert = TodoEntitiesCompanion.insert( | ||
title: text, | ||
); | ||
await _database.into(_database.todoEntities).insert(insert); | ||
} | ||
|
||
Future<void> remove(TodoEntity todo) async { | ||
final query = _database.delete(_database.todoEntities) | ||
..where((tbl) => tbl.id.equals(todo.id)); | ||
await query.go(); | ||
} | ||
|
||
Future<void> update(TodoEntity todo) async { | ||
final query = _database.update(_database.todoEntities) | ||
..where((tbl) => tbl.id.equals(todo.id)); | ||
|
||
await query.write(todo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:todo/todo.dart'; | ||
import 'package:todo/features/todo/todo.dart'; | ||
import 'package:todo/features/todo/todo_local_data_source.dart'; | ||
import 'package:todo/shared/database/database.dart'; | ||
|
||
class TodoRepository { | ||
TodoRepository(); | ||
TodoRepository({required TodoLocalDataSource todoLocalDataSource}) | ||
: _todoLocalDataSource = todoLocalDataSource; | ||
|
||
// this should live in a server or local storage and we should provide a listenable approach to this item | ||
// ValueNotifier here is just an example but most storage solutions and packages provide a reactive way to get data. | ||
final ValueNotifier<List<Todo>> todos = ValueNotifier([]); | ||
final TodoLocalDataSource _todoLocalDataSource; | ||
|
||
void addTodo(Todo todo) { | ||
todos.value = [...todos.value, todo]; | ||
Stream<List<Todo>> listenAll() { | ||
return _todoLocalDataSource.listenAll().map((todos) { | ||
return todos.map((todo) { | ||
return todo.toTodo(); | ||
}).toList(); | ||
}); | ||
} | ||
|
||
void removeTodo(Todo todo) { | ||
todos.value = todos.value.where((element) => element != todo).toList(); | ||
Future<void> addTodo({required String title}) async { | ||
await _todoLocalDataSource.add(title); | ||
} | ||
|
||
void toggleDone(Todo todo) { | ||
todos.value = todos.value.map((oldTodo) { | ||
if (oldTodo == todo) { | ||
return oldTodo.copyWith(completed: !oldTodo.completed); | ||
} | ||
return oldTodo; | ||
}).toList(); | ||
Future<void> removeTodo(Todo todo) async { | ||
await _todoLocalDataSource.remove(todo.toDatabase()); | ||
} | ||
|
||
Future<void> toggleDone(Todo todo) async { | ||
final toggledTodo = todo.copyWith(completed: !todo.completed); | ||
await _todoLocalDataSource.update(toggledTodo.toDatabase()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:drift/drift.dart'; | ||
import 'package:drift_flutter/drift_flutter.dart'; | ||
import 'package:todo/features/todo/todo.dart'; | ||
import 'package:uuid/uuid.dart'; | ||
|
||
part 'database.g.dart'; | ||
|
||
const _uuid = Uuid(); | ||
|
||
extension TodoItemX on TodoEntity { | ||
Todo toTodo() { | ||
return Todo( | ||
id: id, | ||
title: title, | ||
completed: completed, | ||
); | ||
} | ||
} | ||
|
||
class TodoEntities extends Table { | ||
TextColumn get id => text().clientDefault(() => _uuid.v4())(); | ||
TextColumn get title => text()(); | ||
BoolColumn get completed => boolean().withDefault(const Constant(false))(); | ||
} | ||
|
||
@DriftDatabase(tables: [ | ||
TodoEntities, | ||
]) | ||
class AppDatabase extends _$AppDatabase { | ||
AppDatabase() : super(_openConnection()); | ||
|
||
@override | ||
int get schemaVersion => 1; | ||
|
||
static QueryExecutor _openConnection() { | ||
return driftDatabase(name: 'database'); | ||
} | ||
} |
Oops, something went wrong.