diff --git a/lib/expose/expose_service.dart b/lib/expose/expose_service.dart index 553eb9867..9689bd094 100644 --- a/lib/expose/expose_service.dart +++ b/lib/expose/expose_service.dart @@ -8,8 +8,9 @@ class ExposeService { required FlutterDiscordRPC? discordRPC, required LastFMAuthorized? lastFm, required bool lastFmEnabled, - }) - : _discordRPC = discordRPC, _lastFm = lastFm, _lastFmEnabled = lastFmEnabled; + }) : _discordRPC = discordRPC, + _lastFm = lastFm, + _lastFmEnabled = lastFmEnabled; final FlutterDiscordRPC? _discordRPC; final LastFMAuthorized? _lastFm; @@ -31,7 +32,7 @@ class ExposeService { additionalInfo: additionalInfo, imageUrl: imageUrl, ); - if(_lastFmEnabled){ + if (_lastFmEnabled) { await _exposeTitleToLastfm( title: title, artist: artist, @@ -70,7 +71,7 @@ class ExposeService { Future _exposeTitleToLastfm({ required String title, required String artist, - }) async{ + }) async { try { await _lastFm?.scrobble( track: title, @@ -106,4 +107,4 @@ class ExposeService { await disconnectFromDiscord(); await _errorController.close(); } -} \ No newline at end of file +} diff --git a/lib/main.dart b/lib/main.dart index 1400bedd7..506c84ce7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -108,44 +108,42 @@ void registerServicesAndViewModels({ ), dispose: (s) => s.dispose(), ) - ..registerFactory( - (){ - final apiKey = sharedPreferences.getString(kLastFmApiKey) ?? ''; - final apiSecret = sharedPreferences.getString(klastFmSecret) ?? ''; - final sessionKey = sharedPreferences.getString(kLastFmSessionKey); - final username = sharedPreferences.getString(kLastFmUsername); + ..registerFactory(() { + final apiKey = sharedPreferences.getString(kLastFmApiKey) ?? ''; + final apiSecret = sharedPreferences.getString(klastFmSecret) ?? ''; + final sessionKey = sharedPreferences.getString(kLastFmSessionKey); + final username = sharedPreferences.getString(kLastFmUsername); - if (sessionKey != null && username != null) { - return LastFMAuthorized( - apiKey, secret: apiSecret, - sessionKey: sessionKey, - username: username, - ); - } else { - return LastFMUnauthorized(apiKey, apiSecret); - } - } - ) + if (sessionKey != null && username != null) { + return LastFMAuthorized( + apiKey, + secret: apiSecret, + sessionKey: sessionKey, + username: username, + ); + } else { + return LastFMUnauthorized(apiKey, apiSecret); + } + }) ..registerLazySingleton( - (){ - final sessionKey = sharedPreferences.getString(kLastFmSessionKey); - final lastFMEnabled = - sharedPreferences.getBool(kEnableLastFmScrobbling) ?? false; - if(sessionKey != null){ - return ExposeService( - discordRPC: allowDiscordRPC ? di() : null, - lastFm: di() as LastFMAuthorized, - lastFmEnabled: lastFMEnabled, - ); - } - else { - return ExposeService( - discordRPC: allowDiscordRPC ? di() : null, - lastFm: null, - lastFmEnabled: lastFMEnabled, - ); - } - }, + () { + final sessionKey = sharedPreferences.getString(kLastFmSessionKey); + final lastFMEnabled = + sharedPreferences.getBool(kEnableLastFmScrobbling) ?? false; + if (sessionKey != null) { + return ExposeService( + discordRPC: allowDiscordRPC ? di() : null, + lastFm: di() as LastFMAuthorized, + lastFmEnabled: lastFMEnabled, + ); + } else { + return ExposeService( + discordRPC: allowDiscordRPC ? di() : null, + lastFm: null, + lastFmEnabled: lastFMEnabled, + ); + } + }, dispose: (s) => s.dispose(), ) ..registerLazySingleton( diff --git a/lib/settings/settings_service.dart b/lib/settings/settings_service.dart index e992f2a38..dcc2395dd 100644 --- a/lib/settings/settings_service.dart +++ b/lib/settings/settings_service.dart @@ -45,35 +45,39 @@ class SettingsService { String? get lastFmUsername => _preferences.getString(kLastFmUsername); void setEnableLastFmScrobbling(bool value) { _preferences.setBool(kEnableLastFmScrobbling, value).then( - (saved) { + (saved) { if (saved) _propertiesChangedController.add(true); }, ); } + void setLastFmApiKey(String value) { _preferences.setString(kLastFmApiKey, value).then( - (saved) { + (saved) { if (saved) _propertiesChangedController.add(true); }, ); } + void setLastFmSecret(String value) { _preferences.setString(klastFmSecret, value).then( - (saved) { + (saved) { if (saved) _propertiesChangedController.add(true); }, ); } + void setLastFmSessionKey(String value) { _preferences.setString(kLastFmSessionKey, value).then( - (saved) { + (saved) { if (saved) _propertiesChangedController.add(true); }, ); } + void setLastFmUsername(String value) { _preferences.setString(kLastFmUsername, value).then( - (saved) { + (saved) { if (saved) _propertiesChangedController.add(true); }, ); diff --git a/lib/settings/view/settings_page.dart b/lib/settings/view/settings_page.dart index 356ef4c5a..7f307e6f3 100644 --- a/lib/settings/view/settings_page.dart +++ b/lib/settings/view/settings_page.dart @@ -1,4 +1,3 @@ - import 'package:flutter/material.dart'; import 'package:flutter_tabler_icons/flutter_tabler_icons.dart'; import 'package:lastfm/lastfm.dart'; @@ -99,7 +98,7 @@ class _ThemeSection extends StatelessWidget with WatchItMixin { Padding( padding: const EdgeInsets.all(8.0), child: - Text(ThemeMode.values[i].localize(context.l10n)), + Text(ThemeMode.values[i].localize(context.l10n)), ), ], ), @@ -113,7 +112,7 @@ class _ThemeSection extends StatelessWidget with WatchItMixin { trailing: CommonSwitch( onChanged: di().setUseMoreAnimations, value: - watchPropertyValue((SettingsModel m) => m.useMoreAnimations), + watchPropertyValue((SettingsModel m) => m.useMoreAnimations), ), ), ], @@ -134,7 +133,7 @@ class _CloseActionSection extends StatelessWidget with WatchItMixin { final model = di(); final closeBtnAction = - watchPropertyValue((SettingsModel m) => m.closeBtnActionIndex); + watchPropertyValue((SettingsModel m) => m.closeBtnActionIndex); return YaruSection( margin: const EdgeInsets.only( left: kYaruPagePadding, @@ -159,7 +158,7 @@ class _CloseActionSection extends StatelessWidget with WatchItMixin { PopupMenuItem( value: CloseBtnAction.values[i], child: - Text(CloseBtnAction.values[i].localize(context.l10n)), + Text(CloseBtnAction.values[i].localize(context.l10n)), ), ]; }, @@ -206,11 +205,11 @@ class _PodcastSectionState extends State<_PodcastSection> { final l10n = context.l10n; final model = di(); final usePodcastIndex = - watchPropertyValue((SettingsModel m) => m.usePodcastIndex); + watchPropertyValue((SettingsModel m) => m.usePodcastIndex); final podcastIndexApiKey = - watchPropertyValue((SettingsModel m) => m.podcastIndexApiKey); + watchPropertyValue((SettingsModel m) => m.podcastIndexApiKey); final podcastIndexApiSecret = - watchPropertyValue((SettingsModel m) => m.podcastIndexApiSecret); + watchPropertyValue((SettingsModel m) => m.podcastIndexApiSecret); return YaruSection( margin: const EdgeInsets.all(kYaruPagePadding), @@ -357,15 +356,15 @@ class _LocalAudioSection extends StatelessWidget with WatchItMixin { final settingsModel = di(); final localAudioModel = di(); final directory = - watchPropertyValue((SettingsModel m) => m.directory ?? ''); + watchPropertyValue((SettingsModel m) => m.directory ?? ''); Future onDirectorySelected(String directoryPath) async { settingsModel.setDirectory(directoryPath).then( (_) async => localAudioModel.init( - forceInit: true, - directory: directoryPath, - ), - ); + forceInit: true, + directory: directoryPath, + ), + ); } return YaruSection( @@ -438,44 +437,44 @@ class _AboutTileState extends State<_AboutTile> { final theme = context.theme; final appModel = di(); final updateAvailable = - watchPropertyValue((AppModel m) => m.updateAvailable); + watchPropertyValue((AppModel m) => m.updateAvailable); final onlineVersion = watchPropertyValue((AppModel m) => m.onlineVersion); final currentVersion = watchPropertyValue((AppModel m) => m.version); return YaruTile( title: !di().isOnline == true || - !appModel.allowManualUpdate + !appModel.allowManualUpdate ? Text(di().version ?? '') : updateAvailable == null - ? Center( - child: SizedBox.square( - dimension: yaruStyled ? kYaruTitleBarItemHeight : 40, - child: const Progress( - padding: EdgeInsets.all(10), - ), - ), - ) - : TapAbleText( - text: updateAvailable == true - ? '${context.l10n.updateAvailable}: $onlineVersion' - : currentVersion ?? context.l10n.unknown, - style: updateAvailable == true - ? TextStyle( - color: context.theme.colorScheme.success - .scale(lightness: theme.isLight ? 0 : 0.3), - ) - : null, - onTap: () => launchUrl( - Uri.parse( - p.join( - kRepoUrl, - 'releases', - 'tag', - onlineVersion, - ), - ), - ), - ), + ? Center( + child: SizedBox.square( + dimension: yaruStyled ? kYaruTitleBarItemHeight : 40, + child: const Progress( + padding: EdgeInsets.all(10), + ), + ), + ) + : TapAbleText( + text: updateAvailable == true + ? '${context.l10n.updateAvailable}: $onlineVersion' + : currentVersion ?? context.l10n.unknown, + style: updateAvailable == true + ? TextStyle( + color: context.theme.colorScheme.success + .scale(lightness: theme.isLight ? 0 : 0.3), + ) + : null, + onTap: () => launchUrl( + Uri.parse( + p.join( + kRepoUrl, + 'releases', + 'tag', + onlineVersion, + ), + ), + ), + ), trailing: OutlinedButton( onPressed: () => settingsNavigatorKey.currentState?.pushNamed('/about'), child: Text(context.l10n.contributors), @@ -515,19 +514,19 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin { : false; final lastFmEnabled = - watchPropertyValue((SettingsModel m) => m.enableLastFmScrobbling); + watchPropertyValue((SettingsModel m) => m.enableLastFmScrobbling); final lastFmApiKey = - watchPropertyValue((SettingsModel m) => m.lastFmApiKey); + watchPropertyValue((SettingsModel m) => m.lastFmApiKey); final lastFmSecret = - watchPropertyValue((SettingsModel m) => m.lastFmSecret); + watchPropertyValue((SettingsModel m) => m.lastFmSecret); final TextEditingController lastFmApiKeyController = - TextEditingController(text: lastFmApiKey); + TextEditingController(text: lastFmApiKey); final TextEditingController lastFmSecretController = - TextEditingController(text: lastFmSecret); + TextEditingController(text: lastFmSecret); final _formkey = GlobalKey(); @@ -546,12 +545,12 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin { children: [ allowDiscordRPC ? const Icon( - TablerIcons.brand_discord_filled, - ) + TablerIcons.brand_discord_filled, + ) : Icon( - TablerIcons.brand_discord_filled, - color: context.theme.disabledColor, - ), + TablerIcons.brand_discord_filled, + color: context.theme.disabledColor, + ), Text(l10n.exposeToDiscordTitle), ], ), @@ -565,14 +564,14 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin { value: discordEnabled, onChanged: allowDiscordRPC ? (v) { - di().setEnableDiscordRPC(v); - final appModel = di(); - if (v) { - appModel.connectToDiscord(); - } else { - appModel.disconnectFromDiscord(); - } - } + di().setEnableDiscordRPC(v); + final appModel = di(); + if (v) { + appModel.connectToDiscord(); + } else { + appModel.disconnectFromDiscord(); + } + } : null, ), ), @@ -608,8 +607,8 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin { } return null; }, - onFieldSubmitted: (value) async{ - if(_formkey.currentState!.validate()){ + onFieldSubmitted: (value) async { + if (_formkey.currentState!.validate()) { di().setLastFmApiKey(value); } }, @@ -622,14 +621,14 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin { decoration: InputDecoration( hintText: l10n.lastfmSecret, ), - validator: (value){ + validator: (value) { if (value == null || value.isEmpty) { return l10n.lastfmSecretEmpty; } return null; }, - onFieldSubmitted: (value) async{ - if(_formkey.currentState!.validate()){ + onFieldSubmitted: (value) async { + if (_formkey.currentState!.validate()) { di().setLastFmSecret(value); } }, @@ -649,22 +648,26 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin { di().setEnableLastFmScrobbling(v); }, ), - if(lastFmEnabled) + if (lastFmEnabled) ImportantButton( - onPressed: () async{ - if(lastFmApiKeyController.text.isNotEmpty && lastFmSecretController.text.isNotEmpty){ + onPressed: () async { + if (lastFmApiKeyController.text.isNotEmpty && + lastFmSecretController.text.isNotEmpty) { final lastfmua = di() as LastFMUnauthorized; - launchUrl(Uri.parse(await lastfmua.authorizeDesktop())); + launchUrl( + Uri.parse(await lastfmua.authorizeDesktop())); await Future.delayed(const Duration(seconds: 20)); - final lastfm = await lastfmua.finishAuthorizeDesktop(); - di().setLastFmSessionKey(lastfm.sessionKey); - di().setLastFmUsername(lastfm.username); + final lastfm = + await lastfmua.finishAuthorizeDesktop(); + di() + .setLastFmSessionKey(lastfm.sessionKey); + di() + .setLastFmUsername(lastfm.username); di.unregister(); di.registerFactory(() => lastfm); } }, - child: Text(l10n.save) - ), + child: Text(l10n.save)), ], ), ) @@ -672,4 +675,4 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin { ), ); } -} \ No newline at end of file +}