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

Basic last.fm integration #1000

Merged
merged 8 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 2 additions & 5 deletions lib/expose/expose_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ class ExposeService {
ExposeService({
required FlutterDiscordRPC? discordRPC,
required LastFMAuthorized? lastFm,
required bool lastFmEnabled,
}) : _discordRPC = discordRPC,
_lastFm = lastFm,
_lastFmEnabled = lastFmEnabled;
_lastFm = lastFm;

final FlutterDiscordRPC? _discordRPC;
final LastFMAuthorized? _lastFm;
final bool _lastFmEnabled;
final _errorController = StreamController<String?>.broadcast();
Stream<String?> get discordErrorStream => _errorController.stream;
Stream<bool> get isDiscordConnectedStream =>
Expand All @@ -32,7 +29,7 @@ class ExposeService {
additionalInfo: additionalInfo,
imageUrl: imageUrl,
);
if (_lastFmEnabled) {
if (_lastFm != null) {
await _exposeTitleToLastfm(
title: title,
artist: artist,
Expand Down
5 changes: 2 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,12 @@ void registerServicesAndViewModels({
if (sessionKey != null) {
return ExposeService(
discordRPC: allowDiscordRPC ? di<FlutterDiscordRPC>() : null,
lastFm: di<LastFM>() as LastFMAuthorized,
lastFmEnabled: lastFMEnabled,
lastFm: lastFMEnabled ? di<LastFM>() as LastFMAuthorized : null,
);
} else {
return ExposeService(
discordRPC: allowDiscordRPC ? di<FlutterDiscordRPC>() : null,
lastFm: null,
Feichtmeier marked this conversation as resolved.
Show resolved Hide resolved
lastFmEnabled: lastFMEnabled,
);
}
},
Expand All @@ -162,6 +160,7 @@ void registerServicesAndViewModels({
() => SettingsService(
sharedPreferences: di<SharedPreferences>(),
downloadsDefaultDir: downloadsDefaultDir,
lastFm: di<LastFM>(),
),
dispose: (s) async => s.dispose(),
)
Expand Down
1 change: 1 addition & 0 deletions lib/settings/settings_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class SettingsModel extends SafeChangeNotifier {
void setLastFmSecret(String value) => _service.setLastFmSecret(value);
void setLastFmSessionKey(String value) => _service.setLastFmSessionKey(value);
void setLastFmUsername(String value) => _service.setLastFmUsername(value);
Future<void> setLastFmAuth() async => _service.setLastFmAuth();

bool get useMoreAnimations => _service.useMoreAnimations;
void setUseMoreAnimations(bool value) => _service.setUseMoreAnimations(value);
Expand Down
18 changes: 17 additions & 1 deletion lib/settings/settings_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:async';
import 'dart:io';

import 'package:lastfm/lastfm.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';

import '../common/data/close_btn_action.dart';
import '../constants.dart';
Expand All @@ -10,11 +12,14 @@ class SettingsService {
SettingsService({
required String? downloadsDefaultDir,
required SharedPreferences sharedPreferences,
required LastFM lastFm,
}) : _preferences = sharedPreferences,
_downloadsDefaultDir = downloadsDefaultDir;
_downloadsDefaultDir = downloadsDefaultDir,
_lastFm = lastFm;

final String? _downloadsDefaultDir;
final SharedPreferences _preferences;
final LastFM _lastFm;
final _propertiesChangedController = StreamController<bool>.broadcast();
Stream<bool> get propertiesChanged => _propertiesChangedController.stream;

Expand Down Expand Up @@ -83,6 +88,17 @@ class SettingsService {
);
}

Future<void> setLastFmAuth() async {
Feichtmeier marked this conversation as resolved.
Show resolved Hide resolved
final lastfmua = _lastFm as LastFMUnauthorized;
launchUrl(
Uri.parse(await lastfmua.authorizeDesktop()),
);
await Future.delayed(const Duration(seconds: 20));
final lastfm = await lastfmua.finishAuthorizeDesktop();
setLastFmSessionKey(lastfm.sessionKey);
setLastFmUsername(lastfm.username);
}

bool get enableDiscordRPC => _preferences.getBool(kEnableDiscordRPC) ?? false;
void setEnableDiscordRPC(bool value) {
_preferences.setBool(kEnableDiscordRPC, value).then(
Expand Down
15 changes: 2 additions & 13 deletions lib/settings/view/settings_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
import 'package:lastfm/lastfm.dart';
import 'package:path/path.dart' as p;
import 'package:url_launcher/url_launcher.dart';
import 'package:watch_it/watch_it.dart';
Expand Down Expand Up @@ -650,20 +649,10 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin {
),
if (lastFmEnabled)
ImportantButton(
onPressed: () async {
onPressed: () {
if (lastFmApiKeyController.text.isNotEmpty &&
lastFmSecretController.text.isNotEmpty) {
final lastfmua = di<LastFM>() as LastFMUnauthorized;
launchUrl(
Uri.parse(await lastfmua.authorizeDesktop()),
);
await Future.delayed(const Duration(seconds: 20));
final lastfm = await lastfmua.finishAuthorizeDesktop();
di<SettingsModel>()
.setLastFmSessionKey(lastfm.sessionKey);
di<SettingsModel>().setLastFmUsername(lastfm.username);
di.unregister<LastFM>();
di.registerFactory<LastFM>(() => lastfm);
di<SettingsModel>().setLastFmAuth();
}
},
child: Text(l10n.save),
Expand Down