Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow control over patches update #1063

Merged
merged 12 commits into from
Aug 3, 2023
9 changes: 9 additions & 0 deletions assets/i18n/en_US.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"okButton": "OK",
"cancelButton": "Cancel",
"quitButton": "Quit",
"updateButton": "Update",
"enabledLabel": "Enabled",
"disabledLabel": "Disabled",
Expand Down Expand Up @@ -31,8 +32,14 @@
"installUpdate": "Continue to install the update?",

"updateDialogTitle": "Update Manager",
"updatePatchesDialogTitle": "Update ReVanced Patches",
"updateChangelogTitle": "Changelog",

"patchesConsentDialogText": "ReVanced Patches need to be downloaded to patch apps.",
"patchesConsentDialogText2": "This will reveal your IP address to {url}.",
"patchesConsentDialogText3": "Auto update",
"patchesConsentDialogText3Sub": "You can still change this in the settings later",

"notificationTitle": "Update downloaded",
"notificationText": "Tap to install the update",

Expand Down Expand Up @@ -171,6 +178,7 @@
"sourcesResetDialogTitle": "Reset",
"sourcesResetDialogText": "Are you sure you want to reset custom sources to their default values?",
"apiURLResetDialogText": "Are you sure you want to reset API URL to its default value?",
"sourcesUpdateNote": "Note: ReVanced Patches will be updated to the latest version automatically.\n\nThis will reveal your IP address to the server.",

"apiURLLabel": "API URL",
"apiURLHint": "Configure your custom API URL",
Expand All @@ -186,6 +194,7 @@
"logsLabel": "Logs",
"logsHint": "Share Manager's logs",

"autoUpdatePatchesHint": "Automatically update ReVanced Patches to the latest version",
"experimentalUniversalPatchesLabel": "Experimental universal patches support",
"experimentalUniversalPatchesHint": "Display all applications to use with universal patches, loading list of apps may be slower",
"experimentalPatchesLabel": "Experimental patches support",
Expand Down
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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';
Expand All @@ -24,7 +23,6 @@ Future main() async {
await locator<RevancedAPI>().initialize(apiUrl);
final String repoUrl = locator<ManagerAPI>().getRepoUrl();
locator<GithubAPI>().initialize(repoUrl);
await locator<PatcherAPI>().initialize();
tz.initializeTimeZones();
prefs = await SharedPreferences.getInstance();

Expand Down
75 changes: 59 additions & 16 deletions lib/services/github_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ class GithubAPI {
}
}

Future<Map<String, dynamic>?> getPatchesRelease(
String repoName,
String version,
) async {
try {
final response = await _dio.get(
'/repos/$repoName/releases/tags/$version',
);
return response.data;
} on Exception catch (e) {
if (kDebugMode) {
print(e);
}
return null;
}
}

Future<Map<String, dynamic>?> getLatestPatchesRelease(
String repoName,
) async {
try {
final response = await _dio.get(
'/repos/$repoName/releases/latest',
);
return response.data;
} on Exception catch (e) {
if (kDebugMode) {
print(e);
}
return null;
}
}

Future<Map<String, dynamic>?> getLatestManagerRelease(
String repoName,
) async {
Expand Down Expand Up @@ -164,37 +197,47 @@ class GithubAPI {
return null;
}

Future<List<Patch>> getPatches(String repoName) async {
List<Patch> patches = [];
Future<File?> getPatchesReleaseFile(
String extension,
String repoName,
String version,
) async {
try {
final File? f = await getLatestReleaseFile('.json', repoName);
if (f != null) {
final List<dynamic> list = jsonDecode(f.readAsStringSync());
patches = list.map((patch) => Patch.fromJson(patch)).toList();
final Map<String, dynamic>? release =
await getPatchesRelease(repoName, version);
if (release != null) {
final Map<String, dynamic>? asset =
(release['assets'] as List<dynamic>).firstWhereOrNull(
(asset) => (asset['name'] as String).endsWith(extension),
);
if (asset != null) {
return await DefaultCacheManager().getSingleFile(
asset['browser_download_url'],
);
}
}
} on Exception catch (e) {
if (kDebugMode) {
print(e);
}
}

return patches;
return null;
}

Future<String> getLastestReleaseVersion(String repoName) async {
Future<List<Patch>> getPatches(String repoName, String version) async {
List<Patch> patches = [];
try {
final Map<String, dynamic>? release = await getLatestRelease(repoName);
if (release != null) {
return release['tag_name'];
} else {
return 'Unknown';
final File? f = await getPatchesReleaseFile('.json', repoName, version);
if (f != null) {
final List<dynamic> list = jsonDecode(f.readAsStringSync());
patches = list.map((patch) => Patch.fromJson(patch)).toList();
}
} on Exception catch (e) {
if (kDebugMode) {
print(e);
}

return 'Unknown';
}

return patches;
}
}
96 changes: 65 additions & 31 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:convert';

Check warning on line 1 in lib/services/manager_api.dart

View workflow job for this annotation

GitHub Actions / Static analysis & format check

lib/services/manager_api.dart is not formatted
import 'dart:io';
import 'package:device_apps/device_apps.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -13,6 +13,7 @@
import 'package:revanced_manager/services/root_api.dart';
import 'package:revanced_manager/utils/check_for_supported_patch.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:timeago/timeago.dart';

@lazySingleton
class ManagerAPI {
Expand Down Expand Up @@ -81,6 +82,22 @@
await _prefs.setString('patchesRepo', value);
}

bool getPatchesConsent() {
return _prefs.getBool('patchesConsent') ?? false;
}

Future<void> setPatchesConsent(bool consent) async {
await _prefs.setBool('patchesConsent', consent);
}

bool isPatchesAutoUpdate() {
return _prefs.getBool('patchesAutoUpdate') ?? false;
}

Future<void> setPatchesAutoUpdate(bool value) async {
await _prefs.setBool('patchesAutoUpdate', value);
}

String getIntegrationsRepo() {
return _prefs.getString('integrationsRepo') ?? defaultIntegrationsRepo;
}
Expand Down Expand Up @@ -205,11 +222,8 @@
Future<List<Patch>> getPatches() async {
try {
final String repoName = getPatchesRepo();
if (repoName == defaultPatchesRepo) {
return await _revancedAPI.getPatches();
} else {
return await _githubAPI.getPatches(repoName);
}
final String currentVersion = await getCurrentPatchesVersion();
return await _githubAPI.getPatches(repoName, currentVersion);
} on Exception catch (e) {
if (kDebugMode) {
print(e);
Expand All @@ -221,14 +235,12 @@
Future<File?> downloadPatches() async {
try {
final String repoName = getPatchesRepo();
if (repoName == defaultPatchesRepo) {
return await _revancedAPI.getLatestReleaseFile(
'.jar',
defaultPatchesRepo,
);
} else {
return await _githubAPI.getLatestReleaseFile('.jar', repoName);
}
final String currentVersion = await getCurrentPatchesVersion();
return await _githubAPI.getPatchesReleaseFile(
'.jar',
repoName,
currentVersion,
);
} on Exception catch (e) {
if (kDebugMode) {
print(e);
Expand Down Expand Up @@ -263,11 +275,23 @@
);
}

Future<String?> getLatestPatcherReleaseTime() async {
return await _revancedAPI.getLatestReleaseTime(
'.gz',
defaultPatcherRepo,
);
Future<String?> getLatestPatchesReleaseTime() async {
if (isDefaultPatchesRepo()) {
return await _revancedAPI.getLatestReleaseTime(
'.json',
defaultPatchesRepo,
);
} else {
final release =
await _githubAPI.getLatestPatchesRelease(getPatchesRepo());
if (release != null) {
final DateTime timestamp =
DateTime.parse(release['created_at'] as String);
return format(timestamp, locale: 'en_short');
} else {
return null;
}
}
}

Future<String?> getLatestManagerReleaseTime() async {
Expand All @@ -285,27 +309,37 @@
}

Future<String?> getLatestPatchesVersion() async {
return await _revancedAPI.getLatestReleaseVersion(
'.json',
defaultPatchesRepo,
);
if (isDefaultPatchesRepo()) {
return await _revancedAPI.getLatestReleaseVersion(
'.json',
defaultPatchesRepo,
);
} else {
final release = await _githubAPI.getLatestPatchesRelease(getPatchesRepo());
if (release != null) {
return release['tag_name'];
} else {
return null;
}
}
}

Future<String> getCurrentManagerVersion() async {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
return packageInfo.version;
}

Future<String?> getCurrentPatchesVersion() async {
if (isDefaultPatchesRepo()) {
patchesVersion = await getLatestPatchesVersion();
// print('Patches version: $patchesVersion');
} else {
// fetch from github
patchesVersion =
await _githubAPI.getLastestReleaseVersion(getPatchesRepo());
Future<String> getCurrentPatchesVersion() async {
patchesVersion = _prefs.getString('patchesVersion') ?? '0.0.0';
if (patchesVersion == '0.0.0' || isPatchesAutoUpdate()) {
patchesVersion = await getLatestPatchesVersion() ?? '0.0.0';
await setCurrentPatchesVersion(patchesVersion!);
}
return patchesVersion ?? '0.0.0';
return patchesVersion!;
}

Future<void> setCurrentPatchesVersion(String version) async {
await _prefs.setString('patchesVersion', version);
}

Future<List<PatchedApplication>> getAppsToRemove(
Expand Down
14 changes: 0 additions & 14 deletions lib/services/revanced_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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:revanced_manager/models/patch.dart';
import 'package:timeago/timeago.dart';

@lazySingleton
Expand Down Expand Up @@ -64,19 +63,6 @@ class RevancedAPI {
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,
Expand Down
4 changes: 1 addition & 3 deletions lib/ui/views/app_selector/app_selector_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/models/patched_application.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/services/toast.dart';
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/shared/custom_material_button.dart';
Expand All @@ -19,7 +18,6 @@ import 'package:stacked/stacked.dart';
class AppSelectorViewModel extends BaseViewModel {
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
final ManagerAPI _managerAPI = locator<ManagerAPI>();
final RevancedAPI _revancedAPI = locator<RevancedAPI>();
final Toast _toast = locator<Toast>();
final List<ApplicationWithIcon> apps = [];
List<String> allApps = [];
Expand All @@ -32,7 +30,7 @@ class AppSelectorViewModel extends BaseViewModel {
List<Patch> patches = [];

Future<void> initialize() async {
patches = await _revancedAPI.getPatches();
patches = await _managerAPI.getPatches();
isRooted = _managerAPI.isRooted;

apps.addAll(
Expand Down
6 changes: 1 addition & 5 deletions lib/ui/views/home/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ class HomeView extends StatelessWidget {
),
),
const SizedBox(height: 10),
LatestCommitCard(
onPressedManager: () =>
model.showUpdateConfirmationDialog(context),
onPressedPatches: () => model.forceRefresh(context),
),
LatestCommitCard(model: model, parentContext: context),
const SizedBox(height: 23),
I18nText(
'homeView.patchedSubtitle',
Expand Down
Loading
Loading