Skip to content

Commit

Permalink
feat: Pre-release patches toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
kitadai31 committed Mar 25, 2024
1 parent a71a930 commit e88e997
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 37 deletions.
4 changes: 4 additions & 0 deletions assets/i18n/strings.i18n.json
Expand Up @@ -208,6 +208,10 @@
"requireSuggestedAppVersionLabel": "Require suggested app version",
"requireSuggestedAppVersionHint": "Prevent selecting an app with a version that is not the suggested",
"requireSuggestedAppVersionDialogText": "Selecting an app that is not the suggested version may cause unexpected issues.\n\nDo you want to proceed anyways?",
"usePrereleasePatchesLabel": "Use pre-release patches",
"usePrereleasePatchesHint": "Use the pre-release versions of patches and integrations",
"usePrereleasePatchesDialogText": "Pre-release patches are for development purposes and may cause unexpected issues.",
"usePrereleasePatchesDialogText2": "It may not be compatible with the current version of ReVanced Manager and may cause patching errors.\n\nDo you want to proceed anyways?",
"aboutLabel": "About",
"snackbarMessage": "Copied to clipboard",
"restartAppForChanges": "Restart the app to apply changes",
Expand Down
31 changes: 3 additions & 28 deletions lib/services/github_api.dart
Expand Up @@ -21,12 +21,12 @@ class GithubAPI {
await _downloadManager.clearAllCache();
}

Future<Map<String, dynamic>?> getLatestRelease(
Future<Map<String, dynamic>?> getLatestReleaseWithPreReleases(
String repoName,
) async {
try {
final response = await _dio.get(
'/repos/$repoName/releases',
'/repos/$repoName/releases?per_page=1',
);
return response.data[0];
} on Exception catch (e) {
Expand Down Expand Up @@ -54,7 +54,7 @@ class GithubAPI {
}
}

Future<Map<String, dynamic>?> getLatestPatchesRelease(
Future<Map<String, dynamic>?> getLatestRelease(
String repoName,
) async {
try {
Expand Down Expand Up @@ -108,31 +108,6 @@ class GithubAPI {
}
}

Future<File?> getLatestReleaseFile(
String extension,
String repoName,
) async {
try {
final Map<String, dynamic>? release = await getLatestRelease(repoName);
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 _downloadManager.getSingleFile(
asset['browser_download_url'],
);
}
}
} on Exception catch (e) {
if (kDebugMode) {
print(e);
}
}
return null;
}

Future<File?> getPatchesReleaseFile(
String extension,
String repoName,
Expand Down
22 changes: 17 additions & 5 deletions lib/services/manager_api.dart
Expand Up @@ -222,6 +222,14 @@ class ManagerAPI {
return _prefs.getBool('useAlternativeSources') ?? false;
}

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

bool isUsingPrereleasePatches() {
return _prefs.getBool('usePrereleasePatches') ?? false;
}

Option? getPatchOption(String packageName, String patchName, String key) {
final String? optionJson =
_prefs.getString('patchOption-$packageName-$patchName-$key');
Expand Down Expand Up @@ -469,8 +477,9 @@ class ManagerAPI {
defaultPatchesRepo,
);
} else {
final release =
await _githubAPI.getLatestPatchesRelease(getPatchesRepo());
final release = !isUsingPrereleasePatches()
? await _githubAPI.getLatestRelease(getPatchesRepo())
: await _githubAPI.getLatestReleaseWithPreReleases(getPatchesRepo());
if (release != null) {
final DateTime timestamp =
DateTime.parse(release['created_at'] as String);
Expand Down Expand Up @@ -502,7 +511,9 @@ class ManagerAPI {
defaultIntegrationsRepo,
);
} else {
final release = await _githubAPI.getLatestRelease(getIntegrationsRepo());
final release = !isUsingPrereleasePatches()
? await _githubAPI.getLatestRelease(getIntegrationsRepo())
: await _githubAPI.getLatestReleaseWithPreReleases(getIntegrationsRepo());
if (release != null) {
return release['tag_name'];
} else {
Expand All @@ -518,8 +529,9 @@ class ManagerAPI {
defaultPatchesRepo,
);
} else {
final release =
await _githubAPI.getLatestPatchesRelease(getPatchesRepo());
final release = !isUsingPrereleasePatches()
? await _githubAPI.getLatestRelease(getPatchesRepo())
: await _githubAPI.getLatestReleaseWithPreReleases(getPatchesRepo());
if (release != null) {
return release['tag_name'];
} else {
Expand Down
8 changes: 7 additions & 1 deletion lib/ui/views/home/home_viewmodel.dart
Expand Up @@ -480,7 +480,13 @@ class HomeViewModel extends BaseViewModel {
}

Future<Map<String, dynamic>?> getLatestPatchesRelease() {
return _githubAPI.getLatestPatchesRelease(_managerAPI.defaultPatchesRepo);
if (_managerAPI.isUsingAlternativeSources() &&
_managerAPI.isUsingPrereleasePatches()) {
return _githubAPI
.getLatestReleaseWithPreReleases(_managerAPI.getPatchesRepo());
} else {
return _githubAPI.getLatestRelease(_managerAPI.getPatchesRepo());
}
}

Future<String?> getLatestPatchesReleaseTime() {
Expand Down
60 changes: 60 additions & 0 deletions lib/ui/views/settings/settings_viewmodel.dart
Expand Up @@ -198,6 +198,66 @@ class SettingsViewModel extends BaseViewModel {
}
}

bool isUsingPrereleasePatches() {
return _managerAPI.isUsingPrereleasePatches();
}

Future<void>? showUsePrelereasePatchesDialog(
BuildContext context,
bool value,
) {
if (value) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(t.warning),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.settingsView.usePrereleasePatchesDialogText,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 16),
Text(
t.settingsView.usePrereleasePatchesDialogText2,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.error,
),
),
],
),
actions: [
TextButton(
onPressed: () {
_managerAPI.usePrereleasePatches(true);
_toast.showBottom(t.settingsView.restartAppForChanges);
Navigator.of(context).pop();
},
child: Text(t.yesButton),
),
FilledButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(t.noButton),
),
],
),
);
} else {
_managerAPI.usePrereleasePatches(false);
_toast.showBottom(t.settingsView.restartAppForChanges);
}
return null;
}

void deleteKeystore() {
_managerAPI.deleteKeystore();
_toast.showBottom(t.settingsView.regeneratedKeystore);
Expand Down
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:revanced_manager/gen/strings.g.dart';
import 'package:revanced_manager/ui/views/settings/settingsFragment/settings_manage_sources.dart';
import 'package:revanced_manager/ui/views/settings/settings_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/settingsView/settings_use_prerelease_patches.dart';
import 'package:revanced_manager/ui/widgets/shared/haptics/haptic_switch_list_tile.dart';

class SUseAlternativeSources extends StatefulWidget {
Expand Down Expand Up @@ -31,12 +32,16 @@ class _SUseAlternativeSourcesState extends State<SUseAlternativeSources> {
subtitle: Text(t.settingsView.useAlternativeSourcesHint),
value: _settingsViewModel.isUsingAlternativeSources(),
onChanged: (value) {
_settingsViewModel.useAlternativeSources(value);
setState(() {});
setState(() {
_settingsViewModel.useAlternativeSources(value);
});
},
),
if (_settingsViewModel.isUsingAlternativeSources())
const SManageSourcesUI(),
...[
const SManageSourcesUI(),
const SUsePrereleasePatches(),
],
],
);
}
Expand Down
35 changes: 35 additions & 0 deletions lib/ui/widgets/settingsView/settings_use_prerelease_patches.dart
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:revanced_manager/gen/strings.g.dart';
import 'package:revanced_manager/ui/views/settings/settings_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/shared/haptics/haptic_switch_list_tile.dart';

class SUsePrereleasePatches extends StatefulWidget {
const SUsePrereleasePatches({super.key});

@override
State<SUsePrereleasePatches> createState() => _SUsePrereleasePatchesState();
}

final _settingsViewModel = SettingsViewModel();

class _SUsePrereleasePatchesState extends State<SUsePrereleasePatches> {
@override
Widget build(BuildContext context) {
return HapticSwitchListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 20.0),
title: Text(
t.settingsView.usePrereleasePatchesLabel,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500,
),
),
subtitle: Text(t.settingsView.usePrereleasePatchesHint),
value: _settingsViewModel.isUsingPrereleasePatches(),
onChanged: (value) async {
await _settingsViewModel.showUsePrelereasePatchesDialog(context, value);
setState(() {});
},
);
}
}

0 comments on commit e88e997

Please sign in to comment.