Repository navigation
macOS Dock 안 읽은 메일 배지 (#47 1단계) #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
08800b7
f74a5ab
06fef1b
14c4096
da27965
547668f
dc13ee9
7e18e05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| count += messages?.where((message) => message.unread).length ?? 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For accounts containing already-read mail, this displays the total loaded message count rather than the unread count: 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). | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, notaccountMessagesProvider. Opening that mailbox for the first time then reuses the prematurely cached list, and_MailboxContentStatestarts 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 👍 / 👎.