This repository was archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks_remote_data_source.dart
More file actions
70 lines (55 loc) · 2.19 KB
/
tasks_remote_data_source.dart
File metadata and controls
70 lines (55 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import 'package:flutter_template/model/task/api/create_task.dart';
import 'package:flutter_template/model/task/api/create_task_group.dart';
import 'package:flutter_template/model/task/task.dart';
import 'package:flutter_template/model/task/task_group.dart';
import 'package:flutter_template/network/tasks_api_service.dart';
import 'tasks_data_source.dart';
/// Implementation of [TasksRepository] that uses [ApiService] to contact a remote server.
class TasksRemoteDataSource implements TasksDataSource {
final TasksApiService _apiService;
@override
final String userId;
TasksRemoteDataSource(this.userId, this._apiService);
@override
Future<void> completeTask(String taskId) => _apiService.completeTask(taskId);
@override
Future<void> reopenTask(String taskId) => _apiService.reopenTask(taskId);
@override
Future<List<TaskGroup>> getTaskGroups() => _apiService.getTaskGroups();
@override
Future<Task> getTask(String taskId) => _apiService.getTask(taskId);
@override
Future<List<Task>> getTasks(String taskGroupId) =>
_apiService.getTasks(taskGroupId);
@override
Future<Map<TaskGroup, List<Task>>> getAllTasksGrouped() {
return getTaskGroups()
.asStream()
.expand((taskGroups) => taskGroups)
.asyncMap((taskGroup) {
return getTasks(taskGroup.id)
.asStream()
.map((taskListForGroup) => new MapEntry(taskGroup, taskListForGroup))
.first;
}).fold<Map<TaskGroup, List<Task>>>(new Map<TaskGroup, List<Task>>(),
(resultMap, entry) {
resultMap.putIfAbsent(entry.key, () => entry.value);
return resultMap;
});
}
@override
Future<Task> createTask(Task createTask) =>
_apiService.createTask(CreateTask.fromTask(createTask));
@override
Future<TaskGroup> createTaskGroup(TaskGroup ctg) =>
_apiService.createTaskGroup(CreateTaskGroup.fromTaskGroup(ctg));
@override
Future<void> deleteAllTaskGroups() => _apiService.deleteAllTaskGroups();
@override
Future<void> deleteAllData() {
throw UnsupportedError('Only cached data can be cleared');
}
@override
Future<TaskGroup> updateTaskGroup(final TaskGroup taskGroup) =>
_apiService.updateTaskGroup(taskGroup);
}