Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Updated background_fetch, adjust plugin #501 OT-779
Browse files Browse the repository at this point in the history
  • Loading branch information
Boehrsi committed May 18, 2020
1 parent f42ea9b commit abbaea3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
56 changes: 32 additions & 24 deletions lib/src/background_refresh/background_refresh_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,30 @@ import 'package:logging/logging.dart';
import 'package:ox_coi/src/notifications/local_notification_manager.dart';
import 'package:ox_coi/src/utils/constants.dart';

void backgroundHeadlessTask() async {
void backgroundHeadlessTask(String taskId) async {
final _logger = Logger("background_refresh_manager");// TODO not logging cause no global logger configured
_logger.info("Callback (background) triggered");
var core = DeltaChatCore();
var init = await core.init(dbName);
if (init) {
await core.start();
var isSetup = await core.setupAsync(dbName);
if (isSetup) {
_logger.info("Callback (background) checking for new messages");
await getMessages();
await core.stop();
await core.tearDownAsync();
}
BackgroundFetch.finish();
_logger.info("Callback (background) finishing");
BackgroundFetch.finish(taskId);
}

Future<void> getMessages() async {
var context = Context();
final context = Context();
await context.interruptIdleForIncomingMessages();
var localNotificationManager = LocalNotificationManager();
final localNotificationManager = LocalNotificationManager.recreate();
localNotificationManager.setup();
await localNotificationManager.triggerNotificationAsync();
}

class BackgroundRefreshManager {
final Logger _logger = Logger("background_refresh_manager");
final _logger = Logger("background_refresh_manager");

static BackgroundRefreshManager _instance;

Expand All @@ -77,22 +80,27 @@ class BackgroundRefreshManager {
BackgroundRefreshManager._internal();

setupAndStart() {
BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask);
BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask).then((value) {
_logger.info("Register headless task");
});
BackgroundFetch.configure(
BackgroundFetchConfig(
minimumFetchInterval: 15,
stopOnTerminate: false,
enableHeadless: true,
startOnBoot: true,
),
_callback);
_running = true;
_logger.info("Configured and started background fetch");
}

Future<void> _callback() async {
await getMessages();
BackgroundFetch.finish();
BackgroundFetchConfig(
minimumFetchInterval: 15,
stopOnTerminate: false,
enableHeadless: true,
startOnBoot: true,
requiredNetworkType: NetworkType.ANY,
),
(String taskId) async {
_logger.info("Callback (foreground) triggered, checking for new messages");
await getMessages();
_logger.info("Callback (foreground) finishing");
BackgroundFetch.finish(taskId);
},
).then((value) {
_logger.info("Configured and started background fetch");
_running = true;
});
}

void start() async {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/main_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class MainBloc extends Bloc<MainEvent, MainState> {
}

Future<void> _initCore() async {
await core.init(dbName);
await core.setupAsync(dbName);
}

Future<void> _setupDefaultValues() async {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/notifications/local_notification_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class LocalNotificationManager {

LocalNotificationManager._internal();

LocalNotificationManager.recreate() {
_instance = LocalNotificationManager._internal();
}

void setup() {
_registerListeners();
}
Expand Down Expand Up @@ -106,7 +110,7 @@ class LocalNotificationManager {
await createInviteNotificationsAsync();
}

Future createChatNotificationsAsync() async {
Future<void> createChatNotificationsAsync() async {
final HashMap<String, int> notificationHistory = await _getNotificationHistoryAsync();
final List<int> freshMessages = await _context.getFreshMessages();
_temporaryMessageRepository.putIfAbsent(ids: freshMessages);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ packages:
name: background_fetch
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
version: "0.5.5"
bloc:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
flutter_localizations:
sdk: flutter
app_settings: ^3.0.0+1
background_fetch: ^0.4.0
background_fetch: ^0.5.5
bloc: ^3.0.0
cached_network_image: ^2.0.0
contacts_service: ^0.3.10
Expand Down

0 comments on commit abbaea3

Please sign in to comment.