Skip to content

Commit

Permalink
General Updates (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored Sep 18, 2024
2 parents fd23a02 + 6093dc9 commit 5d50337
Show file tree
Hide file tree
Showing 39 changed files with 681 additions and 428 deletions.
45 changes: 26 additions & 19 deletions lib/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'dart:convert';
import 'package:bluecherry_client/api/api_helpers.dart';
import 'package:bluecherry_client/models/device.dart';
import 'package:bluecherry_client/models/server.dart';
import 'package:bluecherry_client/utils/logging.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:xml2json/xml2json.dart';
Expand Down Expand Up @@ -106,9 +107,11 @@ class API {
debugPrint(body);
server.online = false;
}
} catch (exception, stack) {
debugPrint(
'Failed to checkServerCredentials on server $server : $exception\n$stack',
} catch (error, stack) {
handleError(
error,
stack,
'Failed to check server credentials on server $server',
);

server.online = false;
Expand Down Expand Up @@ -163,10 +166,8 @@ class API {
..clear()
..addAll(devices);
return devices;
} catch (exception, stacktrace) {
debugPrint('Failed to getDevices on server $server');
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (error, stack) {
handleError(error, stack, 'Failed to get devices on server $server');
}
return null;
}
Expand All @@ -189,10 +190,12 @@ class API {
);
final body = jsonDecode(response.body);
return body['notification_api_endpoint'];
} catch (exception, stacktrace) {
debugPrint('Failed to getNotificationAPIEndpoint on server $server');
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (error, stack) {
handleError(
error,
stack,
'Failed to get notification API endpoint on server $server',
);
}
return null;
}
Expand Down Expand Up @@ -231,10 +234,12 @@ class API {
debugPrint(response.statusCode.toString());
debugPrint(response.body);
return true;
} catch (exception, stacktrace) {
debugPrint('Failed to registerNotificationToToken on server $server');
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (error, stack) {
handleError(
error,
stack,
'Failed to registerNotificationToToken on server $server',
);
return false;
}
}
Expand Down Expand Up @@ -265,10 +270,12 @@ class API {
debugPrint(response.statusCode.toString());
debugPrint(response.body);
return true;
} catch (exception, stacktrace) {
debugPrint('Failed to unregisterNotificationToken on server $server');
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (error, stack) {
handleError(
error,
stack,
'Failed to unregisterNotificationToken on server $server',
);
return false;
}
}
Expand Down
10 changes: 7 additions & 3 deletions lib/api/api_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:bluecherry_client/api/api.dart';
import 'package:bluecherry_client/models/server.dart';
import 'package:bluecherry_client/providers/server_provider.dart';
import 'package:bluecherry_client/providers/settings_provider.dart';
import 'package:bluecherry_client/utils/logging.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
Expand Down Expand Up @@ -118,9 +119,12 @@ abstract class APIHelpers {
return 'file://$filePath';
}
}
} catch (exception, stacktrace) {
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (error, stack) {
handleError(
error,
stack,
'Failed to get thumbnail for media ID $mediaID',
);
}
return null;
}
Expand Down
37 changes: 19 additions & 18 deletions lib/api/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:bluecherry_client/models/device.dart';
import 'package:bluecherry_client/models/event.dart';
import 'package:bluecherry_client/models/server.dart';
import 'package:bluecherry_client/utils/date.dart';
import 'package:bluecherry_client/utils/logging.dart';
import 'package:bluecherry_client/utils/methods.dart';
import 'package:flutter/foundation.dart';
import 'package:xml2json/xml2json.dart';
Expand Down Expand Up @@ -73,20 +74,19 @@ extension EventsExtension on API {
);

assert(server.serverUUID != null && server.hasCookies);
final response = await API.client.get(
Uri.https(
'${Uri.encodeComponent(server.login)}:${Uri.encodeComponent(server.password)}@${server.ip}:${server.port}',
'/events/',
{
'XML': '1',
'limit': '$limit',
if (startTime != null) 'startTime': startTime,
if (endTime != null) 'endTime': endTime,
if (deviceId != null) 'device_id': '$deviceId',
},
),
headers: {'Cookie': server.cookie!},
final uri = Uri.https(
'${Uri.encodeComponent(server.login)}:${Uri.encodeComponent(server.password)}@${server.ip}:${server.port}',
'/events/',
{
'XML': '1',
'limit': '$limit',
if (startTime != null) 'startTime': startTime,
if (endTime != null) 'endTime': endTime,
if (deviceId != null) 'device_id': '$deviceId',
},
);
final response =
await API.client.get(uri, headers: {'Cookie': server.cookie!});

var events = const Iterable<Event>.empty();

Expand Down Expand Up @@ -175,11 +175,12 @@ extension EventsExtension on API {
);
});
}
} catch (exception, stacktrace) {
debugPrint('Failed to getEvents on server ${server.name}');
debugPrint(response.body);
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (error, stack) {
handleError(
error,
stack,
'Failed to getEvents on server ${server.name} $uri ${response.body}',
);
}

debugPrint(
Expand Down
9 changes: 8 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@
"date": "Date",
"time": "Time",
"lastUpdate": "Last Update",
"screens": "Screens",
"screens": "Screens • {layout}",
"@screens": {
"placeholders": {
"layout": {
"type": "String"
}
}
},
"directCamera": "Direct Camera",
"addServer": "Add Server",
"settings": "Settings",
Expand Down
7 changes: 7 additions & 0 deletions lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@
"time": "Time",
"lastUpdate": "Dernière mise à jour",
"screens": "Écrans",
"@screens": {
"placeholders": {
"layout": {
"type": "String"
}
}
},
"directCamera": "Caméra direct",
"addServer": "Ajouter serveur",
"settings": "Paramètres",
Expand Down
7 changes: 7 additions & 0 deletions lib/l10n/app_pl.arb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@
"time": "Time",
"lastUpdate": "Ostatnia aktualizacja",
"screens": "Ekrany",
"@screens": {
"placeholders": {
"layout": {
"type": "String"
}
}
},
"directCamera": "Kamera bezpośrednia",
"addServer": "Dodaj serwer",
"settings": "Ustawienia",
Expand Down
7 changes: 7 additions & 0 deletions lib/l10n/app_pt.arb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@
"time": "Hora",
"lastUpdate": "Última atualização",
"screens": "Câmeras",
"@screens": {
"placeholders": {
"layout": {
"type": "String"
}
}
},
"directCamera": "Câmera específica",
"addServer": "Adicionar servidor",
"settings": "Configurações",
Expand Down
7 changes: 4 additions & 3 deletions lib/models/device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:bluecherry_client/providers/settings_provider.dart';
import 'package:bluecherry_client/screens/layouts/desktop/external_stream.dart';
import 'package:bluecherry_client/utils/config.dart';
import 'package:bluecherry_client/utils/extensions.dart';
import 'package:bluecherry_client/utils/logging.dart';
import 'package:flutter/foundation.dart';

class ExternalDeviceData {
Expand Down Expand Up @@ -138,7 +139,7 @@ class Device {
this.status = true,
this.resolutionX = 640,
this.resolutionY = 480,
this.hasPTZ = false,
this.hasPTZ = true,
this.url,
MatrixType? matrixType,
this.overlays = const [],
Expand Down Expand Up @@ -280,8 +281,8 @@ class Device {
} else {
debugPrint('Request failed with status: ${response.statusCode}');
}
} catch (error, stacktrace) {
debugPrint('Failed to get HLS url($uri): $error, $stacktrace');
} catch (error, stack) {
handleError(error, stack, 'Failed to get HLS URL on $uri');
}

return hlsURL;
Expand Down
4 changes: 4 additions & 0 deletions lib/models/layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,8 @@ class DeviceServerNotFound extends Error {
required this.layoutName,
required this.server,
});

@override
String toString() =>
'DeviceServerNotFound(layoutName: $layoutName, server: $server)';
}
13 changes: 13 additions & 0 deletions lib/providers/app_provider_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import 'package:bluecherry_client/utils/logging.dart';
import 'package:bluecherry_client/utils/storage.dart';
import 'package:flutter/widgets.dart';
import 'package:safe_local_storage/safe_local_storage.dart';
Expand All @@ -25,8 +26,11 @@ abstract class UnityProvider extends ChangeNotifier {
Future<void> initialize();
Future<void> reloadInterface() => initialize();

late SafeLocalStorage storage;

@protected
Future<void> initializeStorage(SafeLocalStorage storage, String key) async {
this.storage = storage;
try {
final hive = await tryReadStorage(() => storage.read());
if (!hive.containsKey(key)) {
Expand Down Expand Up @@ -54,4 +58,13 @@ abstract class UnityProvider extends ChangeNotifier {
this.notifyListeners();
}
}

Future<void> write(dynamic data) {
try {
return storage.write(data);
} catch (error, stack) {
handleError(error, stack);
return Future.value();
}
}
}
14 changes: 5 additions & 9 deletions lib/providers/desktop_view_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,11 @@ class DesktopViewProvider extends UnityProvider {
/// Pass [notifyListeners] as `false` to prevent redundant redraws.
@override
Future<void> save({bool notifyListeners = true}) async {
try {
await desktopView.write({
kStorageDesktopLayouts:
jsonEncode(layouts.map((layout) => layout.toMap()).toList()),
kStorageDesktopCurrentLayout: _currentLayout,
});
} catch (error, stackTrace) {
debugPrint('Failed to save desktop view:\n $error\n$stackTrace');
}
await write({
kStorageDesktopLayouts:
jsonEncode(layouts.map((layout) => layout.toMap()).toList()),
kStorageDesktopCurrentLayout: _currentLayout,
});

super.save(notifyListeners: notifyListeners);
}
Expand Down
19 changes: 8 additions & 11 deletions lib/providers/downloads_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,9 @@ class DownloadsManager extends UnityProvider {

@override
Future<void> save({bool notifyListeners = true}) async {
try {
await downloads.write({
kStorageDownloads: downloadedEvents.map((de) => de.toJson()).toList(),
});
} catch (e) {
debugPrint(e.toString());
}
await write({
kStorageDownloads: downloadedEvents.map((de) => de.toJson()).toList(),
});

super.save(notifyListeners: notifyListeners);
}
Expand Down Expand Up @@ -370,10 +366,11 @@ class DownloadsManager extends UnityProvider {
try {
final file = File(downloading[event]!.$2);
if (file.existsSync()) file.deleteSync();
} catch (e, s) {
debugPrint('Failed to delete file: $e');
writeLogToFile(
'Failed to delete file during cancelDownloading: $e, $s',
} catch (error, stack) {
handleError(
error,
stack,
'Failed to delete file while canceling download',
);
}
downloading.remove(event);
Expand Down
18 changes: 7 additions & 11 deletions lib/providers/events_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:bluecherry_client/providers/app_provider_interface.dart';
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/logging.dart';
import 'package:bluecherry_client/utils/storage.dart';
import 'package:flutter/foundation.dart';

Expand Down Expand Up @@ -118,14 +119,10 @@ class EventsProvider extends UnityProvider {

@override
Future<void> save({bool notifyListeners = true}) async {
try {
await events.write({
kStorageEvents: kStorageEvents,
'selectedDevices': selectedDevices.toList(),
});
} catch (error, stackTrace) {
debugPrint('Failed to save events:\n $error\n$stackTrace');
}
await write({
kStorageEvents: kStorageEvents,
'selectedDevices': selectedDevices.toList(),
});

super.save(notifyListeners: notifyListeners);
}
Expand Down Expand Up @@ -199,9 +196,8 @@ extension EventsScreenProvider on EventsProvider {
loadedEvents!.events[server]!.addAll(iterable);
_notify();
}));
} catch (exception, stacktrace) {
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (error, stack) {
handleError(error, stack, 'Error loading events for $server');
loadedEvents!.invalidResponses.add(server);
}
}));
Expand Down
Loading

0 comments on commit 5d50337

Please sign in to comment.