Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve discord on/off toggling #960

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/app/app_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:flutter/widgets.dart';
import 'package:github/github.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';

import '../common/view/snackbars.dart';
import '../constants.dart';
import '../expose/expose_service.dart';
import '../settings/settings_service.dart';

class AppModel extends SafeChangeNotifier {
Expand All @@ -12,13 +12,24 @@ class AppModel extends SafeChangeNotifier {
required SettingsService settingsService,
required GitHub gitHub,
required bool allowManualUpdates,
required ExposeService exposeService,
}) : _countryCode = WidgetsBinding
.instance.platformDispatcher.locale.countryCode
?.toLowerCase(),
_gitHub = gitHub,
_allowManualUpdates = allowManualUpdates,
_settingsService = settingsService,
_version = appVersion;
_version = appVersion,
_exposeService = exposeService;

final ExposeService _exposeService;
Stream<String?> get errorStream => _exposeService.discordErrorStream;
Stream<bool> get isDiscordConnectedStream =>
_exposeService.isDiscordConnectedStream;

Future<void> connectToDiscord() async => _exposeService.connectToDiscord();
Future<void> disconnectFromDiscord() async =>
_exposeService.disconnectFromDiscord();

final GitHub _gitHub;
final SettingsService _settingsService;
Expand Down Expand Up @@ -55,7 +66,10 @@ class AppModel extends SafeChangeNotifier {
bool? get updateAvailable => _updateAvailable;
String? _onlineVersion;
String? get onlineVersion => _onlineVersion;
Future<void> checkForUpdate(bool isOnline, BuildContext context) async {
Future<void> checkForUpdate({
required bool isOnline,
Function(String error)? onError,
}) async {
_updateAvailable == null;
notifyListeners();

Expand All @@ -66,9 +80,7 @@ class AppModel extends SafeChangeNotifier {
}
_onlineVersion = await getOnlineVersion().onError(
(error, stackTrace) {
if (context.mounted) {
showSnackBar(context: context, content: Text(error.toString()));
}
onError?.call(error.toString());
return null;
},
);
Expand Down
34 changes: 33 additions & 1 deletion lib/app/view/scaffold.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/yaru.dart';

import '../../common/view/snackbars.dart';
import '../../common/view/theme.dart';
import '../../constants.dart';
import '../../extensions/build_context_x.dart';
import '../../l10n/l10n.dart';
import '../../patch_notes/patch_notes_dialog.dart';
import '../../player/view/player_view.dart';
import '../app_model.dart';
Expand All @@ -23,7 +27,9 @@ class _MusicPodScaffoldState extends State<MusicPodScaffold> {
super.initState();
final appModel = di<AppModel>();
appModel
.checkForUpdate(di<ConnectivityModel>().isOnline == true, context)
.checkForUpdate(
isOnline: di<ConnectivityModel>().isOnline == true,
)
.then((_) {
if (!appModel.recentPatchNotesDisposed() && mounted) {
showDialog(
Expand All @@ -38,6 +44,32 @@ class _MusicPodScaffoldState extends State<MusicPodScaffold> {
Widget build(BuildContext context) {
final playerToTheRight = context.mediaQuerySize.width > kSideBarThreshHold;
final isFullScreen = watchPropertyValue((AppModel m) => m.fullWindowMode);
final l10n = context.l10n;
registerStreamHandler(
select: (AppModel m) => m.isDiscordConnectedStream,
handler: (context, snapshot, cancel) {
if (!snapshot.hasData || snapshot.hasError) return;
showSnackBar(
context: context,
content: Row(
mainAxisSize: MainAxisSize.min,
children: space(
widthGap: 10,
children: [
Text(
'${snapshot.data == true ? l10n.connectedTo : l10n.disconnectedFrom}'
' ${l10n.exposeToDiscordTitle}',
),
Icon(
TablerIcons.brand_discord_filled,
color: context.theme.primaryColor,
),
],
),
),
);
},
);

return Stack(
alignment: Alignment.center,
Expand Down
8 changes: 6 additions & 2 deletions lib/common/view/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,15 @@ double get audioCardDimension => kAudioCardDimension - (isMobile ? 15 : 0);

double get bottomPlayerHeight => isMobile ? 80.0 : 90.0;

List<Widget> space({double gap = 5, required Iterable<Widget> children}) =>
List<Widget> space({
double widthGap = 5,
double heightGap = 5,
required Iterable<Widget> children,
}) =>
children
.expand(
(item) sync* {
yield SizedBox(width: gap);
yield SizedBox(width: widthGap);
yield item;
},
)
Expand Down
45 changes: 34 additions & 11 deletions lib/expose/expose_service.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter_discord_rpc/flutter_discord_rpc.dart';

import '../constants.dart';
Expand All @@ -7,20 +9,41 @@ class ExposeService {
: _discordRPC = discordRPC;

final FlutterDiscordRPC? _discordRPC;
final _errorController = StreamController<String?>.broadcast();
Stream<String?> get discordErrorStream => _errorController.stream;
Stream<bool> get isDiscordConnectedStream =>
_discordRPC?.isConnectedStream ?? Stream.value(false);

Future<void>? exposeTitleOnline({
required String songDetails,
required String state,
}) {
return _discordRPC?.setActivity(
activity: RPCActivity(
assets: RPCAssets(
largeText: songDetails,
smallText: kAppTitle,
),
details: songDetails,
state: state,
),
);
}) async {
try {
if (_discordRPC?.isConnected == false) {
await _discordRPC?.connect(autoRetry: true);
}
if (_discordRPC?.isConnected == true) {
await _discordRPC?.setActivity(
activity: RPCActivity(
assets: RPCAssets(
largeText: songDetails,
smallText: kAppTitle,
),
details: songDetails,
state: state,
),
);
}
} on Exception catch (_) {}
}

Future<void> connectToDiscord() async =>
_discordRPC?.connect(autoRetry: true);

Future<void> disconnectFromDiscord() async => _discordRPC?.disconnect();

Future<void> dispose() async {
await _discordRPC?.disconnect();
await _errorController.close();
}
}
3 changes: 2 additions & 1 deletion lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"moreOptions": "Mehr Optionen",
"noRadioServerFound": "Es wurde kein Radio-Server gefunden",
"connectedTo": "Verbunden mit",
"disconnectedFrom": "Getrennt von",
"tryReconnect": "Versuche Neuverbindung",
"addedTo": "Hinzugefügt zu",
"addToPlaylist": "Zu Wiedergabeliste hinzufügen",
Expand Down Expand Up @@ -352,7 +353,7 @@
"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.",
"exposeToDiscordSubTitle": "Der Künstler und Titel des Lieds/Radio-Senders/Podcast das du gerade hörst wird online geteilt.",
"featureDisabledOnPlatform": "Diese Funktion ist momentan nicht für dieses Betriebssystem aktiviert.",
"regionNone": "Keine",
"onlineArtError": "Die Albumkunst-Online-Suche ist momentan nicht verfügbar",
Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"moreOptions": "More options",
"noRadioServerFound": "No radio server found",
"connectedTo": "Connected to",
"disconnectedFrom": "Disconnected from",
"tryReconnect": "Try reconnect",
"addedTo": "Added to",
"addToPlaylist": "Add to playlist",
Expand Down Expand Up @@ -351,7 +352,7 @@
"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.",
"exposeToDiscordSubTitle": "The artist and title of the song/station/podcast you are currently listening to are shared.",
"featureDisabledOnPlatform": "This feature is currently disabled for this operating system.",
"regionNone": "None",
"regionAfghanistan": "Afghanistan",
Expand Down
22 changes: 7 additions & 15 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import 'player/player_service.dart';
import 'podcasts/download_model.dart';
import 'podcasts/podcast_model.dart';
import 'podcasts/podcast_service.dart';
import 'radio/online_art_model.dart';
import 'radio/online_art_service.dart';
import 'radio/radio_model.dart';
import 'radio/radio_service.dart';
Expand All @@ -60,11 +59,8 @@ Future<void> main(List<String> args) async {
final sharedPreferences = await SharedPreferences.getInstance();
final version = (await PackageInfo.fromPlatform()).version;

final enableDiscord =
allowDiscordRPC && sharedPreferences.get(kEnableDiscordRPC) == true;
if (enableDiscord) {
if (allowDiscordRPC) {
await FlutterDiscordRPC.initialize(kDiscordApplicationId);
FlutterDiscordRPC.instance.connect();
di.registerLazySingleton<FlutterDiscordRPC>(
() => FlutterDiscordRPC.instance,
dispose: (s) {
Expand All @@ -80,7 +76,7 @@ Future<void> main(List<String> args) async {
sharedPreferences: sharedPreferences,
args: args,
version: version,
enableDiscord: enableDiscord,
allowDiscordRPC: allowDiscordRPC,
downloadsDefaultDir: downloadsDefaultDir,
);

Expand All @@ -96,7 +92,7 @@ void registerServicesAndViewModels({
required SharedPreferences sharedPreferences,
required List<String> args,
required String version,
required bool enableDiscord,
required bool allowDiscordRPC,
}) {
di
..registerLazySingleton<SharedPreferences>(() => sharedPreferences)
Expand All @@ -112,8 +108,9 @@ void registerServicesAndViewModels({
)
..registerLazySingleton<ExposeService>(
() => ExposeService(
discordRPC: enableDiscord ? di<FlutterDiscordRPC>() : null,
discordRPC: allowDiscordRPC ? di<FlutterDiscordRPC>() : null,
),
dispose: (s) => s.dispose(),
)
..registerLazySingleton<PlayerService>(
() => PlayerService(
Expand Down Expand Up @@ -181,7 +178,7 @@ void registerServicesAndViewModels({
..registerLazySingleton<PlayerModel>(
() => PlayerModel(
service: di<PlayerService>(),
connectivity: di<Connectivity>(),
onlineArtService: di<OnlineArtService>(),
)..init(),
dispose: (s) => s.dispose(),
)
Expand All @@ -190,6 +187,7 @@ void registerServicesAndViewModels({
appVersion: version,
gitHub: di<GitHub>(),
settingsService: di<SettingsService>(),
exposeService: di<ExposeService>(),
allowManualUpdates: Platform.isLinux ? false : true,
),
dispose: (s) => s.dispose(),
Expand Down Expand Up @@ -227,11 +225,5 @@ void registerServicesAndViewModels({
libraryService: di<LibraryService>(),
localAudioService: di<LocalAudioService>(),
)..init(),
)
..registerLazySingleton<OnlineArtModel>(
() => OnlineArtModel(
service: di<OnlineArtService>(),
),
dispose: (m) => m.dispose(),
);
}
Loading