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

feat: Nag user to share referral code #2395

Merged
merged 3 commits into from
Apr 11, 2024
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
Binary file added mobile/assets/referral_10101_laser_eyes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion mobile/lib/common/settings/user_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class _UserSettingsState extends State<UserSettings> {
final referralTier = snapshot.data!.referralTier;
final numberOfActivatedReferrals = snapshot.data!.numberOfActivatedReferrals;
final numberOfTotalReferrals = snapshot.data!.numberOfTotalReferrals;
final referralFeeBonus = snapshot.data!.referralFeeBonus.toStringAsFixed(2);
final referralFeeBonus =
(snapshot.data!.referralFeeBonus * 100.0).toStringAsFixed(0);

String referralTierName = "None";
switch (bonusStatusType) {
Expand Down
292 changes: 292 additions & 0 deletions mobile/lib/features/wallet/referral_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_10101/common/color.dart';
import 'package:get_10101/logger/logger.dart';
import 'package:get_10101/util/preferences.dart';
import 'package:path_provider/path_provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:screenshot/screenshot.dart';
import 'package:share_plus/share_plus.dart';
import 'package:get_10101/ffi.dart' as rust;

class ShareReferralWidget extends StatefulWidget {
const ShareReferralWidget({
super.key,
required this.screenShotController,
required this.pref,
});

final ScreenshotController screenShotController;
final Preferences pref;

@override
State<ShareReferralWidget> createState() => _ShareReferralWidgetState();
}

class _ShareReferralWidgetState extends State<ShareReferralWidget> {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: rust.api.referralStatus(),
builder: (BuildContext context, AsyncSnapshot<rust.ReferralStatus> snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}

final referralStatus = snapshot.data!;

return AlertDialog(
title: const Text(
"Enjoying 10101?",
textAlign: TextAlign.center,
),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Center(
child: RichText(
textAlign: TextAlign.center,
text: const TextSpan(
text: "Invite your friends to join ",
children: [
TextSpan(
text: "10101",
style: TextStyle(fontWeight: FontWeight.bold, color: tenTenOnePurple),
children: [
TextSpan(
text: " and we’ll reduce your order matching fee.",
style:
TextStyle(fontWeight: FontWeight.normal, color: Colors.black))
])
],
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
),
const SizedBox(
height: 10,
),
const Text("Your referral code is "),
const SizedBox(
height: 10,
),
Stack(
children: [
SizedBox(
height: 50,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RichText(
text: TextSpan(
text: referralStatus.referralCode,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: tenTenOnePurple,
fontSize: 18,
),
recognizer: TapGestureRecognizer()
..onTap = () {
Clipboard.setData(
ClipboardData(text: referralStatus.referralCode));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Copied to clipboard'),
),
);
}),
),
],
),
),
),
SizedBox(
height: 50,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
IconButton(
onPressed: () {
Share.share(
"Join me and trade without counter-party risk. Use this referral to get a fee discount: ${referralStatus.referralCode}");
},
icon: const Icon(Icons.share, size: 18))
],
),
),
),
],
),
Screenshot(
controller: widget.screenShotController,
child: FittedBox(
fit: BoxFit.scaleDown,
child: SizedBox(
width: 600,
height: 800,
child: ReferralWidget(
referralStatus: referralStatus,
),
),
))
],
),
actions: <Widget>[
TextButton(
onPressed: () {
widget.pref.storeDontShowReferralDialogFor7Days();
Navigator.pop(context, 'Cancel');
},
child: const Text(
'Not now',
style: TextStyle(
decoration: TextDecoration.underline,
),
),
),
ElevatedButton(
child: const Text("Share"),
onPressed: () async {
await widget.screenShotController
.capture(delay: const Duration(milliseconds: 10))
.then((image) async {
if (image != null) {
final directory = await getApplicationDocumentsDirectory();
final imagePath = await File('${directory.path}/join-the-future.png').create();
await imagePath.writeAsBytes(image);
await Share.shareXFiles(
[XFile(imagePath.path, mimeType: "image/x-png", bytes: image)]);
}
}).catchError((error) {
logger.e("Failed at capturing screenshot", error: error);
}).whenComplete(() {
widget.pref.storeDontShowReferralDialogFor7Days();
Navigator.of(context).pop(); // Close the dialog
});
},
)
],
);
},
);
}
}

class ReferralWidget extends StatelessWidget {
final rust.ReferralStatus referralStatus;

const ReferralWidget({
Key? key,
required this.referralStatus,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
SizedBox(
width: 600,
height: 800,
child: ClipRRect(
borderRadius: BorderRadius.circular(30.0),
child: Image.asset("assets/referral_10101_laser_eyes.png"),
),
),
Align(
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: SizedBox(
width: 150,
height: 150,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
decoration: const BoxDecoration(
color: Colors.white,
),
child: QrImageView(
data:
"https://referral.10101.finance?referral=${referralStatus.referralCode}",
eyeStyle: const QrEyeStyle(
eyeShape: QrEyeShape.square,
color: Colors.black,
),
dataModuleStyle: const QrDataModuleStyle(
dataModuleShape: QrDataModuleShape.square,
color: Colors.black,
),
version: QrVersions.auto,
padding: const EdgeInsets.all(1),
)),
)),
),
),
Container(
alignment: Alignment.center,
child: Column(
children: [
const Spacer(flex: 2),
SizedBox(
height: 247,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.only(left: 20, right: 20),
child: Text(
"Join 10101 and trade without counterparty risk.",
style: TextStyle(fontSize: 22, color: Colors.white),
textAlign: TextAlign.center,
),
),
const Padding(
padding: EdgeInsets.only(left: 20, right: 20),
child: Text(
"The future of finance is here!",
style: TextStyle(fontSize: 22, color: Colors.white),
textAlign: TextAlign.center,
),
),
const SizedBox(
height: 20,
),
Text(
referralStatus.referralCode,
style: const TextStyle(
fontSize: 30, color: Colors.white, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
const SizedBox(
height: 20,
),
const Padding(
padding: EdgeInsets.only(left: 20, right: 20),
child: Text(
"Use my referral code to reduce your trading fee",
style: TextStyle(fontSize: 22, color: Colors.white),
textAlign: TextAlign.center,
),
),
const SizedBox(
height: 20,
),
],
),
),
],
),
),
],
);
}
}
40 changes: 39 additions & 1 deletion mobile/lib/features/wallet/wallet_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,56 @@ import 'package:get_10101/common/poll_widget.dart';
import 'package:get_10101/common/secondary_action_button.dart';
import 'package:get_10101/features/wallet/balance.dart';
import 'package:get_10101/features/wallet/receive_screen.dart';
import 'package:get_10101/features/wallet/referral_widget.dart';
import 'package:get_10101/features/wallet/scanner_screen.dart';
import 'package:get_10101/features/wallet/wallet_change_notifier.dart';
import 'package:get_10101/util/poll_change_notified.dart';
import 'package:get_10101/util/preferences.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter/scheduler.dart';
import 'package:provider/provider.dart';
import 'package:screenshot/screenshot.dart';
import 'package:get_10101/ffi.dart' as rust;

class WalletScreen extends StatelessWidget {
class WalletScreen extends StatefulWidget {
static const route = "/wallet";
static const label = "Wallet";

const WalletScreen({Key? key}) : super(key: key);

@override
State<WalletScreen> createState() => _WalletScreenState();
}

class _WalletScreenState extends State<WalletScreen> {
ScreenshotController screenShotController = ScreenshotController();

@override
void initState() {
super.initState();

if (rust.api.hasTradedOnce()) {
SchedulerBinding.instance.addPostFrameCallback((_) {
_afterLoaded();
});
}
}

_afterLoaded() async {
final Preferences preferences = Preferences.instance;
preferences.hasReferralDialogTimePassedMoreThan7days().then((timeToShowDialogAgain) {
if (timeToShowDialogAgain) {
showDialog(
context: context,
builder: (BuildContext context) {
return ShareReferralWidget(
screenShotController: screenShotController, pref: preferences);
},
);
}
});
}

@override
Widget build(BuildContext context) {
final pollChangeNotifier = context.watch<PollChangeNotifier>();
Expand Down
22 changes: 22 additions & 0 deletions mobile/lib/util/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Preferences {
static const openPosition = "openPosition";
static const fullBackup = "fullBackup";
static const logLevelTrace = "logLevelTrace";
static const _hasSeenReferralDialogTimePassed = "hasSeenReferralDialogTimePassed";

Future<bool> setLogLevelTrace(bool trace) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
Expand Down Expand Up @@ -63,6 +64,27 @@ class Preferences {
return preferences.getString(contactDetails) ?? "";
}

Future<bool> hasReferralDialogTimePassedMoreThan7days() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
int? storedTimestamp = preferences.getInt(_hasSeenReferralDialogTimePassed);

if (storedTimestamp != null) {
// Calculate the difference to today
DateTime now = DateTime.now();
int differenceInDays =
now.difference(DateTime.fromMillisecondsSinceEpoch(storedTimestamp)).inDays;
return differenceInDays > 7;
} else {
// If no timestamp is stored, it is considered older than 7 days
return true;
}
}

Future<void> storeDontShowReferralDialogFor7Days() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setInt(_hasSeenReferralDialogTimePassed, DateTime.now().millisecondsSinceEpoch);
}

Future<bool> hasContactDetails() async {
var contact = await getContactDetails();
return contact.isNotEmpty;
Expand Down
Loading
Loading