Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Shift actions before server response to improve UX
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Nov 19, 2023
1 parent c78c00b commit a3c768a
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions lib/controllers/profile_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ class ProfileController {
return;
}

animesInWatchlist.add(anime.uuid);
anime.notify();

final Response? response = await URLController.instance.put(
'$baseUrl/watchlist?anime=${anime.uuid}',
token: profile?.token,
Expand All @@ -175,18 +178,20 @@ class ProfileController {
'Cannot add anime to watchlist, response: $response',
);

animesInWatchlist.remove(anime.uuid);
anime.notify();
return;
}

animesInWatchlist.add(anime.uuid);
anime.notify();
}

Future<void> removeAnimeFromWatchlist(Anime anime) async {
if (!isAnimeInWatchlist(anime)) {
return;
}

animesInWatchlist.remove(anime.uuid);
anime.notify();

final Response? response = await URLController.instance.delete(
'$baseUrl/watchlist?anime=${anime.uuid}',
token: profile?.token,
Expand All @@ -198,11 +203,10 @@ class ProfileController {
'Cannot remove anime from watchlist, response: $response',
);

animesInWatchlist.add(anime.uuid);
anime.notify();
return;
}

animesInWatchlist.remove(anime.uuid);
anime.notify();
}

Future<void> toggleAnimeInWatchlist(Anime anime) async {
Expand All @@ -226,6 +230,10 @@ class ProfileController {
return;
}

episodesInWatchlist.add(episode.uuid);
profile?.totalDurationSeen += episode.duration;
episode.notify();

final Response? response = await URLController.instance.put(
'$baseUrl/watchlist?episode=${episode.uuid}',
token: profile?.token,
Expand All @@ -237,19 +245,22 @@ class ProfileController {
'Cannot add episode to watchlist, response: $response',
);

episodesInWatchlist.remove(episode.uuid);
profile?.totalDurationSeen -= episode.duration;
episode.notify();
return;
}

episodesInWatchlist.add(episode.uuid);
profile?.totalDurationSeen += episode.duration;
episode.notify();
}

Future<void> removeEpisodeFromWatchlist(Episode episode) async {
if (!isEpisodeInWatchlist(episode)) {
return;
}

episodesInWatchlist.remove(episode.uuid);
profile?.totalDurationSeen -= episode.duration;
episode.notify();

final Response? response = await URLController.instance.delete(
'$baseUrl/watchlist?episode=${episode.uuid}',
token: profile?.token,
Expand All @@ -261,12 +272,11 @@ class ProfileController {
'Cannot remove episode from watchlist, response: $response',
);

episodesInWatchlist.add(episode.uuid);
profile?.totalDurationSeen += episode.duration;
episode.notify();
return;
}

episodesInWatchlist.remove(episode.uuid);
profile?.totalDurationSeen -= episode.duration;
episode.notify();
}

Future<void> toggleEpisodeInWatchlist(Episode episode) async {
Expand Down

0 comments on commit a3c768a

Please sign in to comment.