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

124 lines
3.6 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';
import '../../core/models.dart';
import '../../core/state.dart';
2023-02-03 11:56:23 +03:00
import '../../management/models.dart';
2022-06-05 14:49:16 +03:00
import '../../widgets/custom_icons.dart';
2023-02-03 11:56:23 +03:00
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(
badge: isDesktop && data.node is NfcReaderNode ? nfcIcon : null,
2022-06-02 15:49:38 +03:00
radius: radius,
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,
child: const ProductImage(
2023-02-03 11:56:23 +03:00
name: '',
formFactor: FormFactor.unknown,
isNfc: false,
),
);
},
nfcReader: (_) => DeviceAvatar(
2022-06-02 15:49:38 +03:00
radius: radius,
2022-06-05 14:49:16 +03:00
child: nfcIcon,
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,
2022-07-07 21:20:04 +03:00
child: const Icon(Icons.usb),
);
}
}
@override
Widget build(BuildContext context) {
final radius = this.radius ?? 20;
2022-02-01 15:30:03 +03:00
return Stack(
alignment: AlignmentDirectional.bottomEnd,
children: [
CircleAvatar(
2022-06-02 15:49:38 +03:00
radius: radius,
2023-12-20 17:09:31 +03:00
backgroundColor: Colors.transparent,
child: IconTheme(
data: IconTheme.of(context).copyWith(
size: radius,
2022-06-05 14:49:16 +03:00
),
child: child,
2022-02-01 15:30:03 +03:00
),
),
if (badge != null)
CircleAvatar(
2022-06-02 15:49:38 +03:00
radius: radius / 3,
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,
size: radius * 0.5,
),
child: badge!,
2022-02-01 15:30:03 +03:00
),
),
],
);
}
}