|
1 | 1 | import 'dart:async';
|
2 | 2 |
|
3 | 3 | import 'package:flutter/foundation.dart';
|
| 4 | +import 'package:flutter/widgets.dart'; |
4 | 5 |
|
5 | 6 | import '../api/model/events.dart';
|
| 7 | +import '../api/route/messages.dart'; |
| 8 | +import '../api/route/typing.dart'; |
6 | 9 | import '../log.dart';
|
7 | 10 | import 'narrow.dart';
|
| 11 | +import 'store.dart'; |
8 | 12 |
|
9 | 13 | /// The model for tracking the typing status organized by narrows.
|
10 | 14 | ///
|
@@ -85,3 +89,73 @@ class TypingStatus extends ChangeNotifier {
|
85 | 89 | }
|
86 | 90 | }
|
87 | 91 | }
|
| 92 | + |
| 93 | +class TypingNotifier { |
| 94 | + TypingNotifier({ |
| 95 | + required this.typingStoppedWaitPeriod, |
| 96 | + required this.typingStartedWaitPeriod, |
| 97 | + }); |
| 98 | + |
| 99 | + final Duration typingStoppedWaitPeriod; |
| 100 | + final Duration typingStartedWaitPeriod; |
| 101 | + |
| 102 | + MessageDestination? _currentDestination; |
| 103 | + DateTime? _nextStartTime; |
| 104 | + Timer? _stopTimer; |
| 105 | + |
| 106 | + void dispose() { |
| 107 | + _stopTimer?.cancel(); |
| 108 | + } |
| 109 | + |
| 110 | + void handleTypingStop(PerAccountStore store, MessageDestination destination) { |
| 111 | + if (destination != _currentDestination) return; |
| 112 | + |
| 113 | + _notifyStop(store); |
| 114 | + } |
| 115 | + |
| 116 | + void handleTyping(PerAccountStore store, MessageDestination destination) { |
| 117 | + // Two cases. |
| 118 | + // Renew a previous typing status, |
| 119 | + // or start a new typing status |
| 120 | + if (destination == _currentDestination) { |
| 121 | + _maybeNotifyStart(store); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + _nextStartTime = null; |
| 126 | + // Start new typing status |
| 127 | + _currentDestination = destination; |
| 128 | + _notifyStart(store); |
| 129 | + } |
| 130 | + |
| 131 | + Future<void> _maybeNotifyStart(PerAccountStore store) async { |
| 132 | + if (_nextStartTime == null || DateTime.now().isAfter(_nextStartTime!)) { |
| 133 | + await _notifyStart(store); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + Future<void> _notifyStart(PerAccountStore store) async { |
| 138 | + _nextStartTime = DateTime.now().add(typingStartedWaitPeriod); |
| 139 | + _startOrExtendIdleTimer(store); |
| 140 | + assert(debugLog('[$_currentDestination] notify start')); |
| 141 | + await setTypingStatus(store.connection, |
| 142 | + op: TypingOp.start, |
| 143 | + destination: _currentDestination!, |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + Future<void> _notifyStop(PerAccountStore store) async { |
| 148 | + assert(debugLog('[$_currentDestination] notify stop')); |
| 149 | + _stopTimer?.cancel(); |
| 150 | + await setTypingStatus(store.connection, |
| 151 | + op: TypingOp.stop, |
| 152 | + destination: _currentDestination!, |
| 153 | + ); |
| 154 | + _currentDestination = null; |
| 155 | + } |
| 156 | + |
| 157 | + void _startOrExtendIdleTimer(PerAccountStore store) { |
| 158 | + _stopTimer?.cancel(); |
| 159 | + _stopTimer = Timer(typingStoppedWaitPeriod, () => _notifyStop(store)); |
| 160 | + } |
| 161 | +} |
0 commit comments