Skip to content

Commit 53d3611

Browse files
committed
feat: delete media form list
1 parent af90312 commit 53d3611

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

lib/Api/Anilist/AnilistMutations.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import 'package:dartotsu/DataClass/Media.dart';
22
import 'package:dartotsu/Services/Api/Mutations.dart';
33
import 'package:flutter/cupertino.dart';
44

5+
import 'Data/data.dart';
6+
7+
58
part 'AnilistMutations/SetUserList.dart';
9+
part 'AnilistMutations/DeleteFromList.dart';
610

711
class AnilistMutations extends Mutations {
812
final Future<T?> Function<T>(
@@ -18,4 +22,8 @@ class AnilistMutations extends Mutations {
1822
@override
1923
Future<void> editList(Media media, {List<String>? customList}) =>
2024
_editList(media, customList: customList);
25+
26+
@override
27+
Future<void> deleteFromList(Media media) => _deleteFromList(media);
28+
2129
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
part of '../AnilistMutations.dart';
2+
3+
extension on AnilistMutations {
4+
Future<void> _deleteFromList(Media media) async {
5+
6+
if (media.userListId == null) {
7+
var id = await getMediaListId(media);
8+
if (id == null) {
9+
debugPrint('MediaListId not found for media: ${media.name}');
10+
return;
11+
}
12+
media.userListId = id;
13+
}
14+
var query = '''
15+
mutation(\$id: Int) {
16+
DeleteMediaListEntry(id: \$id) {
17+
deleted
18+
}
19+
}
20+
'''.trim();
21+
var variables = '{''"id": ${media.userListId}''}';
22+
await executeQuery<Map<String, dynamic>>(query, variables: variables);
23+
}
24+
Future<int?> getMediaListId(Media media) async {
25+
var query = '''{Media(id:${media.id}){id,mediaListEntry{id}}}''';
26+
var res = await executeQuery<MediaResponse>(query, useToken: true);
27+
return res?.data?.media?.mediaListEntry?.id;
28+
}
29+
}
30+

lib/Api/Anilist/AnilistMutations/SetUserList.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ extension on AnilistMutations {
6060
'${customList != null ? ',"customLists": [${customList.map((e) => '"$e"').join(', ')}]' : ''}'
6161
'}';
6262
debugPrint(variables);
63-
executeQuery<Map<String, dynamic>>(query, variables: variables, show: true);
63+
await executeQuery<Map<String, dynamic>>(query, variables: variables, show: true);
6464
}
6565
}

lib/Api/Anilist/ListEditor.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class _ListEditorDialogState extends State<ListEditorDialog> {
125125
positiveText: 'Save',
126126
positiveCallback: _onSave,
127127
negativeText: 'Delete',
128+
negativeCallback: _onDelete,
128129
);
129130
}
130131

@@ -388,13 +389,13 @@ class _ListEditorDialogState extends State<ListEditorDialog> {
388389
);
389390
}
390391

391-
void _onSave() {
392+
Future<void> _onSave() async {
393+
var score = (double.tryParse(scoreController.text));
394+
392395
widget.media
393396
..userStatus = status
394397
..userProgress = int.tryParse(progressController.text)
395-
..userScore = ((double.tryParse(scoreController.text) ?? 0) * 10)
396-
.toInt()
397-
.clamp(0, 100)
398+
..userScore = score != null ? (score * 10).toInt().clamp(0, 100) : null
398399
..isListPrivate = isPrivate;
399400

400401
List<String>? list;
@@ -410,9 +411,14 @@ class _ListEditorDialogState extends State<ListEditorDialog> {
410411
.map((entry) => entry.key)
411412
.toList();
412413
}
414+
Get.back();
415+
await Anilist.mutations?.editList(widget.media, customList: list);
416+
Refresh.activity[RefreshId.Anilist.homePage]?.value = true;
417+
}
413418

414-
Anilist.mutations?.editList(widget.media, customList: list);
419+
Future<void> _onDelete() async {
415420
Get.back();
421+
await Anilist.mutations?.deleteFromList(widget.media);
416422
Refresh.activity[RefreshId.Anilist.homePage]?.value = true;
417423
}
418424
}

lib/Services/Api/Mutations.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ abstract class Mutations {
66
Media media, {
77
List<String>? customList,
88
});
9+
10+
Future<void> deleteFromList(Media media);
911
}

0 commit comments

Comments
 (0)