feat(contacts): обмен номером телефона по NFC/BLE

This commit is contained in:
klockky
2026-06-21 12:26:01 +00:00
parent 2f3b280fa4
commit ad04225a62
6 changed files with 65 additions and 32 deletions
+6 -2
View File
@@ -60,8 +60,9 @@ class ContactsModule {
static Future<CachedContact?> addContact(
Api api,
int id,
String firstName,
) async {
String firstName, {
int phone = 0,
}) async {
final resp = await api.sendRequest(Opcode.contactUpdate, {
'action': 'ADD',
'contactId': id,
@@ -92,6 +93,9 @@ class ContactsModule {
};
if (row == null) return null;
if (phone > 0 && ((row['phone'] as int?) ?? 0) == 0) {
row['phone'] = phone;
}
await AppDatabase.saveContacts([row]);
if (contact != null) _primeContactCache(contact);
revision.value++;
+7 -4
View File
@@ -8,9 +8,10 @@ enum NfcEventType { received, exchanging, cancelled, error }
class NfcEvent {
final NfcEventType type;
final int? id;
final int? phone;
final String? reason;
const NfcEvent(this.type, this.id, {this.reason});
const NfcEvent(this.type, this.id, {this.phone, this.reason});
}
class NfcStatus {
@@ -47,8 +48,8 @@ class NfcExchangeService {
Stream<NfcEvent> get events =>
_events.receiveBroadcastStream().map(_decodeEvent);
Future<void> start(int selfId) =>
_method.invokeMethod('start', {'selfId': selfId});
Future<void> start(int selfId, int selfPhone) =>
_method.invokeMethod('start', {'selfId': selfId, 'selfPhone': selfPhone});
Future<void> stop() async {
if (!_supported) return;
@@ -61,9 +62,11 @@ class NfcExchangeService {
final map = raw is Map ? raw : const {};
final id = map['id'];
final parsedId = id is int ? id : (id is num ? id.toInt() : null);
final phone = map['phone'];
final parsedPhone = phone is int ? phone : (phone is num ? phone.toInt() : null);
switch (map['event']) {
case 'received':
return NfcEvent(NfcEventType.received, parsedId);
return NfcEvent(NfcEventType.received, parsedId, phone: parsedPhone);
case 'exchanging':
return const NfcEvent(NfcEventType.exchanging, null);
case 'error':
@@ -9,6 +9,7 @@ import '../../../backend/modules/contacts.dart';
import '../../../core/cache/info_cache.dart';
import '../../../core/nfc/nfc_exchange_service.dart';
import '../../../core/storage/app_database.dart';
import '../../../core/utils/format.dart';
import '../../../main.dart';
import '../../widgets/custom_notification.dart';
import '../../widgets/komet_avatar.dart';
@@ -41,6 +42,7 @@ class _NfcExchangeSheetState extends State<NfcExchangeSheet>
_Stage _stage = _Stage.checking;
int? _peerId;
int? _peerPhone;
Map<String, dynamic>? _peerInfo;
String _failReason = '';
@@ -85,7 +87,7 @@ class _NfcExchangeSheetState extends State<NfcExchangeSheet>
return;
}
_sub = _nfc.events.listen(_onEvent);
await _nfc.start(profile.id);
await _nfc.start(profile.id, profile.phone);
if (mounted) setState(() => _stage = _Stage.scanning);
}
@@ -114,6 +116,7 @@ class _NfcExchangeSheetState extends State<NfcExchangeSheet>
final id = event.id;
if (id == null || _peerId != null) return;
_peerId = id;
_peerPhone = (event.phone != null && event.phone! > 0) ? event.phone : null;
HapticFeedback.mediumImpact();
_reveal.forward(from: 0);
setState(() => _stage = _Stage.found);
@@ -163,7 +166,12 @@ class _NfcExchangeSheetState extends State<NfcExchangeSheet>
if (id == null) return;
setState(() => _stage = _Stage.adding);
try {
await ContactsModule.addContact(api, id, _firstNameForAdd());
await ContactsModule.addContact(
api,
id,
_firstNameForAdd(),
phone: _peerPhone ?? 0,
);
if (!mounted) return;
setState(() => _stage = _Stage.added);
showCustomNotification(context, 'Контакт добавлен');
@@ -413,7 +421,7 @@ class _NfcExchangeSheetState extends State<NfcExchangeSheet>
),
const SizedBox(height: 4),
Text(
'ID ${_peerId ?? ''}',
formatPhone(_peerPhone) ?? 'ID ${_peerId ?? ''}',
style: TextStyle(color: cs.onSurfaceVariant, fontSize: 13),
),
const SizedBox(height: 24),