Skip to content

Commit

Permalink
feat: toggle for discord rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Dec 8, 2023
1 parent b92583d commit 24a2294
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 61 deletions.
1 change: 1 addition & 0 deletions lib/collections/spotube_icons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ abstract class SpotubeIcons {
static const noEye = FeatherIcons.eyeOff;
static const normalize = FeatherIcons.barChart2;
static const wikipedia = SimpleIcons.wikipedia;
static const discord = SimpleIcons.discord;
}
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,6 @@
"login": "Login",
"login_with_your_lastfm": "Login with your Last.fm account",
"scrobble_to_lastfm": "Scrobble to Last.fm",
"go_to_album": "Go to Album"
"go_to_album": "Go to Album",
"discord_rich_presence": "Discord Rich Presence"
}
8 changes: 8 additions & 0 deletions lib/pages/settings/sections/desktop.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/components/settings/section_card_with_heading.dart';
Expand Down Expand Up @@ -50,6 +51,13 @@ class SettingsDesktopSection extends HookConsumerWidget {
value: preferences.systemTitleBar,
onChanged: preferencesNotifier.setSystemTitleBar,
),
if (!DesktopTools.platform.isMacOS)
SwitchListTile(
secondary: const Icon(SpotubeIcons.discord),
title: Text(context.l10n.discord_rich_presence),
value: preferences.discordPresence,
onChanged: preferencesNotifier.setDiscordPresence,
),
],
);
}
Expand Down
70 changes: 70 additions & 0 deletions lib/provider/discord_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'package:dart_discord_rpc/dart_discord_rpc.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/collections/env.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
import 'package:spotube/utils/type_conversion_utils.dart';

class Discord extends ChangeNotifier {
final DiscordRPC? discordRPC;
final bool isEnabled;

Discord(this.isEnabled)
: discordRPC = (DesktopTools.platform.isWindows ||
DesktopTools.platform.isLinux) &&
isEnabled
? 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();
}

@override
void dispose() {
clear();
shutdown();
super.dispose();
}
}

final discordProvider = ChangeNotifierProvider(
(ref) {
final isEnabled =
ref.watch(userPreferencesProvider.select((s) => s.discordPresence));
final playback = ref.read(ProxyPlaylistNotifier.provider);
final discord = Discord(isEnabled);

if (playback.activeTrack != null) {
discord.updatePresence(playback.activeTrack!);
}

return discord;
},
);
3 changes: 2 additions & 1 deletion lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +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/provider/discord_provider.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 @@ -64,6 +64,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
ProxyPlaylist get playlist => state;
BlackListNotifier get blacklist =>
ref.read(BlackListNotifier.provider.notifier);
Discord get discord => ref.read(discordProvider);

static final provider =
StateNotifierProvider<ProxyPlaylistNotifier, ProxyPlaylist>(
Expand Down
4 changes: 4 additions & 0 deletions lib/provider/user_preferences/user_preferences_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class UserPreferencesNotifier extends PersistedStateNotifier<UserPreferences> {
}
}

void setDiscordPresence(bool discordPresence) {
state = state.copyWith(discordPresence: discordPresence);
}

void setAmoledDarkTheme(bool isAmoled) {
state = state.copyWith(amoledDarkTheme: isAmoled);
}
Expand Down
6 changes: 6 additions & 0 deletions lib/provider/user_preferences/user_preferences_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ final class UserPreferences {
)
final SourceCodecs downloadMusicCodec;

@JsonKey(defaultValue: true)
final bool discordPresence;

UserPreferences({
required this.audioQuality,
required this.albumColorSync,
Expand All @@ -219,6 +222,7 @@ final class UserPreferences {
required this.audioSource,
required this.streamMusicCodec,
required this.downloadMusicCodec,
required this.discordPresence,
});

factory UserPreferences.withDefaults() {
Expand Down Expand Up @@ -255,6 +259,7 @@ final class UserPreferences {
SourceCodecs? downloadMusicCodec,
SourceCodecs? streamMusicCodec,
bool? systemTitleBar,
bool? discordPresence,
}) {
return UserPreferences(
themeMode: themeMode ?? this.themeMode,
Expand All @@ -277,6 +282,7 @@ final class UserPreferences {
normalizeAudio: normalizeAudio ?? this.normalizeAudio,
streamMusicCodec: streamMusicCodec ?? this.streamMusicCodec,
systemTitleBar: systemTitleBar ?? this.systemTitleBar,
discordPresence: discordPresence ?? this.discordPresence,
);
}
}
2 changes: 2 additions & 0 deletions lib/provider/user_preferences/user_preferences_state.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 0 additions & 44 deletions lib/services/discord/discord.dart

This file was deleted.

45 changes: 30 additions & 15 deletions untranslated_messages.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,76 @@
{
"ar": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"bn": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"ca": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"de": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"es": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"fa": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"fr": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"hi": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"ja": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"pl": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"pt": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"ru": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"tr": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"uk": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
],

"zh": [
"go_to_album"
"go_to_album",
"discord_rich_presence"
]
}

0 comments on commit 24a2294

Please sign in to comment.