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/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/contributors/contributors_view.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: ManagerAPI),
LazySingleton(classType: PatcherAPI),
LazySingleton(classType: RevancedAPI),
LazySingleton(classType: GithubAPI),
],
)
class AppSetup {}

View File

@ -3,8 +3,10 @@ import 'package:flutter_i18n/flutter_i18n.dart';
// ignore: depend_on_referenced_packages
import 'package:flutter_localizations/flutter_localizations.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/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/views/navigation/navigation_view.dart';
import 'package:stacked_themes/stacked_themes.dart';
@ -15,6 +17,8 @@ Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await locator<ManagerAPI>().initialize();
await locator<PatcherAPI>().initialize();
locator<RevancedAPI>().initialize();
locator<GithubAPI>().initialize();
runApp(const MyApp());
}

View File

@ -5,16 +5,17 @@ import 'package:collection/collection.dart';
import 'package:dio/dio.dart';
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:injectable/injectable.dart';
import 'package:revanced_manager/models/patch.dart';
@lazySingleton
class GithubAPI {
final String apiUrl = 'https://api.github.com';
final Dio _dio = Dio();
final DioCacheManager _dioCacheManager = DioCacheManager(
CacheConfig(
defaultMaxAge: const Duration(hours: 1),
defaultMaxStale: const Duration(days: 7),
),
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions(
const Duration(hours: 1),
maxStale: const Duration(days: 7),
);
final Map<String, String> repoAppPath = {
'com.google.android.youtube': 'youtube',
@ -38,13 +39,8 @@ class GithubAPI {
try {
var response = await _dio.get(
'$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;
} on Exception {
// ignore
@ -67,13 +63,8 @@ class GithubAPI {
'per_page': 3,
'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;
return commits
.map((commit) =>

View File

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

View File

@ -14,7 +14,7 @@ class RevancedAPI {
final Dio _dio = Dio();
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions(
const Duration(minutes: 10),
const Duration(hours: 1),
maxStale: const Duration(days: 7),
);
@ -33,11 +33,6 @@ class RevancedAPI {
'$apiUrl/contributors',
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'];
for (Map<String, dynamic> repo in repositories) {
String name = repo['name'];
@ -52,11 +47,6 @@ class RevancedAPI {
Future<List<Patch>> getPatches() async {
try {
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;
return patches.map((patch) => Patch.fromJson(patch)).toList();
} on Exception {
@ -71,11 +61,6 @@ class RevancedAPI {
) async {
try {
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'];
return tools.firstWhereOrNull(
(t) =>