2024-08-29 07:02:49 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:developer';
|
|
|
|
|
2024-08-26 12:26:07 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-08-29 07:02:49 +00:00
|
|
|
import 'package:rhythm_box/providers/auth.dart';
|
2024-08-26 12:26:07 +00:00
|
|
|
import 'package:spotify/spotify.dart';
|
|
|
|
|
|
|
|
class SpotifyProvider extends GetxController {
|
2024-08-29 07:02:49 +00:00
|
|
|
late SpotifyApi api;
|
|
|
|
|
|
|
|
List<StreamSubscription>? _subscriptions;
|
2024-08-26 12:26:07 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void onInit() {
|
2024-08-29 07:02:49 +00:00
|
|
|
final AuthenticationProvider authenticate = Get.find();
|
|
|
|
if (authenticate.auth.value == null) {
|
|
|
|
api = _initApiWithClientCredentials();
|
|
|
|
} else {
|
|
|
|
api = _initApiWithUserCredentials();
|
|
|
|
}
|
|
|
|
_subscriptions = [
|
|
|
|
authenticate.auth.listen((value) {
|
|
|
|
if (value == null) {
|
|
|
|
api = _initApiWithClientCredentials();
|
|
|
|
} else {
|
|
|
|
api = _initApiWithUserCredentials();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
super.onInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
SpotifyApi _initApiWithClientCredentials() {
|
|
|
|
log('[SpotifyApi] Using client credentials...');
|
|
|
|
return SpotifyApi(
|
2024-08-26 12:26:07 +00:00
|
|
|
SpotifyApiCredentials(
|
2024-08-27 06:48:31 +00:00
|
|
|
'f73d4bff91d64d89be9930036f553534',
|
|
|
|
'5cbec0b928d247cd891d06195f07b8c9',
|
2024-08-26 12:26:07 +00:00
|
|
|
),
|
|
|
|
);
|
2024-08-29 07:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SpotifyApi _initApiWithUserCredentials() {
|
|
|
|
log('[SpotifyApi] Using user credentials...');
|
|
|
|
final AuthenticationProvider authenticate = Get.find();
|
|
|
|
return SpotifyApi.withAccessToken(
|
2024-09-06 04:41:35 +00:00
|
|
|
authenticate.auth.value!.spotifyAccessToken.value);
|
2024-08-29 07:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
if (_subscriptions != null) {
|
|
|
|
for (final subscription in _subscriptions!) {
|
|
|
|
subscription.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.dispose();
|
2024-08-26 12:26:07 +00:00
|
|
|
}
|
|
|
|
}
|