import 'dart:async'; import 'dart:io'; import 'package:collection/collection.dart'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:injectable/injectable.dart'; import 'package:revanced_manager/app/app.locator.dart'; import 'package:revanced_manager/services/download_manager.dart'; import 'package:revanced_manager/services/manager_api.dart'; import 'package:synchronized/synchronized.dart'; import 'package:timeago/timeago.dart'; @lazySingleton class RevancedAPI { late final Dio _dio; late final DownloadManager _downloadManager = locator(); final Lock getToolsLock = Lock(); Future initialize(String repoUrl) async { _dio = _downloadManager.initDio(repoUrl); } Future clearAllCache() async { await _downloadManager.clearAllCache(); } Future>> getContributors() async { final Map> contributors = {}; try { final response = await _dio.get('/contributors'); final List repositories = response.data['repositories']; for (final Map repo in repositories) { final String name = repo['name']; contributors[name] = repo['contributors']; } } on Exception catch (e) { if (kDebugMode) { print(e); } return {}; } return contributors; } Future?> _getLatestRelease( String extension, String repoName, ) { if (!locator().getDownloadConsent()) { return Future(() => null); } return getToolsLock.synchronized(() async { try { final response = await _dio.get('/tools'); final List tools = response.data['tools']; return tools.firstWhereOrNull( (t) => (t['repository'] as String) == repoName && (t['name'] as String).endsWith(extension), ); } on Exception catch (e) { if (kDebugMode) { print(e); } return null; } }); } Future getLatestReleaseVersion( String extension, String repoName, ) async { try { final Map? release = await _getLatestRelease( extension, repoName, ); if (release != null) { return release['version']; } } on Exception catch (e) { if (kDebugMode) { print(e); } return null; } return null; } Future getLatestReleaseFile( String extension, String repoName, ) async { try { final Map? release = await _getLatestRelease( extension, repoName, ); if (release != null) { final String url = release['browser_download_url']; return await _downloadManager.getSingleFile(url); } } on Exception catch (e) { if (kDebugMode) { print(e); } return null; } return null; } StreamController managerUpdateProgress = StreamController.broadcast(); void updateManagerDownloadProgress(int progress) { managerUpdateProgress.add(progress.toDouble()); } Stream getManagerUpdateProgress() { return managerUpdateProgress.stream; } void disposeManagerUpdateProgress() { managerUpdateProgress.close(); } Future downloadManager() async { final Map? release = await _getLatestRelease( '.apk', 'revanced/revanced-manager', ); File? outputFile; await for (final result in _downloadManager.getFileStream( release!['browser_download_url'] as String, )) { if (result is DownloadProgress) { final totalSize = result.totalSize ?? 10000000; final progress = (result.downloaded / totalSize * 100).round(); updateManagerDownloadProgress(progress); } else if (result is FileInfo) { disposeManagerUpdateProgress(); // The download is complete; convert the FileInfo object to a File object outputFile = File(result.file.path); } } return outputFile; } Future getLatestReleaseTime( String extension, String repoName, ) async { try { final Map? release = await _getLatestRelease( extension, repoName, ); if (release != null) { final DateTime timestamp = DateTime.parse(release['timestamp'] as String); return format(timestamp, locale: 'en_short'); } } on Exception catch (e) { if (kDebugMode) { print(e); } return null; } return null; } }