|
| 1 | +import 'package:flutter/widgets.dart'; |
| 2 | + |
| 3 | +import '../utils/logging/clix_logger.dart'; |
| 4 | +import 'event_service.dart'; |
| 5 | +import 'storage_service.dart'; |
| 6 | + |
| 7 | +enum SessionEvent { |
| 8 | + sessionStart('SESSION_START'); |
| 9 | + |
| 10 | + final String value; |
| 11 | + const SessionEvent(this.value); |
| 12 | +} |
| 13 | + |
| 14 | +class SessionService with WidgetsBindingObserver { |
| 15 | + static const String _lastActivityKey = 'clix_session_last_activity'; |
| 16 | + |
| 17 | + final StorageService _storageService; |
| 18 | + final EventService _eventService; |
| 19 | + final int _effectiveTimeoutMs; |
| 20 | + |
| 21 | + String? _pendingMessageId; |
| 22 | + |
| 23 | + SessionService({ |
| 24 | + required StorageService storageService, |
| 25 | + required EventService eventService, |
| 26 | + required int sessionTimeoutMs, |
| 27 | + }) : _storageService = storageService, |
| 28 | + _eventService = eventService, |
| 29 | + _effectiveTimeoutMs = sessionTimeoutMs < 5000 ? 5000 : sessionTimeoutMs; |
| 30 | + |
| 31 | + Future<void> start() async { |
| 32 | + WidgetsBinding.instance.addObserver(this); |
| 33 | + |
| 34 | + try { |
| 35 | + final lastActivity = await _storageService.get<int>(_lastActivityKey); |
| 36 | + if (lastActivity != null) { |
| 37 | + final elapsed = DateTime.now().millisecondsSinceEpoch - lastActivity; |
| 38 | + if (elapsed <= _effectiveTimeoutMs) { |
| 39 | + _pendingMessageId = null; |
| 40 | + await _updateLastActivity(); |
| 41 | + ClixLogger.debug('Continuing existing session'); |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + await _startNewSession(); |
| 46 | + } catch (e) { |
| 47 | + ClixLogger.error('Failed to start session', e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + void didChangeAppLifecycleState(AppLifecycleState state) { |
| 53 | + if (state == AppLifecycleState.resumed) { |
| 54 | + _onResumed(); |
| 55 | + } else if (state == AppLifecycleState.paused) { |
| 56 | + _onPaused(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + void setPendingMessageId(String? messageId) { |
| 61 | + _pendingMessageId = messageId; |
| 62 | + } |
| 63 | + |
| 64 | + Future<void> _onResumed() async { |
| 65 | + try { |
| 66 | + // Small delay to allow notification tap handlers to set pendingMessageId |
| 67 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 68 | + |
| 69 | + final lastActivity = await _storageService.get<int>(_lastActivityKey); |
| 70 | + if (lastActivity != null) { |
| 71 | + final elapsed = DateTime.now().millisecondsSinceEpoch - lastActivity; |
| 72 | + if (elapsed <= _effectiveTimeoutMs) { |
| 73 | + _pendingMessageId = null; |
| 74 | + await _updateLastActivity(); |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | + await _startNewSession(); |
| 79 | + } catch (e) { |
| 80 | + ClixLogger.error('Failed to handle app resumed', e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + Future<void> _onPaused() async { |
| 85 | + try { |
| 86 | + await _updateLastActivity(); |
| 87 | + } catch (e) { |
| 88 | + ClixLogger.error('Failed to handle app paused', e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Future<void> _startNewSession() async { |
| 93 | + final messageId = _pendingMessageId; |
| 94 | + _pendingMessageId = null; |
| 95 | + await _updateLastActivity(); |
| 96 | + |
| 97 | + try { |
| 98 | + await _eventService.trackEvent( |
| 99 | + SessionEvent.sessionStart.value, |
| 100 | + messageId: messageId, |
| 101 | + ); |
| 102 | + ClixLogger.debug('${SessionEvent.sessionStart.value} tracked'); |
| 103 | + } catch (e) { |
| 104 | + ClixLogger.error('Failed to track ${SessionEvent.sessionStart.value}', e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + void cleanup() { |
| 109 | + WidgetsBinding.instance.removeObserver(this); |
| 110 | + } |
| 111 | + |
| 112 | + Future<void> _updateLastActivity() async { |
| 113 | + await _storageService.set<int>( |
| 114 | + _lastActivityKey, DateTime.now().millisecondsSinceEpoch); |
| 115 | + } |
| 116 | +} |
0 commit comments