mirror of
https://github.com/KRTirtho/spotube.git
synced 2026-05-08 16:24:36 +00:00
feat: use provider for create playlist
This commit is contained in:
parent
4d774b6ef1
commit
82802fef2c
@ -5,6 +5,7 @@ import 'package:collection/collection.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:form_validator/form_validator.dart';
|
import 'package:form_validator/form_validator.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
import 'package:spotify/spotify.dart';
|
import 'package:spotify/spotify.dart';
|
||||||
@ -13,10 +14,9 @@ import 'package:spotube/collections/spotube_icons.dart';
|
|||||||
import 'package:spotube/components/shared/image/universal_image.dart';
|
import 'package:spotube/components/shared/image/universal_image.dart';
|
||||||
import 'package:spotube/extensions/constrains.dart';
|
import 'package:spotube/extensions/constrains.dart';
|
||||||
import 'package:spotube/extensions/context.dart';
|
import 'package:spotube/extensions/context.dart';
|
||||||
|
import 'package:spotube/provider/spotify/spotify.dart';
|
||||||
import 'package:spotube/provider/spotify_provider.dart';
|
import 'package:spotube/provider/spotify_provider.dart';
|
||||||
import 'package:spotube/services/mutations/mutations.dart';
|
|
||||||
import 'package:spotube/services/mutations/playlist.dart';
|
import 'package:spotube/services/mutations/playlist.dart';
|
||||||
import 'package:spotube/services/queries/queries.dart';
|
|
||||||
import 'package:spotube/utils/type_conversion_utils.dart';
|
import 'package:spotube/utils/type_conversion_utils.dart';
|
||||||
|
|
||||||
class PlaylistCreateDialog extends HookConsumerWidget {
|
class PlaylistCreateDialog extends HookConsumerWidget {
|
||||||
@ -37,13 +37,16 @@ class PlaylistCreateDialog extends HookConsumerWidget {
|
|||||||
child: Scaffold(
|
child: Scaffold(
|
||||||
backgroundColor: Colors.transparent,
|
backgroundColor: Colors.transparent,
|
||||||
body: HookBuilder(builder: (context) {
|
body: HookBuilder(builder: (context) {
|
||||||
final userPlaylists = useQueries.playlist.ofMine(ref);
|
final userPlaylists = ref.watch(favoritePlaylistsProvider);
|
||||||
|
final playlist = ref.watch(playlistProvider(playlistId ?? ""));
|
||||||
|
final playlistNotifier =
|
||||||
|
ref.watch(playlistProvider(playlistId ?? "").notifier);
|
||||||
|
|
||||||
final updatingPlaylist = useMemoized(
|
final updatingPlaylist = useMemoized(
|
||||||
() => userPlaylists.pages
|
() => userPlaylists.asData?.value.items
|
||||||
.expand((p) => p.items ?? <PlaylistSimple>[])
|
|
||||||
.firstWhereOrNull((playlist) => playlist.id == playlistId),
|
.firstWhereOrNull((playlist) => playlist.id == playlistId),
|
||||||
[
|
[
|
||||||
userPlaylists.pages,
|
userPlaylists.asData?.value.items,
|
||||||
playlistId,
|
playlistId,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@ -84,24 +87,6 @@ class PlaylistCreateDialog extends HookConsumerWidget {
|
|||||||
}
|
}
|
||||||
}, [scaffold, l10n, theme]);
|
}, [scaffold, l10n, theme]);
|
||||||
|
|
||||||
final playlistCreateMutation = useMutations.playlist.create(
|
|
||||||
ref,
|
|
||||||
trackIds: trackIds,
|
|
||||||
onData: (value) {
|
|
||||||
Navigator.pop(context);
|
|
||||||
},
|
|
||||||
onError: onError,
|
|
||||||
);
|
|
||||||
|
|
||||||
final playlistUpdateMutation = useMutations.playlist.update(
|
|
||||||
ref,
|
|
||||||
playlistId: playlistId,
|
|
||||||
onData: (value) {
|
|
||||||
Navigator.pop(context);
|
|
||||||
},
|
|
||||||
onError: onError,
|
|
||||||
);
|
|
||||||
|
|
||||||
Future<void> onCreate() async {
|
Future<void> onCreate() async {
|
||||||
if (!formKey.currentState!.validate()) return;
|
if (!formKey.currentState!.validate()) return;
|
||||||
|
|
||||||
@ -118,9 +103,14 @@ class PlaylistCreateDialog extends HookConsumerWidget {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (isUpdatingPlaylist) {
|
if (isUpdatingPlaylist) {
|
||||||
await playlistUpdateMutation.mutate(payload);
|
await playlistNotifier.modify(payload, onError);
|
||||||
} else {
|
} else {
|
||||||
await playlistCreateMutation.mutate(payload);
|
await playlistNotifier.create(payload, onError);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.mounted &&
|
||||||
|
!ref.read(playlistProvider(playlistId ?? "")).hasError) {
|
||||||
|
context.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +128,7 @@ class PlaylistCreateDialog extends HookConsumerWidget {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
FilledButton(
|
FilledButton(
|
||||||
onPressed: onCreate,
|
onPressed: playlist.isLoading ? null : onCreate,
|
||||||
child: Text(
|
child: Text(
|
||||||
isUpdatingPlaylist
|
isUpdatingPlaylist
|
||||||
? context.l10n.update
|
? context.l10n.update
|
||||||
|
|||||||
@ -15,7 +15,7 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
|||||||
return spotify.playlists.get(arg);
|
return spotify.playlists.get(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> create(PlaylistInput input) async {
|
Future<void> create(PlaylistInput input, [ValueChanged? onError]) async {
|
||||||
if (state is AsyncData || state is AsyncLoading) return;
|
if (state is AsyncData || state is AsyncLoading) return;
|
||||||
state = const AsyncLoading();
|
state = const AsyncLoading();
|
||||||
|
|
||||||
@ -25,6 +25,7 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
|||||||
if (me.value == null) return;
|
if (me.value == null) return;
|
||||||
|
|
||||||
state = await AsyncValue.guard(() async {
|
state = await AsyncValue.guard(() async {
|
||||||
|
try {
|
||||||
final playlist = await spotify.playlists.createPlaylist(
|
final playlist = await spotify.playlists.createPlaylist(
|
||||||
me.value!.id!,
|
me.value!.id!,
|
||||||
input.playlistName,
|
input.playlistName,
|
||||||
@ -41,7 +42,13 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return playlist;
|
return playlist;
|
||||||
|
} catch (e) {
|
||||||
|
onError?.call(e);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ref.invalidate(favoritePlaylistsProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> addTracks(List<String> trackIds) async {
|
Future<void> addTracks(List<String> trackIds) async {
|
||||||
@ -57,12 +64,13 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
|||||||
ref.invalidate(playlistTracksProvider(state.value!.id!));
|
ref.invalidate(playlistTracksProvider(state.value!.id!));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> modify(PlaylistInput input) async {
|
Future<void> modify(PlaylistInput input, [ValueChanged? onError]) async {
|
||||||
if (state.value == null) return;
|
if (state.value == null) return;
|
||||||
|
|
||||||
final spotify = ref.read(spotifyProvider);
|
final spotify = ref.read(spotifyProvider);
|
||||||
|
|
||||||
await update((state) async {
|
await update((state) async {
|
||||||
|
try {
|
||||||
await spotify.playlists.updatePlaylist(
|
await spotify.playlists.updatePlaylist(
|
||||||
state.id!,
|
state.id!,
|
||||||
input.playlistName,
|
input.playlistName,
|
||||||
@ -79,7 +87,13 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return spotify.playlists.get(state.id!);
|
return spotify.playlists.get(state.id!);
|
||||||
|
} catch (e) {
|
||||||
|
onError?.call(e);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ref.invalidate(favoritePlaylistsProvider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import 'dart:convert';
|
|||||||
|
|
||||||
import 'package:catcher_2/catcher_2.dart';
|
import 'package:catcher_2/catcher_2.dart';
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:hive_flutter/hive_flutter.dart';
|
import 'package:hive_flutter/hive_flutter.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
import 'package:spotify/spotify.dart';
|
import 'package:spotify/spotify.dart';
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user