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_hooks/flutter_hooks.dart';
|
||||
import 'package:form_validator/form_validator.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:image_picker/image_picker.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/extensions/constrains.dart';
|
||||
import 'package:spotube/extensions/context.dart';
|
||||
import 'package:spotube/provider/spotify/spotify.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/queries/queries.dart';
|
||||
import 'package:spotube/utils/type_conversion_utils.dart';
|
||||
|
||||
class PlaylistCreateDialog extends HookConsumerWidget {
|
||||
@ -37,13 +37,16 @@ class PlaylistCreateDialog extends HookConsumerWidget {
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
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(
|
||||
() => userPlaylists.pages
|
||||
.expand((p) => p.items ?? <PlaylistSimple>[])
|
||||
() => userPlaylists.asData?.value.items
|
||||
.firstWhereOrNull((playlist) => playlist.id == playlistId),
|
||||
[
|
||||
userPlaylists.pages,
|
||||
userPlaylists.asData?.value.items,
|
||||
playlistId,
|
||||
],
|
||||
);
|
||||
@ -84,24 +87,6 @@ class PlaylistCreateDialog extends HookConsumerWidget {
|
||||
}
|
||||
}, [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 {
|
||||
if (!formKey.currentState!.validate()) return;
|
||||
|
||||
@ -118,9 +103,14 @@ class PlaylistCreateDialog extends HookConsumerWidget {
|
||||
);
|
||||
|
||||
if (isUpdatingPlaylist) {
|
||||
await playlistUpdateMutation.mutate(payload);
|
||||
await playlistNotifier.modify(payload, onError);
|
||||
} 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(
|
||||
onPressed: onCreate,
|
||||
onPressed: playlist.isLoading ? null : onCreate,
|
||||
child: Text(
|
||||
isUpdatingPlaylist
|
||||
? context.l10n.update
|
||||
|
||||
@ -15,7 +15,7 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
||||
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;
|
||||
state = const AsyncLoading();
|
||||
|
||||
@ -25,23 +25,30 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
||||
if (me.value == null) return;
|
||||
|
||||
state = await AsyncValue.guard(() async {
|
||||
final playlist = await spotify.playlists.createPlaylist(
|
||||
me.value!.id!,
|
||||
input.playlistName,
|
||||
collaborative: input.collaborative,
|
||||
description: input.description,
|
||||
public: input.public,
|
||||
);
|
||||
|
||||
if (input.base64Image != null) {
|
||||
await spotify.playlists.updatePlaylistImage(
|
||||
playlist.id!,
|
||||
input.base64Image!,
|
||||
try {
|
||||
final playlist = await spotify.playlists.createPlaylist(
|
||||
me.value!.id!,
|
||||
input.playlistName,
|
||||
collaborative: input.collaborative,
|
||||
description: input.description,
|
||||
public: input.public,
|
||||
);
|
||||
}
|
||||
|
||||
return playlist;
|
||||
if (input.base64Image != null) {
|
||||
await spotify.playlists.updatePlaylistImage(
|
||||
playlist.id!,
|
||||
input.base64Image!,
|
||||
);
|
||||
}
|
||||
|
||||
return playlist;
|
||||
} catch (e) {
|
||||
onError?.call(e);
|
||||
rethrow;
|
||||
}
|
||||
});
|
||||
|
||||
ref.invalidate(favoritePlaylistsProvider);
|
||||
}
|
||||
|
||||
Future<void> addTracks(List<String> trackIds) async {
|
||||
@ -57,29 +64,36 @@ class PlaylistNotifier extends FamilyAsyncNotifier<Playlist, String> {
|
||||
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;
|
||||
|
||||
final spotify = ref.read(spotifyProvider);
|
||||
|
||||
await update((state) async {
|
||||
await spotify.playlists.updatePlaylist(
|
||||
state.id!,
|
||||
input.playlistName,
|
||||
collaborative: input.collaborative,
|
||||
description: input.description,
|
||||
public: input.public,
|
||||
);
|
||||
|
||||
if (input.base64Image != null) {
|
||||
await spotify.playlists.updatePlaylistImage(
|
||||
try {
|
||||
await spotify.playlists.updatePlaylist(
|
||||
state.id!,
|
||||
input.base64Image!,
|
||||
input.playlistName,
|
||||
collaborative: input.collaborative,
|
||||
description: input.description,
|
||||
public: input.public,
|
||||
);
|
||||
}
|
||||
|
||||
return spotify.playlists.get(state.id!);
|
||||
if (input.base64Image != null) {
|
||||
await spotify.playlists.updatePlaylistImage(
|
||||
state.id!,
|
||||
input.base64Image!,
|
||||
);
|
||||
}
|
||||
|
||||
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:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:spotify/spotify.dart';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user