Skip to content

Commit

Permalink
Merge pull request #1362 from ardriveapp/PE-3173
Browse files Browse the repository at this point in the history
PE-3173: Utilize Turbo Uploads for Quick Sync
  • Loading branch information
matibat authored Oct 4, 2023
2 parents d48285f + 2ab486d commit ce155d5
Show file tree
Hide file tree
Showing 21 changed files with 1,117 additions and 415 deletions.
305 changes: 259 additions & 46 deletions lib/blocs/create_snapshot/create_snapshot_cubit.dart

Large diffs are not rendered by default.

75 changes: 70 additions & 5 deletions lib/blocs/create_snapshot/create_snapshot_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,85 @@ class CreateSnapshotInsufficientBalance extends CreateSnapshotState {

class ConfirmingSnapshotCreation extends CreateSnapshotState {
final int snapshotSize;
final String arUploadCost;
final double? usdUploadCost;

final UploadCostEstimate costEstimateAr;
final UploadCostEstimate? costEstimateTurbo;
final bool hasNoTurboBalance;
final bool isTurboUploadPossible;
final String arBalance;
final String turboCredits;
final UploadMethod uploadMethod;
final bool isButtonToUploadEnabled;
final bool sufficientBalanceToPayWithAr;
final bool sufficientBalanceToPayWithTurbo;
final bool isFreeThanksToTurbo;

ConfirmingSnapshotCreation({
required this.snapshotSize,
required this.arUploadCost,
required this.usdUploadCost,
required this.costEstimateAr,
required this.costEstimateTurbo,
required this.hasNoTurboBalance,
required this.isTurboUploadPossible,
required this.arBalance,
required this.turboCredits,
required this.uploadMethod,
required this.isButtonToUploadEnabled,
required this.sufficientBalanceToPayWithAr,
required this.sufficientBalanceToPayWithTurbo,
required this.isFreeThanksToTurbo,
});

@override
List<Object> get props => [
snapshotSize,
arUploadCost,
costEstimateAr,
costEstimateTurbo ?? '',
hasNoTurboBalance,
isTurboUploadPossible,
arBalance,
turboCredits,
uploadMethod,
isButtonToUploadEnabled,
sufficientBalanceToPayWithAr,
sufficientBalanceToPayWithTurbo,
isFreeThanksToTurbo,
];

ConfirmingSnapshotCreation copyWith({
int? snapshotSize,
String? arUploadCost,
double? usdUploadCost,
UploadCostEstimate? costEstimateAr,
UploadCostEstimate? costEstimateTurbo,
bool? hasNoTurboBalance,
bool? isTurboUploadPossible,
String? arBalance,
String? turboCredits,
UploadMethod? uploadMethod,
bool? isButtonToUploadEnabled,
bool? sufficientBalanceToPayWithAr,
bool? sufficientBalanceToPayWithTurbo,
bool? isFreeThanksToTurbo,
}) {
return ConfirmingSnapshotCreation(
snapshotSize: snapshotSize ?? this.snapshotSize,
costEstimateAr: costEstimateAr ?? this.costEstimateAr,
costEstimateTurbo: costEstimateTurbo ?? this.costEstimateTurbo,
hasNoTurboBalance: hasNoTurboBalance ?? this.hasNoTurboBalance,
isTurboUploadPossible:
isTurboUploadPossible ?? this.isTurboUploadPossible,
arBalance: arBalance ?? this.arBalance,
turboCredits: turboCredits ?? this.turboCredits,
uploadMethod: uploadMethod ?? this.uploadMethod,
isButtonToUploadEnabled:
isButtonToUploadEnabled ?? this.isButtonToUploadEnabled,
sufficientBalanceToPayWithAr:
sufficientBalanceToPayWithAr ?? this.sufficientBalanceToPayWithAr,
sufficientBalanceToPayWithTurbo: sufficientBalanceToPayWithTurbo ??
this.sufficientBalanceToPayWithTurbo,
isFreeThanksToTurbo: isFreeThanksToTurbo ?? this.isFreeThanksToTurbo,
);
}
}

class UploadingSnapshot extends CreateSnapshotState {}
Expand Down
6 changes: 4 additions & 2 deletions lib/blocs/upload/upload_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ class UploadCubit extends Cubit<UploadState> {
logger.d('Disabling button');
}

emit((state as UploadReady).copyWith(
uploadMethod: method, isButtonToUploadEnabled: isButtonEnabled));
emit(uploadReady.copyWith(
uploadMethod: method,
isButtonToUploadEnabled: isButtonEnabled,
));
}
}

Expand Down
31 changes: 31 additions & 0 deletions lib/blocs/upload/upload_state.dart
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
83 changes: 53 additions & 30 deletions lib/components/create_snapshot_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import 'package:ardrive/authentication/ardrive_auth.dart';
import 'package:ardrive/blocs/blocs.dart';
import 'package:ardrive/blocs/create_snapshot/create_snapshot_cubit.dart';
import 'package:ardrive/blocs/profile/profile_cubit.dart';
import 'package:ardrive/components/components.dart';
import 'package:ardrive/components/payment_method_selector_widget.dart';
import 'package:ardrive/entities/string_types.dart';
import 'package:ardrive/models/models.dart';
import 'package:ardrive/services/arweave/arweave.dart';
import 'package:ardrive/services/config/config.dart';
import 'package:ardrive/services/pst/pst.dart';
import 'package:ardrive/theme/theme.dart';
import 'package:ardrive/turbo/services/payment_service.dart';
import 'package:ardrive/turbo/services/upload_service.dart';
import 'package:ardrive/turbo/turbo.dart';
import 'package:ardrive/utils/app_localizations_wrapper.dart';
import 'package:ardrive/utils/filesize.dart';
import 'package:ardrive/utils/html/html_util.dart';
import 'package:ardrive/utils/logger/logger.dart';
import 'package:ardrive/utils/show_general_dialog.dart';
import 'package:ardrive/utils/split_localizations.dart';
import 'package:ardrive_ui/ardrive_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../utils/usd_upload_cost_to_string.dart';

Future<void> promptToCreateSnapshot(
BuildContext context,
Drive drive,
) async {
return showArDriveDialog(
return showAnimatedDialog(
context,
barrierDismissible: false,
content: BlocProvider(
Expand All @@ -32,6 +35,13 @@ Future<void> promptToCreateSnapshot(
profileCubit: context.read<ProfileCubit>(),
pst: context.read<PstService>(),
tabVisibility: TabVisibilitySingleton(),
auth: context.read<ArDriveAuth>(),
paymentService: context.read<PaymentService>(),
turboBalanceRetriever: TurboBalanceRetriever(
paymentService: context.read<PaymentService>(),
),
configService: context.read<ConfigService>(),
turboService: context.read<TurboUploadService>(),
),
child: CreateSnapshotDialog(
drive: drive,
Expand Down Expand Up @@ -365,31 +375,6 @@ Widget _confirmDialog(
),
style: ArDriveTypography.body.buttonNormalRegular(),
),
const Divider(),
const SizedBox(height: 16),
Text.rich(
TextSpan(
children: [
TextSpan(
text: appLocalizationsOf(context).cost(
state.arUploadCost,
),
),
if (state.usdUploadCost != null)
TextSpan(
text: usdUploadCostToString(
state.usdUploadCost!,
),
)
else
TextSpan(
text:
' ${appLocalizationsOf(context).usdPriceNotAvailable}',
),
],
style: ArDriveTypography.body.buttonNormalRegular(),
),
),
Text.rich(
TextSpan(
children: [
Expand All @@ -402,6 +387,43 @@ Widget _confirmDialog(
style: ArDriveTypography.body.buttonNormalRegular(),
),
),
const Divider(),
const SizedBox(height: 16),
if (state.isFreeThanksToTurbo) ...{
Text(
appLocalizationsOf(context).freeTurboTransaction,
style: ArDriveTypography.body.buttonNormalRegular(
color: ArDriveTheme.of(context)
.themeData
.colors
.themeFgDefault,
),
),
} else ...{
PaymentMethodSelector(
uploadMethod: state.uploadMethod,
costEstimateTurbo: state.costEstimateTurbo,
costEstimateAr: state.costEstimateAr,
hasNoTurboBalance: state.hasNoTurboBalance,
isTurboUploadPossible: true,
arBalance: state.arBalance,
sufficientArBalance: state.sufficientBalanceToPayWithAr,
turboCredits: state.turboCredits,
sufficentCreditsBalance:
state.sufficientBalanceToPayWithTurbo,
isFreeThanksToTurbo: false,
onTurboTopupSucess: () {
createSnapshotCubit.refreshTurboBalance();
},
onArSelect: () {
createSnapshotCubit.setUploadMethod(UploadMethod.ar);
},
onTurboSelect: () {
createSnapshotCubit
.setUploadMethod(UploadMethod.turbo);
},
),
}
],
),
),
Expand All @@ -425,6 +447,7 @@ Widget _confirmDialog(
await createSnapshotCubit.confirmSnapshotCreation(),
},
title: appLocalizationsOf(context).uploadEmphasized,
isEnable: state.isButtonToUploadEnabled,
),
}
],
Expand Down
11 changes: 3 additions & 8 deletions lib/components/new_button/new_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ class NewButton extends StatelessWidget {
icon: ArDriveIcons.iconAttachDrive(size: defaultIconSize),
),
if (driveDetailState is DriveDetailLoadSuccess && drive != null) ...[
if (driveDetailState.currentDrive.privacy == 'public' &&
drive != null)
if (driveDetailState.currentDrive.privacy == 'public')
ArDriveNewButtonItem(
onClick: () {
promptToCreateManifest(
Expand All @@ -319,8 +318,7 @@ class NewButton extends StatelessWidget {
name: appLocalizations.createManifest,
icon: ArDriveIcons.tournament(size: defaultIconSize),
),
if (context.read<ConfigService>().config.enableQuickSyncAuthoring &&
drive != null)
if (context.read<ConfigService>().config.enableQuickSyncAuthoring)
ArDriveNewButtonItem(
onClick: () {
promptToCreateSnapshot(
Expand All @@ -329,10 +327,7 @@ class NewButton extends StatelessWidget {
);
},
isDisabled: !driveDetailState.hasWritePermissions ||
driveDetailState.driveIsEmpty ||
!profile.hasMinimumBalanceForUpload(
minimumWalletBalance: minimumWalletBalance,
),
driveDetailState.driveIsEmpty,
name: appLocalizations.createSnapshot,
icon: ArDriveIcons.iconCreateSnapshot(size: defaultIconSize),
),
Expand Down
Loading

0 comments on commit ce155d5

Please sign in to comment.