ente/lib/ui/growth/referral_screen.dart

300 lines
10 KiB
Dart
Raw Normal View History

import "package:flutter/material.dart";
2023-04-05 07:03:45 +00:00
import "package:photos/generated/l10n.dart";
2023-02-15 16:47:40 +00:00
import "package:photos/models/api/storage_bonus/storage_bonus.dart";
import "package:photos/models/user_details.dart";
import "package:photos/services/storage_bonus_service.dart";
import "package:photos/services/user_service.dart";
import "package:photos/theme/ente_theme.dart";
2023-02-15 16:47:40 +00:00
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/common/web_page.dart";
import 'package:photos/ui/components/buttons/icon_button_widget.dart';
import "package:photos/ui/components/captioned_text_widget.dart";
import "package:photos/ui/components/divider_widget.dart";
import "package:photos/ui/components/menu_item_widget/menu_item_widget.dart";
import "package:photos/ui/components/title_bar_title_widget.dart";
import "package:photos/ui/components/title_bar_widget.dart";
import "package:photos/ui/growth/apply_code_screen.dart";
import "package:photos/ui/growth/referral_code_widget.dart";
2023-02-15 16:47:40 +00:00
import "package:photos/ui/growth/storage_details_screen.dart";
import "package:photos/utils/data_util.dart";
import "package:photos/utils/navigation_util.dart";
2023-02-15 16:51:32 +00:00
import "package:photos/utils/share_util.dart";
2023-03-03 05:23:58 +00:00
import "package:tuple/tuple.dart";
class ReferralScreen extends StatefulWidget {
const ReferralScreen({super.key});
@override
State<ReferralScreen> createState() => _ReferralScreenState();
}
class _ReferralScreenState extends State<ReferralScreen> {
bool canApplyCode = true;
2023-03-03 05:11:15 +00:00
2023-02-16 10:12:20 +00:00
void _safeUIUpdate() {
if (mounted) {
setState(() => {});
}
}
2023-02-15 16:47:40 +00:00
2023-03-03 05:23:58 +00:00
Future<Tuple2<ReferralView, UserDetails>> _fetchData() async {
UserDetails? cachedUserDetails =
UserService.instance.getCachedUserDetails();
cachedUserDetails ??=
await UserService.instance.getUserDetailsV2(memoryCount: false);
final referralView =
await StorageBonusService.instance.getGateway().getReferralView();
2023-03-16 04:43:32 +00:00
return Tuple2(referralView, cachedUserDetails);
2023-03-03 05:23:58 +00:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
primary: false,
slivers: <Widget>[
TitleBarWidget(
2023-04-05 07:03:45 +00:00
flexibleSpaceTitle: TitleBarTitleWidget(
title: S.of(context).claimFreeStorage,
),
2023-04-05 07:03:45 +00:00
flexibleSpaceCaption: S.of(context).inviteYourFriends,
actionIcons: [
IconButtonWidget(
icon: Icons.close_outlined,
iconButtonType: IconButtonType.secondary,
onTap: () {
Navigator.pop(context);
Navigator.pop(context);
},
),
],
),
SliverList(
delegate: SliverChildBuilderDelegate(
(delegateBuildContext, index) {
return Padding(
2023-02-15 16:47:40 +00:00
padding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
2023-03-03 05:23:58 +00:00
child: FutureBuilder<Tuple2<ReferralView, UserDetails>>(
future: _fetchData(),
2023-02-15 16:47:40 +00:00
builder: (context, snapshot) {
if (snapshot.hasData) {
return ReferralWidget(
2023-03-03 05:23:58 +00:00
referralView: snapshot.data!.item1,
userDetails: snapshot.data!.item2,
notifyParent: _safeUIUpdate,
2023-02-15 16:47:40 +00:00
);
2023-02-16 07:56:08 +00:00
} else if (snapshot.hasError) {
2023-04-05 07:03:45 +00:00
return Center(
2023-02-16 07:56:08 +00:00
child: Padding(
2023-04-05 07:03:45 +00:00
padding: const EdgeInsets.all(24.0),
2023-02-16 07:56:08 +00:00
child: Text(
2023-04-05 07:03:45 +00:00
S.of(context).failedToFetchReferralDetails,
2023-02-16 07:56:08 +00:00
),
),
);
}
{
2023-02-15 16:47:40 +00:00
return const EnteLoadingWidget();
}
},
),
);
},
childCount: 1,
),
),
],
),
);
}
}
2023-02-15 16:47:40 +00:00
class ReferralWidget extends StatelessWidget {
final ReferralView referralView;
final UserDetails userDetails;
2023-02-16 10:12:20 +00:00
final Function notifyParent;
2023-02-15 16:47:40 +00:00
2023-03-03 05:23:58 +00:00
const ReferralWidget({
required this.referralView,
required this.userDetails,
required this.notifyParent,
2023-02-16 10:12:20 +00:00
super.key,
});
2023-02-15 16:47:40 +00:00
@override
Widget build(BuildContext context) {
final colorScheme = getEnteColorScheme(context);
final textStyle = getEnteTextTheme(context);
final bool isReferralEnabled = referralView.planInfo.isEnabled;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Container with 8 border radius and red color
isReferralEnabled
2023-02-15 16:51:32 +00:00
? InkWell(
onTap: () {
shareText(
2023-04-05 07:03:45 +00:00
S.of(context).shareTextReferralCode(
referralView.code,
referralView.planInfo.storageInGB,
),
2023-02-23 03:16:11 +00:00
);
2023-02-15 16:51:32 +00:00
},
child: Container(
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(
color: colorScheme.strokeFaint,
width: 1,
),
borderRadius: BorderRadius.circular(8),
2023-02-15 16:47:40 +00:00
),
2023-02-15 16:51:32 +00:00
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 12,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2023-04-05 07:03:45 +00:00
Text(
S.of(context).referralStep1,
2023-02-15 16:51:32 +00:00
),
const SizedBox(height: 12),
ReferralCodeWidget(referralView.code),
2023-02-15 16:51:32 +00:00
const SizedBox(height: 12),
2023-04-05 07:03:45 +00:00
Text(
S.of(context).referralStep2,
2023-02-15 16:51:32 +00:00
),
const SizedBox(height: 12),
Text(
2023-04-05 07:03:45 +00:00
S
.of(context)
.referralStep3(referralView.planInfo.storageInGB),
2023-02-15 16:51:32 +00:00
),
],
),
2023-02-15 16:47:40 +00:00
),
),
)
: Padding(
padding: const EdgeInsets.symmetric(vertical: 48),
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
color: colorScheme.strokeMuted,
),
const SizedBox(height: 12),
2023-02-23 03:16:11 +00:00
Text(
2023-04-05 07:03:45 +00:00
S.of(context).referralsAreCurrentlyPaused,
2023-02-23 03:16:11 +00:00
style: textStyle.small
.copyWith(color: colorScheme.textFaint),
),
2023-02-15 16:47:40 +00:00
],
),
),
),
const SizedBox(height: 4),
isReferralEnabled
? Text(
2023-04-05 07:03:45 +00:00
S.of(context).youCanAtMaxDoubleYourStorage,
2023-02-15 16:47:40 +00:00
style: textStyle.mini.copyWith(
color: colorScheme.textMuted,
),
textAlign: TextAlign.left,
)
: const SizedBox.shrink(),
const SizedBox(height: 24),
referralView.enableApplyCode
? MenuItemWidget(
2023-04-05 07:03:45 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).applyCodeTitle,
2023-02-15 16:47:40 +00:00
),
menuItemColor: colorScheme.fillFaint,
trailingWidget: Icon(
Icons.chevron_right_outlined,
color: colorScheme.strokeBase,
),
singleBorderRadius: 8,
alignCaptionedTextToLeft: true,
isBottomBorderRadiusRemoved: true,
onTap: () async {
2023-02-16 10:12:20 +00:00
await routeToPage(
2023-02-15 16:47:40 +00:00
context,
ApplyCodeScreen(referralView, userDetails),
2023-02-15 16:47:40 +00:00
);
2023-02-16 10:12:20 +00:00
notifyParent();
2023-02-15 16:47:40 +00:00
},
)
: const SizedBox.shrink(),
referralView.enableApplyCode
? DividerWidget(
dividerType: DividerType.menu,
bgColor: colorScheme.fillFaint,
)
: const SizedBox.shrink(),
MenuItemWidget(
2023-04-05 07:03:45 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).faq,
2023-02-15 16:47:40 +00:00
),
menuItemColor: colorScheme.fillFaint,
trailingWidget: Icon(
Icons.chevron_right_outlined,
color: colorScheme.strokeBase,
),
singleBorderRadius: 8,
isTopBorderRadiusRemoved: referralView.enableApplyCode,
alignCaptionedTextToLeft: true,
onTap: () async {
2023-02-18 04:00:11 +00:00
routeToPage(
2023-02-23 03:16:11 +00:00
context,
2023-04-05 07:03:45 +00:00
WebPage(
S.of(context).faq,
2023-02-23 03:16:11 +00:00
"https://ente.io/faq/general/referral-program",
),
);
2023-02-15 16:47:40 +00:00
},
),
const SizedBox(height: 24),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0,
vertical: 6.0,
),
child: Text(
2023-04-05 07:03:45 +00:00
S.of(context).claimedStorageSoFar(
2023-04-05 07:50:02 +00:00
referralView.isFamilyMember.toString().toLowerCase(),
2023-04-05 07:03:45 +00:00
convertBytesToAbsoluteGBs(referralView.claimedStorage),
),
2023-02-15 16:47:40 +00:00
style: textStyle.small.copyWith(
color: colorScheme.textMuted,
),
),
),
MenuItemWidget(
2023-04-05 07:03:45 +00:00
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).details,
2023-02-15 16:47:40 +00:00
),
menuItemColor: colorScheme.fillFaint,
trailingWidget: Icon(
Icons.chevron_right_outlined,
color: colorScheme.strokeBase,
),
singleBorderRadius: 8,
alignCaptionedTextToLeft: true,
onTap: () async {
routeToPage(
context,
StorageDetailsScreen(referralView, userDetails),
);
},
),
],
);
}
}