feat: allow control over patches update (#1063)

Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
aAbed
2023-08-04 02:08:56 +05:45
committed by GitHub
parent e55f427b05
commit f905a52988
15 changed files with 322 additions and 103 deletions

View File

@ -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';
@ -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 = [];
@ -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(

View File

@ -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',

View File

@ -4,6 +4,7 @@ import 'dart:io';
import 'package:cross_connectivity/cross_connectivity.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:injectable/injectable.dart';
@ -42,6 +43,10 @@ class HomeViewModel extends BaseViewModel {
Future<void> initialize(BuildContext context) async {
_latestManagerVersion = await _managerAPI.getLatestManagerVersion();
if(!_managerAPI.getPatchesConsent()){
await showPatchesConsent(context);
}
await _patcherAPI.initialize();
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('ic_notification'),
@ -129,13 +134,13 @@ class HomeViewModel extends BaseViewModel {
Future<bool> hasPatchesUpdates() async {
final String? latestVersion = await _managerAPI.getLatestPatchesVersion();
final String? currentVersion = await _managerAPI.getCurrentPatchesVersion();
final String currentVersion = await _managerAPI.getCurrentPatchesVersion();
if (latestVersion != null) {
try {
final int latestVersionInt =
int.parse(latestVersion.replaceAll(RegExp('[^0-9]'), ''));
final int currentVersionInt =
int.parse(currentVersion!.replaceAll(RegExp('[^0-9]'), ''));
int.parse(currentVersion.replaceAll(RegExp('[^0-9]'), ''));
return latestVersionInt > currentVersionInt;
} on Exception catch (e) {
if (kDebugMode) {
@ -163,6 +168,94 @@ class HomeViewModel extends BaseViewModel {
}
}
Future<void> showPatchesConsent(BuildContext context) async{
final ValueNotifier<bool> autoUpdate = ValueNotifier(true);
await showDialog(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: const Text('ReVanced Patches'),
content: ValueListenableBuilder(
valueListenable: autoUpdate,
builder: (context, value, child) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
I18nText(
'homeView.patchesConsentDialogText',
child: Text(
'',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.secondary,
),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: I18nText(
'homeView.patchesConsentDialogText2',
translationParams: {'url': _managerAPI.defaultApiUrl.split('/')[2]},
child: Text(
'',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.error,
),
),
),
),
CheckboxListTile(
value: value,
contentPadding: EdgeInsets.zero,
title: I18nText('homeView.patchesConsentDialogText3',),
subtitle: I18nText('homeView.patchesConsentDialogText3Sub',),
onChanged: (selected) {
autoUpdate.value = selected!;
},
),
],
);
},
),
actions: [
CustomMaterialButton(
isFilled: false,
onPressed: () async {
await _managerAPI.setPatchesConsent(false);
SystemNavigator.pop();
},
label: I18nText('quitButton'),
),
CustomMaterialButton(
onPressed: () async {
await _managerAPI.setPatchesConsent(true);
await _managerAPI.setPatchesAutoUpdate(autoUpdate.value);
Navigator.of(context).pop();
},
label: I18nText('okButton'),
)
],
),
);
}
Future<void> updatePatches(BuildContext context) async {
_toast.showBottom('homeView.downloadingMessage');
final String patchesVersion =
await _managerAPI.getLatestPatchesVersion() ?? '0.0.0';
if (patchesVersion != '0.0.0') {
_toast.showBottom('homeView.downloadedMessage');
await _managerAPI.setCurrentPatchesVersion(patchesVersion);
forceRefresh(context);
} else {
_toast.showBottom('homeView.errorDownloadMessage');
}
}
Future<void> updateManager(BuildContext context) async {
final ValueNotifier<bool> downloaded = ValueNotifier(false);
try {
@ -336,6 +429,7 @@ class HomeViewModel extends BaseViewModel {
Future<void> showUpdateConfirmationDialog(
BuildContext parentContext,
bool isPatches,
) {
return showModalBottomSheet(
context: parentContext,
@ -343,7 +437,9 @@ class HomeViewModel extends BaseViewModel {
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(24.0)),
),
builder: (context) => const UpdateConfirmationDialog(),
builder: (context) => UpdateConfirmationDialog(
isPatches: isPatches,
),
);
}
@ -351,8 +447,12 @@ class HomeViewModel extends BaseViewModel {
return _githubAPI.getLatestManagerRelease(_managerAPI.defaultManagerRepo);
}
Future<String?> getLatestPatcherReleaseTime() {
return _managerAPI.getLatestPatcherReleaseTime();
Future<Map<String, dynamic>?> getLatestPatchesRelease() {
return _githubAPI.getLatestPatchesRelease(_managerAPI.defaultPatchesRepo);
}
Future<String?> getLatestPatchesReleaseTime() {
return _managerAPI.getLatestPatchesReleaseTime();
}
Future<String?> getLatestManagerReleaseTime() {

View File

@ -2,7 +2,6 @@ import 'package:collection/collection.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';
import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/services/toast.dart';
@ -13,7 +12,6 @@ import 'package:stacked/stacked.dart';
class PatchesSelectorViewModel extends BaseViewModel {
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
final ManagerAPI _managerAPI = locator<ManagerAPI>();
final GithubAPI _githubAPI = locator<GithubAPI>();
final List<Patch> patches = [];
final List<Patch> selectedPatches =
locator<PatcherViewModel>().selectedPatches;
@ -78,17 +76,8 @@ class PatchesSelectorViewModel extends BaseViewModel {
locator<PatcherViewModel>().notifyListeners();
}
Future<String?> getPatchesVersion() async {
if (isDefaultPatchesRepo()) {
patchesVersion = await _managerAPI.getLatestPatchesVersion();
// print('Patches version: $patchesVersion');
return patchesVersion ?? '0.0.0';
} else {
// fetch from github
patchesVersion = await _githubAPI
.getLastestReleaseVersion(_managerAPI.getPatchesRepo());
}
return null;
Future<void> getPatchesVersion() async {
patchesVersion = await _managerAPI.getCurrentPatchesVersion();
}
List<Patch> getQueriedPatches(String query) {

View File

@ -101,6 +101,8 @@ class SManageSources extends BaseViewModel {
hint: integrationsRepo.split('/')[1],
onChanged: (value) => notifyListeners(),
),
const SizedBox(height: 20),
I18nText('settingsView.sourcesUpdateNote')
],
),
),
@ -126,6 +128,7 @@ class SManageSources extends BaseViewModel {
_managerAPI.setIntegrationsRepo(
'${_orgIntSourceController.text.trim()}/${_intSourceController.text.trim()}',
);
_managerAPI.setCurrentPatchesVersion('0.0.0');
_toast.showBottom('settingsView.restartAppForChanges');
Navigator.of(context).pop();
},
@ -154,6 +157,7 @@ class SManageSources extends BaseViewModel {
_managerAPI.setRepoUrl('');
_managerAPI.setPatchesRepo('');
_managerAPI.setIntegrationsRepo('');
_managerAPI.setCurrentPatchesVersion('0.0.0');
_toast.showBottom('settingsView.restartAppForChanges');
Navigator.of(context)
..pop()

View File

@ -28,6 +28,15 @@ class SettingsViewModel extends BaseViewModel {
_navigationService.navigateTo(Routes.contributorsView);
}
bool isPatchesAutoUpdate() {
return _managerAPI.isPatchesAutoUpdate();
}
void setPatchesAutoUpdate(bool value) {
_managerAPI.setPatchesAutoUpdate(value);
notifyListeners();
}
bool areUniversalPatchesEnabled() {
return _managerAPI.areUniversalPatchesEnabled();
}

View File

@ -8,11 +8,11 @@ import 'package:revanced_manager/ui/widgets/shared/custom_material_button.dart';
class LatestCommitCard extends StatefulWidget {
const LatestCommitCard({
Key? key,
required this.onPressedManager,
required this.onPressedPatches,
required this.model,
required this.parentContext,
}) : super(key: key);
final Function() onPressedManager;
final Function() onPressedPatches;
final HomeViewModel model;
final BuildContext parentContext;
@override
State<LatestCommitCard> createState() => _LatestCommitCardState();
@ -63,7 +63,10 @@ class _LatestCommitCardState extends State<LatestCommitCard> {
child: CustomMaterialButton(
label: I18nText('updateButton'),
onPressed: snapshot.hasData && snapshot.data!
? widget.onPressedManager
? () => widget.model.showUpdateConfirmationDialog(
widget.parentContext,
false,
)
: () => {},
),
),
@ -91,7 +94,7 @@ class _LatestCommitCardState extends State<LatestCommitCard> {
Row(
children: <Widget>[
FutureBuilder<String?>(
future: model.getLatestPatcherReleaseTime(),
future: model.getLatestPatchesReleaseTime(),
builder: (context, snapshot) => Text(
snapshot.hasData && snapshot.data!.isNotEmpty
? FlutterI18n.translate(
@ -117,7 +120,10 @@ class _LatestCommitCardState extends State<LatestCommitCard> {
child: CustomMaterialButton(
label: I18nText('updateButton'),
onPressed: snapshot.hasData && snapshot.data!
? widget.onPressedPatches
? () => widget.model.showUpdateConfirmationDialog(
widget.parentContext,
true,
)
: () => {},
),
),

View File

@ -6,8 +6,9 @@ import 'package:revanced_manager/ui/views/home/home_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/shared/custom_material_button.dart';
class UpdateConfirmationDialog extends StatelessWidget {
const UpdateConfirmationDialog({Key? key}) : super(key: key);
const UpdateConfirmationDialog({super.key, required this.isPatches});
final bool isPatches;
@override
Widget build(BuildContext context) {
final HomeViewModel model = locator<HomeViewModel>();
@ -20,7 +21,9 @@ class UpdateConfirmationDialog extends StatelessWidget {
controller: scrollController,
child: SafeArea(
child: FutureBuilder<Map<String, dynamic>?>(
future: model.getLatestManagerRelease(),
future: !isPatches
? model.getLatestManagerRelease()
: model.getLatestPatchesRelease(),
builder: (_, snapshot) {
if (!snapshot.hasData) {
return const SizedBox(
@ -48,7 +51,9 @@ class UpdateConfirmationDialog extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
I18nText(
'homeView.updateDialogTitle',
isPatches
? 'homeView.updatePatchesDialogTitle'
: 'homeView.updateDialogTitle',
child: const Text(
'',
style: TextStyle(
@ -86,7 +91,9 @@ class UpdateConfirmationDialog extends StatelessWidget {
label: I18nText('updateButton'),
onPressed: () {
Navigator.of(context).pop();
model.updateManager(context);
isPatches
? model.updatePatches(context)
: model.updateManager(context);
},
)
],

View File

@ -5,6 +5,7 @@ import 'package:flutter_i18n/widgets/I18nText.dart';
import 'package:revanced_manager/ui/views/settings/settingsFragment/settings_manage_api_url.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_auto_update_patches.dart';
import 'package:revanced_manager/ui/widgets/settingsView/settings_experimental_patches.dart';
import 'package:revanced_manager/ui/widgets/settingsView/settings_experimental_universal_patches.dart';
import 'package:revanced_manager/ui/widgets/settingsView/settings_section.dart';
@ -23,6 +24,7 @@ class SAdvancedSection extends StatelessWidget {
SManageApiUrlUI(),
SManageSourcesUI(),
// SManageKeystorePasswordUI(),
SAutoUpdatePatches(),
SExperimentalUniversalPatches(),
SExperimentalPatches(),
ListTile(

View File

@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
import 'package:flutter_i18n/widgets/I18nText.dart';
import 'package:revanced_manager/ui/views/settings/settings_viewmodel.dart';
class SAutoUpdatePatches extends StatefulWidget {
const SAutoUpdatePatches({super.key});
@override
State<SAutoUpdatePatches> createState() => _SAutoUpdatePatchesState();
}
final _settingsViewModel = SettingsViewModel();
class _SAutoUpdatePatchesState extends State<SAutoUpdatePatches> {
@override
Widget build(BuildContext context) {
return SwitchListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 20.0),
title: I18nText(
'homeView.patchesConsentDialogText3',
child: const Text(
'',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500,
),
),
),
subtitle: I18nText('settingsView.autoUpdatePatchesHint'),
value: _settingsViewModel.isPatchesAutoUpdate(),
onChanged: (value) {
setState(() {
_settingsViewModel.setPatchesAutoUpdate(value);
});
},
);
}
}