Skip to content

Commit

Permalink
feat: discord RPC integration #98
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Nov 22, 2023
1 parent 92deb0c commit 88b8785
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/collections/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ abstract class Env {

static bool get enableUpdateChecker =>
DesktopTools.platform.isFlatpak || _enableUpdateChecker == "1";

static String discordAppId = "1176718791388975124";
}
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:catcher_2/catcher_2.dart';
import 'package:dart_discord_rpc/dart_discord_rpc.dart';
import 'package:device_preview/device_preview.dart';
import 'package:fl_query/fl_query.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -63,6 +64,10 @@ Future<void> main(List<String> rawArgs) async {
MetadataGod.initialize();
}

if (DesktopTools.platform.isWindows || DesktopTools.platform.isLinux) {
DiscordRPC.initialize();
}

final hiveCacheDir =
kIsWeb ? null : (await getApplicationSupportDirectory()).path;

Expand Down
8 changes: 8 additions & 0 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:spotube/provider/user_preferences/user_preferences_provider.dart
import 'package:spotube/provider/user_preferences/user_preferences_state.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:spotube/services/audio_services/audio_services.dart';
import 'package:spotube/services/discord/discord.dart';
import 'package:spotube/services/sourced_track/exceptions.dart';
import 'package:spotube/services/sourced_track/models/source_info.dart';
import 'package:spotube/services/sourced_track/sourced_track.dart';
Expand Down Expand Up @@ -92,6 +93,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
}

notificationService.addTrack(newActiveTrack);
discord.updatePresence(newActiveTrack);
state = state.copyWith(
active: state.tracks
.toList()
Expand Down Expand Up @@ -321,6 +323,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
collections: {},
);
await notificationService.addTrack(indexTrack);
discord.updatePresence(indexTrack);
} else {
final addableTrack = await SourcedTrack.fetchFromTrack(
ref: ref,
Expand All @@ -338,6 +341,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
collections: {},
);
await notificationService.addTrack(addableTrack);
discord.updatePresence(addableTrack);
}

await audioPlayer.openPlaylist(
Expand Down Expand Up @@ -366,6 +370,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>

if (oldTrack != null || track != null) {
await notificationService.addTrack(track ?? oldTrack!);
discord.updatePresence(track ?? oldTrack!);
}
}

Expand Down Expand Up @@ -468,6 +473,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>

if (oldTrack != null || track != null) {
await notificationService.addTrack(track ?? oldTrack!);
discord.updatePresence(track ?? oldTrack!);
}
}

Expand All @@ -493,12 +499,14 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
await audioPlayer.skipToPrevious();
if (oldTrack != null || track != null) {
await notificationService.addTrack(track ?? oldTrack!);
discord.updatePresence(track ?? oldTrack!);
}
}

Future<void> stop() async {
state = ProxyPlaylist({});
await audioPlayer.stop();
discord.clear();
}

Future<void> updatePalette() async {
Expand Down
44 changes: 44 additions & 0 deletions lib/services/discord/discord.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:dart_discord_rpc/dart_discord_rpc.dart';
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/collections/env.dart';
import 'package:spotube/utils/type_conversion_utils.dart';

class Discord {
final DiscordRPC? discordRPC;

Discord()
: discordRPC =
DesktopTools.platform.isWindows || DesktopTools.platform.isLinux
? DiscordRPC(applicationId: Env.discordAppId)
: null {
discordRPC?.start(autoRegister: true);
}

void updatePresence(Track track) {
clear();
final artistNames =
TypeConversionUtils.artists_X_String(track.artists ?? <Artist>[]);
discordRPC?.updatePresence(
DiscordPresence(
details: "Song: ${track.name} by $artistNames",
state: "Vibing in Music",
startTimeStamp: DateTime.now().millisecondsSinceEpoch,
largeImageKey: "spotube-logo-foreground",
largeImageText: "Spotube",
smallImageKey: "spotube-logo-foreground",
smallImageText: "Spotube",
),
);
}

void clear() {
discordRPC?.clearPresence();
}

void shutdown() {
discordRPC?.shutDown();
}
}

final discord = Discord();
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "generated_plugin_registrant.h"

#include <dart_discord_rpc/dart_discord_rpc_plugin.h>
#include <file_selector_linux/file_selector_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <local_notifier/local_notifier_plugin.h>
Expand All @@ -18,6 +19,9 @@
#include <window_size/window_size_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) dart_discord_rpc_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "DartDiscordRpcPlugin");
dart_discord_rpc_plugin_register_with_registrar(dart_discord_rpc_registrar);
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
dart_discord_rpc
file_selector_linux
flutter_secure_storage_linux
local_notifier
Expand Down
9 changes: 9 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,15 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.2"
dart_discord_rpc:
dependency: "direct main"
description:
path: "."
ref: HEAD
resolved-ref: "4d05017838ebeadcdb832e1893fabad1506fddba"
url: "https://github.com/Tommypop2/dart_discord_rpc.git"
source: git
version: "0.0.3"
dart_style:
dependency: transitive
description:
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ dependencies:
very_good_infinite_list: ^0.7.1
gap: ^3.0.1
sliver_tools: ^0.2.12
dart_discord_rpc:
git:
url: https://github.com/Tommypop2/dart_discord_rpc.git

dev_dependencies:
build_runner: ^2.3.2
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "generated_plugin_registrant.h"

#include <dart_discord_rpc/dart_discord_rpc_plugin.h>
#include <file_selector_windows/file_selector_windows.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <local_notifier/local_notifier_plugin.h>
Expand All @@ -19,6 +20,8 @@
#include <window_size/window_size_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
DartDiscordRpcPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DartDiscordRpcPlugin"));
FileSelectorWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FileSelectorWindows"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
dart_discord_rpc
file_selector_windows
flutter_secure_storage_windows
local_notifier
Expand Down

0 comments on commit 88b8785

Please sign in to comment.