Fixed issue with backgorund task not starting

This commit is contained in:
Imran Remtulla
2022-09-30 23:21:35 -04:00
parent 49b9a65053
commit de07583161
4 changed files with 29 additions and 18 deletions

View File

@ -14,7 +14,7 @@ import 'package:dynamic_color/dynamic_color.dart';
import 'package:device_info_plus/device_info_plus.dart'; import 'package:device_info_plus/device_info_plus.dart';
const String currentReleaseTag = const String currentReleaseTag =
'v0.5.2-beta'; // KEEP THIS IN SYNC WITH GITHUB RELEASES 'v0.5.3-beta'; // KEEP THIS IN SYNC WITH GITHUB RELEASES
const String bgUpdateCheckTaskName = 'bg-update-check'; const String bgUpdateCheckTaskName = 'bg-update-check';
@ -107,14 +107,21 @@ void main() async {
ChangeNotifierProvider(create: (context) => SettingsProvider()), ChangeNotifierProvider(create: (context) => SettingsProvider()),
Provider(create: (context) => NotificationsProvider()) Provider(create: (context) => NotificationsProvider())
], ],
child: const MyApp(), child: const Obtainium(),
)); ));
} }
var defaultThemeColour = Colors.deepPurple; var defaultThemeColour = Colors.deepPurple;
class MyApp extends StatelessWidget { class Obtainium extends StatefulWidget {
const MyApp({super.key}); const Obtainium({super.key});
@override
State<Obtainium> createState() => _ObtainiumState();
}
class _ObtainiumState extends State<Obtainium> {
var existingUpdateInterval = -1;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -143,18 +150,21 @@ class MyApp extends StatelessWidget {
]); ]);
} }
// Register the background update task according to the user's setting // Register the background update task according to the user's setting
if (settingsProvider.updateInterval == 0) { if (existingUpdateInterval != settingsProvider.updateInterval) {
Workmanager().cancelByUniqueName(bgUpdateCheckTaskName); existingUpdateInterval = settingsProvider.updateInterval;
} else { if (existingUpdateInterval == 0) {
Workmanager().registerPeriodicTask( Workmanager().cancelByUniqueName(bgUpdateCheckTaskName);
bgUpdateCheckTaskName, bgUpdateCheckTaskName, } else {
frequency: Duration(minutes: settingsProvider.updateInterval), Workmanager().registerPeriodicTask(
initialDelay: Duration(minutes: settingsProvider.updateInterval), bgUpdateCheckTaskName, bgUpdateCheckTaskName,
constraints: Constraints(networkType: NetworkType.connected), frequency: Duration(minutes: existingUpdateInterval),
existingWorkPolicy: ExistingWorkPolicy.keep, initialDelay: Duration(minutes: existingUpdateInterval),
backoffPolicy: BackoffPolicy.linear, constraints: Constraints(networkType: NetworkType.connected),
backoffPolicyDelay: existingWorkPolicy: ExistingWorkPolicy.replace,
const Duration(minutes: minUpdateIntervalMinutes)); backoffPolicy: BackoffPolicy.linear,
backoffPolicyDelay:
const Duration(minutes: minUpdateIntervalMinutes));
}
} }
} }

View File

@ -129,5 +129,6 @@ class SettingsProvider with ChangeNotifier {
void setSettingString(String settingId, String value) { void setSettingString(String settingId, String value) {
prefs?.setString(settingId, value); prefs?.setString(settingId, value);
notifyListeners();
} }
} }

View File

@ -17,7 +17,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 0.5.2+23 # When changing this, update the tag in main() accordingly version: 0.5.3+24 # When changing this, update the tag in main() accordingly
environment: environment:
sdk: '>=2.19.0-79.0.dev <3.0.0' sdk: '>=2.19.0-79.0.dev <3.0.0'

View File

@ -13,7 +13,7 @@ import 'package:obtainium/main.dart';
void main() { void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async { testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame. // Build our app and trigger a frame.
await tester.pumpWidget(const MyApp()); await tester.pumpWidget(const Obtainium());
// Verify that our counter starts at 0. // Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget); expect(find.text('0'), findsOneWidget);