Skip to content

Commit

Permalink
Merge pull request #1848 from ardriveapp/PE-6730
Browse files Browse the repository at this point in the history
PE-6730: Decouple arns loading from the main upload flow
  • Loading branch information
thiagocarvalhodev authored Sep 12, 2024
2 parents c1e73f8 + 071a240 commit e3f5ff6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/arns/domain/arns_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class _ARNSRepository implements ARNSRepository {
} catch (e) {
logger.e('Error getting ANT records for wallet: $e');

_getARNSUndernamesCompleter!.complete([]);
_getARNSUndernamesCompleter!.completeError(e);

_getARNSUndernamesCompleter = null;

Expand Down
70 changes: 53 additions & 17 deletions lib/blocs/upload/upload_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,60 @@ class UploadCubit extends Cubit<UploadState> {
isNextButtonEnabled: canUpload,
));
} else if (state is UploadReadyToPrepare) {
final hasUndernames = (await _arnsRepository
.getAntRecordsForWallet(_auth.currentUser.walletAddress))
.isNotEmpty;
bool showArnsCheckbox = false;

emit(
UploadReady(
params: (state as UploadReadyToPrepare).params,
paymentInfo: paymentInfo,
numberOfFiles: files.length,
uploadIsPublic: !_targetDrive.isPrivate,
isDragNDrop: isDragNDrop,
isNextButtonEnabled: canUpload,
isArConnect: (state as UploadReadyToPrepare).isArConnect,
showArnsCheckbox:
hasUndernames && files.length == 1 && _targetDrive.isPublic,
showArnsNameSelection: false,
),
);
if (_targetDrive.isPublic && files.length == 1) {
emit(
UploadReady(
params: (state as UploadReadyToPrepare).params,
paymentInfo: paymentInfo,
numberOfFiles: files.length,
uploadIsPublic: !_targetDrive.isPrivate,
isDragNDrop: isDragNDrop,
isNextButtonEnabled: canUpload,
isArConnect: (state as UploadReadyToPrepare).isArConnect,
showArnsCheckbox: showArnsCheckbox,
showArnsNameSelection: false,
loadingArNSNames: true,
),
);

try {
final hasUndernames = (await _arnsRepository
.getAntRecordsForWallet(_auth.currentUser.walletAddress))
.isNotEmpty;

showArnsCheckbox = hasUndernames;

if (state is! UploadReady) {
logger.d('State is not UploadReady');
return;
}

final readyState = state as UploadReady;

emit(readyState.copyWith(
loadingArNSNames: false, showArnsCheckbox: showArnsCheckbox));
} catch (e) {
final readyState = state as UploadReady;
emit(readyState.copyWith(
loadingArNSNamesError: true, loadingArNSNames: false));
}
} else {
emit(
UploadReady(
params: (state as UploadReadyToPrepare).params,
paymentInfo: paymentInfo,
numberOfFiles: files.length,
uploadIsPublic: !_targetDrive.isPrivate,
isDragNDrop: isDragNDrop,
isNextButtonEnabled: canUpload,
isArConnect: (state as UploadReadyToPrepare).isArConnect,
showArnsCheckbox: showArnsCheckbox,
showArnsNameSelection: false,
),
);
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/blocs/upload/upload_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class UploadReady extends UploadState {
final UploadParams params;
final bool showArnsCheckbox;
final bool showArnsNameSelection;
final bool loadingArNSNames;
final bool loadingArNSNamesError;

final bool isArConnect;

Expand All @@ -108,6 +110,8 @@ class UploadReady extends UploadState {
required this.isArConnect,
required this.showArnsCheckbox,
required this.showArnsNameSelection,
this.loadingArNSNames = false,
this.loadingArNSNamesError = false,
});

// copyWith
Expand All @@ -123,8 +127,11 @@ class UploadReady extends UploadState {
bool? showSettings,
bool? showArnsCheckbox,
bool? showArnsNameSelection,
bool? loadingArNSNames,
bool? loadingArNSNamesError,
}) {
return UploadReady(
loadingArNSNames: loadingArNSNames ?? this.loadingArNSNames,
isArConnect: isArConnect ?? this.isArConnect,
uploadIsPublic: uploadIsPublic ?? this.uploadIsPublic,
isDragNDrop: isDragNDrop ?? this.isDragNDrop,
Expand All @@ -135,6 +142,8 @@ class UploadReady extends UploadState {
showArnsCheckbox: showArnsCheckbox ?? this.showArnsCheckbox,
showArnsNameSelection:
showArnsNameSelection ?? this.showArnsNameSelection,
loadingArNSNamesError:
loadingArNSNamesError ?? this.loadingArNSNamesError,
);
}

Expand All @@ -143,6 +152,9 @@ class UploadReady extends UploadState {
paymentInfo,
isNextButtonEnabled,
showArnsNameSelection,
loadingArNSNamesError,
loadingArNSNames,
showArnsCheckbox,
];

@override
Expand Down
39 changes: 38 additions & 1 deletion lib/components/upload_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,44 @@ class _UploadFormState extends State<UploadForm> {
],
),
),
if (state.showArnsCheckbox)
if (state.loadingArNSNamesError)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
Text(
'Error loading ARNS names',
style: typography.paragraphLarge(
fontWeight: ArFontWeight.semiBold,
color: colorTokens.textRed,
),
),
],
),
),
if (state.loadingArNSNames)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
Text(
'Loading ArNS names...',
style: typography.paragraphLarge(
fontWeight: ArFontWeight.semiBold,
),
),
const SizedBox(width: 8),
const SizedBox(
height: 16,
width: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
),
)
],
),
),
if (state.showArnsCheckbox && !state.loadingArNSNames)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
Expand Down

0 comments on commit e3f5ff6

Please sign in to comment.