spotube/lib/provider/connect/connect.dart
Alessio fece073def This pull request primarily involves the removal of several configuration files and assets, as well as minor updates to documentation. The most significant changes are the deletion of various .vscode configuration files and the removal of unused assets from the project.
Configuration File Removals:

    .vscode/c_cpp_properties.json: Removed the entire configuration for C/C++ properties.
    .vscode/launch.json: Removed the Dart launch configurations for different environments and modes.
    .vscode/settings.json: Removed settings related to CMake, spell checking, file nesting, and Dart Flutter SDK path.
    .vscode/snippets.code-snippets: Removed code snippets for Dart, including PaginatedState and PaginatedNotifier templates.
    .vscode/tasks.json: Removed the tasks configuration file.

Documentation Updates:

    CONTRIBUTION.md: Removed heart emoji from the introductory text.
    README.md: Updated the logo image and made minor text adjustments, including removing emojis and updating section titles. [1] [2] [3] [4] [5]

Asset Removals:

    lib/collections/assets.gen.dart: Removed multiple unused asset references, including images related to Spotube logos and banners. [1] [2] [3]

Minor Code Cleanups:

    cli/commands/build/linux.dart, cli/commands/build/windows.dart, cli/commands/translated.dart, cli/commands/untranslated.dart: Adjusted import statements for consistency. [1] [2] [3] [4]
    integration_test/app_test.dart: Removed an unnecessary blank line.
    lib/collections/routes.dart: Commented out the TrackRoute configuration.
2025-04-13 18:40:37 +02:00

190 lines
4.7 KiB
Dart

import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:media_kit/media_kit.dart' hide Track;
import 'package:spotify/spotify.dart' hide Playlist;
import 'package:spotube/models/connect/connect.dart';
import 'package:spotube/provider/audio_player/state.dart';
import 'package:spotube/provider/connect/clients.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:spotube/services/logger/logger.dart';
import 'package:web_socket_channel/status.dart' as status;
import 'package:web_socket_channel/web_socket_channel.dart';
final playingProvider = StateProvider<bool>(
(ref) => false,
);
final positionProvider = StateProvider<Duration>(
(ref) => Duration.zero,
);
final durationProvider = StateProvider<Duration>(
(ref) => Duration.zero,
);
final shuffleProvider = StateProvider<bool>(
(ref) => false,
);
final loopModeProvider = StateProvider<PlaylistMode>(
(ref) => PlaylistMode.none,
);
final queueProvider = StateProvider<AudioPlayerState>(
(ref) => AudioPlayerState(
playing: audioPlayer.isPlaying,
loopMode: audioPlayer.loopMode,
shuffled: audioPlayer.isShuffled,
playlist: audioPlayer.playlist,
collections: [],
),
);
final volumeProvider = StateProvider<double>(
(ref) => 1.0,
);
class ConnectNotifier extends AsyncNotifier<WebSocketChannel?> {
@override
build() async {
try {
final connectClients = ref.watch(connectClientsProvider);
if (connectClients.asData?.value.resolvedService == null) return null;
final service = connectClients.asData!.value.resolvedService!;
AppLogger.log.t(
'♾️ Connecting to ${service.name}: ws://${service.host}:${service.port}/ws',
);
final channel = WebSocketChannel.connect(
Uri.parse('ws://${service.host}:${service.port}/ws'),
);
await channel.ready;
AppLogger.log.t(
'✅ Connected to ${service.name}: ws://${service.host}:${service.port}/ws',
);
final subscription = channel.stream.listen(
(message) {
final event =
WebSocketEvent.fromJson(jsonDecode(message), (data) => data);
event.onQueue((event) {
ref.read(queueProvider.notifier).state = event.data;
});
event.onPlaying((event) {
ref.read(playingProvider.notifier).state = event.data;
});
event.onPosition((event) {
ref.read(positionProvider.notifier).state = event.data;
});
event.onDuration((event) {
ref.read(durationProvider.notifier).state = event.data;
});
event.onShuffle((event) {
ref.read(shuffleProvider.notifier).state = event.data;
});
event.onLoop((event) {
ref.read(loopModeProvider.notifier).state = event.data;
});
event.onVolume((event) {
ref.read(volumeProvider.notifier).state = event.data;
});
},
onError: (error) {
AppLogger.reportError(error, StackTrace.current);
},
);
ref.onDispose(() {
subscription.cancel();
channel.sink.close(status.goingAway);
});
return channel;
} catch (e, stack) {
AppLogger.reportError(e, stack);
rethrow;
}
}
Future<void> emit(Object message) async {
if (state.value == null) return;
state.value?.sink.add(
message is String ? message : (message as dynamic).toJson(),
);
}
Future<void> resume() async {
emit(WebSocketResumeEvent());
}
Future<void> pause() async {
emit(WebSocketPauseEvent());
}
Future<void> stop() async {
emit(WebSocketStopEvent());
}
Future<void> jumpTo(int position) async {
emit(WebSocketJumpEvent(position));
}
Future<void> load(WebSocketLoadEventData data) async {
emit(WebSocketLoadEvent(data));
}
Future<void> next() async {
emit(WebSocketNextEvent());
}
Future<void> previous() async {
emit(WebSocketPreviousEvent());
}
Future<void> seek(Duration position) async {
emit(WebSocketSeekEvent(position));
}
Future<void> setShuffle(bool value) async {
emit(WebSocketShuffleEvent(value));
}
Future<void> setLoopMode(PlaylistMode value) async {
emit(WebSocketLoopEvent(value));
}
Future<void> addTrack(Track data) async {
emit(WebSocketAddTrackEvent(data));
}
Future<void> removeTrack(String data) async {
emit(WebSocketRemoveTrackEvent(data));
}
Future<void> reorder(ReorderData data) async {
emit(WebSocketReorderEvent(data));
}
Future<void> setVolume(double value) async {
emit(WebSocketVolumeEvent(value));
}
}
final connectProvider =
AsyncNotifierProvider<ConnectNotifier, WebSocketChannel?>(
() => ConnectNotifier(),
);