Skip to content

Commit

Permalink
fix link mode on cold boot
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Aug 30, 2024
1 parent 1923d27 commit f7daee4
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 144 deletions.
12 changes: 6 additions & 6 deletions example/dapp/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import Flutter

methodsChannel = FlutterMethodChannel(name: AppDelegate.METHODS_CHANNEL, binaryMessenger: controller.binaryMessenger)
methodsChannel?.setMethodCallHandler({ [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
if (call.method == "initialLink") {
if (call.method == "initialLink") {
if let link = self?.initialLink {
self?.initialLink = nil
result(link)
let _ = self?.linkStreamHandler.handleLink(link)
return
} else {
result("")
}
Expand All @@ -46,10 +47,9 @@ import Flutter

override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// Handle universal links
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
if let url = userActivity.webpageURL {
return linkStreamHandler.handleLink(url.absoluteString)
}
if let url = userActivity.webpageURL {
self.initialLink = url.absoluteString
return linkStreamHandler.handleLink(self.initialLink!)
}
return false
}
Expand Down
23 changes: 9 additions & 14 deletions example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,28 @@ class _MyHomePageState extends State<MyHomePage> {
return flavor.replaceAll('-production', '');
}

String _universalLink(bool fromMR) {
Uri link = fromMR
? Uri.parse(
'https://web3modal-laboratory-git-chores-addedmore-3e0f2b-walletconnect1.vercel.app/flutter_appkit')
: Uri.parse('https://lab.web3modal.com/flutter_appkit');
String _universalLink() {
Uri link = Uri.parse('https://lab.web3modal.com/flutter_appkit');
if (_flavor.isNotEmpty) {
if (!fromMR) {
link = link.replace(host: 'dev.${link.host}');
}
return link.replace(path: '${link.path}_internal').toString();
return link
.replace(path: '${link.path}_internal')
.replace(host: 'dev.${link.host}')
.toString();
}
return link.toString();
}

Redirect _constructRedirect(bool fromMR) {
Redirect _constructRedirect() {
return Redirect(
native: 'wcflutterdapp$_flavor://',
universal: _universalLink(fromMR),
universal: _universalLink(),
// enable linkMode on Wallet so Dapps can use relay-less connection
// universal: value must be set on cloud config as well
linkMode: true,
);
}

Future<void> initialize() async {
final prefs = await SharedPreferences.getInstance();
final fromMR = prefs.getBool('_LM_from_MR') ?? true;
_web3App = Web3App(
core: Core(
projectId: DartDefines.projectId,
Expand All @@ -106,7 +101,7 @@ class _MyHomePageState extends State<MyHomePage> {
icons: [
'https://images.prismic.io/wallet-connect/65785a56531ac2845a260732_WalletConnect-App-Logo-1024X1024.png'
],
redirect: _constructRedirect(fromMR),
redirect: _constructRedirect(),
),
);

Expand Down
77 changes: 25 additions & 52 deletions example/dapp/lib/pages/connect_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:fl_toast/fl_toast.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -36,7 +35,6 @@ class ConnectPage extends StatefulWidget {

class ConnectPageState extends State<ConnectPage> {
bool _testnetOnly = false;
bool _testFromMR = true;
final List<ChainMetadata> _selectedChains = [];
bool _shouldDismissQrCode = true;
bool _initialized = false;
Expand All @@ -55,9 +53,6 @@ class ConnectPageState extends State<ConnectPage> {

await _walletConnectModalService.init();

final prefs = await SharedPreferences.getInstance();
_testFromMR = prefs.getBool('_LM_from_MR') ?? true;

setState(() => _initialized = true);

widget.web3App.onSessionConnect.subscribe(_onSessionConnect);
Expand Down Expand Up @@ -186,39 +181,23 @@ class ConnectPageState extends State<ConnectPage> {
textAlign: TextAlign.center,
),
const SizedBox(height: StyleConstants.linear8),
SizedBox(
height: StyleConstants.linear48,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
StringConstants.testnetsOnly,
style: StyleConstants.buttonText,
),
Switch(
value: _testnetOnly,
onChanged: (value) {
setState(() {
_selectedChains.clear();
_testnetOnly = value;
});
},
),
const Expanded(child: SizedBox()),
const Text(
'LM from MR',
style: StyleConstants.buttonText,
),
Switch(
value: _testFromMR,
onChanged: (value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('_LM_from_MR', value);
exit(0);
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
StringConstants.testnetsOnly,
style: StyleConstants.buttonText,
),
Switch(
value: _testnetOnly,
onChanged: (value) {
setState(() {
_selectedChains.clear();
_testnetOnly = value;
});
},
),
],
),
const Text('EVM Chains:', style: StyleConstants.buttonText),
const SizedBox(height: StyleConstants.linear8),
Expand Down Expand Up @@ -261,13 +240,10 @@ class ConnectPageState extends State<ConnectPage> {
style: StyleConstants.buttonText,
),
const SizedBox(height: StyleConstants.linear8),
Wrap(
spacing: 8.0,
runSpacing: 8.0,
children:
WCSampleWallets.getSampleWallets(_testFromMR).map((wallet) {
return SizedBox(
width: (MediaQuery.of(context).size.width / 2) - 16,
Column(
children: WCSampleWallets.getSampleWallets().map((wallet) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: ElevatedButton(
style: _buttonStyle,
onPressed: _selectedChains.isEmpty
Expand Down Expand Up @@ -303,13 +279,10 @@ class ConnectPageState extends State<ConnectPage> {
style: StyleConstants.buttonText,
),
const SizedBox(height: StyleConstants.linear8),
Wrap(
spacing: 8.0,
runSpacing: 8.0,
children:
WCSampleWallets.getSampleWallets(_testFromMR).map((wallet) {
return SizedBox(
width: (MediaQuery.of(context).size.width / 2) - 16,
Column(
children: WCSampleWallets.getSampleWallets().map((wallet) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: ElevatedButton(
style: _buttonStyle,
onPressed: _selectedChains.isEmpty
Expand Down
76 changes: 47 additions & 29 deletions example/dapp/lib/utils/sample_wallets.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

class WCSampleWallets {
static List<Map<String, dynamic>> sampleWallets(bool mr) => [
static List<Map<String, dynamic>> sampleWalletsInternal() => [
{
'name': 'Swift Wallet',
'platform': ['ios'],
Expand All @@ -10,38 +10,57 @@ class WCSampleWallets {
'bundleId': 'com.walletconnect.sample.wallet',
'universal': 'https://lab.web3modal.com/wallet',
},
{
'name': 'Flutter Wallet',
'platform': ['ios', 'android'],
'id': '123456789012345678901234567891',
'schema': 'wcflutterwallet://',
'bundleId': 'com.walletconnect.flutterwallet',
'universal': 'https://lab.web3modal.com/flutter_walletkit',
},
{
'name': 'Flutter Wallet (internal)',
'platform': ['ios', 'android'],
'id': '123456789012345678901234567895',
'schema': 'wcflutterwallet-internal://',
'bundleId': 'com.walletconnect.flutterwallet.internal',
'universal': mr
? 'https://web3modal-laboratory-git-chores-addedmore-3e0f2b-walletconnect1.vercel.app/flutter_walletkit_internal'
: 'https://dev.lab.web3modal.com/flutter_walletkit_internal',
'universal':
'https://dev.lab.web3modal.com/flutter_walletkit_internal',
},
{
'name': 'RN Wallet',
'name': 'RN Wallet (internal)',
'platform': ['ios', 'android'],
'id': '123456789012345678901234567892',
'id': '1234567890123456789012345678922',
'schema': 'rn-web3wallet://wc',
'bundleId': 'com.walletconnect.web3wallet.rnsample',
'bundleId': 'com.walletconnect.web3wallet.rnsample.internal',
'universal': 'https://lab.web3modal.com/rn_walletkit',
},
{
'name': 'RN Wallet (internal)',
'name': 'Kotlin Wallet (Internal)',
'platform': ['android'],
'id': '123456789012345678901234567894',
'schema': 'kotlin-web3wallet://wc',
'bundleId': 'com.walletconnect.sample.wallet.internal',
'universal':
'https://web3modal-laboratory-git-chore-kotlin-assetlinks-walletconnect1.vercel.app/wallet_internal',
},
];

static List<Map<String, dynamic>> sampleWalletsProduction() => [
{
'name': 'Swift Wallet',
'platform': ['ios'],
'id': '123456789012345678901234567890',
'schema': 'walletapp://',
'bundleId': 'com.walletconnect.sample.wallet',
'universal': 'https://lab.web3modal.com/wallet',
},
{
'name': 'Flutter Wallet',
'platform': ['ios', 'android'],
'id': '1234567890123456789012345678922',
'id': '123456789012345678901234567891',
'schema': 'wcflutterwallet://',
'bundleId': 'com.walletconnect.flutterwallet',
'universal': 'https://lab.web3modal.com/flutter_walletkit',
},
{
'name': 'RN Wallet',
'platform': ['ios', 'android'],
'id': '123456789012345678901234567892',
'schema': 'rn-web3wallet://wc',
'bundleId': 'com.walletconnect.web3wallet.rnsample.internal',
'bundleId': 'com.walletconnect.web3wallet.rnsample',
'universal': 'https://lab.web3modal.com/rn_walletkit',
},
{
Expand All @@ -53,19 +72,18 @@ class WCSampleWallets {
'universal':
'https://web3modal-laboratory-git-chore-kotlin-assetlinks-walletconnect1.vercel.app/wallet_release',
},
{
'name': 'Kotlin Wallet (Internal)',
'platform': ['android'],
'id': '123456789012345678901234567894',
'schema': 'kotlin-web3wallet://wc',
'bundleId': 'com.walletconnect.sample.wallet.internal',
'universal':
'https://web3modal-laboratory-git-chore-kotlin-assetlinks-walletconnect1.vercel.app/wallet_internal',
},
];

static List<Map<String, dynamic>> getSampleWallets(bool mr) {
return sampleWallets(mr).where((e) {
static List<Map<String, dynamic>> getSampleWallets() {
String flavor = '-${const String.fromEnvironment('FLUTTER_APP_FLAVOR')}';
flavor = flavor.replaceAll('-production', '');
if (flavor.isNotEmpty) {
return sampleWalletsInternal().where((e) {
return (e['platform'] as List<String>)
.contains(Platform.operatingSystem);
}).toList();
}
return sampleWalletsProduction().where((e) {
return (e['platform'] as List<String>).contains(Platform.operatingSystem);
}).toList();
}
Expand Down
11 changes: 6 additions & 5 deletions example/wallet/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import Flutter
if (call.method == "initialLink") {
if let link = self?.initialLink {
self?.initialLink = nil
result(link)
let _ = self?.linkStreamHandler.handleLink(link)
return
} else {
result("")
}
Expand All @@ -46,13 +47,13 @@ import Flutter

override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// Handle universal links
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
if let url = userActivity.webpageURL {
return linkStreamHandler.handleLink(url.absoluteString)
}
if let url = userActivity.webpageURL {
self.initialLink = url.absoluteString
return linkStreamHandler.handleLink(self.initialLink!)
}
return false
}

}

class LinkStreamHandler: NSObject, FlutterStreamHandler {
Expand Down
23 changes: 9 additions & 14 deletions example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,21 @@ class Web3WalletService extends IWeb3WalletService {
return flavor.replaceAll('-production', '');
}

String _universalLink(fromMR) {
Uri link = fromMR
? Uri.parse(
'https://web3modal-laboratory-git-chores-addedmore-3e0f2b-walletconnect1.vercel.app/flutter_walletkit')
: Uri.parse('https://lab.web3modal.com/flutter_walletkit');
String _universalLink() {
Uri link = Uri.parse('https://lab.web3modal.com/flutter_walletkit');
if (_flavor.isNotEmpty) {
if (!fromMR) {
link = link.replace(host: 'dev.${link.host}');
}
return link.replace(path: '${link.path}_internal').toString();
return link
.replace(path: '${link.path}_internal')
.replace(host: 'dev.${link.host}')
.toString();
}
return link.toString();
}

Redirect _constructRedirect(bool fromMR) {
Redirect _constructRedirect() {
return Redirect(
native: 'wcflutterwallet$_flavor://',
universal: _universalLink(fromMR),
universal: _universalLink(),
// enable linkMode on Wallet so Dapps can use relay-less connection
// universal: value must be set on cloud config as well
linkMode: true,
Expand All @@ -54,8 +51,6 @@ class Web3WalletService extends IWeb3WalletService {

@override
Future<void> create() async {
final prefs = await SharedPreferences.getInstance();
final fromMR = prefs.getBool('_LM_from_MR') ?? true;
// Create the web3wallet
_web3Wallet = Web3Wallet(
core: Core(
Expand All @@ -69,7 +64,7 @@ class Web3WalletService extends IWeb3WalletService {
icons: [
'https://docs.walletconnect.com/assets/images/web3walletLogo-54d3b546146931ceaf47a3500868a73a.png'
],
redirect: _constructRedirect(fromMR),
redirect: _constructRedirect(),
),
);

Expand Down
Loading

0 comments on commit f7daee4

Please sign in to comment.