Skip to content

Commit

Permalink
Add option to claim a custom referral code (#2794)
Browse files Browse the repository at this point in the history
  • Loading branch information
ua741 authored Aug 21, 2024
2 parents 0e63126 + a43b970 commit 487591f
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 40 deletions.
9 changes: 9 additions & 0 deletions mobile/lib/gateways/storage_bonus_gw.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class StorageBonusGateway {
return _enteDio.post("/storage-bonus/referral-claim?code=$code");
}

Future<void> updateCode(String code) {
return _enteDio.post(
"/storage-bonus/change-code?code=$code",
data: {
"code": code,
},
);
}

Future<BonusDetails> getBonusDetails() async {
final response = await _enteDio.get("/storage-bonus/details");
return BonusDetails.fromJson(response.data);
Expand Down
9 changes: 8 additions & 1 deletion mobile/lib/generated/intl/messages_en.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 42 additions & 2 deletions mobile/lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions mobile/lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@
"failedToApplyCode": "Failed to apply code",
"enterReferralCode": "Enter referral code",
"codeAppliedPageTitle": "Code applied",
"changeYourReferralCode": "Change your referral code",
"change": "Change",
"unavailableReferralCode": "Sorry, this code is unavailable.",
"codeChangeLimitReached": "Sorry, you've reached the limit of code changes.",
"storageInGB": "{storageAmountInGB} GB",
"claimed": "Claimed",
"@claimed": {
Expand Down
81 changes: 75 additions & 6 deletions mobile/lib/ui/growth/referral_code_widget.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import "package:dio/dio.dart";
import "package:dotted_border/dotted_border.dart";
import "package:flutter/material.dart";
import "package:logging/logging.dart";
import "package:photos/generated/l10n.dart";
import "package:photos/services/storage_bonus_service.dart";
import "package:photos/theme/ente_theme.dart";
import "package:photos/utils/dialog_util.dart";

// Figma: https://www.figma.com/file/SYtMyLBs5SAOkTbfMMzhqt/ente-Visual-Design?node-id=11219%3A62974&t=BRCLJhxXP11Q3Wyw-0
class ReferralCodeWidget extends StatelessWidget {
final String codeValue;
final bool shouldAllowEdit;
final Function? notifyParent;

const ReferralCodeWidget(this.codeValue, {Key? key}) : super(key: key);
const ReferralCodeWidget(
this.codeValue, {
this.shouldAllowEdit = false,
this.notifyParent,
super.key,
});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -37,16 +49,73 @@ class ReferralCodeWidget extends StatelessWidget {
),
),
const SizedBox(width: 12),
Icon(
Icons.adaptive.share,
size: 22,
color: colorScheme.strokeMuted,
),
shouldAllowEdit
? GestureDetector(
onTap: () {
showUpdateReferralCodeDialog(context);
},
child: Icon(
Icons.edit,
size: 22,
color: colorScheme.strokeMuted,
),
)
: const SizedBox.shrink(),
],
),
),
),
),
);
}

Future<void> showUpdateReferralCodeDialog(BuildContext context) async {
final result = await showTextInputDialog(
context,
title: S.of(context).changeYourReferralCode,
submitButtonLabel: S.of(context).change,
hintText: S.of(context).enterCode,
alwaysShowSuccessState: true,
initialValue: codeValue,
textCapitalization: TextCapitalization.characters,
onSubmit: (String text) async {
// indicates user cancelled the request
if (text == "" || text.trim() == codeValue) {
return;
}

try {
await StorageBonusService.instance
.getGateway()
.updateCode(text.trim().toUpperCase());
notifyParent?.call();
} catch (e, s) {
Logger("ReferralCodeWidget").severe("Failed to update code", e, s);
if (e is DioError) {
if (e.response?.statusCode == 400) {
await showInfoDialog(
context,
title: S.of(context).error,
body: S.of(context).unavailableReferralCode,
icon: Icons.error,
);
return;
} else if (e.response?.statusCode == 429) {
await showInfoDialog(
context,
title: S.of(context).error,
body: S.of(context).codeChangeLimitReached,
icon: Icons.error,
);
return;
}
}
rethrow;
}
},
);
if (result is Exception) {
await showGenericErrorDialog(context: context, error: result);
}
}
}
78 changes: 47 additions & 31 deletions mobile/lib/ui/growth/referral_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,41 +141,57 @@ class ReferralWidget extends StatelessWidget {
),
);
},
child: Container(
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(
color: colorScheme.strokeFaint,
width: 1,
),
borderRadius: BorderRadius.circular(8),
),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 12,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
S.of(context).referralStep1,
child: Stack(
children: [
Container(
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(
color: colorScheme.strokeFaint,
width: 1,
),
const SizedBox(height: 12),
ReferralCodeWidget(referralView.code),
const SizedBox(height: 12),
Text(
S.of(context).referralStep2,
borderRadius: BorderRadius.circular(8),
),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 12,
),
const SizedBox(height: 12),
Text(
S
.of(context)
.referralStep3(referralView.planInfo.storageInGB),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
S.of(context).referralStep1,
),
const SizedBox(height: 12),
ReferralCodeWidget(
referralView.code,
shouldAllowEdit: true,
notifyParent: notifyParent,
),
const SizedBox(height: 12),
Text(
S.of(context).referralStep2,
),
const SizedBox(height: 12),
Text(
S.of(context).referralStep3(
referralView.planInfo.storageInGB,
),
),
],
),
],
),
),
),
Positioned(
right: 8,
top: 8,
child: Icon(
Icons.adaptive.share,
color: colorScheme.blurStrokePressed,
),
),
],
),
)
: Padding(
Expand Down

0 comments on commit 487591f

Please sign in to comment.