fix: improve update manager button

This commit is contained in:
Alberto Ponces 2022-08-18 17:32:58 +01:00
parent 389eae1447
commit 6d3ea7a991
7 changed files with 90 additions and 41 deletions

View File

@ -1,13 +1,23 @@
import 'dart:io';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:github/github.dart';
import 'package:revanced_manager/models/patched_application.dart';
import 'package:timeago/timeago.dart';
class GithubAPI {
final GitHub _github = GitHub();
Future<File?> latestRelease(String org, repoName) async {
Future<String?> latestReleaseVersion(String org, repoName) async {
try {
var latestRelease = await _github.repositories.getLatestRelease(
RepositorySlug(org, repoName),
);
return latestRelease.tagName;
} on Exception {
return null;
}
}
Future<File?> latestReleaseFile(String org, repoName) async {
try {
var latestRelease = await _github.repositories.getLatestRelease(
RepositorySlug(org, repoName),
@ -51,15 +61,4 @@ class GithubAPI {
return List.empty();
}
}
Future<bool> hasUpdates(PatchedApplication app, String org, repoName) async {
// TODO: get status based on last update time on the folder of this app?
return true;
}
Future<String> getChangelog(
PatchedApplication app, String org, repoName) async {
// TODO: get changelog based on last commits on the folder of this app?
return 'fix: incorrect fingerprint version';
}
}

View File

@ -1,4 +1,5 @@
import 'dart:io';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:revanced_manager/constants.dart';
import 'package:revanced_manager/services/github_api.dart';
@ -6,14 +7,43 @@ class ManagerAPI {
final GithubAPI _githubAPI = GithubAPI();
Future<File?> downloadPatches() async {
return await _githubAPI.latestRelease(ghOrg, patchesRepo);
return await _githubAPI.latestReleaseFile(ghOrg, patchesRepo);
}
Future<File?> downloadIntegrations() async {
return await _githubAPI.latestRelease(ghOrg, integrationsRepo);
return await _githubAPI.latestReleaseFile(ghOrg, integrationsRepo);
}
Future<File?> downloadManager() async {
return await _githubAPI.latestRelease(ghOrg, managerRepo);
return await _githubAPI.latestReleaseFile(
'Aunali321',
'revanced-manager-flutter',
);
}
Future<String?> getLatestPatchesVersion() async {
return await _githubAPI.latestReleaseVersion(ghOrg, patchesRepo);
}
Future<String?> getLatestManagerVersion() async {
return await _githubAPI.latestReleaseVersion(
'Aunali321',
'revanced-manager-flutter',
);
}
Future<String> getCurrentManagerVersion() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
return packageInfo.version;
}
Future<bool> hasAppUpdates(String packageName) async {
// TODO: get status based on last update time on the folder of this app?
return true;
}
Future<String> getAppChangelog(String packageName) async {
// TODO: get changelog based on last commits on the folder of this app?
return 'to be implemented';
}
}

View File

@ -8,9 +8,7 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:injectable/injectable.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/app/app.router.dart';
import 'package:revanced_manager/constants.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/ui/views/patcher/patcher_viewmodel.dart';
@ -22,7 +20,6 @@ import 'package:stacked_services/stacked_services.dart';
class HomeViewModel extends BaseViewModel {
final NavigationService _navigationService = locator<NavigationService>();
final ManagerAPI _managerAPI = ManagerAPI();
final GithubAPI _githubAPI = GithubAPI();
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
@ -56,7 +53,7 @@ class HomeViewModel extends BaseViewModel {
List<String> patchedApps = prefs.getStringList('patchedApps') ?? [];
for (String str in patchedApps) {
PatchedApplication app = PatchedApplication.fromJson(json.decode(str));
bool hasUpdates = await _githubAPI.hasUpdates(app, ghOrg, patchesRepo);
bool hasUpdates = await _managerAPI.hasAppUpdates(app.packageName);
if (hasUpdates == isUpdatable) {
list.add(app);
}
@ -64,6 +61,23 @@ class HomeViewModel extends BaseViewModel {
return list;
}
Future<bool> hasManagerUpdates() async {
String? latestVersion = await _managerAPI.getLatestManagerVersion();
String currentVersion = await _managerAPI.getCurrentManagerVersion();
if (latestVersion != null) {
try {
int latestVersionInt =
int.parse(latestVersion.replaceFirst('v', '').replaceAll('.', ''));
int currentVersionInt =
int.parse(currentVersion.replaceFirst('v', '').replaceAll('.', ''));
return latestVersionInt > currentVersionInt;
} on Exception {
return false;
}
}
return false;
}
void updateManager(BuildContext context) async {
File? managerApk = await _managerAPI.downloadManager();
if (managerApk != null) {

View File

@ -1,8 +1,7 @@
import 'package:flutter/material.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/constants.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/ui/views/home/home_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/application_item.dart';
@ -11,7 +10,7 @@ class AvailableUpdatesCard extends StatelessWidget {
Key? key,
}) : super(key: key);
final GithubAPI _githubAPI = GithubAPI();
final ManagerAPI _managerAPI = ManagerAPI();
@override
Widget build(BuildContext context) {
@ -28,10 +27,8 @@ class AvailableUpdatesCard extends StatelessWidget {
physics: const NeverScrollableScrollPhysics(),
itemCount: snapshot.data!.length,
itemBuilder: (context, index) => FutureBuilder<String>(
future: _githubAPI.getChangelog(
snapshot.data![index],
ghOrg,
patchesRepo,
future: _managerAPI.getAppChangelog(
snapshot.data![index].packageName,
),
initialData: '',
builder: (context, snapshot2) => ApplicationItem(

View File

@ -1,8 +1,7 @@
import 'package:flutter/material.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/constants.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/ui/views/home/home_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/application_item.dart';
@ -11,7 +10,7 @@ class InstalledAppsCard extends StatelessWidget {
Key? key,
}) : super(key: key);
final GithubAPI _githubAPI = GithubAPI();
final ManagerAPI _managerAPI = ManagerAPI();
@override
Widget build(BuildContext context) {
@ -28,10 +27,8 @@ class InstalledAppsCard extends StatelessWidget {
physics: const NeverScrollableScrollPhysics(),
itemCount: snapshot.data!.length,
itemBuilder: (context, index) => FutureBuilder<String>(
future: _githubAPI.getChangelog(
snapshot.data![index],
ghOrg,
patchesRepo,
future: _managerAPI.getAppChangelog(
snapshot.data![index].packageName,
),
initialData: '',
builder: (context, snapshot2) => ApplicationItem(

View File

@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/services/github_api.dart';
import 'package:revanced_manager/constants.dart';
import 'package:revanced_manager/ui/views/home/home_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/patch_text_button.dart';
class LatestCommitCard extends StatefulWidget {
@ -96,15 +98,24 @@ class _LatestCommitCardState extends State<LatestCommitCard> {
),
],
),
PatchTextButton(
FutureBuilder<bool>(
future: locator<HomeViewModel>().hasManagerUpdates(),
initialData: false,
builder: (context, snapshot) => Opacity(
opacity: snapshot.hasData && snapshot.data! ? 1.0 : 0.5,
child: PatchTextButton(
text: FlutterI18n.translate(
context,
'latestCommitCard.updateButton',
),
onPressed: widget.onPressed,
onPressed: snapshot.hasData && snapshot.data!
? widget.onPressed
: () => {},
backgroundColor: Theme.of(context).colorScheme.secondary,
borderColor: Theme.of(context).colorScheme.secondary,
),
),
),
],
),
);

View File

@ -32,6 +32,7 @@ dependencies:
http: ^0.13.4
injectable: ^1.5.3
json_annotation: ^4.6.0
package_info_plus: ^1.4.3+1
path_provider: ^2.0.11
root: ^2.0.2
share_extend: ^2.0.0