From 898c78435990239512b4ebf52eb0057c1c55bc43 Mon Sep 17 00:00:00 2001 From: kleponijo Date: Mon, 29 Jun 2026 13:42:06 +0700 Subject: [PATCH] oke --- .../wind_speed/blocs/wind_speed_bloc.dart | 62 ++++++++++++------- .../wind_speed/blocs/wind_speed_event.dart | 7 +++ .../lib/src/firebase_monitoring_repo.dart | 17 +++++ .../lib/src/monitoring_repo.dart | 6 ++ 4 files changed, 69 insertions(+), 23 deletions(-) diff --git a/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart b/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart index 0c0e1c0..3f83bd0 100644 --- a/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart +++ b/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart @@ -19,6 +19,7 @@ class WindSpeedBloc extends Bloc { final MonitoringRepository _repository; final NotificationBloc _notificationBloc; StreamSubscription? _subscription; + StreamSubscription>? _historySubscription; WindSpeedBloc({ required MonitoringRepository repository, @@ -35,6 +36,7 @@ class WindSpeedBloc extends Bloc { on(_onDeleteByDate); on(_onDeleteByDateRange); on(_onDeleteByHourRange); + on<_WindSpeedHistoryUpdated>(_onHistoryUpdated); } Future _getDeviceId() async { @@ -49,42 +51,55 @@ class WindSpeedBloc extends Bloc { WatchWindSpeedStarted event, Emitter emit, ) async { - // Cancel stream lama dulu sebelum apapun await _subscription?.cancel(); + await _historySubscription?.cancel(); _subscription = null; + _historySubscription = null; emit(state.copyWith(isLoading: true, currentSpeed: 0)); final deviceId = await _getDeviceId(); - final historyMap = await _repository.getSensorHistoryWithKeys( - 'anemometer/$deviceId/history', - (json) => MyWindSpeed.fromJson(json), - ); + // ── History stream (realtime, otomatis update) ── + _historySubscription = _repository + .getSensorHistoryStream( + 'anemometer/$deviceId/history', + (json) => MyWindSpeed.fromJson(json), + ) + .listen((newMap) => add(_WindSpeedHistoryUpdated(newMap))); - final history = _sortedList(historyMap); - final graphs = _buildGraphs(history); - - if (history.isNotEmpty) _emitWindAlert(history.last.speed); - - emit(state.copyWith( - historyMap: historyMap, - history: history, - filteredHistory: history, - dailySpeeds: graphs['daily']!, - weeklySpeeds: graphs['weekly']!, - monthlySpeeds: graphs['monthly']!, - isLoading: false, - alertLevel: - history.isNotEmpty ? _getAlertLevel(history.last.speed) : 'Normal', - )); - - // Subscribe stream baru + // ── Realtime stream ── _subscription = _repository .getSensorStream( 'anemometer/$deviceId/realtime', (json) => MyWindSpeed.fromJson(json), ) .listen((data) => add(_WindSpeedRealtimeUpdated(data))); + + // ── Loading selesai, state awal kosong dulu ── + // _onHistoryUpdated akan otomatis mengisi data begitu stream pertama emit + emit(state.copyWith(isLoading: false)); + } + + void _onHistoryUpdated( + _WindSpeedHistoryUpdated event, + Emitter emit, + ) { + final history = _sortedList(event.historyMap); + final graphs = _buildGraphs(history); + final filtered = state.selectedDate != null + ? history + .where((e) => _isSameDate(e.timestamp, state.selectedDate!)) + .toList() + : history; + + emit(state.copyWith( + historyMap: event.historyMap, + history: history, + filteredHistory: filtered, + dailySpeeds: graphs['daily'], + weeklySpeeds: graphs['weekly'], + monthlySpeeds: graphs['monthly'], + )); } // ════════════════════════════════════════════════════════════ @@ -429,6 +444,7 @@ class WindSpeedBloc extends Bloc { @override Future close() async { await _subscription?.cancel(); + await _historySubscription?.cancel(); return super.close(); } } diff --git a/lib/screens/monitoring/wind_speed/blocs/wind_speed_event.dart b/lib/screens/monitoring/wind_speed/blocs/wind_speed_event.dart index e641a5e..4a6f3f8 100644 --- a/lib/screens/monitoring/wind_speed/blocs/wind_speed_event.dart +++ b/lib/screens/monitoring/wind_speed/blocs/wind_speed_event.dart @@ -76,3 +76,10 @@ class WindSpeedDeleteByHourRangeRequested extends WindSpeedEvent { @override List get props => [date, startHour, endHour]; } + +class _WindSpeedHistoryUpdated extends WindSpeedEvent { + final Map historyMap; + const _WindSpeedHistoryUpdated(this.historyMap); + @override + List get props => [historyMap]; +} diff --git a/packages/monitoring_repository/lib/src/firebase_monitoring_repo.dart b/packages/monitoring_repository/lib/src/firebase_monitoring_repo.dart index 0ae24e7..320b883 100644 --- a/packages/monitoring_repository/lib/src/firebase_monitoring_repo.dart +++ b/packages/monitoring_repository/lib/src/firebase_monitoring_repo.dart @@ -200,4 +200,21 @@ class FirebaseMonitoringRepo implements MonitoringRepository { Future sendRemoteRestart(String deviceId) async { await _db.ref('anemometer/$deviceId/command/restart').set(true); } + + @override + Stream> getSensorHistoryStream( + String path, + T Function(Map json) mapper, + ) { + return _db.ref(path).onValue.map((event) { + if (!event.snapshot.exists || event.snapshot.value is! Map) return {}; + final data = event.snapshot.value as Map; + return { + for (final entry in data.entries) + entry.key.toString(): mapper( + entry.value is Map ? entry.value as Map : {}, + ), + }; + }); + } } diff --git a/packages/monitoring_repository/lib/src/monitoring_repo.dart b/packages/monitoring_repository/lib/src/monitoring_repo.dart index f99685c..c432552 100644 --- a/packages/monitoring_repository/lib/src/monitoring_repo.dart +++ b/packages/monitoring_repository/lib/src/monitoring_repo.dart @@ -41,4 +41,10 @@ abstract class MonitoringRepository { ); Future deleteHistoryByKeys(String path, List keys); Future deleteAllHistory(String path); + + // Tambah di abstract class MonitoringRepository: + Stream> getSensorHistoryStream( + String path, + T Function(Map json) mapper, + ); }