Skip to content

Commit

Permalink
hotfix: Timeline of Events fetching (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored Dec 3, 2024
2 parents 9277079 + 36f5e7e commit e04382c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
18 changes: 11 additions & 7 deletions lib/api/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ extension EventsExtension on API {
allowUntrustedCertificates: data['allowUntrustedCertificates'] as bool,
);

debugPrint(
writeLogToFile(
'Getting events for server ${server.name} with limit $limit '
'${startTime != null ? 'from $startTime ' : ''}'
'${endTime != null ? 'to $endTime ' : ''}'
'${startTime != null ? 'from $startTime ' : ''} '
'${endTime != null ? 'to $endTime ' : ''} '
'${deviceId != null ? 'for device $deviceId' : ''}',
print: true,
);

assert(server.serverUUID != null && server.hasCookies);
Expand Down Expand Up @@ -155,7 +156,9 @@ extension EventsExtension on API {
events = (jsonDecode(parser.toGData())['feed']['entry'] as Iterable)
.map<Event>((item) {
final e = item as Map;
if (!e.containsKey('content')) debugPrint(e.toString());
if (!e.containsKey('content')) {
debugPrint('Event does not have a content: $e');
}
return Event(
server: server,
id: int.parse(e['id']['raw']),
Expand Down Expand Up @@ -189,15 +192,16 @@ extension EventsExtension on API {
handleError(
error,
stack,
'Failed to getEvents on server ${server.name} $uri ${response.body}',
'Failed to _getEvents on server ${server.name} $uri ${response.body}',
);
}

debugPrint(
writeLogToFile(
'Loaded ${events.length} events for server ${server.name}'
'${deviceId != null ? ' for device $deviceId' : ''}',
print: true,
);

return events.where((e) => e.duration > const Duration(seconds: 5));
return events;
}
}
2 changes: 1 addition & 1 deletion lib/models/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Event {

@override
String toString() {
return 'Event(server: $server, id: $id, deviceID: $deviceID, title: $title, publishedRaw: $publishedRaw, published: $published, updatedRaw: $updatedRaw, updated: $updated, category: $category, mediaID: $mediaID, mediaURL: $mediaURL)';
return 'Event(server: ${server.ip}, id: $id, deviceID: $deviceID, title: $title, publishedRaw: $publishedRaw, published: $published, updatedRaw: $updatedRaw, updated: $updated, category: $category, mediaID: $mediaID, mediaURL: $mediaURL)';
}

Map<String, dynamic> toJson() => {
Expand Down
30 changes: 24 additions & 6 deletions lib/providers/events_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import 'package:bluecherry_client/providers/server_provider.dart';
import 'package:bluecherry_client/screens/events_browser/filter.dart';
import 'package:bluecherry_client/utils/constants.dart';
import 'package:bluecherry_client/utils/extensions.dart';
import 'package:bluecherry_client/utils/logging.dart';
import 'package:bluecherry_client/utils/logging.dart' as logging;
import 'package:bluecherry_client/utils/storage.dart';
import 'package:flutter/foundation.dart';

Expand Down Expand Up @@ -201,18 +201,36 @@ extension EventsScreenProvider on EventsProvider {
..removeWhere((event) {
if (!isDateSet) return false;

return event.published.toUtc().isBefore(startDate!.toUtc()) ||
event.updated.toUtc().isAfter(endDate!.toUtc());
final isToRemove =
event.published.toUtc().isBefore(startDate!.toUtc()) ||
event.updated.toUtc().isAfter(endDate!.toUtc());

if (isToRemove) {
logging.writeLogToFile(
'Removing future event ${event.id} '
'from ${event.server.name}/${event.deviceID}: '
'{raw: ${event.publishedRaw}, parsed: ${event.published}}.',
print: true,
);
}
return isToRemove;
})
..sort(
(a, b) => b.published.toUtc().compareTo(a.published.toUtc()));
..sort((a, b) => a.published.compareTo(b.published));

loadedEvents!.events[server] ??= [];
loadedEvents!.events[server]!.addAll(iterable);

if (iterable.isNotEmpty) {
logging.writeLogToFile(
'First event: ${iterable.first}',
print: true,
);
logging.writeLogToFile('Last event: ${iterable.last}', print: true);
}
_notify();
}));
} catch (error, stack) {
handleError(error, stack, 'Error loading events for $server');
logging.handleError(error, stack, 'Error loading events for $server');
loadedEvents!.invalidResponses.add(server);
}
}));
Expand Down
34 changes: 25 additions & 9 deletions lib/screens/events_timeline/events_playback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:bluecherry_client/screens/events_timeline/desktop/timeline_sideb
import 'package:bluecherry_client/screens/events_timeline/desktop/timeline_view.dart';
import 'package:bluecherry_client/screens/events_timeline/mobile/timeline_device_view.dart';
import 'package:bluecherry_client/utils/date.dart';
import 'package:bluecherry_client/utils/logging.dart' as logging;
import 'package:bluecherry_client/utils/methods.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -67,7 +68,7 @@ class _EventsPlaybackState extends EventsScreenState<EventsPlayback> {
DateTimeExtension.now().year,
DateTimeExtension.now().month,
DateTimeExtension.now().day,
).toLocal();
);

bool hasEverFetched = false;

Expand All @@ -78,14 +79,13 @@ class _EventsPlaybackState extends EventsScreenState<EventsPlayback> {
final settings = context.read<SettingsProvider>();
setState(() {
hasEverFetched = true;
date = date.toLocal();
startDate = DateTime(date.year, date.month, date.day).toLocal();
endDate = DateTime(date.year, date.month, date.day, 23, 59, 59).toLocal();
startDate = DateTime(date.year, date.month, date.day);
endDate = DateTime(date.year, date.month, date.day, 23, 59, 59);
timeline?.dispose();
timeline = null;
});

// if (true) {
// if (kDebugMode) {
// setState(() => timeline = Timeline.dump());
// return;
// }
Expand All @@ -111,10 +111,23 @@ class _EventsPlaybackState extends EventsScreenState<EventsPlayback> {
return 0;
});
for (final event in events) {
if (event.isAlarm || event.mediaURL == null) continue;
if (event.isAlarm || event.mediaURL == null) {
logging.writeLogToFile(
'Removing corrupted event ${event.id} from ${event.server.name}/${event.deviceID}.',
print: true,
);
continue;
}

if (!DateUtils.isSameDay(event.published, date) ||
!DateUtils.isSameDay(event.published.add(event.duration), date)) {
if (!DateUtils.isSameDay(event.published.toLocal(), date.toLocal()) ||
!DateUtils.isSameDay(event.published.add(event.duration).toLocal(),
date.toLocal())) {
logging.writeLogToFile(
'Removing future event ${event.id} '
'from ${event.server.name}/${event.deviceID}: '
'{raw: ${event.publishedRaw}, parsed: ${event.published}}.',
print: true,
);
continue;
}

Expand Down Expand Up @@ -372,7 +385,10 @@ extension DevicesMapExtension on MapEntry<Device, Iterable<Event>> {
TimelineTile buildTimelineTile(BuildContext context) {
final device = key;
final events = value;
debugPrint('Loaded ${events.length} events for $device');
logging.writeLogToFile(
'Loaded ${events.length} events for tile $device',
print: true,
);

return TimelineTile(
device: device,
Expand Down

0 comments on commit e04382c

Please sign in to comment.