Skip to content

Commit

Permalink
Minor bug fixes and improvements (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored May 17, 2024
2 parents 80ed27b + 721cf10 commit 5c56c07
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 123 deletions.

This file was deleted.

2 changes: 1 addition & 1 deletion lib/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class API {
Iterable<Device> devices;
if (devicesResult is Iterable) {
// This is reached in the case the server has multiple cameras
devices = devicesResult.cast<Map>().map((device) {
devices = List<Map>.from(devicesResult).map((device) {
return Device.fromServerJson(device, server);
});
} else if (devicesResult is Map) {
Expand Down
10 changes: 5 additions & 5 deletions lib/api/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ extension EventsExtension on API {
if (response.headers['content-type'] == 'application/json') {
debugPrint('Server returned a JSON response');
events = ((jsonDecode(response.body) as Map)['entry'] as Iterable)
.cast<Map>()
.map((eventObject) {
.map((item) {
final eventObject = item as Map;
final published = DateTime.parse(eventObject['published']);
final event = Event(
server: server,
Expand Down Expand Up @@ -140,8 +140,8 @@ extension EventsExtension on API {
debugPrint('Server returned a XML response');
final parser = Xml2Json()..parse(response.body);
events = (jsonDecode(parser.toGData())['feed']['entry'] as Iterable)
.cast<Map>()
.map((e) {
.map<Event>((item) {
final e = item as Map;
if (!e.containsKey('content')) debugPrint(e.toString());
return Event(
server: server,
Expand Down Expand Up @@ -170,7 +170,7 @@ extension EventsExtension on API {
),
),
);
}).cast<Event>();
});
}
} catch (exception, stacktrace) {
debugPrint('Failed to getEvents on server ${server.name}');
Expand Down
9 changes: 5 additions & 4 deletions lib/models/device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Device {
final MatrixType? matrixType;

/// A list of text overlays that will be rendered over the video.
final List<VideoOverlay> overlays;
final Iterable<VideoOverlay> overlays;

/// The preferred streaming type.
///
Expand Down Expand Up @@ -343,7 +343,7 @@ class Device {
bool? hasPTZ,
String? url,
MatrixType? matrixType,
List<VideoOverlay>? overlays,
Iterable<VideoOverlay>? overlays,
StreamingType? preferredStreamingType,
ExternalDeviceData? externalData,
}) =>
Expand Down Expand Up @@ -395,8 +395,9 @@ class Device {
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))
? List<VideoOverlay>.from((json['overlays'] as List).map((item) {
return VideoOverlay.fromMap(item as Map);
}))
: [],
preferredStreamingType: StreamingType.values.firstWhereOrNull(
(type) => type.name == json['preferredStreamingType'],
Expand Down
6 changes: 3 additions & 3 deletions lib/models/layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ class Layout {
factory Layout.fromMap(Map<String, dynamic> map) {
return Layout(
name: map['name'] ?? '',
devices: List<Device>.from((map['devices'] as List)
.cast<Map<String, dynamic>>()
.map(Device.fromJson)),
devices: List<Map<String, dynamic>>.from(map['devices'] as List)
.map<Device>(Device.fromJson)
.toList(),
type: DesktopLayoutType.values[map['layoutType'] as int],
);
}
Expand Down
8 changes: 3 additions & 5 deletions lib/models/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,9 @@ class Server {
port: json['port'],
login: json['login'],
password: json['password'],
devices: (json['devices'] as List)
.cast<Map<String, dynamic>>()
.map(Device.fromJson)
.toList()
.cast<Device>(),
devices: List<Map<String, dynamic>>.from(json['devices'] as List)
.map<Device>(Device.fromJson)
.toList(),
rtspPort: json['rtspPort'],
serverUUID: json['serverUUID'],
cookie: json['cookie'],
Expand Down
3 changes: 1 addition & 2 deletions lib/providers/desktop_view_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ class DesktopViewProvider extends UnityProvider {
data[kStorageDesktopLayouts] as String,
) ??
[]) as List)
.cast<Map>()
.map<Layout>((item) {
return Layout.fromMap(item.cast<String, dynamic>());
return Layout.fromMap((item as Map).cast<String, dynamic>());
}).toList();
_currentLayout = data[kStorageDesktopCurrentLayout] ?? 0;

Expand Down
5 changes: 3 additions & 2 deletions lib/providers/downloads_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ class DownloadsManager extends UnityProvider {
? <DownloadedEvent>{}
: ((await compute(jsonDecode, data[kStorageDownloads] as String) ?? [])
as List)
.cast<Map>()
.map<DownloadedEvent>((item) {
return DownloadedEvent.fromJson(item.cast<String, dynamic>());
return DownloadedEvent.fromJson(
(item as Map).cast<String, dynamic>(),
);
}).toSet();

super.restore(notifyListeners: notifyListeners);
Expand Down
3 changes: 2 additions & 1 deletion lib/providers/events_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class EventsProvider extends UnityProvider {
Future<void> restore({bool notifyListeners = true}) async {
final data = await tryReadStorage(() => events.read());

selectedDevices = (data['selectedDevices'] as List).toSet().cast<String>();
selectedDevices =
List<String>.from(data['selectedDevices'] as List).toSet();

super.restore(notifyListeners: notifyListeners);
}
Expand Down
12 changes: 5 additions & 7 deletions lib/providers/mobile_view_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ class MobileViewProvider extends UnityProvider {
// [Device]s present in the new tab.
final items = devices[value]!;
// Find the non-common i.e. new device tiles in this tab & create a new video player for them.
for (final device
in items.where((device) => device != null).cast<Device>()) {
for (final device in items.whereType<Device>()) {
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(device);
}
// Remove & dispose the video player instances that will not be used in this new tab.
Expand Down Expand Up @@ -188,7 +187,7 @@ class MobileViewProvider extends UnityProvider {
final data = devices.map(
(key, value) => MapEntry(
key.toString(),
value.map((e) => e?.toJson()).toList().cast<Map<String, dynamic>?>(),
value.map<Map<String, dynamic>?>((e) => e?.toJson()),
),
);
try {
Expand All @@ -212,10 +211,9 @@ class MobileViewProvider extends UnityProvider {
.map(
(key, value) => MapEntry<int, List<Device?>>(
int.parse(key),
value
.map((e) => e == null ? null : Device.fromJson(e))
.toList()
.cast<Device?>(),
(value as Iterable)
.map<Device?>((e) => e == null ? null : Device.fromJson(e))
.toList(),
),
)
.cast<int, List<Device?>>();
Expand Down
14 changes: 6 additions & 8 deletions lib/providers/server_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,12 @@ class ServersProvider extends UnityProvider {
Future<void> restore({bool notifyListeners = true}) async {
final data = await tryReadStorage(() => serversStorage.read());

final serversData = data[kStorageServers] is String
? await compute(jsonDecode, data[kStorageServers] as String)
: data[kStorageServers] as List;
servers = serversData
.cast<Map<String, dynamic>>()
.map(Server.fromJson)
.toList()
.cast<Server>();
final serversData = List<Map<String, dynamic>>.from(
data[kStorageServers] is String
? (await compute(jsonDecode, data[kStorageServers] as String) as List)
: data[kStorageServers] as List,
);
servers = serversData.map<Server>(Server.fromJson).toList();
super.restore(notifyListeners: notifyListeners);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/screens/downloads/downloads_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class DownloadTile extends StatefulWidget {

final Size size;
final Event event;
final List<Event> upcomingEvents;
final Iterable<Event> upcomingEvents;
final double progress;
final String? downloadPath;
final bool initiallyExpanded;
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/layouts/desktop/external_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension MatrixTypeExtension on MatrixType {

class AddExternalStreamDialog extends StatefulWidget {
final String? defaultUrl;
final List<VideoOverlay> overlays;
final Iterable<VideoOverlay> overlays;

const AddExternalStreamDialog({
super.key,
Expand Down
16 changes: 8 additions & 8 deletions lib/screens/layouts/desktop/layout_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class _LayoutManagerState extends State<LayoutManager> with Searchable {
super.dispose();
}

@override
void onSearchChanged(String text) {
super.onSearchChanged(text);
widget.onSearchChanged(text);
}

@override
Widget build(BuildContext context) {
final loc = AppLocalizations.of(context);
Expand All @@ -131,10 +137,7 @@ class _LayoutManagerState extends State<LayoutManager> with Searchable {
widget.collapseButton,
const SizedBox(width: 5.0),
Expanded(child: Text(loc.view, maxLines: 1)),
SearchToggleButton(
searchable: this,
iconSize: 18.0,
),
SearchToggleButton(searchable: this, iconSize: 18.0),
SquaredIconButton(
icon: Icon(
Icons.cyclone,
Expand Down Expand Up @@ -180,10 +183,7 @@ class _LayoutManagerState extends State<LayoutManager> with Searchable {
},
),
),
ToggleSearchBar(
searchable: this,
showBottomDivider: false,
),
ToggleSearchBar(searchable: this, showBottomDivider: false),
const Divider(height: 1.0),
]),
);
Expand Down
26 changes: 15 additions & 11 deletions lib/screens/layouts/desktop/layout_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,21 @@ class _LargeDeviceGridState extends State<LargeDeviceGrid> {
},
),
Expanded(
child: LayoutView(
layout: view.currentLayout,
onAccept: view.add,
onReorder: view.reorder,
onWillAccept: (device) {
if (device == null) return false;
if (view.currentLayout.type == DesktopLayoutType.singleView) {
return view.currentLayout.devices.isEmpty;
}
return !view.currentLayout.devices.contains(device);
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 500),
child: LayoutView(
key: ValueKey(view.currentLayout.hashCode),
layout: view.currentLayout,
onAccept: view.add,
onReorder: view.reorder,
onWillAccept: (device) {
if (device == null) return false;
if (view.currentLayout.type == DesktopLayoutType.singleView) {
return view.currentLayout.devices.isEmpty;
}
return !view.currentLayout.devices.contains(device);
},
),
),
),
];
Expand Down
34 changes: 19 additions & 15 deletions lib/screens/layouts/desktop/sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,17 @@ class _DesktopSidebarState extends State<DesktopSidebar> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
final servers = context.watch<ServersProvider>();
_updateServers();
}

void _updateServers() {
final servers = context.read<ServersProvider>();

_servers.clear();
for (final server in (servers.servers)
// online servers are rendered first
..sort((a, b) => b.online.toString().compareTo(a.online.toString()))) {
final devices = server.devices
.where(
(device) => device.name.toLowerCase().contains(
searchQuery.toLowerCase(),
),
)
.sorted();
for (final server in servers.servers) {
final devices = server.devices.sorted(
searchQuery: searchQuery,
);
_servers[server] = devices;
}
}
Expand All @@ -75,7 +73,10 @@ class _DesktopSidebarState extends State<DesktopSidebar> {
LayoutManager(
collapseButton: widget.collapseButton,
onSearchChanged: (text) {
setState(() => searchQuery = text);
setState(() {
searchQuery = text;
_updateServers();
});
},
),
if (servers.servers.isEmpty)
Expand All @@ -89,8 +90,10 @@ class _DesktopSidebarState extends State<DesktopSidebar> {
child: Material(
type: MaterialType.transparency,
child: CustomScrollView(slivers: [
for (final MapEntry(key: server, value: devices)
in _servers.entries)
for (final MapEntry<Server, Iterable<Device>>(
key: server,
value: devices,
) in _servers.entries)
() {
final isLoading = servers.isServerLoading(server);
if (!isLoading &&
Expand Down Expand Up @@ -161,7 +164,8 @@ class _DesktopSidebarState extends State<DesktopSidebar> {
onPressed: () {
if (isAllInView) {
view.removeDevicesFromCurrentLayout(
devices);
devices,
);
} else {
for (final device in devices) {
if (device.status &&
Expand Down
3 changes: 1 addition & 2 deletions lib/screens/layouts/mobile/device_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ class _MobileDeviceViewState extends State<MobileDeviceView> {
);
}

final selected =
view.devices[widget.tab]!.where((d) => d != null).cast<Device>();
final selected = view.devices[widget.tab]!.whereType<Device>();

return SizedBox(
width: double.infinity,
Expand Down
Loading

0 comments on commit 5c56c07

Please sign in to comment.