-
-
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
a17540f
commit 31d3dea
Showing
7 changed files
with
69 additions
and
98 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 was deleted.
Oops, something went wrong.
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
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,48 +1,31 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:todo/fake_data_source.dart'; | ||
import 'package:todo/features/todo/todo.dart'; | ||
import 'package:todo/database.dart'; | ||
import 'package:todo/features/todo/todo_entity.dart'; | ||
import 'package:uuid/uuid.dart'; | ||
|
||
// Provide a uniform way for services and view models to interact with your data | ||
class TodoRepository { | ||
TodoRepository({required FakeDataSource fakeDataSource}) | ||
: _fakeDataSource = fakeDataSource; | ||
TodoRepository({required InMemoryDataSource inMemoryDataSource}) | ||
: _inMemoryDataSource = inMemoryDataSource; | ||
|
||
final FakeDataSource _fakeDataSource; | ||
final BehaviorSubject<List<Todo>> _todosController = BehaviorSubject([]); | ||
// This is an example but could be for example Firestore | ||
final InMemoryDataSource _inMemoryDataSource; | ||
|
||
Stream<List<Todo>> watch() { | ||
return _todosController.stream; | ||
Stream<List<TodoEntity>> watch() { | ||
return _inMemoryDataSource.stream; | ||
} | ||
|
||
Future<void> addTodo({required String title}) async { | ||
const uuid = Uuid(); | ||
final id = uuid.v4(); | ||
final todo = TodoEntity(id: id, title: title, completed: false); | ||
|
||
_fakeDataSource.add(); | ||
_todosController.add([..._todosController.value, todo.toTodo()]); | ||
_inMemoryDataSource.add(title: title); | ||
} | ||
|
||
Future<void> removeTodo(Todo todo) async { | ||
_fakeDataSource.remove(); | ||
|
||
_todosController | ||
.add(_todosController.value.where((t) => t.id != todo.id).toList()); | ||
Future<void> removeTodo(TodoEntity todo) async { | ||
_inMemoryDataSource.remove(todo); | ||
} | ||
|
||
Future<void> toggleDone(Todo todo) async { | ||
_fakeDataSource.update(); | ||
|
||
final updatedTodos = _todosController.value.map((t) { | ||
if (t.id == todo.id) { | ||
return Todo(id: t.id, title: t.title, completed: !t.completed); | ||
} | ||
return t; | ||
}).toList(); | ||
Future<void> toggleDone(TodoEntity todo) async { | ||
final updatedTodo = todo.copyWith(completed: !todo.completed); | ||
|
||
_todosController.add(updatedTodos); | ||
_inMemoryDataSource.update(updatedTodo); | ||
} | ||
} |
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