ada
This commit is contained in:
parent
2ff3bd3306
commit
546f1287f8
|
|
@ -105,17 +105,39 @@ class EvaporasiBloc extends Bloc<EvaporasiEvent, EvaporasiState> {
|
||||||
|
|
||||||
await _subscription?.cancel();
|
await _subscription?.cancel();
|
||||||
_subscription = _repository
|
_subscription = _repository
|
||||||
.getSensorStream('Monitoring', (json) => Evaporasi.fromJson(json))
|
.getSensorStream('Monitoring/History', _latestHistoryEntry)
|
||||||
.listen((data) => add(_EvaporasiRealtimeUpdated(data)));
|
.listen((data) => add(_EvaporasiRealtimeUpdated(data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Evaporasi _latestHistoryEntry(Map<dynamic, dynamic> json) {
|
||||||
|
if (json.isEmpty) return Evaporasi.empty;
|
||||||
|
|
||||||
|
final entries = json.values
|
||||||
|
.whereType<Map<dynamic, dynamic>>()
|
||||||
|
.map((item) => Evaporasi.fromJson(item))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
if (entries.isEmpty) return Evaporasi.empty;
|
||||||
|
|
||||||
|
entries.sort((a, b) => a.timestamp.compareTo(b.timestamp));
|
||||||
|
return entries.last;
|
||||||
|
}
|
||||||
|
|
||||||
void _onRealtimeUpdated(
|
void _onRealtimeUpdated(
|
||||||
_EvaporasiRealtimeUpdated event,
|
_EvaporasiRealtimeUpdated event,
|
||||||
Emitter<EvaporasiState> emit,
|
Emitter<EvaporasiState> emit,
|
||||||
) {
|
) {
|
||||||
// Hindari update dobel: jika timestamp event sama dengan yang terakhir, jangan ubah bucket.
|
final updatedHistory = List<Evaporasi>.from(state.history);
|
||||||
final previous =
|
final duplicateIndex = updatedHistory.indexWhere(
|
||||||
state.history.isNotEmpty ? state.history.last.timestamp : null;
|
(item) => item.timestamp.toUtc() == event.data.timestamp.toUtc(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (duplicateIndex >= 0) {
|
||||||
|
updatedHistory[duplicateIndex] = event.data;
|
||||||
|
} else {
|
||||||
|
updatedHistory.add(event.data);
|
||||||
|
}
|
||||||
|
updatedHistory.sort((a, b) => a.timestamp.compareTo(b.timestamp));
|
||||||
|
|
||||||
// Update bucket berdasarkan timestamp event (bukan jam lokal sekarang).
|
// Update bucket berdasarkan timestamp event (bukan jam lokal sekarang).
|
||||||
final updated = List<double>.from(state.dailyValues);
|
final updated = List<double>.from(state.dailyValues);
|
||||||
|
|
@ -140,6 +162,8 @@ class EvaporasiBloc extends Bloc<EvaporasiEvent, EvaporasiState> {
|
||||||
_emitEvaporasiAlert(status, rain, event.data.evaporasi);
|
_emitEvaporasiAlert(status, rain, event.data.evaporasi);
|
||||||
|
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
|
history: updatedHistory,
|
||||||
|
listData: updatedHistory,
|
||||||
currentValue: event.data.evaporasi,
|
currentValue: event.data.evaporasi,
|
||||||
temperature: event.data.suhu,
|
temperature: event.data.suhu,
|
||||||
waterLevel: event.data.tinggiAir,
|
waterLevel: event.data.tinggiAir,
|
||||||
|
|
|
||||||
|
|
@ -290,20 +290,15 @@ class _EvaporasiScreenState extends State<EvaporasiScreen> {
|
||||||
/// 🧾 LIST DATA EVAPORASI
|
/// 🧾 LIST DATA EVAPORASI
|
||||||
/// =========================
|
/// =========================
|
||||||
Widget _evaporasiList(EvaporasiState state) {
|
Widget _evaporasiList(EvaporasiState state) {
|
||||||
final allData = [...state.history];
|
|
||||||
if (state.currentData != null) {
|
|
||||||
allData.add(state.currentData!);
|
|
||||||
}
|
|
||||||
|
|
||||||
final data = (state.viewMode == EvaporasiViewMode.customDate &&
|
final data = (state.viewMode == EvaporasiViewMode.customDate &&
|
||||||
state.selectedDate != null
|
state.selectedDate != null
|
||||||
? allData
|
? state.history
|
||||||
.where((e) =>
|
.where((e) =>
|
||||||
e.timestamp.year == state.selectedDate!.year &&
|
e.timestamp.year == state.selectedDate!.year &&
|
||||||
e.timestamp.month == state.selectedDate!.month &&
|
e.timestamp.month == state.selectedDate!.month &&
|
||||||
e.timestamp.day == state.selectedDate!.day)
|
e.timestamp.day == state.selectedDate!.day)
|
||||||
.toList()
|
.toList()
|
||||||
: List.of(allData))
|
: List.of(state.history))
|
||||||
..sort((a, b) => b.timestamp.compareTo(a.timestamp));
|
..sort((a, b) => b.timestamp.compareTo(a.timestamp));
|
||||||
|
|
||||||
if (data.isEmpty) {
|
if (data.isEmpty) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue