From c2021d508eb05bfd48d3b65d8ff440dd41311273 Mon Sep 17 00:00:00 2001 From: Aunali321 Date: Thu, 16 Mar 2023 19:44:13 +0530 Subject: [PATCH] fix: potentially fix manager stuck on black screen --- lib/services/github_api.dart | 33 +++++++++++++++++++------- lib/services/revanced_api.dart | 42 ++++++++++++++++++++++------------ pubspec.yaml | 7 +++--- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/lib/services/github_api.dart b/lib/services/github_api.dart index 0af9fbb1..74bbde69 100644 --- a/lib/services/github_api.dart +++ b/lib/services/github_api.dart @@ -3,20 +3,23 @@ import 'dart:io'; import 'package:collection/collection.dart'; import 'package:dio/dio.dart'; -import 'package:dio_http_cache_lts/dio_http_cache_lts.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'; @lazySingleton class GithubAPI { late Dio _dio = Dio(); - final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig()); - final Options _cacheOptions = buildCacheOptions( - const Duration(hours: 6), + + final _cacheOptions = CacheOptions( + store: MemCacheStore(), maxStale: const Duration(days: 1), + priority: CachePriority.high, ); + final Map repoAppPath = { 'com.google.android.youtube': 'youtube', 'com.google.android.apps.youtube.music': 'music', @@ -30,13 +33,29 @@ class GithubAPI { Future initialize(String repoUrl) async { try { + 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: repoUrl, + ), + ); + } + _dio = Dio( BaseOptions( baseUrl: repoUrl, ), ); - _dio.interceptors.add(_dioCacheManager.interceptor); + _dio.interceptors.add(DioCacheInterceptor(options: _cacheOptions)); } on Exception catch (e) { if (kDebugMode) { print(e); @@ -46,7 +65,7 @@ class GithubAPI { Future clearAllCache() async { try { - await _dioCacheManager.clearAll(); + await _cacheOptions.store!.clean(); } on Exception catch (e) { if (kDebugMode) { print(e); @@ -58,7 +77,6 @@ class GithubAPI { try { final response = await _dio.get( '/repos/$repoName/releases', - options: _cacheOptions, ); return response.data[0]; } on Exception catch (e) { @@ -83,7 +101,6 @@ class GithubAPI { 'path': path, 'since': since.toIso8601String(), }, - options: _cacheOptions, ); final List commits = response.data; return commits diff --git a/lib/services/revanced_api.dart b/lib/services/revanced_api.dart index f7626d45..eac575b1 100644 --- a/lib/services/revanced_api.dart +++ b/lib/services/revanced_api.dart @@ -3,11 +3,11 @@ import 'dart:io'; import 'package:collection/collection.dart'; import 'package:dio/dio.dart'; -import 'package:dio_http_cache_lts/dio_http_cache_lts.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_client/native_dio_client.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'; @@ -15,10 +15,11 @@ import 'package:timeago/timeago.dart'; @lazySingleton class RevancedAPI { late Dio _dio = Dio(); - final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig()); - final Options _cacheOptions = buildCacheOptions( - const Duration(hours: 6), + + final _cacheOptions = CacheOptions( + store: MemCacheStore(), maxStale: const Duration(days: 1), + priority: CachePriority.high, ); Future initialize(String apiUrl) async { @@ -33,14 +34,25 @@ class RevancedAPI { ); log('ReVanced API: Using default engine + $isGMSInstalled'); } else { - _dio = Dio( - BaseOptions( - baseUrl: apiUrl, - ), - )..httpClientAdapter = NativeAdapter(); + 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(_dioCacheManager.interceptor); + _dio.interceptors.add(DioCacheInterceptor(options: _cacheOptions)); } on Exception catch (e) { if (kDebugMode) { print(e); @@ -50,7 +62,7 @@ class RevancedAPI { Future clearAllCache() async { try { - await _dioCacheManager.clearAll(); + await _cacheOptions.store!.clean(); } on Exception catch (e) { if (kDebugMode) { print(e); @@ -61,7 +73,7 @@ class RevancedAPI { Future>> getContributors() async { final Map> contributors = {}; try { - final response = await _dio.get('/contributors', options: _cacheOptions); + final response = await _dio.get('/contributors'); final List repositories = response.data['repositories']; for (final Map repo in repositories) { final String name = repo['name']; @@ -78,7 +90,7 @@ class RevancedAPI { Future> getPatches() async { try { - final response = await _dio.get('/patches', options: _cacheOptions); + final response = await _dio.get('/patches'); final List patches = response.data; return patches.map((patch) => Patch.fromJson(patch)).toList(); } on Exception catch (e) { @@ -94,7 +106,7 @@ class RevancedAPI { String repoName, ) async { try { - final response = await _dio.get('/tools', options: _cacheOptions); + final response = await _dio.get('/tools'); final List tools = response.data['tools']; return tools.firstWhereOrNull( (t) => diff --git a/pubspec.yaml b/pubspec.yaml index 3e9aa0d7..e75260e5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,9 +20,7 @@ dependencies: url: https://github.com/ponces/flutter_plugin_device_apps ref: revanced-manager device_info_plus: ^4.1.2 - dio: ^4.0.6 - dio_brotli_transformer: ^1.0.1 - dio_http_cache_lts: ^0.4.1 + dio: ^5.0.0 dynamic_color: ^1.5.4 dynamic_themes: ^1.1.0 expandable: ^5.0.1 @@ -52,7 +50,7 @@ dependencies: git: url: https://github.com/SuaMusica/logcat ref: feature/nullSafe - native_dio_client: ^0.0.1-dev+1 + native_dio_adapter: ^0.1.0 package_info_plus: ^1.4.3+1 path_provider: ^2.0.11 permission_handler: ^10.0.0 @@ -75,6 +73,7 @@ dependencies: flutter_dotenv: ^5.0.2 pub_release: ^8.0.3 flutter_markdown: ^0.6.13 + dio_cache_interceptor: ^3.4.0 dev_dependencies: json_serializable: ^6.3.1