Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PE-7123: auto manifest update default to last name #1916

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/arns/domain/arns_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ abstract class ARNSRepository {
Future<void> saveAllFilesWithAssignedNames();
Future<List<ArnsRecord>> getActiveARNSRecordsForFile(String fileId);
Future<void> waitForARNSRecordsToUpdate();
Future<sdk.ARNSUndername> getUndernameByDomainAndName(
String domain, String name);
Future<List<sdk.ArNSNameModel>> getARNSNameModelsForWallet(String address);

factory ARNSRepository({
required ArioSDK sdk,
Expand Down Expand Up @@ -371,6 +374,32 @@ class _ARNSRepository implements ARNSRepository {

await _getARNSUndernamesCompleter!.future;
}

@override
Future<sdk.ARNSUndername> getUndernameByDomainAndName(
String domain, String name) async {
if (_cachedUndernames.isEmpty) {
await waitForARNSRecordsToUpdate();
}

if (_cachedUndernames[domain] == null) {
throw Exception('Domain not cached');
}

final undername = _cachedUndernames[domain]![name];

if (undername == null) {
throw Exception('Undername not cached');
}

return undername;
}

@override
Future<List<sdk.ArNSNameModel>> getARNSNameModelsForWallet(
String address) async {
return _sdk.getArNSNames(address);
}
}

AntRecord toAntRecordFromSDK(sdk.ANTRecord record) {
Expand Down
60 changes: 43 additions & 17 deletions lib/arns/presentation/assign_name_bloc/assign_name_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class AssignNameBloc extends Bloc<AssignNameEvent, AssignNameState> {
final ARNSRepository _arnsRepository;
final ArDriveAuth _auth;
ARNSUndername? _selectedUndername;
ANTRecord? _selectedANTRecord;
// TODO: remove this later
ArNSNameModel? _selectedNameModel;

AssignNameBloc({
required ArDriveAuth auth,
Expand All @@ -32,10 +33,19 @@ class AssignNameBloc extends Bloc<AssignNameEvent, AssignNameState> {
final names =
await _arnsRepository.getAntRecordsForWallet(walletAddress!);

final nameModels =
await _arnsRepository.getARNSNameModelsForWallet(walletAddress);

for (var nameModel in nameModels) {
logger.i('Name model: ${nameModel.toString()}');
}

if (names.isEmpty) {
emit(AssignNameEmptyState());
} else {
emit(NamesLoaded(names: names));
emit(NamesLoaded(
nameModels: nameModels,
));
}
} catch (e) {
logger.e('Failed to load ArNS names', e);
Expand All @@ -44,40 +54,44 @@ class AssignNameBloc extends Bloc<AssignNameEvent, AssignNameState> {
});

on<SelectName>((event, emit) async {
_selectedANTRecord = event.name;

_selectedNameModel = event.nameModel;
if (state is NamesLoaded) {
emit(
(state as NamesLoaded).copyWith(
selectedName: event.name,
selectedName: event.nameModel,
),
);
}
if (state is UndernamesLoaded) {
emit(
NamesLoaded(
names: (state as UndernamesLoaded).names,
selectedName: _selectedANTRecord),
nameModels: (state as UndernamesLoaded).nameModels,
selectedName: _selectedNameModel,
),
);
}
});

on<LoadUndernames>(
(event, emit) async {
final names = (state as NamesLoaded).names;
final names = (state as NamesLoaded).nameModels;
emit(LoadingUndernames());

final undernames =
await _arnsRepository.getARNSUndernames(_selectedANTRecord!);
final undernames = await _arnsRepository.getARNSUndernames(
ANTRecord(
domain: _selectedNameModel!.name,
processId: _selectedNameModel!.processId,
),
);

if (undernames.length > 1) {
undernames.removeWhere((element) => element.name == '@');
}

emit(
UndernamesLoaded(
selectedName: _selectedANTRecord!,
names: names,
nameModels: names,
selectedName: _selectedNameModel!,
undernames: undernames,
selectedUndername: null,
),
Expand Down Expand Up @@ -111,7 +125,7 @@ class AssignNameBloc extends Bloc<AssignNameEvent, AssignNameState> {
name: '@',
record: ARNSRecord(
transactionId: fileDataTableItem.dataTxId, ttlSeconds: 3600),
domain: _selectedANTRecord!.domain,
domain: _selectedNameModel!.name,
);
} else {
undername = ARNSUndername(
Expand All @@ -120,19 +134,19 @@ class AssignNameBloc extends Bloc<AssignNameEvent, AssignNameState> {
transactionId: fileDataTableItem.dataTxId,
ttlSeconds: 3600,
),
domain: _selectedANTRecord!.domain,
domain: _selectedNameModel!.name,
);
}

await _arnsRepository.setUndernamesToFile(
undername: undername,
fileId: fileDataTableItem.fileId,
driveId: fileDataTableItem.driveId,
processId: _selectedANTRecord!.processId,
processId: _selectedNameModel!.processId,
);

final (address, arAddress) = getAddressesFromArns(
domain: _selectedANTRecord!.domain,
domain: _selectedNameModel!.name,
undername: _selectedUndername?.name,
);

Expand All @@ -146,9 +160,21 @@ class AssignNameBloc extends Bloc<AssignNameEvent, AssignNameState> {
}
});

on<ShowSuccessModal>((event, emit) async {
final (address, arAddress) = getAddressesFromArns(
domain: event.undername.domain,
undername: event.undername.name,
);

emit(NameAssignedWithSuccess(
address: address,
arAddress: arAddress,
));
});

on<ConfirmSelection>((event, emit) async {
emit(SelectionConfirmed(
selectedName: _selectedANTRecord!,
selectedName: _selectedNameModel!,
selectedUndername: _selectedUndername,
));
});
Expand Down
17 changes: 13 additions & 4 deletions lib/arns/presentation/assign_name_bloc/assign_name_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ final class LoadNames extends AssignNameEvent {
final class CloseAssignName extends AssignNameEvent {}

final class SelectName extends AssignNameEvent {
final ANTRecord name;

const SelectName(this.name);
final ArNSNameModel nameModel;
const SelectName(this.nameModel);

@override
List<Object> get props => [name];
List<Object> get props => [nameModel];
}

final class LoadUndernames extends AssignNameEvent {
Expand All @@ -47,3 +46,13 @@ final class SelectUndername extends AssignNameEvent {
final class ConfirmSelectionAndUpload extends AssignNameEvent {}

final class ConfirmSelection extends AssignNameEvent {}

final class ShowSuccessModal extends AssignNameEvent {
final ARNSUndername undername;

const ShowSuccessModal({required this.undername});

@override
List<Object> get props => [undername];
}
//
30 changes: 15 additions & 15 deletions lib/arns/presentation/assign_name_bloc/assign_name_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ final class AssignNameInitial extends AssignNameState {}
final class LoadingNames extends AssignNameState {}

final class NamesLoaded extends AssignNameState {
final List<ANTRecord> names;
final ANTRecord? selectedName;
final List<ArNSNameModel> nameModels;
final ArNSNameModel? selectedName;

const NamesLoaded({required this.names, this.selectedName});
const NamesLoaded({required this.nameModels, this.selectedName});

@override
List<Object?> get props => [names, selectedName];
List<Object?> get props => [nameModels, selectedName];

NamesLoaded copyWith({
List<ANTRecord>? names,
ANTRecord? selectedName,
List<ArNSNameModel>? nameModels,
ArNSNameModel? selectedName,
}) {
return NamesLoaded(
names: names ?? this.names,
nameModels: nameModels ?? this.nameModels,
selectedName: selectedName ?? this.selectedName,
);
}
Expand All @@ -34,34 +34,34 @@ final class NamesLoaded extends AssignNameState {
final class AssignNameEmptyState extends AssignNameState {}

final class UndernamesLoaded extends AssignNameState {
final List<ANTRecord> names;
final ANTRecord selectedName;
final List<ArNSNameModel> nameModels;
final List<ARNSUndername> undernames;
final ARNSUndername? selectedUndername;
final ArNSNameModel? selectedName;

const UndernamesLoaded({
required this.names,
required this.nameModels,
required this.undernames,
required this.selectedUndername,
required this.selectedName,
});

@override
List<Object?> get props => [
names,
nameModels,
selectedName,
undernames,
selectedUndername,
];

UndernamesLoaded copyWith({
List<ANTRecord>? names,
ANTRecord? selectedName,
List<ARNSUndername>? undernames,
ARNSUndername? selectedUndername,
List<ArNSNameModel>? nameModels,
ArNSNameModel? selectedName,
}) {
return UndernamesLoaded(
names: names ?? this.names,
nameModels: nameModels ?? this.nameModels,
selectedName: selectedName ?? this.selectedName,
undernames: undernames ?? this.undernames,
selectedUndername: selectedUndername ?? this.selectedUndername,
Expand All @@ -80,7 +80,7 @@ final class NameAssignedWithSuccess extends AssignNameState {
}

final class SelectionConfirmed extends AssignNameState {
final ANTRecord selectedName;
final ArNSNameModel selectedName;
final ARNSUndername? selectedUndername;

const SelectionConfirmed({
Expand Down
49 changes: 42 additions & 7 deletions lib/arns/presentation/assign_name_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'package:ardrive/arns/domain/arns_repository.dart';
import 'package:ardrive/arns/presentation/assign_name_bloc/assign_name_bloc.dart';
import 'package:ardrive/arns/presentation/create_undername.dart';
import 'package:ardrive/authentication/ardrive_auth.dart';
import 'package:ardrive/blocs/blocs.dart';
import 'package:ardrive/blocs/drive_detail/drive_detail_cubit.dart';
Expand Down Expand Up @@ -188,9 +189,9 @@ class _AssignArNSNameModalState extends State<_AssignArNSNameModal> {
const SizedBox(
height: 16,
),
_NameSelectorDropdown<ANTRecord>(
_NameSelectorDropdown<ArNSNameModel>(
label: 'ArNS name',
names: state.names,
names: state.nameModels,
hintText: 'Choose ArNS name',
selectedName: state.selectedName,
onSelected: (name) {
Expand Down Expand Up @@ -238,10 +239,10 @@ class _AssignArNSNameModalState extends State<_AssignArNSNameModal> {
const SizedBox(
height: 16,
),
_NameSelectorDropdown<ANTRecord>(
_NameSelectorDropdown<ArNSNameModel>(
selectedName: state.selectedName,
label: 'ArNS name',
names: state.names,
names: state.nameModels,
hintText: 'Choose ArNS name',
onSelected: (name) {
context.read<AssignNameBloc>().add(SelectName(name));
Expand All @@ -264,6 +265,40 @@ class _AssignArNSNameModalState extends State<_AssignArNSNameModal> {
.add(SelectUndername(undername: name));
},
),
// or add a new name
if (state.selectedName != null) ...[
const SizedBox(height: 16),
Align(
alignment: Alignment.centerLeft,
child:
Text('or', style: typography.paragraphNormal()),
),
const SizedBox(height: 16),
ArDriveButtonNew(
text: state.selectedName!.records >=
state.selectedName!.undernameLimit
? 'You cant create more undernames ${state.selectedName!.records} of ${state.selectedName!.undernameLimit} in use'
: 'Add new undername ${state.selectedName!.records} of ${state.selectedName!.undernameLimit} in use',
onPressed: () {
showArDriveDialog(
context,
content: BlocProvider(
create: (context) =>
this.context.read<AssignNameBloc>(),
child: CreateUndernameModal(
nameModel: state.selectedName!,
driveId: widget.file!.driveId,
fileId: widget.file!.id,
transactionId: widget.file!.dataTxId,
),
),
);
},
typography: typography,
isDisabled: state.selectedName!.records >=
state.selectedName!.undernameLimit,
),
],
],
),
);
Expand Down Expand Up @@ -326,7 +361,7 @@ class _AssignArNSNameModalState extends State<_AssignArNSNameModal> {
ModalAction(
action: () {
Navigator.of(context).pop();
},
},
title: 'Cancel',
),
ModalAction(
Expand Down Expand Up @@ -547,8 +582,8 @@ class __NameSelectorDropdownState<T> extends State<_NameSelectorDropdown<T>> {
String _getName(T item) {
String name;

if (item is ANTRecord) {
name = item.domain;
if (item is ArNSNameModel) {
name = item.name;
} else if (item is ARNSUndername) {
name = item.name;
} else {
Expand Down
Loading
Loading