2023-04-20 01:15:46 +02:00
|
|
|
import 'dart:async';
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'dart:io';
|
2023-01-30 13:35:06 +01:00
|
|
|
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
2023-03-05 10:12:46 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
|
|
import 'package:injectable/injectable.dart';
|
2023-10-15 11:51:31 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
|
|
|
import 'package:revanced_manager/services/download_manager.dart';
|
2023-09-29 18:39:07 +02:00
|
|
|
import 'package:synchronized/synchronized.dart';
|
2023-01-30 13:35:06 +01:00
|
|
|
import 'package:timeago/timeago.dart';
|
2022-09-11 03:01:06 +02:00
|
|
|
|
|
|
|
@lazySingleton
|
|
|
|
class RevancedAPI {
|
2023-10-15 11:51:31 +02:00
|
|
|
late final Dio _dio;
|
|
|
|
late final DownloadManager _downloadManager = locator<DownloadManager>();
|
2023-04-20 01:15:46 +02:00
|
|
|
|
2023-09-29 18:39:07 +02:00
|
|
|
final Lock getToolsLock = Lock();
|
|
|
|
|
2023-10-15 11:51:31 +02:00
|
|
|
Future<void> initialize(String repoUrl) async {
|
|
|
|
_dio = _downloadManager.initDio(repoUrl);
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clearAllCache() async {
|
2023-10-15 11:51:31 +02:00
|
|
|
await _downloadManager.clearAllCache();
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 01:28:26 +02:00
|
|
|
Future<Map<String, List<dynamic>>> getContributors() async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Map<String, List<dynamic>> contributors = {};
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2023-04-18 09:57:26 +02:00
|
|
|
final response = await _dio.get('/contributors');
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<dynamic> repositories = response.data['repositories'];
|
|
|
|
for (final Map<String, dynamic> repo in repositories) {
|
|
|
|
final String name = repo['name'];
|
2022-09-11 03:01:06 +02:00
|
|
|
contributors[name] = repo['contributors'];
|
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-12 10:18:03 +02:00
|
|
|
return {};
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
return contributors;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>?> _getLatestRelease(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
2023-09-29 18:39:07 +02:00
|
|
|
) {
|
|
|
|
return getToolsLock.synchronized(() async {
|
|
|
|
try {
|
|
|
|
final response = await _dio.get('/tools');
|
|
|
|
final List<dynamic> tools = response.data['tools'];
|
|
|
|
return tools.firstWhereOrNull(
|
|
|
|
(t) =>
|
|
|
|
t['repository'] == repoName &&
|
|
|
|
(t['name'] as String).endsWith(extension),
|
|
|
|
);
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return null;
|
2023-03-05 10:12:46 +01:00
|
|
|
}
|
2023-09-29 18:39:07 +02:00
|
|
|
});
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<String?> getLatestReleaseVersion(
|
2022-09-18 19:42:30 +02:00
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Map<String, dynamic>? release = await _getLatestRelease(
|
2022-09-19 01:28:26 +02:00
|
|
|
extension,
|
|
|
|
repoName,
|
|
|
|
);
|
2022-09-11 03:01:06 +02:00
|
|
|
if (release != null) {
|
|
|
|
return release['version'];
|
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-04-18 16:15:29 +02:00
|
|
|
Future<File?> getLatestReleaseFile(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Map<String, dynamic>? release = await _getLatestRelease(
|
2022-09-11 03:01:06 +02:00
|
|
|
extension,
|
|
|
|
repoName,
|
|
|
|
);
|
|
|
|
if (release != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String url = release['browser_download_url'];
|
2023-10-15 11:51:31 +02:00
|
|
|
return await _downloadManager.getSingleFile(url);
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-07-10 14:36:50 +02:00
|
|
|
StreamController<double> managerUpdateProgress =
|
|
|
|
StreamController<double>.broadcast();
|
2023-04-20 01:15:46 +02:00
|
|
|
|
|
|
|
void updateManagerDownloadProgress(int progress) {
|
|
|
|
managerUpdateProgress.add(progress.toDouble());
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<double> getManagerUpdateProgress() {
|
|
|
|
return managerUpdateProgress.stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disposeManagerUpdateProgress() {
|
|
|
|
managerUpdateProgress.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<File?> downloadManager() async {
|
|
|
|
final Map<String, dynamic>? release = await _getLatestRelease(
|
|
|
|
'.apk',
|
|
|
|
'revanced/revanced-manager',
|
|
|
|
);
|
|
|
|
File? outputFile;
|
2023-10-15 11:51:31 +02:00
|
|
|
await for (final result in _downloadManager.getFileStream(
|
2023-04-20 01:15:46 +02:00
|
|
|
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) {
|
2023-06-23 15:36:36 +02:00
|
|
|
disposeManagerUpdateProgress();
|
2023-04-20 01:15:46 +02:00
|
|
|
// The download is complete; convert the FileInfo object to a File object
|
|
|
|
outputFile = File(result.file.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outputFile;
|
|
|
|
}
|
|
|
|
|
2022-09-11 03:01:06 +02:00
|
|
|
Future<String?> getLatestReleaseTime(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Map<String, dynamic>? release = await _getLatestRelease(
|
2022-09-11 03:01:06 +02:00
|
|
|
extension,
|
|
|
|
repoName,
|
|
|
|
);
|
|
|
|
if (release != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final DateTime timestamp =
|
|
|
|
DateTime.parse(release['timestamp'] as String);
|
2022-09-11 03:01:06 +02:00
|
|
|
return format(timestamp, locale: 'en_short');
|
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|