2023-01-30 13:35:06 +01:00
|
|
|
import 'dart:developer';
|
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-04-18 09:57:26 +02:00
|
|
|
import 'package:dio_cache_interceptor/dio_cache_interceptor.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-04-18 09:57:26 +02:00
|
|
|
import 'package:native_dio_adapter/native_dio_adapter.dart';
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'package:revanced_manager/models/patch.dart';
|
2022-10-11 16:49:50 +02:00
|
|
|
import 'package:revanced_manager/utils/check_for_gms.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 {
|
2022-09-19 01:28:26 +02:00
|
|
|
late Dio _dio = Dio();
|
2023-04-18 16:15:29 +02:00
|
|
|
|
2023-04-18 09:57:26 +02:00
|
|
|
final _cacheOptions = CacheOptions(
|
|
|
|
store: MemCacheStore(),
|
2022-09-28 18:56:54 +02:00
|
|
|
maxStale: const Duration(days: 1),
|
2023-04-18 09:57:26 +02:00
|
|
|
priority: CachePriority.high,
|
2022-09-11 03:01:06 +02:00
|
|
|
);
|
|
|
|
|
2022-09-19 01:28:26 +02:00
|
|
|
Future<void> initialize(String apiUrl) async {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool isGMSInstalled = await checkForGMS();
|
2022-10-11 16:49:50 +02:00
|
|
|
|
2022-10-14 20:05:33 +02:00
|
|
|
if (!isGMSInstalled) {
|
2023-01-30 13:35:06 +01:00
|
|
|
_dio = Dio(
|
|
|
|
BaseOptions(
|
|
|
|
baseUrl: apiUrl,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
log('ReVanced API: Using default engine + $isGMSInstalled');
|
2022-10-14 20:05:33 +02:00
|
|
|
} else {
|
2023-04-18 09:57:26 +02:00
|
|
|
if (Platform.isIOS || Platform.isMacOS || Platform.isAndroid) {
|
|
|
|
final CronetEngine androidCronetEngine = await CronetEngine.build(
|
|
|
|
userAgent: 'ReVanced Manager',
|
|
|
|
enableBrotli: true,
|
|
|
|
enableQuic: true,
|
|
|
|
);
|
|
|
|
_dio.httpClientAdapter =
|
|
|
|
NativeAdapter(androidCronetEngine: androidCronetEngine);
|
|
|
|
|
|
|
|
_dio = Dio(
|
|
|
|
BaseOptions(
|
|
|
|
baseUrl: apiUrl,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-30 13:35:06 +01:00
|
|
|
log('ReVanced API: Using CronetEngine + $isGMSInstalled');
|
2022-10-14 20:05:33 +02:00
|
|
|
}
|
2023-04-18 09:57:26 +02:00
|
|
|
_dio.interceptors.add(DioCacheInterceptor(options: _cacheOptions));
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-10-11 16:49:50 +02:00
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clearAllCache() async {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
2023-04-18 09:57:26 +02:00
|
|
|
await _cacheOptions.store!.clean();
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-19 01:28:26 +02:00
|
|
|
Future<List<Patch>> getPatches() async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2023-04-18 09:57:26 +02:00
|
|
|
final response = await _dio.get('/patches');
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<dynamic> patches = response.data;
|
2022-09-11 03:01:06 +02:00
|
|
|
return patches.map((patch) => Patch.fromJson(patch)).toList();
|
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 List.empty();
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>?> _getLatestRelease(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
|
|
|
try {
|
2023-04-18 09:57:26 +02:00
|
|
|
final response = await _dio.get('/tools');
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<dynamic> tools = response.data['tools'];
|
2022-09-11 03:01:06 +02:00
|
|
|
return tools.firstWhereOrNull(
|
|
|
|
(t) =>
|
|
|
|
t['repository'] == repoName &&
|
|
|
|
(t['name'] as String).endsWith(extension),
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'];
|
2022-09-11 03:01:06 +02:00
|
|
|
return await DefaultCacheManager().getSingleFile(url);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|