Skip to content

Commit

Permalink
feat(task): cover basis for editing tasks
Browse files Browse the repository at this point in the history
update/delete tasks and insert/delete sub-tasks

closes #9
  • Loading branch information
rafoolin committed Aug 5, 2022
1 parent 218a690 commit e3e955d
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 151 deletions.
2 changes: 1 addition & 1 deletion app/lib/src/common/models/converters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ class TimeZConverter extends JsonConverter<TimeOfDay, String> {

@override
String toJson(TimeOfDay object) {
return object.toString();
return '${object.hour}:${object.minute}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class TaskService {
TaskService(this._remoteTaskRepository);

Stream<AsyncValue<TodoTask>> watchTask() {
return _remoteTaskRepository.watchTask();
}

Stream<AsyncValue<TodoTask>> subscribeTask() {
return _remoteTaskRepository.subscribeTask();
}

Expand All @@ -29,14 +33,16 @@ class TaskService {
String? categoryId,
DateTime? dueDatetime,
String? note,
List<TodoSubTask>? subTasks,
List<TodoSubTask> addedSubTasks = const [],
List<TodoSubTask> removedSubTasks = const [],
}) async {
return _remoteTaskRepository.editTask(
categoryId: categoryId,
dueDatetime: dueDatetime,
title: title,
note: note,
subTasks: subTasks,
addedSubTasks: addedSubTasks,
removedSubTasks: removedSubTasks,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RemoteTaskRepository {
createdAt
createdAt
completedAt
sub_tasks (order_by: {dueAt: asc}){
sub_tasks (order_by: {title: asc}){
__typename
id
title
Expand Down Expand Up @@ -76,7 +76,7 @@ class RemoteTaskRepository {
createdAt
createdAt
completedAt
sub_tasks (order_by: {dueAt: asc}){
sub_tasks (order_by: {title: asc}){
__typename
id
title
Expand All @@ -93,6 +93,7 @@ class RemoteTaskRepository {

return _qlClient
.watchQuery(WatchQueryOptions(
fetchResults: true,
document: gql(query),
variables: {'id': taskId},
fetchPolicy: FetchPolicy.cacheAndNetwork,
Expand Down Expand Up @@ -167,23 +168,29 @@ class RemoteTaskRepository {
required String? categoryId,
required DateTime? dueDatetime,
String? note,
List<TodoSubTask>? subTasks,
List<TodoSubTask> addedSubTasks = const [],
List<TodoSubTask> removedSubTasks = const [],
}) async {
const query = '''mutation (\$id: uuid!, \$set: tasks_set_input!,
\$sub_tasks: [sub_tasks_insert_input!] = []) {
update_tasks_by_pk(pk_columns: {id: \$id}, _set: \$set) {
__typename
completed
}
insert_sub_tasks(objects: \$sub_tasks,
on_conflict: {constraint: sub_tasks_pkey, update_columns: [title,
completed, dueAt, note]}) {
affected_rows
}
}''';
\$added_sub_tasks: [sub_tasks_insert_input!] = [], \$removed_ids: [uuid!] = []) {
update_tasks_by_pk(pk_columns: {id: \$id}, _set: \$set) {
__typename
completed
}
delete_sub_tasks(where: {id: {_in: \$removed_ids}}) {
affected_rows
}
insert_sub_tasks(objects: \$added_sub_tasks,
on_conflict: {constraint: sub_tasks_pkey,
update_columns: [title, completed, dueAt, note]}) {
affected_rows
}
}
''';

await _qlClient.mutate(MutationOptions(
document: gql(query),
fetchPolicy: FetchPolicy.cacheAndNetwork,
variables: {
'id': taskId,
'set': {
Expand All @@ -192,10 +199,18 @@ class RemoteTaskRepository {
if (note != null) 'note': note,
if (title != null) 'title': title,
},
if (subTasks != null)
'subTasks': subTasks.map((s) => s.toJson()).toList(),
if (addedSubTasks.isNotEmpty)
'added_sub_tasks': addedSubTasks
.map(
(s) => {
'title': s.title,
'taskId': s.taskId,
},
)
.toList(),
if (removedSubTasks.isNotEmpty)
'removed_ids': removedSubTasks.map((s) => s.id).toList(),
},
fetchPolicy: FetchPolicy.cacheAndNetwork,
));
}
}
14 changes: 10 additions & 4 deletions app/lib/src/features/task_management/domain/edit_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class EditTask with _$EditTask {
String? categoryId,
DateTime? dueDatetime,
String? note,
List<TodoSubTask>? subTasks,
@Default([]) List<TodoSubTask> addedSubTasks,
@Default([]) List<TodoSubTask> removedSubTasks,
@Default([]) List<TodoCategory>? categories,
}) = _EditTask;

Expand All @@ -26,15 +27,20 @@ class EditTask with _$EditTask {
categoryId: categoryId ?? initialTask!.categoryId,
dueDatetime: dueDatetime ?? initialTask!.dueDatetime,
note: note ?? initialTask!.note,
subTasks: [...(subTasks ?? []), ...initialTask?.subTasks ?? []]
..toSet().toList(),
subTasks: [
for (TodoSubTask subTask in [
...addedSubTasks,
...initialTask?.subTasks ?? const []
])
if (!removedSubTasks.contains(subTask)) subTask
],
title: title ?? initialTask!.title,
);

bool get changed =>
(title != null && title != initialTask?.title) ||
(dueDatetime != null && dueDatetime != initialTask?.dueDatetime) ||
(note != null && note != initialTask?.note) ||
(subTasks != null && subTasks != initialTask?.subTasks) ||
(addedSubTasks.isNotEmpty || removedSubTasks.isNotEmpty) ||
(categoryId != null && categoryId != initialTask?.categoryId);
}
79 changes: 54 additions & 25 deletions app/lib/src/features/task_management/domain/edit_task.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ mixin _$EditTask {
String? get categoryId => throw _privateConstructorUsedError;
DateTime? get dueDatetime => throw _privateConstructorUsedError;
String? get note => throw _privateConstructorUsedError;
List<TodoSubTask>? get subTasks => throw _privateConstructorUsedError;
List<TodoSubTask> get addedSubTasks => throw _privateConstructorUsedError;
List<TodoSubTask> get removedSubTasks => throw _privateConstructorUsedError;
List<TodoCategory>? get categories => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
Expand All @@ -39,7 +40,8 @@ abstract class $EditTaskCopyWith<$Res> {
String? categoryId,
DateTime? dueDatetime,
String? note,
List<TodoSubTask>? subTasks,
List<TodoSubTask> addedSubTasks,
List<TodoSubTask> removedSubTasks,
List<TodoCategory>? categories});

$TodoTaskCopyWith<$Res>? get initialTask;
Expand All @@ -60,7 +62,8 @@ class _$EditTaskCopyWithImpl<$Res> implements $EditTaskCopyWith<$Res> {
Object? categoryId = freezed,
Object? dueDatetime = freezed,
Object? note = freezed,
Object? subTasks = freezed,
Object? addedSubTasks = freezed,
Object? removedSubTasks = freezed,
Object? categories = freezed,
}) {
return _then(_value.copyWith(
Expand All @@ -84,10 +87,14 @@ class _$EditTaskCopyWithImpl<$Res> implements $EditTaskCopyWith<$Res> {
? _value.note
: note // ignore: cast_nullable_to_non_nullable
as String?,
subTasks: subTasks == freezed
? _value.subTasks
: subTasks // ignore: cast_nullable_to_non_nullable
as List<TodoSubTask>?,
addedSubTasks: addedSubTasks == freezed
? _value.addedSubTasks
: addedSubTasks // ignore: cast_nullable_to_non_nullable
as List<TodoSubTask>,
removedSubTasks: removedSubTasks == freezed
? _value.removedSubTasks
: removedSubTasks // ignore: cast_nullable_to_non_nullable
as List<TodoSubTask>,
categories: categories == freezed
? _value.categories
: categories // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -119,7 +126,8 @@ abstract class _$$_EditTaskCopyWith<$Res> implements $EditTaskCopyWith<$Res> {
String? categoryId,
DateTime? dueDatetime,
String? note,
List<TodoSubTask>? subTasks,
List<TodoSubTask> addedSubTasks,
List<TodoSubTask> removedSubTasks,
List<TodoCategory>? categories});

@override
Expand All @@ -143,7 +151,8 @@ class __$$_EditTaskCopyWithImpl<$Res> extends _$EditTaskCopyWithImpl<$Res>
Object? categoryId = freezed,
Object? dueDatetime = freezed,
Object? note = freezed,
Object? subTasks = freezed,
Object? addedSubTasks = freezed,
Object? removedSubTasks = freezed,
Object? categories = freezed,
}) {
return _then(_$_EditTask(
Expand All @@ -167,10 +176,14 @@ class __$$_EditTaskCopyWithImpl<$Res> extends _$EditTaskCopyWithImpl<$Res>
? _value.note
: note // ignore: cast_nullable_to_non_nullable
as String?,
subTasks: subTasks == freezed
? _value._subTasks
: subTasks // ignore: cast_nullable_to_non_nullable
as List<TodoSubTask>?,
addedSubTasks: addedSubTasks == freezed
? _value._addedSubTasks
: addedSubTasks // ignore: cast_nullable_to_non_nullable
as List<TodoSubTask>,
removedSubTasks: removedSubTasks == freezed
? _value._removedSubTasks
: removedSubTasks // ignore: cast_nullable_to_non_nullable
as List<TodoSubTask>,
categories: categories == freezed
? _value._categories
: categories // ignore: cast_nullable_to_non_nullable
Expand All @@ -188,9 +201,11 @@ class _$_EditTask extends _EditTask {
this.categoryId,
this.dueDatetime,
this.note,
final List<TodoSubTask>? subTasks,
final List<TodoSubTask> addedSubTasks = const [],
final List<TodoSubTask> removedSubTasks = const [],
final List<TodoCategory>? categories = const []})
: _subTasks = subTasks,
: _addedSubTasks = addedSubTasks,
_removedSubTasks = removedSubTasks,
_categories = categories,
super._();

Expand All @@ -204,13 +219,20 @@ class _$_EditTask extends _EditTask {
final DateTime? dueDatetime;
@override
final String? note;
final List<TodoSubTask>? _subTasks;
final List<TodoSubTask> _addedSubTasks;
@override
List<TodoSubTask>? get subTasks {
final value = _subTasks;
if (value == null) return null;
@JsonKey()
List<TodoSubTask> get addedSubTasks {
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(value);
return EqualUnmodifiableListView(_addedSubTasks);
}

final List<TodoSubTask> _removedSubTasks;
@override
@JsonKey()
List<TodoSubTask> get removedSubTasks {
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_removedSubTasks);
}

final List<TodoCategory>? _categories;
Expand All @@ -225,7 +247,7 @@ class _$_EditTask extends _EditTask {

@override
String toString() {
return 'EditTask(initialTask: $initialTask, title: $title, categoryId: $categoryId, dueDatetime: $dueDatetime, note: $note, subTasks: $subTasks, categories: $categories)';
return 'EditTask(initialTask: $initialTask, title: $title, categoryId: $categoryId, dueDatetime: $dueDatetime, note: $note, addedSubTasks: $addedSubTasks, removedSubTasks: $removedSubTasks, categories: $categories)';
}

@override
Expand All @@ -241,7 +263,10 @@ class _$_EditTask extends _EditTask {
const DeepCollectionEquality()
.equals(other.dueDatetime, dueDatetime) &&
const DeepCollectionEquality().equals(other.note, note) &&
const DeepCollectionEquality().equals(other._subTasks, _subTasks) &&
const DeepCollectionEquality()
.equals(other._addedSubTasks, _addedSubTasks) &&
const DeepCollectionEquality()
.equals(other._removedSubTasks, _removedSubTasks) &&
const DeepCollectionEquality()
.equals(other._categories, _categories));
}
Expand All @@ -254,7 +279,8 @@ class _$_EditTask extends _EditTask {
const DeepCollectionEquality().hash(categoryId),
const DeepCollectionEquality().hash(dueDatetime),
const DeepCollectionEquality().hash(note),
const DeepCollectionEquality().hash(_subTasks),
const DeepCollectionEquality().hash(_addedSubTasks),
const DeepCollectionEquality().hash(_removedSubTasks),
const DeepCollectionEquality().hash(_categories));

@JsonKey(ignore: true)
Expand All @@ -270,7 +296,8 @@ abstract class _EditTask extends EditTask {
final String? categoryId,
final DateTime? dueDatetime,
final String? note,
final List<TodoSubTask>? subTasks,
final List<TodoSubTask> addedSubTasks,
final List<TodoSubTask> removedSubTasks,
final List<TodoCategory>? categories}) = _$_EditTask;
_EditTask._() : super._();

Expand All @@ -285,7 +312,9 @@ abstract class _EditTask extends EditTask {
@override
String? get note;
@override
List<TodoSubTask>? get subTasks;
List<TodoSubTask> get addedSubTasks;
@override
List<TodoSubTask> get removedSubTasks;
@override
List<TodoCategory>? get categories;
@override
Expand Down
20 changes: 16 additions & 4 deletions app/lib/src/features/task_management/domain/todo_sub_task.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:uuid/uuid.dart';

import '../../../common/models/converters.dart';

Expand All @@ -13,14 +14,14 @@ class TodoSubTask with _$TodoSubTask {
factory TodoSubTask({
required String id,
required String title,
required String userId,
required String taskId,
required DateTime createdAt,
@TimeZConverter() required TimeOfDay dueAt,
@Default(false) bool completed,
DateTime? createdAt,
@TimeZConverter() TimeOfDay? dueAt,
String? note,
int? indexValue,
DateTime? updatedAt,
DateTime? completedAt,
@Default(false) bool completed,
}) = _TodoSubTask;

TodoSubTask._();
Expand All @@ -29,6 +30,17 @@ class TodoSubTask with _$TodoSubTask {
factory TodoSubTask.fromJson(Map<String, dynamic> json) =>
_$TodoSubTaskFromJson(json);

factory TodoSubTask.fromTitle({
required String title,
required String taskId,
}) =>
TodoSubTask(
id: const Uuid().v4(),
title: title,
taskId: taskId,
dueAt: TimeOfDay.now(),
);

@override
Map<String, dynamic> toJson() => _$TodoSubTaskToJson(this);
}
Loading

0 comments on commit e3e955d

Please sign in to comment.