Skip to content

Commit

Permalink
Moved auth logic to expose service and logic to app model
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicRaptor committed Nov 13, 2024
1 parent e08c629 commit d3fa726
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
10 changes: 10 additions & 0 deletions lib/app/app_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ class AppModel extends SafeChangeNotifier {
Stream<String?> get errorStream => _exposeService.discordErrorStream;
Stream<bool> get isDiscordConnectedStream =>
_exposeService.isDiscordConnectedStream;
Stream<bool> get isLastFmAuthenticatedStream =>
_exposeService.isLastFmAuthenticatedStream;
//TODO: Do something(ex:change the text of the save button) once authenticated

Future<void> connectToDiscord() async => _exposeService.connectToDiscord();
Future<void> disconnectFromDiscord() async =>
_exposeService.disconnectFromDiscord();
Future<void> setLastFmAuth() async {
final sessionData = await _exposeService.setLastFmAuth();
if (sessionData != null) {
_settingsService.setLastFmSessionKey(sessionData['sessionKey']!);
_settingsService.setLastFmUsername(sessionData['username']!);
}
}

final GitHub _gitHub;
final SettingsService _settingsService;
Expand Down
35 changes: 35 additions & 0 deletions lib/expose/expose_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

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

class ExposeService {
ExposeService({
Expand All @@ -13,9 +14,11 @@ class ExposeService {
final FlutterDiscordRPC? _discordRPC;
final LastFMAuthorized? _lastFm;
final _errorController = StreamController<String?>.broadcast();
final _lastFmAuthController = StreamController<bool>.broadcast();
Stream<String?> get discordErrorStream => _errorController.stream;
Stream<bool> get isDiscordConnectedStream =>
_discordRPC?.isConnectedStream ?? Stream.value(false);
Stream<bool> get isLastFmAuthenticatedStream => _lastFmAuthController.stream;

Future<void> exposeTitleOnline({
required String title,
Expand All @@ -37,6 +40,37 @@ class ExposeService {
}
}

Future<Map<String, String>?> setLastFmAuth() async {
final lastfmua = _lastFm as LastFMUnauthorized;
launchUrl(
Uri.parse(await lastfmua.authorizeDesktop()),
);

const maxWaitDuration = Duration(minutes: 2); // Customize as needed
final startTime = DateTime.now();
await Future.delayed(const Duration(seconds: 10));
while (DateTime.now().difference(startTime) < maxWaitDuration) {
try {
final lastfm = await lastfmua.finishAuthorizeDesktop();
final sessionKey = lastfm.sessionKey;
final username = lastfm.username;
_updateLastFmAuthStatus(true);
return {
'sessionKey': sessionKey,
'username': username,
};
} catch (e) {
await Future.delayed(const Duration(seconds: 10));
}
}
_updateLastFmAuthStatus(false);
return null;
}

void _updateLastFmAuthStatus(bool status) {
_lastFmAuthController.add(status);
}

Future<void> _exposeTitleToDiscord({
required String title,
required String artist,
Expand Down Expand Up @@ -103,5 +137,6 @@ class ExposeService {
Future<void> dispose() async {
await disconnectFromDiscord();
await _errorController.close();
await _lastFmAuthController.close();
}
}
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ void registerServicesAndViewModels({
() => SettingsService(
sharedPreferences: di<SharedPreferences>(),
downloadsDefaultDir: downloadsDefaultDir,
lastFm: di<LastFM>(),
),
dispose: (s) async => s.dispose(),
)
Expand Down
1 change: 0 additions & 1 deletion lib/settings/settings_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ 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
29 changes: 1 addition & 28 deletions lib/settings/settings_service.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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 @@ -12,14 +10,11 @@ class SettingsService {
SettingsService({
required String? downloadsDefaultDir,
required SharedPreferences sharedPreferences,
required LastFM lastFm,
}) : _preferences = sharedPreferences,
_downloadsDefaultDir = downloadsDefaultDir,
_lastFm = lastFm;
_downloadsDefaultDir = downloadsDefaultDir;

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

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

Future<void> setLastFmAuth() async {
final lastfmua = _lastFm as LastFMUnauthorized;
launchUrl(
Uri.parse(await lastfmua.authorizeDesktop()),
);

const maxWaitDuration = Duration(minutes: 2); // Customize as needed
final startTime = DateTime.now();
await Future.delayed(const Duration(seconds: 10));
while (DateTime.now().difference(startTime) < maxWaitDuration) {
try {
final lastfm = await lastfmua.finishAuthorizeDesktop();
setLastFmSessionKey(lastfm.sessionKey);
setLastFmUsername(lastfm.username);
return;
} catch (e) {
await Future.delayed(const Duration(seconds: 10));
}
}
//TODO: Do something if it fails
}

bool get enableDiscordRPC => _preferences.getBool(kEnableDiscordRPC) ?? false;
void setEnableDiscordRPC(bool value) {
_preferences.setBool(kEnableDiscordRPC, value).then(
Expand Down
2 changes: 1 addition & 1 deletion lib/settings/view/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ class _ExposeOnlineSection extends StatelessWidget with WatchItMixin {
onPressed: () {
if (lastFmApiKeyController.text.isNotEmpty &&
lastFmSecretController.text.isNotEmpty) {
di<SettingsModel>().setLastFmAuth();
di<AppModel>().setLastFmAuth();
}
},
child: Text(l10n.save),
Expand Down

0 comments on commit d3fa726

Please sign in to comment.