Skip to content

Commit

Permalink
feat: add discord toggle in settings, disable for linux (#947)
Browse files Browse the repository at this point in the history
Ref #946
  • Loading branch information
Feichtmeier authored Oct 12, 2024
1 parent de23cf3 commit 6aa080e
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 22 deletions.
7 changes: 7 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:yaru/constants.dart';

Expand Down Expand Up @@ -146,6 +148,7 @@ const kSelectedPageId = 'selectedPageId';
const kRadioIndex = 'radioIndex';
const kPodcastIndex = 'podcastIndex';
const kNeverShowImportFails = 'neverShowImportFails';
const kEnableDiscordRPC = 'enableDiscordRPC';
const kLastCountryCode = 'lastCountryCode';
const kLastLanguageCode = 'lastLanguageCode';
const kSearchResult = 'searchResult';
Expand Down Expand Up @@ -185,3 +188,7 @@ const kAlbumArtHeaders = {
const kAudioHeaderDescriptionWidth = 400.0;

const kShowLeadingThreshold = 3000;

// TODO(#946): make discord work inside the snap
// or leave it for linux disabled if this won't work
bool kDiscordEnabledOnPlatform = !Platform.isLinux;
8 changes: 4 additions & 4 deletions lib/expose/expose_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import 'package:flutter_discord_rpc/flutter_discord_rpc.dart';
import '../constants.dart';

class ExposeService {
ExposeService({required FlutterDiscordRPC discordRPC})
ExposeService({required FlutterDiscordRPC? discordRPC})
: _discordRPC = discordRPC;

final FlutterDiscordRPC _discordRPC;
final FlutterDiscordRPC? _discordRPC;

Future<void> exposeTitleOnline({
Future<void>? exposeTitleOnline({
required String songDetails,
required String state,
}) {
return _discordRPC.setActivity(
return _discordRPC?.setActivity(
activity: RPCActivity(
assets: RPCAssets(
largeText: songDetails,
Expand Down
4 changes: 4 additions & 0 deletions lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@
"skipToLivStream": "Zur Live-Übertragung springen",
"searchSimilarStation": "Ähnlichen Sender suchen",
"clicks": "Clicks",
"exposeOnlineHeadline": "Veröffentliche deine Höraktivität online",
"exposeToDiscordTitle": "Discord",
"exposeToDiscordSubTitle": "Der Künstler und Titel des Lieds/Radio-Senders/Podcast das du gerade hörst wird online geteilt. Benötigt App-Neustart.",
"featureDisabledOnPlatform": "Diese Funktion ist momentan nicht für dieses Betriebssystem aktiviert.",
"regionNone": "Keine",
"onlineArtError": "Die Albumkunst-Online-Suche ist momentan nicht verfügbar",
"regionAfghanistan": "Afghanistan",
Expand Down
4 changes: 4 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@
"searchSimilarStation": "Search similar station",
"onlineArtError": "Online art lookup is currently not available",
"clicks": "Clicks",
"exposeOnlineHeadline": "Expose your listening activity online",
"exposeToDiscordTitle": "Discord",
"exposeToDiscordSubTitle": "The artist and title of the song/station/podcast you are currently listening to are shared. Requires app restart.",
"featureDisabledOnPlatform": "This feature is currently disabled for this operating system.",
"regionNone": "None",
"regionAfghanistan": "Afghanistan",
"regionAlandislands": "Alandislands",
Expand Down
30 changes: 18 additions & 12 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,25 @@ Future<void> main(List<String> args) async {
final sharedPreferences = await SharedPreferences.getInstance();
final version = (await PackageInfo.fromPlatform()).version;

await FlutterDiscordRPC.initialize(kDiscordApplicationId);
FlutterDiscordRPC.instance.connect();
final enableDiscord = kDiscordEnabledOnPlatform &&
sharedPreferences.get(kEnableDiscordRPC) == true;
if (enableDiscord) {
await FlutterDiscordRPC.initialize(kDiscordApplicationId);
FlutterDiscordRPC.instance.connect();
di.registerLazySingleton<FlutterDiscordRPC>(
() => FlutterDiscordRPC.instance,
dispose: (s) {
s.disconnect();
s.dispose();
},
);
}

registerServicesAndViewModels(
sharedPreferences: sharedPreferences,
args: args,
version: version,
discordRPC: FlutterDiscordRPC.instance,
enableDiscord: enableDiscord,
);

runApp(
Expand All @@ -77,9 +88,9 @@ Future<void> main(List<String> args) async {

void registerServicesAndViewModels({
required SharedPreferences sharedPreferences,
required FlutterDiscordRPC discordRPC,
required List<String> args,
required String version,
required bool enableDiscord,
}) {
di
..registerLazySingleton<SharedPreferences>(() => sharedPreferences)
Expand All @@ -93,15 +104,10 @@ void registerServicesAndViewModels({
),
dispose: (s) => s.dispose(),
)
..registerLazySingleton<FlutterDiscordRPC>(
() => discordRPC,
dispose: (s) {
s.disconnect();
s.dispose();
},
)
..registerLazySingleton<ExposeService>(
() => ExposeService(discordRPC: di<FlutterDiscordRPC>()),
() => ExposeService(
discordRPC: enableDiscord ? di<FlutterDiscordRPC>() : null,
),
)
..registerLazySingleton<PlayerService>(
() => PlayerService(
Expand Down
3 changes: 3 additions & 0 deletions lib/settings/settings_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class SettingsModel extends SafeChangeNotifier {
void setNeverShowFailedImports(bool value) =>
_service.setNeverShowFailedImports(value);

bool get enableDiscordRPC => _service.enableDiscordRPC;
void setEnableDiscordRPC(bool value) => _service.setEnableDiscordRPC(value);

bool get usePodcastIndex => _service.usePodcastIndex;
Future<void> setUsePodcastIndex(bool value) async =>
_service.setUsePodcastIndex(value);
Expand Down
12 changes: 11 additions & 1 deletion lib/settings/settings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class SettingsService {
: _preferences = sharedPreferences;

final SharedPreferences _preferences;

final _propertiesChangedController = StreamController<bool>.broadcast();
Stream<bool> get propertiesChanged => _propertiesChangedController.stream;

int get themeIndex => _preferences.getInt(kThemeIndex) ?? 0;
void setThemeIndex(int value) {
_preferences.setInt(kThemeIndex, value).then(
Expand All @@ -33,6 +33,16 @@ class SettingsService {
);
}

bool get enableDiscordRPC => _preferences.getBool(kEnableDiscordRPC) ?? false;

void setEnableDiscordRPC(bool value) {
_preferences.setBool(kEnableDiscordRPC, value).then(
(saved) {
if (saved) _propertiesChangedController.add(true);
},
);
}

bool recentPatchNotesDisposed(String version) =>
_preferences.getString(kPatchNotesDisposed) == version;

Expand Down
38 changes: 38 additions & 0 deletions lib/settings/view/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SettingsPage extends StatelessWidget {
_ThemeSection(),
_PodcastSection(),
_LocalAudioSection(),
_ExposeOnlineSection(),
_AboutSection(),
],
),
Expand Down Expand Up @@ -412,3 +413,40 @@ class _LicenseTile extends StatelessWidget {
);
}
}

class _ExposeOnlineSection extends StatelessWidget with WatchItMixin {
const _ExposeOnlineSection();

@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return YaruSection(
headline: Text(l10n.exposeOnlineHeadline),
margin: const EdgeInsets.only(
top: kYaruPagePadding,
right: kYaruPagePadding,
left: kYaruPagePadding,
),
child: Column(
children: [
YaruTile(
title: Text(l10n.exposeToDiscordTitle),
subtitle: Text(
kDiscordEnabledOnPlatform
? l10n.exposeToDiscordSubTitle
: l10n.featureDisabledOnPlatform,
),
trailing: CommonSwitch(
value: kDiscordEnabledOnPlatform
? watchPropertyValue((SettingsModel m) => m.enableDiscordRPC)
: false,
onChanged: kDiscordEnabledOnPlatform
? di<SettingsModel>().setEnableDiscordRPC
: null,
),
),
],
),
);
}
}
Loading

0 comments on commit 6aa080e

Please sign in to comment.