правки лаунчера
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import 'package:moonwell_launcher/features/launcher/data/launcher_api_client.dart';
|
||||
import 'package:moonwell_launcher/features/launcher/domain/entities/client_manifest.dart';
|
||||
import 'package:moonwell_launcher/features/launcher/domain/entities/launcher_session.dart';
|
||||
import 'package:moonwell_launcher/features/preferences/domain/repositories/preferences_repository.dart';
|
||||
|
||||
final class RestoreLauncherSessionResult {
|
||||
final LauncherSession? session;
|
||||
final ClientManifest? manifest;
|
||||
|
||||
const RestoreLauncherSessionResult._({
|
||||
required this.session,
|
||||
required this.manifest,
|
||||
});
|
||||
|
||||
const RestoreLauncherSessionResult.unauthenticated()
|
||||
: this._(session: null, manifest: null);
|
||||
|
||||
const RestoreLauncherSessionResult.authenticated({
|
||||
required LauncherSession session,
|
||||
required ClientManifest manifest,
|
||||
}) : this._(session: session, manifest: manifest);
|
||||
|
||||
bool get isAuthenticated => session != null && manifest != null;
|
||||
}
|
||||
|
||||
class RestoreLauncherSessionUseCase {
|
||||
RestoreLauncherSessionUseCase({
|
||||
required LauncherApiClient launcherApiClient,
|
||||
required PreferencesRepository preferencesRepository,
|
||||
}) : _launcherApiClient = launcherApiClient,
|
||||
_preferencesRepository = preferencesRepository;
|
||||
|
||||
final LauncherApiClient _launcherApiClient;
|
||||
final PreferencesRepository _preferencesRepository;
|
||||
|
||||
Future<RestoreLauncherSessionResult> call() async {
|
||||
final savedSession = await _preferencesRepository.getLauncherSession();
|
||||
if (savedSession == null || !savedSession.hasAccessToken) {
|
||||
return const RestoreLauncherSessionResult.unauthenticated();
|
||||
}
|
||||
|
||||
if (savedSession.isExpired) {
|
||||
await _preferencesRepository.clearLauncherSession();
|
||||
return const RestoreLauncherSessionResult.unauthenticated();
|
||||
}
|
||||
|
||||
try {
|
||||
final manifest = await _launcherApiClient.fetchManifest(
|
||||
savedSession.accessToken,
|
||||
);
|
||||
return RestoreLauncherSessionResult.authenticated(
|
||||
session: savedSession,
|
||||
manifest: manifest,
|
||||
);
|
||||
} catch (_) {
|
||||
return const RestoreLauncherSessionResult.unauthenticated();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,25 @@ final class LauncherSession {
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return <String, dynamic>{
|
||||
'access_token': accessToken,
|
||||
'token_type': tokenType,
|
||||
'expires_at': expiresAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
bool get hasAccessToken => accessToken.trim().isNotEmpty;
|
||||
|
||||
bool get isExpired {
|
||||
final expiresAt = this.expiresAt;
|
||||
if (expiresAt == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !expiresAt.toUtc().isAfter(DateTime.now().toUtc());
|
||||
}
|
||||
|
||||
static DateTime? _parseDateTime(String? rawValue) {
|
||||
if (rawValue == null || rawValue.isEmpty) {
|
||||
return null;
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:moonwell_launcher/features/launcher/domain/entities/launcher_session.dart';
|
||||
import 'package:moonwell_launcher/features/preferences/domain/repositories/preferences_repository.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
const _outputDirKey = 'output_dir';
|
||||
const _launcherSessionKey = 'launcher_session';
|
||||
|
||||
@LazySingleton(as: PreferencesRepository, env: ['flutter'])
|
||||
class SharedPrefsPreferencesRepository implements PreferencesRepository {
|
||||
@@ -27,4 +31,37 @@ class SharedPrefsPreferencesRepository implements PreferencesRepository {
|
||||
Future<void> setOutputDir(Uri uri) {
|
||||
return _preferences.setString(_outputDirKey, uri.toString());
|
||||
}
|
||||
|
||||
@override
|
||||
Future<LauncherSession?> getLauncherSession() {
|
||||
final rawSession = _preferences.getString(_launcherSessionKey);
|
||||
if (rawSession == null || rawSession.isEmpty) {
|
||||
return Future.value(null);
|
||||
}
|
||||
|
||||
try {
|
||||
final decoded = jsonDecode(rawSession);
|
||||
if (decoded is! Map) {
|
||||
return Future.value(null);
|
||||
}
|
||||
|
||||
final json = decoded.map((key, value) => MapEntry(key.toString(), value));
|
||||
return Future.value(LauncherSession.fromJson(json));
|
||||
} catch (_) {
|
||||
return Future.value(null);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setLauncherSession(LauncherSession session) {
|
||||
return _preferences.setString(
|
||||
_launcherSessionKey,
|
||||
jsonEncode(session.toJson()),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clearLauncherSession() {
|
||||
return _preferences.remove(_launcherSessionKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
import 'package:moonwell_launcher/features/launcher/domain/entities/launcher_session.dart';
|
||||
|
||||
abstract interface class PreferencesRepository {
|
||||
Future<Uri?> getOutputDir();
|
||||
|
||||
Future<void> setOutputDir(Uri uri);
|
||||
|
||||
Future<LauncherSession?> getLauncherSession();
|
||||
|
||||
Future<void> setLauncherSession(LauncherSession session);
|
||||
|
||||
Future<void> clearLauncherSession();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user