yubioath-flutter/lib/app/views/device_avatar.dart

129 lines
3.8 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'package:flutter/material.dart';
2022-07-07 21:20:04 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2024-03-08 11:30:47 +03:00
import 'package:material_symbols_icons/symbols.dart';
import '../../core/models.dart';
import '../../core/state.dart';
2023-02-03 11:56:23 +03:00
import '../../management/models.dart';
import '../../widgets/product_image.dart';
import '../models.dart';
2022-07-07 21:20:04 +03:00
import '../state.dart';
import 'keys.dart';
class DeviceAvatar extends StatelessWidget {
2022-02-01 15:30:03 +03:00
final Widget child;
2022-06-05 14:49:16 +03:00
final Widget? badge;
2022-06-02 15:49:38 +03:00
final double? radius;
const DeviceAvatar({super.key, required this.child, this.badge, this.radius});
factory DeviceAvatar.yubiKeyData(YubiKeyData data, {double? radius}) =>
DeviceAvatar(
2024-03-08 11:30:47 +03:00
badge: isDesktop && data.node is NfcReaderNode
? const Icon(Symbols.contactless)
: null,
2022-06-02 15:49:38 +03:00
radius: radius,
2024-03-08 14:56:35 +03:00
child: CircleAvatar(
backgroundColor: Colors.transparent,
child: ProductImage(
name: data.name,
formFactor: data.info.formFactor,
isNfc:
data.info.supportedCapabilities.containsKey(Transport.nfc)),
),
2022-02-01 15:30:03 +03:00
);
factory DeviceAvatar.deviceNode(DeviceNode node, {double? radius}) =>
2022-02-01 15:30:03 +03:00
node.map(
usbYubiKey: (node) {
final info = node.info;
if (info != null) {
return DeviceAvatar.yubiKeyData(
YubiKeyData(node, node.name, info),
2022-06-02 15:49:38 +03:00
radius: radius,
);
}
return DeviceAvatar(
2022-06-02 15:49:38 +03:00
radius: radius,
2024-03-08 14:56:35 +03:00
child: const CircleAvatar(
backgroundColor: Colors.transparent,
child: ProductImage(
name: '',
formFactor: FormFactor.unknown,
isNfc: false,
),
2023-02-03 11:56:23 +03:00
),
);
},
nfcReader: (_) => DeviceAvatar(
2022-06-02 15:49:38 +03:00
radius: radius,
2024-03-08 14:56:35 +03:00
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Icon(Symbols.contactless),
),
2022-02-01 15:30:03 +03:00
),
);
2022-07-07 21:20:04 +03:00
factory DeviceAvatar.currentDevice(WidgetRef ref, {double? radius}) {
final deviceNode = ref.watch(currentDeviceProvider);
if (deviceNode != null) {
return ref.watch(currentDeviceDataProvider).maybeWhen(
data: (data) => DeviceAvatar.yubiKeyData(
data,
radius: radius,
),
orElse: () => DeviceAvatar.deviceNode(
deviceNode,
radius: radius,
),
);
} else {
return DeviceAvatar(
radius: radius,
key: noDeviceAvatar,
2024-03-08 14:56:35 +03:00
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Icon(Symbols.usb),
),
2022-07-07 21:20:04 +03:00
);
}
}
@override
Widget build(BuildContext context) {
2022-02-01 15:30:03 +03:00
return Stack(
alignment: AlignmentDirectional.bottomEnd,
children: [
2024-03-08 14:56:35 +03:00
child,
2022-02-01 15:30:03 +03:00
if (badge != null)
CircleAvatar(
2024-03-08 14:56:35 +03:00
radius: 10,
2023-12-20 17:09:31 +03:00
backgroundColor: Colors.transparent,
2022-06-05 14:49:16 +03:00
child: IconTheme(
data: IconTheme.of(context).copyWith(
color: Theme.of(context).colorScheme.onPrimary,
2024-03-08 14:56:35 +03:00
size: 18,
2022-06-05 14:49:16 +03:00
),
child: badge!,
2022-02-01 15:30:03 +03:00
),
),
],
);
}
}