Skip to content

Commit

Permalink
feat: VideoOverlay serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa committed Nov 12, 2023
1 parent 41dfcdd commit d7be623
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/models/device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'dart:convert';

import 'package:bluecherry_client/models/server.dart';
import 'package:bluecherry_client/providers/server_provider.dart';
import 'package:bluecherry_client/utils/config.dart';
import 'package:bluecherry_client/utils/extensions.dart';
import 'package:bluecherry_client/widgets/device_grid/desktop/external_stream.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -53,6 +54,8 @@ class Device {

final MatrixType matrixType;

final List<VideoOverlay> overlays;

/// Creates a device.
Device(
this.name,
Expand All @@ -64,6 +67,7 @@ class Device {
this.hasPTZ = false,
this.url,
this.matrixType = MatrixType.t16,
this.overlays = const [],
});

Device.dump({
Expand All @@ -75,6 +79,7 @@ class Device {
this.hasPTZ = false,
this.url,
this.matrixType = MatrixType.t16,
this.overlays = const [],
}) : server = Server.dump();

String get uri => 'live/$id';
Expand Down Expand Up @@ -226,7 +231,8 @@ class Device {
resolutionY == other.resolutionY &&
hasPTZ == other.hasPTZ &&
url == other.url &&
matrixType == other.matrixType;
matrixType == other.matrixType &&
overlays == other.overlays;
}

@override
Expand All @@ -238,7 +244,8 @@ class Device {
resolutionY.hashCode ^
hasPTZ.hashCode ^
url.hashCode ^
matrixType.hashCode;
matrixType.hashCode ^
overlays.hashCode;

Device copyWith({
String? name,
Expand All @@ -250,6 +257,7 @@ class Device {
bool? hasPTZ,
String? url,
MatrixType? matrixType,
List<VideoOverlay>? overlays,
}) =>
Device(
name ?? this.name,
Expand All @@ -261,6 +269,7 @@ class Device {
hasPTZ: hasPTZ ?? this.hasPTZ,
url: url ?? this.url,
matrixType: matrixType ?? this.matrixType,
overlays: overlays ?? this.overlays,
);

Map<String, dynamic> toJson() {
Expand All @@ -274,6 +283,7 @@ class Device {
'hasPTZ': hasPTZ,
'url': url,
'matrixType': matrixType.index,
'overlays': overlays.map((e) => e.toMap()).toList(),
};
}

Expand All @@ -291,6 +301,10 @@ class Device {
hasPTZ: json['hasPTZ'] ?? false,
url: json['url'],
matrixType: MatrixType.values[json['matrixType'] ?? 0],
overlays: json['overlays'] != null
? List<VideoOverlay>.from(
(json['overlays'] as List).cast<Map>().map(VideoOverlay.fromMap))
: [],
);
}
}
29 changes: 29 additions & 0 deletions lib/utils/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ class VideoOverlay {
this.position = Offset.zero,
this.visible = true,
});

Map<String, dynamic> toMap() {
return {
'text': text,
'textStyle': {
'color': textStyle?.color?.value.toRadixString(16),
'fontSize': textStyle?.fontSize,
},
'position_x': position.dx,
'position_y': position.dy,
'visible': visible,
};
}

factory VideoOverlay.fromMap(Map map) {
return VideoOverlay(
text: map['text'] ?? '',
textStyle: TextStyle(
color: map['textStyle']['color'] == null
? null
: Color(int.parse(
'0xFF${(map['textStyle']['color'] as String).replaceAll('#', '')}',
)),
fontSize: (map['textStyle']['fontSize'] as num?)?.toDouble(),
),
position: Offset(map['position_x'] ?? 0.0, map['position_y'] ?? 0.0),
visible: map['visible'] ?? false,
);
}
}

/// Parses the config file content and returns a map with the config values.
Expand Down
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.8.1"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "2c30f27ada446b4d36307aade893faaaeb6d8f55b2362b7f424a9668fcc43f4d"
url: "https://pub.dev"
source: hosted
version: "9.0.14"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: b06739349ec2477e943055aea30172c5c7000225f79dad4702e2ec0eda79a6ff
url: "https://pub.dev"
source: hosted
version: "1.0.5"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -998,6 +1014,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "13.0.0"
volume_controller:
dependency: transitive
description:
Expand Down
38 changes: 38 additions & 0 deletions test/configuration_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:bluecherry_client/utils/config.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('VideoOverlay serialization', () {
const overlay = VideoOverlay(
text: 'My overlay',
textStyle: TextStyle(
color: Color(0xFF000000),
fontSize: 12.0,
),
position: Offset(10.0, 10.0),
);

final map = overlay.toMap();
expect(
map,
{
'text': 'My overlay',
'textStyle': {
'color': 'ff000000',
'fontSize': 12.0,
},
'position_x': 10.0,
'position_y': 10.0,
'visible': true,
},
);

final overlay2 = VideoOverlay.fromMap(map);
expect(overlay2.text, 'My overlay');
expect(overlay2.textStyle?.color, const Color(0xFF000000));
expect(overlay2.textStyle?.fontSize, 12.0);
expect(overlay2.position, const Offset(10.0, 10.0));
expect(overlay2.visible, true);
});
}

0 comments on commit d7be623

Please sign in to comment.