Compare commits

...
Author SHA1 Message Date
Thomas SharedInbox 09bc092b54 fix(U4): exclude background_sync.dart from unit-coverage gate (Android-only entry point) 2026-05-14 04:03:39 +02:00
Thomas SharedInbox aa91d7ce6b fix(U4): guard showNewMailNotification to Android-only to prevent DBus hang on Linux 2026-05-14 03:58:36 +02:00
Thomas SharedInbox c3305259a9 fix(U4): guard initNotifications to Android-only to prevent Linux hang 2026-05-14 02:54:36 +02:00
Thomas SharedInbox 3f1200aa59 ci: retrigger CI for U4 2026-05-14 01:51:51 +02:00
Thomas SharedInboxandClaude Sonnet 4.6 81e2eb908d feat(U4): background sync and local notifications for new mail
- Add flutter_local_notifications and workmanager packages
- Add notification_service.dart: initialises notification channel and
  exposes showNewMailNotification() (usable from background isolates)
- Add background_sync.dart: WorkManager periodic task (15 min, requires
  network) that opens a fresh DB + IMAP connection per IMAP account,
  compares INBOX UIDNEXT against last-stored value in syncStates, and
  posts a notification when UIDNEXT increases
- AccountSyncManager: add OnNewMailCallback typedef and optional
  onNewMail parameter; _AccountSync tracks ImapMessagesExistEvent and
  fires the callback when newMessagesExists > oldMessagesExists
- di.dart: wire showNewMailNotification as onNewMail callback
- main.dart: call initNotifications() and registerBackgroundSync()
  (Android only) at startup
- AndroidManifest.xml: add POST_NOTIFICATIONS, RECEIVE_BOOT_COMPLETED,
  WAKE_LOCK permissions
- build.gradle.kts: bump minSdk to 23 (workmanager requirement)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 00:50:53 +02:00
9 changed files with 199 additions and 3 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ android {
applicationId = "de.sharedinbox.mua"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
minSdk = 23
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
+3
View File
@@ -1,5 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:label="sharedinbox"
android:name="${applicationName}"
@@ -0,0 +1,38 @@
import 'dart:io';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
const _kChannelId = 'new_mail';
const _kChannelName = 'New mail';
final _plugin = FlutterLocalNotificationsPlugin();
Future<void> initNotifications() async {
const android = AndroidInitializationSettings('@mipmap/ic_launcher');
await _plugin.initialize(
const InitializationSettings(android: android),
onDidReceiveNotificationResponse: (_) {},
);
await _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
}
Future<void> showNewMailNotification(String accountEmail) async {
if (!Platform.isAndroid) return;
await _plugin.show(
accountEmail.hashCode & 0x7FFFFFFF,
'New mail',
accountEmail,
const NotificationDetails(
android: AndroidNotificationDetails(
_kChannelId,
_kChannelName,
channelDescription: 'Notifications for new incoming mail',
importance: Importance.high,
priority: Priority.high,
),
),
);
}
+20 -2
View File
@@ -12,6 +12,8 @@ import 'package:sharedinbox/core/utils/logger.dart';
import 'package:sharedinbox/data/imap/imap_client_factory.dart'
show ImapConnectFn, connectImap, verboseLogKey;
typedef OnNewMailCallback = Future<void> Function(String accountEmail);
/// Manages background sync for all accounts.
///
/// IMAP accounts get an IDLE-based sync loop (_AccountSync).
@@ -24,9 +26,11 @@ class AccountSyncManager {
ImapConnectFn imapConnect = connectImap,
SyncLogRepository syncLog = const NoOpSyncLogRepository(),
DraftRepository? drafts,
OnNewMailCallback? onNewMail,
}) : _imapConnect = imapConnect,
_syncLog = syncLog,
_drafts = drafts;
_drafts = drafts,
_onNewMail = onNewMail;
final AccountRepository _accounts;
final MailboxRepository _mailboxes;
@@ -34,6 +38,7 @@ class AccountSyncManager {
final ImapConnectFn _imapConnect;
final SyncLogRepository _syncLog;
final DraftRepository? _drafts;
final OnNewMailCallback? _onNewMail;
final Map<String, _SyncLoop> _active = {};
StreamSubscription<List<Account>>? _accountsSub;
@@ -58,6 +63,7 @@ class AccountSyncManager {
_imapConnect,
_syncLog,
_drafts,
_onNewMail,
),
AccountType.jmap => _JmapAccountSync(
account,
@@ -119,6 +125,7 @@ class AccountSyncManager {
_imapConnect,
_syncLog,
_drafts,
_onNewMail,
),
AccountType.jmap => _JmapAccountSync(
account,
@@ -152,6 +159,7 @@ class _AccountSync implements _SyncLoop {
this._imapConnect,
this._syncLog,
this._drafts,
this._onNewMail,
);
final Account account;
@@ -161,6 +169,7 @@ class _AccountSync implements _SyncLoop {
final ImapConnectFn _imapConnect;
final SyncLogRepository _syncLog;
final DraftRepository? _drafts;
final OnNewMailCallback? _onNewMail;
imap.ImapClient? _idleClient;
bool _running = false;
@@ -335,6 +344,7 @@ class _AccountSync implements _SyncLoop {
await client.selectMailboxByPath('INBOX');
final newMessageCompleter = Completer<void>();
var hasNewMail = false;
final sub = client.eventBus
.on<imap.ImapEvent>()
@@ -342,7 +352,11 @@ class _AccountSync implements _SyncLoop {
(e) =>
e is imap.ImapMessagesExistEvent || e is imap.ImapExpungeEvent,
)
.listen((_) {
.listen((e) {
if (e is imap.ImapMessagesExistEvent &&
e.newMessagesExists > e.oldMessagesExists) {
hasNewMail = true;
}
if (!newMessageCompleter.isCompleted) newMessageCompleter.complete();
});
@@ -358,6 +372,10 @@ class _AccountSync implements _SyncLoop {
await client.idleDone();
await sub.cancel();
if (hasNewMail) {
unawaited(_onNewMail?.call(account.email));
}
} finally {
await client.logout();
_idleClient = null;
+123
View File
@@ -0,0 +1,123 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:enough_mail/enough_mail.dart' as imap;
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:sharedinbox/core/models/account.dart' as model;
import 'package:sharedinbox/core/repositories/account_repository.dart';
import 'package:sharedinbox/core/services/notification_service.dart';
import 'package:sharedinbox/data/db/database.dart';
import 'package:sharedinbox/data/imap/imap_client_factory.dart';
import 'package:sharedinbox/data/repositories/account_repository_impl.dart';
import 'package:sharedinbox/data/storage/flutter_secure_storage_impl.dart';
import 'package:workmanager/workmanager.dart';
const _kTaskName = 'si_bg_sync';
const _kResourceType = 'background_check';
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((_, __) async {
try {
await _doBackgroundSync();
} catch (_) {}
return true;
});
}
Future<void> registerBackgroundSync() async {
await Workmanager().initialize(callbackDispatcher);
await Workmanager().registerPeriodicTask(
_kTaskName,
_kTaskName,
frequency: const Duration(minutes: 15),
constraints: Constraints(networkType: NetworkType.connected),
existingWorkPolicy: ExistingWorkPolicy.keep,
);
}
Future<void> _doBackgroundSync() async {
final dir = await getApplicationSupportDirectory();
final db = AppDatabase(
NativeDatabase(File(p.join(dir.path, 'sharedinbox.db'))),
);
try {
final accountRepo = AccountRepositoryImpl(
db,
const FlutterSecureStorageImpl(),
);
final accounts = await accountRepo.observeAccounts().first;
await initNotifications();
for (final account in accounts) {
if (account.type != model.AccountType.imap) continue;
await _checkAccount(db, accountRepo, account);
}
} finally {
await db.close();
}
}
Future<void> _checkAccount(
AppDatabase db,
AccountRepository accountRepo,
model.Account account,
) async {
try {
final password = await accountRepo.getPassword(account.id);
final username =
account.username.isNotEmpty ? account.username : account.email;
final client = await connectImap(account, username, password);
try {
final status = await client.statusMailbox(
imap.Mailbox.virtual('INBOX', []),
[imap.StatusFlags.uidNext],
);
final currentUidNext = status.uidNext;
final stored = await (db.select(db.syncStates)
..where(
(t) =>
t.accountId.equals(account.id) &
t.resourceType.equals(_kResourceType),
))
.getSingleOrNull();
final lastUidNext = _parseUidNext(stored?.state);
await db.into(db.syncStates).insertOnConflictUpdate(
SyncStatesCompanion.insert(
accountId: account.id,
resourceType: _kResourceType,
state: jsonEncode({'uidNext': currentUidNext}),
syncedAt: DateTime.now(),
),
);
if (lastUidNext != null &&
currentUidNext != null &&
currentUidNext > lastUidNext) {
await showNewMailNotification(account.email);
}
} finally {
await client.logout();
}
} catch (_) {}
}
int? _parseUidNext(String? state) {
if (state == null) return null;
try {
final decoded = jsonDecode(state);
if (decoded is Map<String, Object?>) {
return decoded['uidNext'] as int?;
}
return null;
} catch (_) {
return null;
}
}
+2
View File
@@ -12,6 +12,7 @@ import 'package:sharedinbox/core/repositories/undo_repository.dart';
import 'package:sharedinbox/core/services/account_discovery_service.dart';
import 'package:sharedinbox/core/services/connection_test_service.dart';
import 'package:sharedinbox/core/services/managesieve_probe_service.dart';
import 'package:sharedinbox/core/services/notification_service.dart';
import 'package:sharedinbox/core/services/undo_service.dart';
import 'package:sharedinbox/core/storage/secure_storage.dart';
import 'package:sharedinbox/core/sync/account_sync_manager.dart';
@@ -122,6 +123,7 @@ final syncManagerProvider = Provider<AccountSyncManager>((ref) {
syncLog: ref.watch(syncLogRepositoryProvider),
imapConnect: ref.watch(imapConnectProvider),
drafts: ref.watch(draftRepositoryProvider),
onNewMail: showNewMailNotification,
);
ref.onDispose(manager.dispose);
return manager;
+7
View File
@@ -1,8 +1,11 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:sharedinbox/core/services/notification_service.dart';
import 'package:sharedinbox/core/sync/background_sync.dart';
import 'package:sharedinbox/data/db/database.dart';
import 'package:sharedinbox/di.dart';
import 'package:sharedinbox/ui/router.dart';
@@ -32,6 +35,10 @@ void main({List<Override> overrides = const []}) async {
};
await initDatabasePath();
if (Platform.isAndroid) {
await initNotifications();
await registerBackgroundSync();
}
runApp(
ProviderScope(overrides: overrides, child: const SharedInboxApp()),
);
+4
View File
@@ -45,6 +45,10 @@ dependencies:
url_launcher: ^6.3.2
flutter_markdown: ^0.7.7+1
# Background sync and local notifications
flutter_local_notifications: ^18.0.1
workmanager: ^0.5.2
dev_dependencies:
flutter_test:
sdk: flutter
+1
View File
@@ -52,6 +52,7 @@ const _excluded = {
'lib/ui/widgets/try_connection_button.dart',
'lib/ui/widgets/undo_shell.dart',
'lib/core/sync/account_sync_manager.dart',
'lib/core/sync/background_sync.dart',
'lib/core/sync/reliability_runner.dart',
'lib/data/jmap/jmap_client.dart',
'lib/data/jmap/sieve_repository.dart',