fix: use singletons to enable cache and remove debug messages

This commit is contained in:
Alberto Ponces 2022-09-12 01:41:53 +01:00
parent 6ca47d7ec3
commit 0e4c19060e
5 changed files with 20 additions and 35 deletions

View File

@ -1,5 +1,7 @@
import 'package:revanced_manager/services/github_api.dart';
import 'package:revanced_manager/services/manager_api.dart'; import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/services/patcher_api.dart'; import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/services/revanced_api.dart';
import 'package:revanced_manager/ui/views/app_selector/app_selector_view.dart'; import 'package:revanced_manager/ui/views/app_selector/app_selector_view.dart';
import 'package:revanced_manager/ui/views/contributors/contributors_view.dart'; import 'package:revanced_manager/ui/views/contributors/contributors_view.dart';
import 'package:revanced_manager/ui/views/home/home_viewmodel.dart'; import 'package:revanced_manager/ui/views/home/home_viewmodel.dart';
@ -32,6 +34,8 @@ import 'package:stacked_services/stacked_services.dart';
LazySingleton(classType: NavigationService), LazySingleton(classType: NavigationService),
LazySingleton(classType: ManagerAPI), LazySingleton(classType: ManagerAPI),
LazySingleton(classType: PatcherAPI), LazySingleton(classType: PatcherAPI),
LazySingleton(classType: RevancedAPI),
LazySingleton(classType: GithubAPI),
], ],
) )
class AppSetup {} class AppSetup {}

View File

@ -3,8 +3,10 @@ import 'package:flutter_i18n/flutter_i18n.dart';
// ignore: depend_on_referenced_packages // ignore: depend_on_referenced_packages
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:revanced_manager/app/app.locator.dart'; import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/services/github_api.dart';
import 'package:revanced_manager/services/manager_api.dart'; import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/services/patcher_api.dart'; import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/services/revanced_api.dart';
import 'package:revanced_manager/ui/theme/dynamic_theme_builder.dart'; import 'package:revanced_manager/ui/theme/dynamic_theme_builder.dart';
import 'package:revanced_manager/ui/views/navigation/navigation_view.dart'; import 'package:revanced_manager/ui/views/navigation/navigation_view.dart';
import 'package:stacked_themes/stacked_themes.dart'; import 'package:stacked_themes/stacked_themes.dart';
@ -15,6 +17,8 @@ Future main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
await locator<ManagerAPI>().initialize(); await locator<ManagerAPI>().initialize();
await locator<PatcherAPI>().initialize(); await locator<PatcherAPI>().initialize();
locator<RevancedAPI>().initialize();
locator<GithubAPI>().initialize();
runApp(const MyApp()); runApp(const MyApp());
} }

View File

@ -5,16 +5,17 @@ import 'package:collection/collection.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart'; import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:injectable/injectable.dart';
import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patch.dart';
@lazySingleton
class GithubAPI { class GithubAPI {
final String apiUrl = 'https://api.github.com'; final String apiUrl = 'https://api.github.com';
final Dio _dio = Dio(); final Dio _dio = Dio();
final DioCacheManager _dioCacheManager = DioCacheManager( final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
CacheConfig( final Options _cacheOptions = buildCacheOptions(
defaultMaxAge: const Duration(hours: 1), const Duration(hours: 1),
defaultMaxStale: const Duration(days: 7), maxStale: const Duration(days: 7),
),
); );
final Map<String, String> repoAppPath = { final Map<String, String> repoAppPath = {
'com.google.android.youtube': 'youtube', 'com.google.android.youtube': 'youtube',
@ -38,13 +39,8 @@ class GithubAPI {
try { try {
var response = await _dio.get( var response = await _dio.get(
'$apiUrl/repos/$repoName/releases/latest', '$apiUrl/repos/$repoName/releases/latest',
options: buildCacheOptions(const Duration(hours: 1)), options: _cacheOptions,
); );
if (response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE) != null) {
print('1 - From cache');
} else {
print('1 - From net');
}
return response.data; return response.data;
} on Exception { } on Exception {
// ignore // ignore
@ -67,13 +63,8 @@ class GithubAPI {
'per_page': 3, 'per_page': 3,
'since': since.toIso8601String(), 'since': since.toIso8601String(),
}, },
options: buildCacheOptions(const Duration(hours: 1)), options: _cacheOptions,
); );
if (response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE) != null) {
print('2 - From cache');
} else {
print('2 - From net');
}
List<dynamic> commits = response.data; List<dynamic> commits = response.data;
return commits return commits
.map((commit) => .map((commit) =>

View File

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:device_apps/device_apps.dart'; import 'package:device_apps/device_apps.dart';
import 'package:injectable/injectable.dart'; import 'package:injectable/injectable.dart';
import 'package:package_info_plus/package_info_plus.dart'; import 'package:package_info_plus/package_info_plus.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/models/patched_application.dart'; import 'package:revanced_manager/models/patched_application.dart';
import 'package:revanced_manager/services/github_api.dart'; import 'package:revanced_manager/services/github_api.dart';
@ -12,8 +13,8 @@ import 'package:shared_preferences/shared_preferences.dart';
@lazySingleton @lazySingleton
class ManagerAPI { class ManagerAPI {
final RevancedAPI _revancedAPI = RevancedAPI(); final RevancedAPI _revancedAPI = locator<RevancedAPI>();
final GithubAPI _githubAPI = GithubAPI(); final GithubAPI _githubAPI = locator<GithubAPI>();
final RootAPI _rootAPI = RootAPI(); final RootAPI _rootAPI = RootAPI();
final String patcherRepo = 'revanced-patcher'; final String patcherRepo = 'revanced-patcher';
final String cliRepo = 'revanced-cli'; final String cliRepo = 'revanced-cli';

View File

@ -14,7 +14,7 @@ class RevancedAPI {
final Dio _dio = Dio(); final Dio _dio = Dio();
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig()); final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions( final Options _cacheOptions = buildCacheOptions(
const Duration(minutes: 10), const Duration(hours: 1),
maxStale: const Duration(days: 7), maxStale: const Duration(days: 7),
); );
@ -33,11 +33,6 @@ class RevancedAPI {
'$apiUrl/contributors', '$apiUrl/contributors',
options: _cacheOptions, options: _cacheOptions,
); );
if (response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE) != null) {
print('3 - From cache');
} else {
print('3 - From net');
}
List<dynamic> repositories = response.data['repositories']; List<dynamic> repositories = response.data['repositories'];
for (Map<String, dynamic> repo in repositories) { for (Map<String, dynamic> repo in repositories) {
String name = repo['name']; String name = repo['name'];
@ -52,11 +47,6 @@ class RevancedAPI {
Future<List<Patch>> getPatches() async { Future<List<Patch>> getPatches() async {
try { try {
var response = await _dio.get('$apiUrl/patches', options: _cacheOptions); var response = await _dio.get('$apiUrl/patches', options: _cacheOptions);
if (response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE) != null) {
print('4 - From cache');
} else {
print('4 - From net');
}
List<dynamic> patches = response.data; List<dynamic> patches = response.data;
return patches.map((patch) => Patch.fromJson(patch)).toList(); return patches.map((patch) => Patch.fromJson(patch)).toList();
} on Exception { } on Exception {
@ -71,11 +61,6 @@ class RevancedAPI {
) async { ) async {
try { try {
var response = await _dio.get('$apiUrl/tools', options: _cacheOptions); var response = await _dio.get('$apiUrl/tools', options: _cacheOptions);
if (response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE) != null) {
print('5 - From cache');
} else {
print('5 - From net');
}
List<dynamic> tools = response.data['tools']; List<dynamic> tools = response.data['tools'];
return tools.firstWhereOrNull( return tools.firstWhereOrNull(
(t) => (t) =>