Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09bc092b54 | ||
|
|
aa91d7ce6b | ||
|
|
c3305259a9 | ||
|
|
3f1200aa59 | ||
|
|
81e2eb908d |
@@ -35,7 +35,7 @@ android {
|
|||||||
applicationId = "de.sharedinbox.mua"
|
applicationId = "de.sharedinbox.mua"
|
||||||
// You can update the following values to match your application needs.
|
// You can update the following values to match your application needs.
|
||||||
// For more information, see: https://flutter.dev/to/review-gradle-config.
|
// For more information, see: https://flutter.dev/to/review-gradle-config.
|
||||||
minSdk = flutter.minSdkVersion
|
minSdk = 23
|
||||||
targetSdk = flutter.targetSdkVersion
|
targetSdk = flutter.targetSdkVersion
|
||||||
versionCode = flutter.versionCode
|
versionCode = flutter.versionCode
|
||||||
versionName = flutter.versionName
|
versionName = flutter.versionName
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<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
|
<application
|
||||||
android:label="sharedinbox"
|
android:label="sharedinbox"
|
||||||
android:name="${applicationName}"
|
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,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -12,6 +12,8 @@ import 'package:sharedinbox/core/utils/logger.dart';
|
|||||||
import 'package:sharedinbox/data/imap/imap_client_factory.dart'
|
import 'package:sharedinbox/data/imap/imap_client_factory.dart'
|
||||||
show ImapConnectFn, connectImap, verboseLogKey;
|
show ImapConnectFn, connectImap, verboseLogKey;
|
||||||
|
|
||||||
|
typedef OnNewMailCallback = Future<void> Function(String accountEmail);
|
||||||
|
|
||||||
/// Manages background sync for all accounts.
|
/// Manages background sync for all accounts.
|
||||||
///
|
///
|
||||||
/// IMAP accounts get an IDLE-based sync loop (_AccountSync).
|
/// IMAP accounts get an IDLE-based sync loop (_AccountSync).
|
||||||
@@ -24,9 +26,11 @@ class AccountSyncManager {
|
|||||||
ImapConnectFn imapConnect = connectImap,
|
ImapConnectFn imapConnect = connectImap,
|
||||||
SyncLogRepository syncLog = const NoOpSyncLogRepository(),
|
SyncLogRepository syncLog = const NoOpSyncLogRepository(),
|
||||||
DraftRepository? drafts,
|
DraftRepository? drafts,
|
||||||
|
OnNewMailCallback? onNewMail,
|
||||||
}) : _imapConnect = imapConnect,
|
}) : _imapConnect = imapConnect,
|
||||||
_syncLog = syncLog,
|
_syncLog = syncLog,
|
||||||
_drafts = drafts;
|
_drafts = drafts,
|
||||||
|
_onNewMail = onNewMail;
|
||||||
|
|
||||||
final AccountRepository _accounts;
|
final AccountRepository _accounts;
|
||||||
final MailboxRepository _mailboxes;
|
final MailboxRepository _mailboxes;
|
||||||
@@ -34,6 +38,7 @@ class AccountSyncManager {
|
|||||||
final ImapConnectFn _imapConnect;
|
final ImapConnectFn _imapConnect;
|
||||||
final SyncLogRepository _syncLog;
|
final SyncLogRepository _syncLog;
|
||||||
final DraftRepository? _drafts;
|
final DraftRepository? _drafts;
|
||||||
|
final OnNewMailCallback? _onNewMail;
|
||||||
|
|
||||||
final Map<String, _SyncLoop> _active = {};
|
final Map<String, _SyncLoop> _active = {};
|
||||||
StreamSubscription<List<Account>>? _accountsSub;
|
StreamSubscription<List<Account>>? _accountsSub;
|
||||||
@@ -58,6 +63,7 @@ class AccountSyncManager {
|
|||||||
_imapConnect,
|
_imapConnect,
|
||||||
_syncLog,
|
_syncLog,
|
||||||
_drafts,
|
_drafts,
|
||||||
|
_onNewMail,
|
||||||
),
|
),
|
||||||
AccountType.jmap => _JmapAccountSync(
|
AccountType.jmap => _JmapAccountSync(
|
||||||
account,
|
account,
|
||||||
@@ -119,6 +125,7 @@ class AccountSyncManager {
|
|||||||
_imapConnect,
|
_imapConnect,
|
||||||
_syncLog,
|
_syncLog,
|
||||||
_drafts,
|
_drafts,
|
||||||
|
_onNewMail,
|
||||||
),
|
),
|
||||||
AccountType.jmap => _JmapAccountSync(
|
AccountType.jmap => _JmapAccountSync(
|
||||||
account,
|
account,
|
||||||
@@ -152,6 +159,7 @@ class _AccountSync implements _SyncLoop {
|
|||||||
this._imapConnect,
|
this._imapConnect,
|
||||||
this._syncLog,
|
this._syncLog,
|
||||||
this._drafts,
|
this._drafts,
|
||||||
|
this._onNewMail,
|
||||||
);
|
);
|
||||||
|
|
||||||
final Account account;
|
final Account account;
|
||||||
@@ -161,6 +169,7 @@ class _AccountSync implements _SyncLoop {
|
|||||||
final ImapConnectFn _imapConnect;
|
final ImapConnectFn _imapConnect;
|
||||||
final SyncLogRepository _syncLog;
|
final SyncLogRepository _syncLog;
|
||||||
final DraftRepository? _drafts;
|
final DraftRepository? _drafts;
|
||||||
|
final OnNewMailCallback? _onNewMail;
|
||||||
|
|
||||||
imap.ImapClient? _idleClient;
|
imap.ImapClient? _idleClient;
|
||||||
bool _running = false;
|
bool _running = false;
|
||||||
@@ -335,6 +344,7 @@ class _AccountSync implements _SyncLoop {
|
|||||||
await client.selectMailboxByPath('INBOX');
|
await client.selectMailboxByPath('INBOX');
|
||||||
|
|
||||||
final newMessageCompleter = Completer<void>();
|
final newMessageCompleter = Completer<void>();
|
||||||
|
var hasNewMail = false;
|
||||||
|
|
||||||
final sub = client.eventBus
|
final sub = client.eventBus
|
||||||
.on<imap.ImapEvent>()
|
.on<imap.ImapEvent>()
|
||||||
@@ -342,7 +352,11 @@ class _AccountSync implements _SyncLoop {
|
|||||||
(e) =>
|
(e) =>
|
||||||
e is imap.ImapMessagesExistEvent || e is imap.ImapExpungeEvent,
|
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();
|
if (!newMessageCompleter.isCompleted) newMessageCompleter.complete();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -358,6 +372,10 @@ class _AccountSync implements _SyncLoop {
|
|||||||
|
|
||||||
await client.idleDone();
|
await client.idleDone();
|
||||||
await sub.cancel();
|
await sub.cancel();
|
||||||
|
|
||||||
|
if (hasNewMail) {
|
||||||
|
unawaited(_onNewMail?.call(account.email));
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await client.logout();
|
await client.logout();
|
||||||
_idleClient = null;
|
_idleClient = null;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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/account_discovery_service.dart';
|
||||||
import 'package:sharedinbox/core/services/connection_test_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/managesieve_probe_service.dart';
|
||||||
|
import 'package:sharedinbox/core/services/notification_service.dart';
|
||||||
import 'package:sharedinbox/core/services/undo_service.dart';
|
import 'package:sharedinbox/core/services/undo_service.dart';
|
||||||
import 'package:sharedinbox/core/storage/secure_storage.dart';
|
import 'package:sharedinbox/core/storage/secure_storage.dart';
|
||||||
import 'package:sharedinbox/core/sync/account_sync_manager.dart';
|
import 'package:sharedinbox/core/sync/account_sync_manager.dart';
|
||||||
@@ -122,6 +123,7 @@ final syncManagerProvider = Provider<AccountSyncManager>((ref) {
|
|||||||
syncLog: ref.watch(syncLogRepositoryProvider),
|
syncLog: ref.watch(syncLogRepositoryProvider),
|
||||||
imapConnect: ref.watch(imapConnectProvider),
|
imapConnect: ref.watch(imapConnectProvider),
|
||||||
drafts: ref.watch(draftRepositoryProvider),
|
drafts: ref.watch(draftRepositoryProvider),
|
||||||
|
onNewMail: showNewMailNotification,
|
||||||
);
|
);
|
||||||
ref.onDispose(manager.dispose);
|
ref.onDispose(manager.dispose);
|
||||||
return manager;
|
return manager;
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.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/data/db/database.dart';
|
||||||
import 'package:sharedinbox/di.dart';
|
import 'package:sharedinbox/di.dart';
|
||||||
import 'package:sharedinbox/ui/router.dart';
|
import 'package:sharedinbox/ui/router.dart';
|
||||||
@@ -32,6 +35,10 @@ void main({List<Override> overrides = const []}) async {
|
|||||||
};
|
};
|
||||||
|
|
||||||
await initDatabasePath();
|
await initDatabasePath();
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
await initNotifications();
|
||||||
|
await registerBackgroundSync();
|
||||||
|
}
|
||||||
runApp(
|
runApp(
|
||||||
ProviderScope(overrides: overrides, child: const SharedInboxApp()),
|
ProviderScope(overrides: overrides, child: const SharedInboxApp()),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -45,6 +45,10 @@ dependencies:
|
|||||||
url_launcher: ^6.3.2
|
url_launcher: ^6.3.2
|
||||||
flutter_markdown: ^0.7.7+1
|
flutter_markdown: ^0.7.7+1
|
||||||
|
|
||||||
|
# Background sync and local notifications
|
||||||
|
flutter_local_notifications: ^18.0.1
|
||||||
|
workmanager: ^0.5.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ const _excluded = {
|
|||||||
'lib/ui/widgets/try_connection_button.dart',
|
'lib/ui/widgets/try_connection_button.dart',
|
||||||
'lib/ui/widgets/undo_shell.dart',
|
'lib/ui/widgets/undo_shell.dart',
|
||||||
'lib/core/sync/account_sync_manager.dart',
|
'lib/core/sync/account_sync_manager.dart',
|
||||||
|
'lib/core/sync/background_sync.dart',
|
||||||
'lib/core/sync/reliability_runner.dart',
|
'lib/core/sync/reliability_runner.dart',
|
||||||
'lib/data/jmap/jmap_client.dart',
|
'lib/data/jmap/jmap_client.dart',
|
||||||
'lib/data/jmap/sieve_repository.dart',
|
'lib/data/jmap/sieve_repository.dart',
|
||||||
|
|||||||
Reference in New Issue
Block a user