Skip to content

Commit

Permalink
Updates to checkout flow on desktop (#1021)
Browse files Browse the repository at this point in the history
* cherry-pick desktop checkout changes

* Add desktop checkout page changes

* update openUrl signature
  • Loading branch information
atavism authored Mar 14, 2024
1 parent 731c028 commit 56a7f56
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 12 deletions.
10 changes: 10 additions & 0 deletions desktop/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ func emailAddress() *C.char {
return C.CString("")
}

//export emailExists
func emailExists(email *C.char) *C.char {
err := proClient.EmailExists(userConfig(), C.GoString(email))
if err != nil {
log.Error(err)
return sendError(err)
}
return C.CString("false")
}

//export referral
func referral() *C.char {
referralCode, err := a.ReferralCode(userConfig())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
github.com/getlantern/eventual v1.0.0
github.com/getlantern/eventual/v2 v2.0.2
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c
github.com/getlantern/flashlight/v7 v7.6.57
github.com/getlantern/flashlight/v7 v7.6.60
github.com/getlantern/golog v0.0.0-20230503153817-8e72de7e0a65
github.com/getlantern/i18n v0.0.0-20181205222232-2afc4f49bb1c
github.com/getlantern/idletiming v0.0.0-20231030193830-6767b09f86db
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,13 @@ github.com/getlantern/fdcount v0.0.0-20210503151800-5decd65b3731/go.mod h1:XZwE+
github.com/getlantern/filepersist v0.0.0-20160317154340-c5f0cd24e799/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c h1:mcz27xtAkb1OuOLBct/uFfL1p3XxAIcFct82GbT+UZM=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/flashlight v0.0.0-20230403092335-5f84ae10c585 h1:lVu4HyDAxXkX4kJEJQWpJJz+Poprb0crUGW5zhoh3GU=
github.com/getlantern/flashlight/v7 v7.6.57 h1:K0MsM7RiOYqq5wzAJkdv4MrHA6Luq78lUrf0vzEknWE=
github.com/getlantern/flashlight/v7 v7.6.57/go.mod h1:I90ekr9DZ5NyDWyR3HYQFCy1Vdt3EI1MciRBMdRyJfc=
github.com/getlantern/flashlight/v7 v7.6.59 h1:4Ev1f7K1TPXOwUe4UFhdzXVvqXNTbwgM4x4DAenB7ps=
github.com/getlantern/flashlight/v7 v7.6.59/go.mod h1:I90ekr9DZ5NyDWyR3HYQFCy1Vdt3EI1MciRBMdRyJfc=
github.com/getlantern/flashlight/v7 v7.6.60 h1:Fz7+5PaDfa+pqiNAyvVtpqj9Rvoj34brQCtVRpkDFus=
github.com/getlantern/flashlight/v7 v7.6.60/go.mod h1:I90ekr9DZ5NyDWyR3HYQFCy1Vdt3EI1MciRBMdRyJfc=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede h1:yrU6Px3ZkvCsDLPryPGi6FN+2iqFPq+JeCb7EFoDBhw=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede/go.mod h1:nhnoiS6DE6zfe+BaCMU4YI01UpsuiXnDqM5S8jxHuuI=
github.com/getlantern/fronted v0.0.0-20230601004823-7fec719639d8 h1:r/Z/SPPIfLXDI3QA7/tE6nOfPncrqeUPDjiFjnNGP50=
Expand Down
9 changes: 6 additions & 3 deletions lib/common/session_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,12 @@ class SessionModel extends Model {
Future<void> checkEmailExists(
String email,
) async {
return methodChannel.invokeMethod('checkEmailExists', <String, dynamic>{
'emailAddress': email,
}).then((value) => value as String);
if (isMobile()) {
return methodChannel.invokeMethod('checkEmailExists', <String, dynamic>{
'emailAddress': email,
}).then((value) => value as String);
}
await ffiEmailExists(email.toNativeUtf8());
}

Future<void> openWebview(String url) {
Expand Down
54 changes: 52 additions & 2 deletions lib/common/ui/app_webview.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:lantern/common/common.dart';
import 'package:lantern/common/common_desktop.dart';
import 'package:lantern/plans/utils.dart';

@RoutePage(name: 'AppWebview')
class AppWebView extends StatefulWidget {
Expand All @@ -15,7 +17,8 @@ class AppWebView extends StatefulWidget {
}

class _AppWebViewState extends State<AppWebView> {
InAppWebViewSettings settings = InAppWebViewSettings(

final InAppWebViewSettings settings = InAppWebViewSettings(
isInspectable: false,
javaScriptEnabled: true,
mediaPlaybackRequiresUserGesture: false,
Expand All @@ -24,7 +27,6 @@ class _AppWebViewState extends State<AppWebView> {
allowBackgroundAudioPlaying: false,
allowFileAccessFromFileURLs: true,
preferredContentMode: UserPreferredContentMode.MOBILE,

);


Expand All @@ -39,3 +41,51 @@ class _AppWebViewState extends State<AppWebView> {
);
}
}

class AppBrowser extends InAppBrowser {

Future<void> Function()? _onLoadStop;

final InAppBrowserClassSettings settings = InAppBrowserClassSettings(
browserSettings: InAppBrowserSettings(hideUrlBar: true),
webViewSettings: InAppWebViewSettings(
javaScriptEnabled: true, isInspectable: kDebugMode));

AppBrowser();

@override
Future onBrowserCreated() async {
print("Browser created");
}

@override
Future onLoadStart(url) async {
print("Started displaying $url");
}

@override
Future onLoadStop(url) async {
print("Stopped displaying $url");
this._onLoadStop?.call();
}

Future<void> openUrl(String url, Future<void> Function() cb) async {
this._onLoadStop = cb;
await this.openUrlRequest(urlRequest: URLRequest(url: WebUri(url)), settings: settings);
}

@override
void onReceivedError(WebResourceRequest request, WebResourceError error) {
print("Can't load ${request.url}.. Error: ${error.description}");
}

@override
void onProgressChanged(progress) {
print("Progress: $progress");
}

@override
void onExit() {
print("Browser closed");
}
}
2 changes: 2 additions & 0 deletions lib/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Pointer<Utf8> ffiAcceptedTermsVersion() =>

Pointer<Utf8> ffiEmailAddress() => _bindings.emailAddress().cast<Utf8>();

Pointer<Utf8> ffiEmailExists(email) => _bindings.emailExists(email).cast<Utf8>();

Pointer<Utf8> ffiReferral() => _bindings.referral().cast<Utf8>();

Pointer<Utf8> ffiReplicaAddr() => _bindings.replicaAddr().cast<Utf8>();
Expand Down
34 changes: 29 additions & 5 deletions lib/plans/checkout.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:email_validator/email_validator.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:lantern/common/common.dart';
import 'package:lantern/common/common_desktop.dart';
import 'package:lantern/common/ui/app_webview.dart';
import 'package:lantern/plans/payment_provider.dart';
import 'package:lantern/plans/plan_details.dart';
import 'package:lantern/plans/utils.dart';
Expand All @@ -24,6 +24,8 @@ class Checkout extends StatefulWidget {
class _CheckoutState extends State<Checkout>
with SingleTickerProviderStateMixin {
bool showMoreOptions = false;
bool showContinueButton = false;
final browser = AppBrowser();
final emailFieldKey = GlobalKey<FormState>();
late final emailController = CustomTextEditingController(
formKey: emailFieldKey,
Expand Down Expand Up @@ -162,7 +164,13 @@ class _CheckoutState extends State<Checkout>
if (!Platform.isMacOS) {
await context.pushRoute(AppWebview(url: redirectUrl));
} else {
await InAppBrowser.openWithSystemBrowser(url: WebUri(redirectUrl));
await browser.openUrl(redirectUrl, () async {
final res = await ffiProUser();
if (!widget.isPro && res == "true") {
// show success dialog if user becomes Pro during browser session
showSuccessDialog(context, widget.isPro);
}
});
}
return;
}
Expand Down Expand Up @@ -215,6 +223,14 @@ class _CheckoutState extends State<Checkout>
}
}

bool enableContinueButton() {
final isEmailValid = !emailController.value.text.isEmpty && emailFieldKey.currentState!.validate();
if (!isRefCodeFieldShowing || refCodeController.text.isEmpty) {
return isEmailValid;
}
return isEmailValid && refCodeFieldKey.currentState!.validate();
}

@override
Widget build(BuildContext context) {
return BaseScreen(
Expand Down Expand Up @@ -257,6 +273,11 @@ class _CheckoutState extends State<Checkout>
child: CTextField(
initialValue: widget.isPro ? emailAddress : '',
controller: emailController,
onChanged: (text) {
setState(() {
showContinueButton = enableContinueButton();
});
},
autovalidateMode: widget.isPro
? AutovalidateMode.always
: AutovalidateMode.disabled,
Expand Down Expand Up @@ -285,6 +306,11 @@ class _CheckoutState extends State<Checkout>
child: CTextField(
controller: refCodeController,
autovalidateMode: AutovalidateMode.disabled,
onChanged: (text) {
setState(() {
showContinueButton = enableContinueButton();
});
},
textCapitalization:
TextCapitalization.characters,
label: 'referral_code'.i18n,
Expand Down Expand Up @@ -349,9 +375,7 @@ class _CheckoutState extends State<Checkout>
message: AppKeys.continueCheckout,
child: Button(
text: 'continue'.i18n,
disabled: emailController.value.text.isEmpty ||
emailFieldKey.currentState?.validate() == false ||
refCodeFieldKey.currentState?.validate() == false,
disabled: !showContinueButton,
onPressed: onContinueTapped,
),
),
Expand Down
1 change: 1 addition & 0 deletions liblantern.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ extern char* paymentMethods();
extern char* userData();
extern char* serverInfo();
extern char* emailAddress();
extern char* emailExists(char* email);
extern char* referral();
extern char* chatEnabled();
extern char* playVersion();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ dependencies:
# URL & Sharing utilities
url_launcher: ^6.1.12
share_plus: ^7.1.0
flutter_inappwebview: ^6.0.0-beta.25
flutter_inappwebview: ^6.0.0

# Desktop
window_manager: ^0.3.8
Expand Down

0 comments on commit 56a7f56

Please sign in to comment.