|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart' show Navigator, Text, showAdaptiveDialog; |
| 4 | +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; |
| 5 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 6 | +import 'package:lichess_mobile/src/binding.dart' show LichessBinding; |
| 7 | +import 'package:lichess_mobile/src/model/account/account_repository.dart'; |
| 8 | +import 'package:lichess_mobile/src/model/auth/auth_session.dart'; |
| 9 | +import 'package:lichess_mobile/src/model/common/id.dart'; |
| 10 | +import 'package:lichess_mobile/src/model/notifications/notification_service.dart'; |
| 11 | +import 'package:lichess_mobile/src/model/notifications/notifications.dart' |
| 12 | + show LocalNotification, PlaybanNotification; |
| 13 | +import 'package:lichess_mobile/src/model/user/user.dart' show TemporaryBan, User; |
| 14 | +import 'package:lichess_mobile/src/navigation.dart' show currentNavigatorKeyProvider; |
| 15 | +import 'package:lichess_mobile/src/network/http.dart'; |
| 16 | +import 'package:lichess_mobile/src/view/play/playban.dart'; |
| 17 | +import 'package:lichess_mobile/src/widgets/platform_alert_dialog.dart'; |
| 18 | +import 'package:riverpod_annotation/riverpod_annotation.dart'; |
| 19 | + |
| 20 | +part 'account_service.g.dart'; |
| 21 | + |
| 22 | +@Riverpod(keepAlive: true) |
| 23 | +AccountService accountService(Ref ref) { |
| 24 | + final service = AccountService(ref); |
| 25 | + ref.onDispose(() { |
| 26 | + service.dispose(); |
| 27 | + }); |
| 28 | + return service; |
| 29 | +} |
| 30 | + |
| 31 | +class AccountService { |
| 32 | + AccountService(this._ref); |
| 33 | + |
| 34 | + ProviderSubscription<AsyncValue<User?>>? _accountProviderSubscription; |
| 35 | + StreamSubscription<(NotificationResponse, LocalNotification)>? _notificationResponseSubscription; |
| 36 | + |
| 37 | + final Ref _ref; |
| 38 | + |
| 39 | + static const _storageKey = 'account.playban_notification_date'; |
| 40 | + |
| 41 | + void start() { |
| 42 | + final prefs = LichessBinding.instance.sharedPreferences; |
| 43 | + |
| 44 | + _accountProviderSubscription = _ref.listen(accountProvider, (_, account) { |
| 45 | + final playban = account.valueOrNull?.playban; |
| 46 | + final storedDate = prefs.getString(_storageKey); |
| 47 | + final lastPlaybanNotificationDate = storedDate != null ? DateTime.parse(storedDate) : null; |
| 48 | + |
| 49 | + if (playban != null && lastPlaybanNotificationDate != playban.date) { |
| 50 | + _savePlaybanNotificationDate(playban.date); |
| 51 | + _ref.read(notificationServiceProvider).show(PlaybanNotification(playban)); |
| 52 | + } else if (playban == null && lastPlaybanNotificationDate != null) { |
| 53 | + _ref |
| 54 | + .read(notificationServiceProvider) |
| 55 | + .cancel(lastPlaybanNotificationDate.toIso8601String().hashCode); |
| 56 | + _clearPlaybanNotificationDate(); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + _notificationResponseSubscription = NotificationService.responseStream.listen((data) { |
| 61 | + final (_, notification) = data; |
| 62 | + switch (notification) { |
| 63 | + case PlaybanNotification(:final playban): |
| 64 | + _onPlaybanNotificationResponse(playban); |
| 65 | + case _: |
| 66 | + break; |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + void _savePlaybanNotificationDate(DateTime date) { |
| 72 | + LichessBinding.instance.sharedPreferences.setString(_storageKey, date.toIso8601String()); |
| 73 | + } |
| 74 | + |
| 75 | + void _clearPlaybanNotificationDate() { |
| 76 | + LichessBinding.instance.sharedPreferences.remove(_storageKey); |
| 77 | + } |
| 78 | + |
| 79 | + void dispose() { |
| 80 | + _accountProviderSubscription?.close(); |
| 81 | + _notificationResponseSubscription?.cancel(); |
| 82 | + } |
| 83 | + |
| 84 | + Future<void> _onPlaybanNotificationResponse(TemporaryBan playban) async { |
| 85 | + final context = _ref.read(currentNavigatorKeyProvider).currentContext; |
| 86 | + if (context == null || !context.mounted) return; |
| 87 | + |
| 88 | + return showAdaptiveDialog( |
| 89 | + context: context, |
| 90 | + barrierDismissible: true, |
| 91 | + builder: (context) { |
| 92 | + return PlatformAlertDialog( |
| 93 | + content: PlaybanMessage(playban: playban, centerText: true), |
| 94 | + actions: [ |
| 95 | + PlatformDialogAction( |
| 96 | + child: const Text('OK'), |
| 97 | + onPressed: () { |
| 98 | + Navigator.of(context).pop(); |
| 99 | + }, |
| 100 | + ), |
| 101 | + ], |
| 102 | + ); |
| 103 | + }, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + Future<void> setGameBookmark(GameId id, {required bool bookmark}) async { |
| 108 | + final session = _ref.read(authSessionProvider); |
| 109 | + if (session == null) return; |
| 110 | + |
| 111 | + await _ref.withClient((client) => AccountRepository(client).bookmark(id, bookmark: bookmark)); |
| 112 | + |
| 113 | + _ref.invalidate(accountProvider); |
| 114 | + } |
| 115 | +} |
0 commit comments