import 'dart:ffi'; import 'package:ffi/ffi.dart'; import 'package:flutter/foundation.dart'; import 'package:cw_zano/api/signatures.dart'; import 'package:cw_zano/api/types.dart'; import 'package:cw_zano/api/zano_api.dart'; import 'package:cw_zano/api/structs/subaddress_row.dart'; import 'package:cw_zano/api/wallet.dart'; final subaddressSizeNative = zanoApi .lookup>('subaddrress_size') .asFunction(); final subaddressRefreshNative = zanoApi .lookup>('subaddress_refresh') .asFunction(); final subaddrressGetAllNative = zanoApi .lookup>('subaddrress_get_all') .asFunction(); final subaddrressAddNewNative = zanoApi .lookup>('subaddress_add_row') .asFunction(); final subaddrressSetLabelNative = zanoApi .lookup>('subaddress_set_label') .asFunction(); bool isUpdating = false; void refreshSubaddresses({required int accountIndex}) { try { isUpdating = true; subaddressRefreshNative(accountIndex); isUpdating = false; } catch (e) { isUpdating = false; rethrow; } } List getAllSubaddresses() { final size = subaddressSizeNative(); final subaddressAddressesPointer = subaddrressGetAllNative(); final subaddressAddresses = subaddressAddressesPointer.asTypedList(size); return subaddressAddresses .map((addr) => Pointer.fromAddress(addr).ref) .toList(); } void addSubaddressSync({required int accountIndex, required String label}) { final labelPointer = label.toNativeUtf8(); subaddrressAddNewNative(accountIndex, labelPointer); calloc.free(labelPointer); } void setLabelForSubaddressSync( {required int accountIndex, required int addressIndex, required String label}) { final labelPointer = label.toNativeUtf8(); subaddrressSetLabelNative(accountIndex, addressIndex, labelPointer); calloc.free(labelPointer); } void _addSubaddress(Map args) { final label = args['label'] as String; final accountIndex = args['accountIndex'] as int; addSubaddressSync(accountIndex: accountIndex, label: label); } void _setLabelForSubaddress(Map args) { final label = args['label'] as String; final accountIndex = args['accountIndex'] as int; final addressIndex = args['addressIndex'] as int; setLabelForSubaddressSync( accountIndex: accountIndex, addressIndex: addressIndex, label: label); } Future addSubaddress({required int accountIndex, required String label}) async { await compute, void>( _addSubaddress, {'accountIndex': accountIndex, 'label': label}); await store(); } Future setLabelForSubaddress( {required int accountIndex, required int addressIndex, required String label}) async { await compute, void>(_setLabelForSubaddress, { 'accountIndex': accountIndex, 'addressIndex': addressIndex, 'label': label }); await store(); }