Skip to content

Commit

Permalink
fixed missing device linking functionality (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
reflog authored Dec 13, 2023
1 parent 992d1fe commit 689b9ab
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
19 changes: 16 additions & 3 deletions android/app/src/main/kotlin/io/lantern/model/SessionModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class SessionModel(
"authorizeViaEmail" -> authorizeViaEmail(call.argument("emailAddress")!!, result)
"checkEmailExists" -> checkEmailExists(call.argument("emailAddress")!!, result)
"requestLinkCode" -> requestLinkCode(result)
"redeemLinkCode" -> redeemLinkCode(result)
"resendRecoveryCode" -> sendRecoveryCode(result)
"validateRecoveryCode" -> validateRecoveryCode(call.argument("code")!!, result)
"approveDevice" -> approveDevice(call.argument("code")!!, result)
Expand Down Expand Up @@ -366,27 +367,38 @@ class SessionModel(
val code = result["code"].asString
val expireAt = result["expireAt"].asLong
LanternApp.getSession().setDeviceCode(code, expireAt)
methodCallResult.success(null)
methodCallResult.success(code)
}
}
},
)
}

private fun redeemLinkCode() {
private fun redeemLinkCode(methodCallResult: MethodChannel.Result) {
val formBody = FormBody.Builder()
.add("code", LanternApp.getSession().deviceCode()!!)
.add("deviceName", LanternApp.getSession().deviceName())
.build()
Logger.info(TAG, "Redeeming link code")
lanternClient.post(
LanternHttpClient.createProUrl("/link-code-request"),
LanternHttpClient.createProUrl("/link-code-redeem"),
formBody,
object : ProCallback {
override fun onFailure(t: Throwable?, error: ProError?) {
Logger.error(TAG, "Error making link redeem request..", t)
if (error == null) {
activity.runOnUiThread {
methodCallResult.error("unknownError", null, null)
}
return
}
activity.runOnUiThread {
methodCallResult.error("linkCodeError", error.id, null)
}
}

override fun onSuccess(response: Response?, result: JsonObject?) {
Logger.debug(TAG, "redeem link code response: $result")
if (result == null || result["token"] == null || result["userID"] == null) return
Logger.debug(TAG, "Successfully redeemed link code")
val userID = result["userID"].asLong
Expand All @@ -398,6 +410,7 @@ class SessionModel(
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
activity.startActivity(intent)
// methodCallResult.success(null)
}
},
)
Expand Down
22 changes: 19 additions & 3 deletions lib/account/device_linking/link_device.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:lantern/common/common.dart';
import 'package:retry/retry.dart';

import 'explanation_step.dart';

@RoutePage<void>(name: 'LinkDevice')
Expand All @@ -12,19 +14,33 @@ class LinkDevice extends StatefulWidget {
}

class _LinkDeviceState extends State<LinkDevice> {

@override
void initState() {
super.initState();
sessionModel.requestLinkCode();
sessionModel.requestLinkCode().then(
(code) => {
print("requestLinkCode success code is $code"),
retry(
// Make a GET request
() => sessionModel
.redeemLinkCode()
.timeout(const Duration(hours: 1)),
onRetry: (e) => print("error , retry: $e"),
).then(
(x) => {
print("redeemLinkCode success"),
},
)
},
);
}

@override
Widget build(BuildContext context) {
return BaseScreen(
title: 'Authorize Device for Pro'.i18n,
body: sessionModel.deviceLinkingCode((BuildContext context,
String deviceCode, Widget? child) =>
String deviceCode, Widget? child) =>
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expand Down
12 changes: 6 additions & 6 deletions lib/common/session_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,14 @@ class SessionModel extends Model {
}

Future<void> reportIssue(
String email,
String issue,
String description
) async {
String email, String issue, String description) async {
return methodChannel.invokeMethod('reportIssue', <String, dynamic>{
'email': email,
'issue': issue,
'description': description
}).then((value) => value as String);
}


Widget getUserId(ValueWidgetBuilder<String> builder) {
return subscribedSingleValueBuilder<String>(
'userId',
Expand All @@ -337,7 +333,11 @@ class SessionModel extends Model {
Future<String> requestLinkCode() {
return methodChannel
.invokeMethod('requestLinkCode')
.then((value) => value as String);
.then((value) => value.toString());
}

Future<void> redeemLinkCode() {
return methodChannel.invokeMethod('redeemLinkCode');
}

Widget deviceLinkingCode(ValueWidgetBuilder<String> builder) {
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.0"
retry:
dependency: "direct main"
description:
name: retry
sha256: "822e118d5b3aafed083109c72d5f484c6dc66707885e07c0fbcb8b986bba7efc"
url: "https://pub.dev"
source: hosted
version: "3.1.2"
rxdart:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ dependencies:
# Ads
google_mobile_ads: ^3.0.0
clever_ads_solutions: ^0.1.0
retry: ^3.1.2


# wakelock ^0.6.2 requires win32 ^2.0.0 or ^3.0.0
Expand Down

0 comments on commit 689b9ab

Please sign in to comment.