Skip to content
Open
2 changes: 2 additions & 0 deletions apps/client/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:intl/date_symbol_data_local.dart';
import 'core/app_router.dart';
import 'core/app_theme.dart';
import 'core/i18n/language_controller.dart';
import 'services/badge_service.dart';
import 'services/push_service.dart';

Future<void> main() async {
Expand Down Expand Up @@ -35,6 +36,7 @@ class DailyPostBoxApp extends ConsumerWidget {
final router = ref.watch(appRouterProvider);
final language = ref.watch(languageControllerProvider);
final strings = ref.watch(appStringsProvider);
ref.watch(badgeSyncProvider);

return MaterialApp.router(
title: strings.appTitle,
Expand Down
44 changes: 44 additions & 0 deletions apps/client/lib/services/badge_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../features/accounts/account_providers.dart';

final badgeServiceProvider = Provider<BadgeService>((ref) => BadgeService());

/// Unread count over the mailbox lists loaded so far.
final unreadCountProvider = Provider<int>((ref) {
final accounts = ref.watch(accountsProvider).valueOrNull ?? const [];
var count = 0;
for (final account in accounts) {
final messages = ref.watch(accountMessagesProvider(account.id)).valueOrNull;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh mailbox data fetched before synchronization finishes

If an account is synchronizing when the app starts, this watch eagerly fetches and caches its partial or empty message list while the user is still on account management. After synchronization completes, refreshing account management invalidates only accountsProvider, not accountMessagesProvider. Opening that mailbox for the first time then reuses the prematurely cached list, and _MailboxContentState starts neither polling nor a completion refresh because the account is already idle. Previously, the first mailbox visit fetched the current list. Refresh these eagerly loaded lists when synchronization completes, or avoid populating the mailbox provider until it can be kept current.

Useful? React with 👍 / 👎.

count += messages?.where((message) => message.unread).length ?? 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Supply real unread state before enabling the Dock count

For accounts containing already-read mail, this displays the total loaded message count rather than the unread count: MailSummary.fromJson in apps/client/lib/services/mail_api_client.dart hardcodes unread: true, and the account-messages API does not supply synchronized read state. Consequently, even a mailbox with no unread messages receives a nonzero Dock badge. Merge the read-state dependency and deserialize its value before enabling this badge; the new test bypasses this problem by constructing MailSummary objects directly.

Useful? React with 👍 / 👎.

}
return count;
});

/// Pushes every change of the unread count to the Dock badge.
final badgeSyncProvider = Provider<void>((ref) {
ref.listen<int>(
unreadCountProvider,
(_, count) => ref.read(badgeServiceProvider).setUnreadCount(count),
fireImmediately: true,
);
});

class BadgeService {
static const _channel = MethodChannel('dailypostbox/badge');

/// Shows [count] on the macOS Dock icon; 0 clears it.
Future<void> setUnreadCount(int count) async {
if (!Platform.isMacOS) return;
try {
await _channel.invokeMethod<void>('setBadge', count);
} on PlatformException {
// The badge is cosmetic; ignore native failures.
} on MissingPluginException {
// No native side (tests).
}
}
}
13 changes: 13 additions & 0 deletions apps/client/macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
self?.requestDeviceToken(result: result)
}
}
if let controller = mainFlutterWindow?.contentViewController as? FlutterViewController {
FlutterMethodChannel(
name: "dailypostbox/badge",
binaryMessenger: controller.engine.binaryMessenger
).setMethodCallHandler { call, result in
guard call.method == "setBadge", let count = call.arguments as? Int else {
result(FlutterMethodNotImplemented)
return
}
NSApplication.shared.dockTile.badgeLabel = count > 0 ? String(count) : nil
result(nil)
}
}
super.applicationDidFinishLaunching(notification)
}

Expand Down
43 changes: 43 additions & 0 deletions apps/client/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:daily_postbox/features/message/message_page.dart';
import 'package:daily_postbox/core/i18n/language_controller.dart';
import 'package:daily_postbox/features/message/reply_draft.dart';
import 'package:daily_postbox/main.dart';
import 'package:daily_postbox/services/badge_service.dart';
import 'package:daily_postbox/services/mail_api_client.dart';

void main() {
Expand Down Expand Up @@ -730,6 +731,48 @@ void main() {
expect(openedUri, Uri.parse(url));
});

test('unread count sums unread messages across accounts', () async {
MailAccountSummary account(String id) => MailAccountSummary(
id: id,
providerId: 'gmail',
emailAddress: '$id@example.com',
incomingProtocol: 'imap',
authMode: 'oauth2',
syncStatus: 'idle',
syncError: null,
initialSync: InitialSyncOption.recommended,
lastSyncedAtLabel: 'now',
);
MailSummary mail(String id, bool unread) => MailSummary(
id: id,
sender: 's',
subject: 's',
snippet: '',
receivedAtLabel: '',
unread: unread,
);
final container = ProviderContainer(
overrides: [
accountsProvider.overrideWith(
(ref) async => [account('a'), account('b')],
),
accountMessagesProvider(
'a',
).overrideWith((ref) async => [mail('1', true), mail('2', false)]),
accountMessagesProvider(
'b',
).overrideWith((ref) async => [mail('3', true)]),
],
);
addTearDown(container.dispose);
container.listen(unreadCountProvider, (_, __) {});
await container.read(accountsProvider.future);
await container.read(accountMessagesProvider('a').future);
await container.read(accountMessagesProvider('b').future);

expect(container.read(unreadCountProvider), 2);
});

test('merges word boxes into one continuous underline per line', () {
final merged = mergeLinkUnderlineBoxes(const [
TextBox.fromLTRBD(0, 0, 42, 20, TextDirection.ltr),
Expand Down
Loading