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-4676: Issues around the balance for snapshot creation #1385

Merged
merged 2 commits into from
Sep 27, 2023
Merged
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
50 changes: 26 additions & 24 deletions lib/blocs/create_snapshot/create_snapshot_cubit.dart
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the reviewer - This was making the button enabled state not to be updated.

Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class CreateSnapshotCubit extends Cubit<CreateSnapshotState> {
_computeIsSufficientBalance();
_computeIsTurboEnabled();
_computeIsFreeThanksToTurbo();
_computeIsButtonEnabled();

await _emitConfirming(
dataSize: data.length,
Expand Down Expand Up @@ -461,34 +462,35 @@ class CreateSnapshotCubit extends Cubit<CreateSnapshotState> {
logger.d('Upload method set to $method');
_uploadMethod = method;

_refreshIsButtonEnabled();
_computeIsButtonEnabled();
if (state is ConfirmingSnapshotCreation) {
final stateAsConfirming = state as ConfirmingSnapshotCreation;
emit(
stateAsConfirming.copyWith(
uploadMethod: method,
isButtonToUploadEnabled: _isButtonToUploadEnabled,
),
);
}
}

void _refreshIsButtonEnabled() {
void _computeIsButtonEnabled() {
_isButtonToUploadEnabled = false;
if (state is ConfirmingSnapshotCreation) {
final stateAsConfirming = state as ConfirmingSnapshotCreation;
logger.d('Sufficient Balance To Pay With AR: $_sufficientArBalance');

if (_uploadMethod == UploadMethod.ar && _sufficientArBalance) {
logger.d('Enabling button for AR payment method');
_isButtonToUploadEnabled = true;
} else if (_uploadMethod == UploadMethod.turbo &&
stateAsConfirming.isTurboUploadPossible &&
_sufficentCreditsBalance) {
logger.d('Enabling button for Turbo payment method');
_isButtonToUploadEnabled = true;
} else if (stateAsConfirming.isFreeThanksToTurbo) {
logger.d('Enabling button for free upload using Turbo');
_isButtonToUploadEnabled = true;
} else {
logger.d('Disabling button');
}

emit(stateAsConfirming.copyWith(
uploadMethod: _uploadMethod,
isButtonToUploadEnabled: _isButtonToUploadEnabled,
));
logger.d('Sufficient Balance To Pay With AR: $_sufficientArBalance');
if (_uploadMethod == UploadMethod.ar && _sufficientArBalance) {
logger.d('Enabling button for AR payment method');
_isButtonToUploadEnabled = true;
} else if (_uploadMethod == UploadMethod.turbo &&
_isTurboUploadPossible &&
_sufficentCreditsBalance) {
logger.d('Enabling button for Turbo payment method');
_isButtonToUploadEnabled = true;
} else if (_isFreeThanksToTurbo) {
logger.d('Enabling button for free upload using Turbo');
_isButtonToUploadEnabled = true;
} else {
logger.d('Disabling button');
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/blocs/upload/upload_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class UploadCubit extends Cubit<UploadState> {
logger.d('Disabling button');
}

emit((state as UploadReady).copyWith(
emit(uploadReady.copyWith(
uploadMethod: method,
isButtonToUploadEnabled: isButtonEnabled,
));
Expand Down
31 changes: 31 additions & 0 deletions lib/blocs/upload/upload_state.dart
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the reviewer - This was causing the state for Upload File to not detect when the upload method changed so the "insufficient message" wasn't being updated.

Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,42 @@ class UploadReady extends UploadState {
@override
List<Object?> get props => [
costEstimateAr,
costEstimateTurbo,
sufficientArBalance,
isZeroBalance,
sufficentCreditsBalance,
uploadIsPublic,
uploadPlanForAR,
uploadPlanForTurbo,
isTurboUploadPossible,
isFreeThanksToTurbo,
uploadSize,
credits,
arBalance,
turboCredits,
uploadMethod,
isButtonToUploadEnabled,
];

@override
toString() => 'UploadReady { '
'costEstimateAr: $costEstimateAr, '
'costEstimateTurbo: $costEstimateTurbo, '
'sufficientArBalance: $sufficientArBalance, '
'isZeroBalance: $isZeroBalance, '
'sufficentCreditsBalance: $sufficentCreditsBalance, '
'uploadIsPublic: $uploadIsPublic, '
'uploadPlanForAR: $uploadPlanForAR, '
'uploadPlanForTurbo: $uploadPlanForTurbo, '
'isTurboUploadPossible: $isTurboUploadPossible, '
'isFreeThanksToTurbo: $isFreeThanksToTurbo, '
'uploadSize: $uploadSize, '
'credits: $credits, '
'arBalance: $arBalance, '
'turboCredits: $turboCredits, '
'uploadMethod: $uploadMethod, '
'isButtonToUploadEnabled: $isButtonToUploadEnabled, '
'}';
}

class UploadInProgress extends UploadState {
Expand Down
5 changes: 5 additions & 0 deletions lib/components/create_snapshot_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ Widget _confirmDialog(
hasNoTurboBalance: state.hasNoTurboBalance,
isTurboUploadPossible: true,
arBalance: state.arBalance,
sufficientArBalance: state.sufficientBalanceToPayWithAr,
turboCredits: state.turboCredits,
sufficentCreditsBalance:
state.sufficientBalanceToPayWithTurbo,
isFreeThanksToTurbo: false,
onTurboTopupSucess: () {
createSnapshotCubit
.setUploadMethod(UploadMethod.turbo);
Expand Down Expand Up @@ -444,6 +448,7 @@ Widget _confirmDialog(
await createSnapshotCubit.confirmSnapshotCreation(),
},
title: appLocalizationsOf(context).uploadEmphasized,
isEnable: state.isButtonToUploadEnabled,
),
}
],
Expand Down
121 changes: 120 additions & 1 deletion lib/components/payment_method_selector_widget.dart
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the reviewer - Now this component is also having the "insufficient balance" message.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class PaymentMethodSelector extends StatelessWidget {
final bool hasNoTurboBalance;
final bool isTurboUploadPossible;
final String arBalance;
final bool sufficientArBalance;
final String turboCredits;
final bool sufficentCreditsBalance;
final bool isFreeThanksToTurbo;
final void Function() onTurboTopupSucess;
final void Function() onArSelect;
final void Function() onTurboSelect;
Expand All @@ -25,15 +28,26 @@ class PaymentMethodSelector extends StatelessWidget {
required this.hasNoTurboBalance,
required this.isTurboUploadPossible,
required this.arBalance,
required this.sufficientArBalance,
required this.turboCredits,
required this.sufficentCreditsBalance,
required this.isFreeThanksToTurbo,
required this.onTurboTopupSucess,
required this.onArSelect,
required this.onTurboSelect,
});

@override
Widget build(context) {
return _buildContent(context);
return Column(
children: [
if (!isFreeThanksToTurbo) ...[
_buildContent(context),
const SizedBox(height: 16),
_getInsufficientBalanceMessage(context: context),
],
],
);
}

Widget _buildContent(BuildContext context) {
Expand Down Expand Up @@ -146,4 +160,109 @@ class PaymentMethodSelector extends StatelessWidget {
],
);
}

Widget _getInsufficientBalanceMessage({
required BuildContext context,
}) {
if (uploadMethod == UploadMethod.turbo &&
!sufficentCreditsBalance &&
sufficientArBalance) {
return GestureDetector(
onTap: () {
showTurboTopupModal(context, onSuccess: () {
onTurboTopupSucess();
});
},
child: ArDriveClickArea(
child: Text.rich(
TextSpan(
text: 'Insufficient Credit balance for purchase. ',
style: ArDriveTypography.body.captionBold(
color:
ArDriveTheme.of(context).themeData.colors.themeErrorDefault,
),
children: [
TextSpan(
text: 'Add Credits',
style: ArDriveTypography.body
.captionBold(
color: ArDriveTheme.of(context)
.themeData
.colors
.themeErrorDefault,
)
.copyWith(decoration: TextDecoration.underline),
),
TextSpan(
text: ' to use Turbo.',
style: ArDriveTypography.body.captionBold(
color: ArDriveTheme.of(context)
.themeData
.colors
.themeErrorDefault,
),
),
],
),
),
),
);
} else if (uploadMethod == UploadMethod.ar && !sufficientArBalance) {
return Text(
'Insufficient AR balance for purchase.',
style: ArDriveTypography.body.captionBold(
color: ArDriveTheme.of(context).themeData.colors.themeErrorDefault,
),
);
} else if (!sufficentCreditsBalance && !sufficientArBalance) {
return GestureDetector(
onTap: () {
showTurboTopupModal(context, onSuccess: () {
onTurboTopupSucess();
});
},
child: ArDriveClickArea(
child: RichText(
text: TextSpan(
children: [
TextSpan(
text:
'Insufficient balance to pay for this upload. You can either',
style: ArDriveTypography.body.captionBold(
color: ArDriveTheme.of(context)
.themeData
.colors
.themeErrorDefault,
),
),
TextSpan(
text: ' add Turbo credits to your profile',
style: ArDriveTypography.body
.captionBold(
color: ArDriveTheme.of(context)
.themeData
.colors
.themeErrorDefault,
)
.copyWith(
decoration: TextDecoration.underline,
),
),
TextSpan(
text: ' or use AR',
style: ArDriveTypography.body.captionBold(
color: ArDriveTheme.of(context)
.themeData
.colors
.themeErrorDefault,
),
),
],
),
),
),
);
}
return const SizedBox();
}
}
Loading