48 lines
1.5 KiB
Dart
48 lines
1.5 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
import '../../features/settings/server_connections.dart';
|
|
|
|
/// The machine owning this mounted app. A profile rename rebuilds consumers;
|
|
/// changing machines creates a new gateway and app instead.
|
|
class ServerContext extends InheritedNotifier<ServerConnections> {
|
|
const ServerContext({
|
|
super.key,
|
|
required ServerConnections connections,
|
|
required this.serverId,
|
|
required super.child,
|
|
}) : super(notifier: connections);
|
|
|
|
final String? serverId;
|
|
static ServerContext? maybeOf(BuildContext context) =>
|
|
context.dependOnInheritedWidgetOfExactType<ServerContext>();
|
|
|
|
String? get name {
|
|
for (final server in notifier!.catalog.servers) {
|
|
if (server.id == serverId) return server.name;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static String? nameFor(BuildContext context, String id) {
|
|
final scope = maybeOf(context);
|
|
for (final machine
|
|
in scope?.notifier?.catalog.servers ?? const <dynamic>[]) {
|
|
if (machine.id == id) return machine.name as String;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static Widget forServer(BuildContext context, String id, Widget child) {
|
|
final scope = maybeOf(context);
|
|
return scope == null || id.isEmpty
|
|
? child
|
|
: ServerContext(
|
|
connections: scope.notifier!,
|
|
serverId: id,
|
|
child: child,
|
|
);
|
|
}
|
|
|
|
static String metadata(BuildContext context, String detail) =>
|
|
[?maybeOf(context)?.name, if (detail.isNotEmpty) detail].join(' · ');
|
|
}
|