Bugfixes + BG task seems to work

This commit is contained in:
Imran Remtulla
2022-08-19 14:59:00 -04:00
parent 15d5ac1eef
commit 6b43d4ed60
4 changed files with 70 additions and 15 deletions

View File

@@ -1,11 +1,42 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:obtainium/pages/apps.dart'; import 'package:obtainium/pages/apps.dart';
import 'package:obtainium/services/apps_provider.dart'; import 'package:obtainium/services/apps_provider.dart';
import 'package:obtainium/services/source_service.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:toast/toast.dart'; import 'package:workmanager/workmanager.dart';
void backgroundUpdateCheck() {
Workmanager().executeTask((task, inputData) async {
var appsProvider = AppsProvider(bg: true);
await appsProvider.loadApps();
List<App> updates = await appsProvider.getUpdates();
if (updates.isNotEmpty) {
String message = updates.length == 1
? '${updates[0].name} has an update.'
: '${(updates.length == 2 ? '${updates[0].name} and ${updates[1].name}' : '${updates[0].name} and ${updates.length - 1} more apps')} have updates.';
appsProvider.downloaderNotifications.cancel(2);
appsProvider.notify(
2,
'Updates Available',
message,
'UPDATES_AVAILABLE',
'Updates Available',
'Notifies the user that updates are available for one or more Apps tracked by Obtainium');
}
return Future.value(true);
});
}
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
Workmanager().initialize(
backgroundUpdateCheck,
);
await Workmanager().cancelByUniqueName('update-apps-task');
await Workmanager().registerPeriodicTask(
'update-apps-task', 'backgroundUpdateCheck',
frequency: const Duration(minutes: 15),
constraints: Constraints(networkType: NetworkType.connected));
runApp(MultiProvider( runApp(MultiProvider(
providers: [ChangeNotifierProvider(create: (context) => AppsProvider())], providers: [ChangeNotifierProvider(create: (context) => AppsProvider())],
child: const MyApp(), child: const MyApp(),

View File

@@ -19,11 +19,14 @@ class AppsProvider with ChangeNotifier {
bool loadingApps = false; bool loadingApps = false;
bool gettingUpdates = false; bool gettingUpdates = false;
AppsProvider() { AppsProvider({bool bg = false}) {
initializeDownloader(); initializeNotifs();
loadApps().then((_) { loadApps().then((_) {
clearDownloadStates(); clearDownloadStates();
}); });
if (!bg) {
initializeDownloader();
}
} }
// Notifications plugin for downloads // Notifications plugin for downloads
@@ -53,15 +56,19 @@ class AppsProvider with ChangeNotifier {
int progress = data[2]; int progress = data[2];
downloadCallbackForeground(id, status, progress); downloadCallbackForeground(id, status, progress);
}); });
// Initialize the notifications service
await downloaderNotifications.initialize(const InitializationSettings(
android: AndroidInitializationSettings('ic_launcher')));
// Subscribe to changes in the app foreground status // Subscribe to changes in the app foreground status
foregroundSubscription = FGBGEvents.stream.listen((event) async { foregroundSubscription = FGBGEvents.stream.listen((event) async {
isForeground = event == FGBGType.foreground; isForeground = event == FGBGType.foreground;
if (isForeground) await loadApps();
}); });
} }
Future<void> initializeNotifs() async {
// Initialize the notifications service
await downloaderNotifications.initialize(const InitializationSettings(
android: AndroidInitializationSettings('ic_launcher')));
}
// Callback that receives FlutterDownloader status and forwards to a foreground function // Callback that receives FlutterDownloader status and forwards to a foreground function
@pragma('vm:entry-point') @pragma('vm:entry-point')
static void downloadCallbackBackground( static void downloadCallbackBackground(
@@ -71,24 +78,33 @@ class AppsProvider with ChangeNotifier {
send!.send([id, status, progress]); send!.send([id, status, progress]);
} }
Future<void> notify(int id, String title, String message, String channelCode,
String channelName, String channelDescription) {
return downloaderNotifications.show(
id,
title,
message,
NotificationDetails(
android: AndroidNotificationDetails(channelCode, channelName,
channelDescription: channelDescription,
importance: Importance.max,
priority: Priority.max,
groupKey: 'dev.imranr.obtainium.$channelCode')));
}
// Foreground function to act on FlutterDownloader status updates (install downloaded APK) // Foreground function to act on FlutterDownloader status updates (install downloaded APK)
void downloadCallbackForeground( void downloadCallbackForeground(
String id, DownloadTaskStatus status, int progress) async { String id, DownloadTaskStatus status, int progress) async {
if (status == DownloadTaskStatus.complete) { if (status == DownloadTaskStatus.complete) {
// Wait for app to come to the foreground if not already, and notify the user // Wait for app to come to the foreground if not already, and notify the user
while (!isForeground) { while (!isForeground) {
await downloaderNotifications.show( await notify(
1, 1,
'Complete App Installation', 'Complete App Installation',
'Obtainium must be open to install Apps', 'Obtainium must be open to install Apps',
const NotificationDetails( 'COMPLETE_INSTALL',
android: AndroidNotificationDetails( 'Complete App Installation',
'COMPLETE_INSTALL', 'Complete App Installation', 'Asks the user to return to Obtanium to finish installing an App');
channelDescription:
'Ask the user to return to Obtanium to finish installing an App',
importance: Importance.max,
priority: Priority.max,
groupKey: 'dev.imranr.obtainium.COMPLETE_INSTALL')));
if (await FGBGEvents.stream.first == FGBGType.foreground) { if (await FGBGEvents.stream.first == FGBGType.foreground) {
break; break;
} }

View File

@@ -392,6 +392,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.7.0" version: "2.7.0"
workmanager:
dependency: "direct main"
description:
name: workmanager
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.0"
xdg_directories: xdg_directories:
dependency: transitive dependency: transitive
description: description:

View File

@@ -44,6 +44,7 @@ dependencies:
http: ^0.13.5 http: ^0.13.5
toast: ^0.3.0 toast: ^0.3.0
webview_flutter: ^3.0.4 webview_flutter: ^3.0.4
workmanager: ^0.5.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: