From 45f4a5b2071b9481e5e06680460f843802ca678c Mon Sep 17 00:00:00 2001 From: Alberto Ponces Date: Fri, 26 Aug 2022 02:01:53 +0100 Subject: [PATCH] feat: fix list contributors and add patched apps changelog (wip) --- lib/services/github_api.dart | 17 ++++--- lib/services/manager_api.dart | 30 +++++++---- lib/services/root_api.dart | 5 +- .../contributors/contributors_viewmodel.dart | 1 + lib/ui/views/home/home_viewmodel.dart | 4 +- .../homeView/available_updates_card.dart | 51 ++++++++++--------- .../widgets/homeView/installed_apps_card.dart | 49 +++++++++--------- 7 files changed, 88 insertions(+), 69 deletions(-) diff --git a/lib/services/github_api.dart b/lib/services/github_api.dart index 84bef88e..fdfdc6be 100644 --- a/lib/services/github_api.dart +++ b/lib/services/github_api.dart @@ -56,13 +56,14 @@ class GithubAPI { } Future> getContributors(String org, repoName) async { - try { - var contributors = _github.repositories.listContributors( - RepositorySlug(org, repoName), - ); - return contributors.toList(); - } on Exception { - return List.empty(); - } + return await (_github.repositories.listContributors( + RepositorySlug(org, repoName), + )).toList(); + } + + Future> getCommits(String org, repoName) async { + return await (_github.repositories.listCommits( + RepositorySlug(org, repoName), + )).toList(); } } diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index c301a2f2..1febe15c 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -94,6 +94,7 @@ class ManagerAPI { Future reAssessSavedApps() async { List patchedApps = getPatchedApps(); bool isRoot = isRooted() ?? false; + List toRemove = []; for (PatchedApplication app in patchedApps) { bool existsRoot = false; if (isRoot) { @@ -101,23 +102,23 @@ class ManagerAPI { } bool existsNonRoot = await DeviceApps.isAppInstalled(app.packageName); if (!existsRoot && !existsNonRoot) { - patchedApps.remove(app); + toRemove.add(app); } else if (existsNonRoot) { ApplicationWithIcon? application = await DeviceApps.getApp(app.packageName, true) as ApplicationWithIcon?; if (application != null) { int savedVersionInt = - int.parse(app.version.replaceFirst('v', '').replaceAll('.', '')); - int currentVersionInt = int.parse(application.versionName! - .replaceFirst('v', '') - .replaceAll('.', '')); + int.parse(app.version.replaceAll(RegExp('[^0-9]'), '')); + int currentVersionInt = int.parse( + application.versionName!.replaceAll(RegExp('[^0-9]'), '')); if (savedVersionInt < currentVersionInt) { - patchedApps.remove(app); + toRemove.add(app); } } } } + patchedApps.removeWhere((a) => toRemove.contains(a)); setPatchedApps(patchedApps); List apps = await _rootAPI.getInstalledApps(); for (String packageName in apps) { @@ -136,8 +137,19 @@ class ManagerAPI { return false; } - Future getAppChangelog(String packageName) async { - // TODO: get changelog based on last commits on the folder of this app? - return 'To be implemented'; + Future> getAppChangelog( + String packageName, + DateTime lastUpdated, + ) async { + return (await _githubAPI.getCommits(ghOrg, patchesRepo)) + .where((c) => + c.commit != null && + c.commit!.message != null && + !c.commit!.message!.startsWith('chore') && + c.commit!.author != null && + c.commit!.author!.date != null) + .map((c) => ' - ${c.commit!.message!}') + .toList() + .sublist(0, 3); } } diff --git a/lib/services/root_api.dart b/lib/services/root_api.dart index c1926e52..c92da35f 100644 --- a/lib/services/root_api.dart +++ b/lib/services/root_api.dart @@ -27,12 +27,15 @@ class RootAPI { ); if (res != null) { List apps = res.split('\n'); + List toRemove = []; for (String packageName in apps) { bool isInstalled = await isAppInstalled(packageName); if (!isInstalled) { - apps.remove(packageName); + toRemove.add(packageName); } } + apps.removeWhere((a) => toRemove.contains(a)); + return apps; } } on Exception { return List.empty(); diff --git a/lib/ui/views/contributors/contributors_viewmodel.dart b/lib/ui/views/contributors/contributors_viewmodel.dart index a3f2549a..38865c82 100644 --- a/lib/ui/views/contributors/contributors_viewmodel.dart +++ b/lib/ui/views/contributors/contributors_viewmodel.dart @@ -32,5 +32,6 @@ class ContributorsViewModel extends BaseViewModel { ghOrg, managerRepo, ); + notifyListeners(); } } diff --git a/lib/ui/views/home/home_viewmodel.dart b/lib/ui/views/home/home_viewmodel.dart index eab5d17f..c0c8a3d0 100644 --- a/lib/ui/views/home/home_viewmodel.dart +++ b/lib/ui/views/home/home_viewmodel.dart @@ -65,9 +65,9 @@ class HomeViewModel extends BaseViewModel { if (latestVersion != null) { try { int latestVersionInt = - int.parse(latestVersion.replaceFirst('v', '').replaceAll('.', '')); + int.parse(latestVersion.replaceAll(RegExp('[^0-9]'), '')); int currentVersionInt = - int.parse(currentVersion.replaceFirst('v', '').replaceAll('.', '')); + int.parse(currentVersion.replaceAll(RegExp('[^0-9]'), '')); return latestVersionInt > currentVersionInt; } on Exception { return false; diff --git a/lib/ui/widgets/homeView/available_updates_card.dart b/lib/ui/widgets/homeView/available_updates_card.dart index a61fb382..fdae192a 100644 --- a/lib/ui/widgets/homeView/available_updates_card.dart +++ b/lib/ui/widgets/homeView/available_updates_card.dart @@ -20,32 +20,33 @@ class AvailableUpdatesCard extends StatelessWidget { children: [ FutureBuilder>( future: locator().getPatchedApps(true), - builder: (context, snapshot) => - snapshot.hasData && snapshot.data!.isNotEmpty - ? ListView.builder( - shrinkWrap: true, - physics: const NeverScrollableScrollPhysics(), - padding: EdgeInsets.zero, - itemCount: snapshot.data!.length, - itemBuilder: (context, index) => FutureBuilder( - future: _managerAPI.getAppChangelog( - snapshot.data![index].packageName, - ), - initialData: '', - builder: (context, snapshot2) => ApplicationItem( - icon: snapshot.data![index].icon, - name: snapshot.data![index].name, - patchDate: snapshot.data![index].patchDate, - changelog: snapshot2.data!, - isUpdatableApp: true, - onPressed: () => - locator().navigateToPatcher( - snapshot.data![index], - ), - ), + builder: (context, snapshot) => snapshot.hasData && + snapshot.data!.isNotEmpty + ? ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + padding: EdgeInsets.zero, + itemCount: snapshot.data!.length, + itemBuilder: (context, index) => FutureBuilder>( + future: _managerAPI.getAppChangelog( + snapshot.data![index].packageName, + snapshot.data![index].patchDate, + ), + initialData: List.empty(), + builder: (context, snapshot2) => ApplicationItem( + icon: snapshot.data![index].icon, + name: snapshot.data![index].name, + patchDate: snapshot.data![index].patchDate, + changelog: '${snapshot2.data!.join('\n')}\n...', + isUpdatableApp: true, + onPressed: () => + locator().navigateToPatcher( + snapshot.data![index], ), - ) - : Container(), + ), + ), + ) + : Container(), ), ], ); diff --git a/lib/ui/widgets/homeView/installed_apps_card.dart b/lib/ui/widgets/homeView/installed_apps_card.dart index bb4078dc..7036e8e5 100644 --- a/lib/ui/widgets/homeView/installed_apps_card.dart +++ b/lib/ui/widgets/homeView/installed_apps_card.dart @@ -21,31 +21,32 @@ class InstalledAppsCard extends StatelessWidget { children: [ FutureBuilder>( future: locator().getPatchedApps(false), - builder: (context, snapshot) => - snapshot.hasData && snapshot.data!.isNotEmpty - ? ListView.builder( - shrinkWrap: true, - physics: const NeverScrollableScrollPhysics(), - padding: EdgeInsets.zero, - itemCount: snapshot.data!.length, - itemBuilder: (context, index) => FutureBuilder( - future: _managerAPI.getAppChangelog( - snapshot.data![index].packageName, - ), - initialData: '', - builder: (context, snapshot2) => ApplicationItem( - icon: snapshot.data![index].icon, - name: snapshot.data![index].name, - patchDate: snapshot.data![index].patchDate, - changelog: snapshot2.data!, - isUpdatableApp: false, - onPressed: () => DeviceApps.openApp( - snapshot.data![index].packageName, - ), - ), + builder: (context, snapshot) => snapshot.hasData && + snapshot.data!.isNotEmpty + ? ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + padding: EdgeInsets.zero, + itemCount: snapshot.data!.length, + itemBuilder: (context, index) => FutureBuilder>( + future: _managerAPI.getAppChangelog( + snapshot.data![index].packageName, + snapshot.data![index].patchDate, + ), + initialData: const ['Loading'], + builder: (context, snapshot2) => ApplicationItem( + icon: snapshot.data![index].icon, + name: snapshot.data![index].name, + patchDate: snapshot.data![index].patchDate, + changelog: '${snapshot2.data!.join('\n')}\n(...)', + isUpdatableApp: false, + onPressed: () => DeviceApps.openApp( + snapshot.data![index].packageName, ), - ) - : Container(), + ), + ), + ) + : Container(), ), ], );