108 lines
3.2 KiB
Dart
108 lines
3.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:core';
|
|
import 'package:invoiceninja_flutter/.env.dart';
|
|
import 'package:built_collection/built_collection.dart';
|
|
import 'package:invoiceninja_flutter/constants.dart';
|
|
import 'package:invoiceninja_flutter/data/mock/mock_tasks.dart';
|
|
import 'package:invoiceninja_flutter/data/models/serializers.dart';
|
|
import 'package:invoiceninja_flutter/redux/app/app_state.dart';
|
|
import 'package:invoiceninja_flutter/data/models/models.dart';
|
|
import 'package:invoiceninja_flutter/data/web_client.dart';
|
|
|
|
class TaskRepository {
|
|
const TaskRepository({
|
|
this.webClient = const WebClient(),
|
|
});
|
|
|
|
final WebClient webClient;
|
|
|
|
Future<TaskEntity> loadItem(Credentials credentials, String entityId) async {
|
|
final dynamic response = await webClient.get(
|
|
'${credentials.url}/tasks/$entityId', credentials.token);
|
|
|
|
final TaskItemResponse taskResponse =
|
|
serializers.deserializeWith(TaskItemResponse.serializer, response);
|
|
|
|
return taskResponse.data;
|
|
}
|
|
|
|
Future<BuiltList<TaskEntity>> loadList(
|
|
Credentials credentials, int updatedAt) async {
|
|
String url = credentials.url + '/tasks?';
|
|
|
|
if (updatedAt > 0) {
|
|
url += '&updated_at=${updatedAt - kUpdatedAtBufferSeconds}';
|
|
}
|
|
|
|
dynamic response;
|
|
|
|
if (Config.DEMO_MODE) {
|
|
response = json.decode(kMockTasks);
|
|
} else {
|
|
response = await webClient.get(url, credentials.token);
|
|
}
|
|
|
|
final TaskListResponse taskResponse =
|
|
serializers.deserializeWith(TaskListResponse.serializer, response);
|
|
|
|
return taskResponse.data;
|
|
}
|
|
|
|
Future<List<TaskEntity>> bulkAction(
|
|
Credentials credentials, List<String> ids, EntityAction action) async {
|
|
dynamic response;
|
|
|
|
switch (action) {
|
|
case EntityAction.restore:
|
|
case EntityAction.archive:
|
|
case EntityAction.delete:
|
|
var url = credentials.url + '/tasks/bulk?include=activities';
|
|
if (action != null) {
|
|
url += '&action=' + action.toString();
|
|
}
|
|
response = await webClient.post(url, credentials.token,
|
|
data: json.encode(ids));
|
|
break;
|
|
default:
|
|
// Might have other actions in the future
|
|
break;
|
|
}
|
|
|
|
final TaskListResponse taskResponse =
|
|
serializers.deserializeWith(TaskListResponse.serializer, response);
|
|
|
|
return taskResponse.data.toList();
|
|
}
|
|
|
|
Future<TaskEntity> saveData(Credentials credentials, TaskEntity task,
|
|
[EntityAction action]) async {
|
|
// Workaround for API issue
|
|
if (task.isNew) {
|
|
task = task.rebuild((b) => b..id = null);
|
|
}
|
|
|
|
final data = serializers.serializeWith(TaskEntity.serializer, task);
|
|
|
|
dynamic response;
|
|
|
|
if (task.isNew) {
|
|
response = await webClient.post(
|
|
credentials.url + '/tasks', credentials.token,
|
|
data: json.encode(data));
|
|
} else {
|
|
var url = credentials.url + '/tasks/' + task.id.toString();
|
|
if (action != null) {
|
|
url += '?action=' + action.toString();
|
|
}
|
|
response =
|
|
await webClient.put(url, credentials.token, data: json.encode(data));
|
|
}
|
|
|
|
final TaskItemResponse taskResponse =
|
|
serializers.deserializeWith(TaskItemResponse.serializer, response);
|
|
|
|
return taskResponse.data;
|
|
}
|
|
}
|