Skip to content

Commit

Permalink
#253 追加対応とか
Browse files Browse the repository at this point in the history
  • Loading branch information
shiosyakeyakini-info committed Dec 16, 2023
1 parent 74c0d3f commit 518064a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 54 deletions.
6 changes: 6 additions & 0 deletions lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ final accountRepositoryProvider =
final accountsProvider =
Provider<List<Account>>((ref) => ref.watch(accountRepositoryProvider));

final iProvider = Provider.family<IResponse, Acct>((ref, acct) {
final accounts = ref.watch(accountsProvider);
final account = accounts.firstWhere((account) => account.acct == acct);
return account.i;
});

final accountProvider = Provider.family<Account, Acct>(
(ref, acct) => ref.watch(
accountsProvider.select(
Expand Down
94 changes: 59 additions & 35 deletions lib/repository/account_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,67 +53,73 @@ class AccountRepository extends Notifier<List<Account>> {
}
}

Future<void> loadFromSourceIfNeed(Acct acct) async {
final index = state.indexWhere((e) => e.acct == acct);
Future<void> updateI(Account account) async {
final setting =
ref.read(accountSettingsRepositoryProvider).fromAccount(account);
_validatedAccts.add(account.acct);

final i = await ref.read(misskeyProvider(account)).i.i();
ref
.read(accountSettingsRepositoryProvider)
.save(setting.copyWith(latestICached: DateTime.now()));

final accounts = List.of(state);
final index = state.indexWhere((e) => e.acct == account.acct);
if (index < 0) return;
final account = state[index];
accounts[index] = account.copyWith(i: i);
state = accounts;

ref.read(notesProvider(account)).updateMute(i.mutedWords, i.hardMutedWords);
}

Future<void> updateMeta(Account account) async {
final setting =
ref.read(accountSettingsRepositoryProvider).fromAccount(account);
_validateMetaAccts.add(account.acct);

Future<void> updateI() async {
_validatedAccts.add(acct);
final meta = await ref.read(misskeyProvider(account)).meta();
ref
.read(accountSettingsRepositoryProvider)
.save(setting.copyWith(latestMetaCached: DateTime.now()));

final accounts = List.of(state);
final index = state.indexWhere((e) => e.acct == account.acct);
if (index < 0) return;

final i = await ref.read(misskeyProvider(account)).i.i();
ref
.read(accountSettingsRepositoryProvider)
.save(setting.copyWith(latestICached: DateTime.now()));
accounts[index] = account.copyWith(meta: meta);
state = accounts;
}

final accounts = List.of(state);
accounts[index] = account.copyWith(i: i);
state = accounts;
}
Future<void> loadFromSourceIfNeed(Acct acct) async {
final setting = ref.read(accountSettingsRepositoryProvider).fromAcct(acct);

final account = state.firstWhere((element) => element.acct == acct);

switch (setting.iCacheStrategy) {
case CacheStrategy.whenLaunch:
if (!_validatedAccts.contains(acct)) updateI();
if (!_validatedAccts.contains(acct)) updateI(account);
break;
case CacheStrategy.whenOneDay:
final latestUpdated = setting.latestICached;
if (latestUpdated == null || latestUpdated.day != DateTime.now().day) {
updateI();
updateI(account);
}
case CacheStrategy.whenTabChange:
updateI();
updateI(account);
break;
}
ref
.read(notesProvider(account))
.updateMute(account.i.mutedWords, account.i.hardMutedWords);

Future<void> updateMeta() async {
_validateMetaAccts.add(acct);

final meta = await ref.read(misskeyProvider(account)).meta();
ref
.read(accountSettingsRepositoryProvider)
.save(setting.copyWith(latestMetaCached: DateTime.now()));

final accounts = List.of(state);
accounts[index] = account.copyWith(meta: meta);
state = accounts;
}

switch (setting.metaChacheStrategy) {
case CacheStrategy.whenLaunch:
if (!_validatedAccts.contains(acct)) await updateMeta();
if (!_validatedAccts.contains(acct)) await updateMeta(account);
break;
case CacheStrategy.whenOneDay:
final latestUpdated = setting.latestMetaCached;
if (latestUpdated == null || latestUpdated.day != DateTime.now().day) {
await updateMeta();
await updateMeta(account);
}
case CacheStrategy.whenTabChange:
await updateMeta();
await updateMeta(account);
break;
}

Expand Down Expand Up @@ -148,6 +154,24 @@ class AccountRepository extends Notifier<List<Account>> {
state = accounts;
}

Future<void> addUnreadNotification(Account account) async {
final index = state.indexOf(account);
final i = state[index].i.copyWith(hasUnreadNotification: true);

final accounts = List.of(state);
accounts[index] = account.copyWith(i: i);
state = accounts;
}

Future<void> readAllNotification(Account account) async {
final index = state.indexOf(account);
final i = state[index].i.copyWith(hasUnreadNotification: false);

final accounts = List.of(state);
accounts[index] = account.copyWith(i: i);
state = accounts;
}

Future<void> remove(Account account) async {
state = state.where((e) => e != account).toList();
_validatedAccts.remove(account.acct);
Expand Down
12 changes: 4 additions & 8 deletions lib/repository/main_stream_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@ class MainStreamRepository extends ChangeNotifier {
);

Future<void> confirmNotification() async {
final i = await misskey.i.i();
hasUnreadNotification = i.hasUnreadNotification;
await accountRepository.updateI(account);

notifyListeners();
}

Future<void> connect() async {
socketController = misskey.mainStream(
onReadAllNotifications: () {
hasUnreadNotification = false;

notifyListeners();
accountRepository.readAllNotification(account);
},
onUnreadNotification: (_) {
hasUnreadNotification = true;
notifyListeners();
accountRepository.addUnreadNotification(account);
},
onReadAllAnnouncements: () {
accountRepository.removeUnreadAnnouncement(account);
Expand All @@ -58,7 +54,7 @@ class MainStreamRepository extends ChangeNotifier {

Future<void> reconnect() async {
if (isReconnecting) {
// 排他制御
// 排他制御11
while (isReconnecting) {
await Future.delayed(const Duration(milliseconds: 100));
}
Expand Down
9 changes: 3 additions & 6 deletions lib/view/common/main_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ class MainStreamState extends ConsumerState<MainStream> {
}

var isConnected = false;
var latestAccountCount = 0;

@override
Widget build(BuildContext context) {
final accounts = ref.watch(accountsProvider);

if (isConnected) {
for (final account in accounts) {
ref.read(mainStreamRepositoryProvider(account)).reconnect();
}
} else {
if (accounts.length != latestAccountCount) {
for (final account in accounts) {
ref.read(mainStreamRepositoryProvider(account)).connect();
}
latestAccountCount = accounts.length;
}

return widget.child;
Expand Down
5 changes: 2 additions & 3 deletions lib/view/common/notification_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ class NotificationIcon extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final hasUnread = ref.watch(
mainStreamRepositoryProvider(AccountScope.of(context))
.select((repository) => repository.hasUnreadNotification));
final hasUnread = ref.watch(iProvider(AccountScope.of(context).acct)
.select((value) => value.hasUnreadNotification));

if (hasUnread) {
return IconButton(
Expand Down
4 changes: 2 additions & 2 deletions lib/view/time_line_page/time_line_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ class AnnoucementInfo extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final hasUnread = ref.watch(
accountProvider(tabSetting.acct)
.select((account) => account.i.unreadAnnouncements.isNotEmpty),
iProvider(tabSetting.acct)
.select((i) => i.unreadAnnouncements.isNotEmpty),
);

if (hasUnread) {
Expand Down

0 comments on commit 518064a

Please sign in to comment.