init
This commit is contained in:
50
lib/app/features/peers/controller/peers_controller.dart
Normal file
50
lib/app/features/peers/controller/peers_controller.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../../../backend/api/commands.dart' as commands;
|
||||
import '../../../../backend/discovery/model.dart';
|
||||
|
||||
part 'peers_controller.g.dart';
|
||||
|
||||
@riverpod
|
||||
class PeersController extends _$PeersController {
|
||||
@override
|
||||
Future<List<Peer>> build() async {
|
||||
return commands.getPeers();
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
state = const AsyncLoading();
|
||||
state = await AsyncValue.guard(commands.getPeers);
|
||||
}
|
||||
|
||||
void upsertPeer(Peer peer) {
|
||||
final current = state.asData?.value ?? const <Peer>[];
|
||||
final index = current.indexWhere((item) => item.id == peer.id);
|
||||
|
||||
if (index < 0) {
|
||||
state = AsyncData([...current, peer]);
|
||||
return;
|
||||
}
|
||||
|
||||
final old = current[index];
|
||||
if (old == peer) {
|
||||
return;
|
||||
}
|
||||
|
||||
final next = [...current];
|
||||
next[index] = peer;
|
||||
state = AsyncData(next);
|
||||
}
|
||||
|
||||
void removePeer(String id) {
|
||||
final current = state.asData?.value;
|
||||
if (current == null || current.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
final next = current.where((item) => item.id != id).toList(growable: false);
|
||||
if (next.length != current.length) {
|
||||
state = AsyncData(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
54
lib/app/features/peers/controller/peers_controller.g.dart
Normal file
54
lib/app/features/peers/controller/peers_controller.g.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'peers_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(PeersController)
|
||||
final peersControllerProvider = PeersControllerProvider._();
|
||||
|
||||
final class PeersControllerProvider
|
||||
extends $AsyncNotifierProvider<PeersController, List<Peer>> {
|
||||
PeersControllerProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'peersControllerProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$peersControllerHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
PeersController create() => PeersController();
|
||||
}
|
||||
|
||||
String _$peersControllerHash() => r'd6d108b39274dd5de380523373f115d2e4c10e0e';
|
||||
|
||||
abstract class _$PeersController extends $AsyncNotifier<List<Peer>> {
|
||||
FutureOr<List<Peer>> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<AsyncValue<List<Peer>>, List<Peer>>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<List<Peer>>, List<Peer>>,
|
||||
AsyncValue<List<Peer>>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
73
lib/app/features/peers/pages/peers_page.dart
Normal file
73
lib/app/features/peers/pages/peers_page.dart
Normal file
@@ -0,0 +1,73 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../controller/peers_controller.dart';
|
||||
import '../widgets/peer_card.dart';
|
||||
import '../../../shared/widgets/empty_state.dart';
|
||||
|
||||
class PeersPage extends ConsumerWidget {
|
||||
const PeersPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final peersAsync = ref.watch(peersControllerProvider);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Peers Discovery'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: '刷新',
|
||||
onPressed: () =>
|
||||
ref.read(peersControllerProvider.notifier).refresh(),
|
||||
icon: const Icon(Icons.refresh_rounded),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: peersAsync.when(
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (error, _) => AppEmptyState(
|
||||
icon: Icons.cloud_off_rounded,
|
||||
title: '发现失败',
|
||||
message: error.toString(),
|
||||
action: FilledButton.icon(
|
||||
onPressed: () =>
|
||||
ref.read(peersControllerProvider.notifier).refresh(),
|
||||
icon: const Icon(Icons.refresh_rounded),
|
||||
label: const Text('重试'),
|
||||
),
|
||||
),
|
||||
data: (peers) {
|
||||
if (peers.isEmpty) {
|
||||
return AppEmptyState(
|
||||
icon: Icons.wifi_tethering,
|
||||
title: '扫描中',
|
||||
message: '请确认局域网连接和防火墙配置。',
|
||||
);
|
||||
}
|
||||
|
||||
final width = MediaQuery.sizeOf(context).width;
|
||||
final columns = width >= 1320
|
||||
? 4
|
||||
: width >= 1100
|
||||
? 3
|
||||
: width >= 720
|
||||
? 2
|
||||
: 1;
|
||||
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: columns,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
childAspectRatio: 1.36,
|
||||
),
|
||||
itemCount: peers.length,
|
||||
itemBuilder: (context, index) => PeerCard(peer: peers[index]),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
266
lib/app/features/peers/widgets/peer_card.dart
Normal file
266
lib/app/features/peers/widgets/peer_card.dart
Normal file
@@ -0,0 +1,266 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:desktop_drop/desktop_drop.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../backend/api/commands.dart' as commands;
|
||||
import '../../../../backend/discovery/model.dart';
|
||||
import '../../modals/send_files_modal.dart';
|
||||
import '../../modals/send_text_modal.dart';
|
||||
|
||||
class PeerCard extends StatefulWidget {
|
||||
const PeerCard({super.key, required this.peer});
|
||||
|
||||
final Peer peer;
|
||||
|
||||
@override
|
||||
State<PeerCard> createState() => _PeerCardState();
|
||||
}
|
||||
|
||||
class _PeerCardState extends State<PeerCard> {
|
||||
bool _dragging = false;
|
||||
|
||||
String? get _targetIp {
|
||||
if (widget.peer.routes.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
return widget.peer.routes.values.first.ip;
|
||||
}
|
||||
|
||||
Future<void> _openTextModal() async {
|
||||
final targetIp = _targetIp;
|
||||
if (targetIp == null || !mounted) {
|
||||
return;
|
||||
}
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (_) => SendTextModal(peer: widget.peer, targetIp: targetIp),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openFilesModal(List<String> files) async {
|
||||
final targetIp = _targetIp;
|
||||
if (targetIp == null || !mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (_) => SendFilesModal(
|
||||
peer: widget.peer,
|
||||
targetIp: targetIp,
|
||||
initialPaths: files,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool get _enableDragDrop =>
|
||||
Platform.isLinux || Platform.isWindows || Platform.isMacOS;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final targetIp = _targetIp;
|
||||
|
||||
final card = Card(
|
||||
clipBehavior: Clip.antiAlias,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(18),
|
||||
side: BorderSide(
|
||||
color: _dragging
|
||||
? theme.colorScheme.primary
|
||||
: theme.colorScheme.outlineVariant,
|
||||
width: _dragging ? 2 : 1,
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(14),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
CircleAvatar(
|
||||
backgroundColor: theme.colorScheme.primaryContainer,
|
||||
child: Icon(
|
||||
_iconForOs(widget.peer.os),
|
||||
color: theme.colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.peer.name,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: theme.textTheme.titleMedium,
|
||||
),
|
||||
Text(
|
||||
targetIp ?? '无路由',
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
Chip(
|
||||
avatar: const Icon(Icons.shield_rounded, size: 16),
|
||||
label: Text(widget.peer.enableTls ? 'TLS on' : 'TLS off'),
|
||||
),
|
||||
if (widget.peer.trustMismatch)
|
||||
const Chip(
|
||||
avatar: Icon(Icons.warning_amber_rounded, size: 16),
|
||||
label: Text('Trust Mismatch'),
|
||||
),
|
||||
FutureBuilder<bool>(
|
||||
future: commands.isTrusted(peerId: widget.peer.id),
|
||||
builder: (context, snapshot) {
|
||||
final trusted = snapshot.data ?? false;
|
||||
return Chip(
|
||||
avatar: Icon(
|
||||
trusted
|
||||
? Icons.verified_user_rounded
|
||||
: Icons.gpp_maybe_rounded,
|
||||
size: 16,
|
||||
),
|
||||
label: Text(trusted ? 'Trusted' : 'Untrusted'),
|
||||
);
|
||||
},
|
||||
),
|
||||
Chip(
|
||||
avatar: const Icon(Icons.hub_rounded, size: 16),
|
||||
label: Text('${widget.peer.routes.length} routes'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const Spacer(),
|
||||
if (_enableDragDrop && _dragging)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
color: theme.colorScheme.primaryContainer.withValues(
|
||||
alpha: 0.55,
|
||||
),
|
||||
),
|
||||
child: const Center(child: Text('释放后发送文件')),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: FilledButton.tonalIcon(
|
||||
onPressed: targetIp == null ? null : _openTextModal,
|
||||
icon: const Icon(Icons.chat_bubble_rounded),
|
||||
label: const Text('文本'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: FilledButton.icon(
|
||||
onPressed: targetIp == null
|
||||
? null
|
||||
: () => _openFilesModal(const []),
|
||||
icon: const Icon(Icons.upload_file_rounded),
|
||||
label: const Text('文件/文件夹'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
FutureBuilder<bool>(
|
||||
future: commands.isTrusted(peerId: widget.peer.id),
|
||||
builder: (context, snapshot) {
|
||||
final trusted = snapshot.data ?? false;
|
||||
return SizedBox(
|
||||
width: double.infinity,
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () async {
|
||||
if (trusted) {
|
||||
await commands.untrustPeer(peerId: widget.peer.id);
|
||||
} else {
|
||||
await commands.trustPeer(peerId: widget.peer.id);
|
||||
}
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
},
|
||||
icon: Icon(
|
||||
trusted
|
||||
? Icons.verified_user_rounded
|
||||
: Icons.gpp_maybe_rounded,
|
||||
),
|
||||
label: Text(trusted ? '取消信任' : '设为信任设备'),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
if (targetIp == null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
'该设备暂无可用传输路由',
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.error,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (!_enableDragDrop) {
|
||||
return card;
|
||||
}
|
||||
|
||||
return DropTarget(
|
||||
onDragEntered: (_) => setState(() => _dragging = true),
|
||||
onDragExited: (_) => setState(() => _dragging = false),
|
||||
onDragDone: (details) {
|
||||
final files = details.files
|
||||
.map((f) => f.path)
|
||||
.where((e) => e.isNotEmpty)
|
||||
.toList();
|
||||
setState(() => _dragging = false);
|
||||
if (files.isNotEmpty) {
|
||||
_openFilesModal(files);
|
||||
}
|
||||
},
|
||||
child: card,
|
||||
);
|
||||
}
|
||||
|
||||
IconData _iconForOs(String os) {
|
||||
final normalized = os.toLowerCase();
|
||||
if (normalized.contains('windows')) {
|
||||
return Icons.laptop_windows_rounded;
|
||||
}
|
||||
if (normalized.contains('mac') || normalized.contains('darwin')) {
|
||||
return Icons.laptop_mac_rounded;
|
||||
}
|
||||
if (normalized.contains('linux')) {
|
||||
return Icons.computer_rounded;
|
||||
}
|
||||
if (normalized.contains('android')) {
|
||||
return Icons.phone_android_rounded;
|
||||
}
|
||||
if (normalized.contains('ios') || normalized.contains('iphone')) {
|
||||
return Icons.phone_iphone_rounded;
|
||||
}
|
||||
return Platform.isAndroid
|
||||
? Icons.devices_rounded
|
||||
: Icons.device_hub_rounded;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user