mirror of
https://github.com/revanced/revanced-manager
synced 2024-05-14 13:56:57 +02:00
* feat: restart app toast when changing sources, api url * fix: potentially fix manager stuck on black screen * feat: remove select all patches chip * feat: show all apps and recommended versions * chore(i18n): remove unused strings remove unused strings left out in7e05bca
* feat: select install type before patching * feat: update patches button (#782) * feat: update patches button * feat: show toast when force refresh * chore: don't translate "ReVanced Manager" and "ReVanced Patches" * Revert "feat: select install type before patching" This reverts commit74e0c09b54
. * feat: rename recommended patches to default patches * feat: add missing localization * feat: display restart app toast for resetting source --------- Co-authored-by: EvadeMaster <93124920+EvadeMaster@users.noreply.github.com>
187 lines
4.7 KiB
Dart
187 lines
4.7 KiB
Dart
import 'dart:developer';
|
|
import 'dart:io';
|
|
|
|
import 'package:collection/collection.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:native_dio_adapter/native_dio_adapter.dart';
|
|
import 'package:revanced_manager/models/patch.dart';
|
|
import 'package:revanced_manager/utils/check_for_gms.dart';
|
|
import 'package:timeago/timeago.dart';
|
|
|
|
@lazySingleton
|
|
class RevancedAPI {
|
|
late Dio _dio = Dio();
|
|
|
|
final _cacheOptions = CacheOptions(
|
|
store: MemCacheStore(),
|
|
maxStale: const Duration(days: 1),
|
|
priority: CachePriority.high,
|
|
);
|
|
|
|
Future<void> initialize(String apiUrl) async {
|
|
try {
|
|
final bool isGMSInstalled = await checkForGMS();
|
|
|
|
if (!isGMSInstalled) {
|
|
_dio = Dio(
|
|
BaseOptions(
|
|
baseUrl: apiUrl,
|
|
),
|
|
);
|
|
log('ReVanced API: Using default engine + $isGMSInstalled');
|
|
} else {
|
|
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,
|
|
),
|
|
);
|
|
}
|
|
|
|
log('ReVanced API: Using CronetEngine + $isGMSInstalled');
|
|
}
|
|
_dio.interceptors.add(DioCacheInterceptor(options: _cacheOptions));
|
|
} on Exception catch (e) {
|
|
if (kDebugMode) {
|
|
print(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> clearAllCache() async {
|
|
try {
|
|
await _cacheOptions.store!.clean();
|
|
} on Exception catch (e) {
|
|
if (kDebugMode) {
|
|
print(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<Map<String, List<dynamic>>> getContributors() async {
|
|
final Map<String, List<dynamic>> contributors = {};
|
|
try {
|
|
final response = await _dio.get('/contributors');
|
|
final List<dynamic> repositories = response.data['repositories'];
|
|
for (final Map<String, dynamic> repo in repositories) {
|
|
final String name = repo['name'];
|
|
contributors[name] = repo['contributors'];
|
|
}
|
|
} on Exception catch (e) {
|
|
if (kDebugMode) {
|
|
print(e);
|
|
}
|
|
return {};
|
|
}
|
|
return contributors;
|
|
}
|
|
|
|
Future<List<Patch>> getPatches() async {
|
|
try {
|
|
final response = await _dio.get('/patches');
|
|
final List<dynamic> patches = response.data;
|
|
return patches.map((patch) => Patch.fromJson(patch)).toList();
|
|
} on Exception catch (e) {
|
|
if (kDebugMode) {
|
|
print(e);
|
|
}
|
|
return List.empty();
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>?> _getLatestRelease(
|
|
String extension,
|
|
String repoName,
|
|
) 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;
|
|
}
|
|
}
|
|
|
|
Future<String?> getLatestReleaseVersion(
|
|
String extension,
|
|
String repoName,
|
|
) async {
|
|
try {
|
|
final Map<String, dynamic>? release = await _getLatestRelease(
|
|
extension,
|
|
repoName,
|
|
);
|
|
if (release != null) {
|
|
return release['version'];
|
|
}
|
|
} on Exception catch (e) {
|
|
if (kDebugMode) {
|
|
print(e);
|
|
}
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<File?> getLatestReleaseFile(String extension, String repoName) async {
|
|
try {
|
|
final Map<String, dynamic>? release = await _getLatestRelease(
|
|
extension,
|
|
repoName,
|
|
);
|
|
if (release != null) {
|
|
final String url = release['browser_download_url'];
|
|
return await DefaultCacheManager().getSingleFile(url);
|
|
}
|
|
} on Exception catch (e) {
|
|
if (kDebugMode) {
|
|
print(e);
|
|
}
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<String?> getLatestReleaseTime(
|
|
String extension,
|
|
String repoName,
|
|
) async {
|
|
try {
|
|
final Map<String, dynamic>? 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;
|
|
}
|
|
}
|