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

Fix embedded web browser RPC. #48

Merged
merged 4 commits into from
Jul 22, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Changelog
=========

## 2.0.5
- Use Logging lib for logs
- Prevent simultaneous connexion/deconnexion requests
- Fix embedded webview RPC communication

## 2.0.4
- Update Archethic Dart SDK version 3.4.2
- Example app: Regenerate all platform's configurations
Expand Down
2 changes: 1 addition & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Miscellaneous
*.class
*.log
*.print
*.pyc
*.swp
.DS_Store
Expand Down
27 changes: 27 additions & 0 deletions example/lib/logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';

class LoggerSetup {
LoggerSetup._();
factory LoggerSetup.instance() => _instance ??= LoggerSetup._();

static LoggerSetup? _instance;

void setup() {
Logger.root.level = Level.ALL;

_registerLogListeners();
}

void _registerLogListeners() {
Logger.root.onRecord.listen((event) {
debugPrint('[${event.loggerName}] ${event.message}');
if (event.error != null) {
debugPrint('\t${event.error}');
}
if (event.stackTrace != null) {
debugPrint('\t${event.stackTrace}');
}
});
}
}
6 changes: 5 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:archethic_wallet_client/archethic_wallet_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dapp_example/logger.dart';
import 'package:flutter_dapp_example/main_screen.dart';

void main() {
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

LoggerSetup.instance().setup();
runApp(const MyApp());
}

Expand Down
10 changes: 6 additions & 4 deletions example/lib/tab/add_service.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:convert';
import 'dart:developer';

import 'package:archethic_wallet_client/archethic_wallet_client.dart' as awc;
import 'package:archethic_wallet_client/archethic_wallet_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dapp_example/widgets/snackbar.dart';
import 'package:flutter_dapp_example/widgets/space.dart';
import 'package:logging/logging.dart';

class AddServiceTab extends StatefulWidget {
const AddServiceTab({required this.aewalletClient, super.key});
Expand All @@ -21,6 +21,8 @@ class _AddServiceTabState extends State<AddServiceTab> {
text: '',
);

final _logger = Logger('Tab-AddService');

@override
void dispose() {
payloadTextController.dispose();
Expand Down Expand Up @@ -52,16 +54,16 @@ class _AddServiceTabState extends State<AddServiceTab> {
);
response.when(
failure: (failure) {
log(
_logger.severe(
'Transaction failed',
error: failure,
failure,
);
ScaffoldMessenger.of(context).showSnackBar(
ResultSnackbar.error(failure.message),
);
},
success: (result) {
log(
_logger.info(
'Transaction succeed : ${json.encode(result)}',
);
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
10 changes: 6 additions & 4 deletions example/lib/tab/keychain_derive_address.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:convert';
import 'dart:developer';

import 'package:archethic_wallet_client/archethic_wallet_client.dart' as awc;
import 'package:archethic_wallet_client/archethic_wallet_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dapp_example/widgets/snackbar.dart';
import 'package:flutter_dapp_example/widgets/space.dart';
import 'package:logging/logging.dart';

class KeychainDeriveAddressTab extends StatefulWidget {
const KeychainDeriveAddressTab({required this.aewalletClient, super.key});
Expand All @@ -29,6 +29,8 @@ class _KeychainDeriveAddressTabState extends State<KeychainDeriveAddressTab> {
text: '',
);

final _logger = Logger('Tab-KeychainDeriveAddressTab');

@override
void dispose() {
payloadServiceNameController.dispose();
Expand Down Expand Up @@ -87,16 +89,16 @@ class _KeychainDeriveAddressTabState extends State<KeychainDeriveAddressTab> {
);
response.when(
failure: (failure) {
log(
_logger.severe(
'Command failed',
error: failure,
failure,
);
ScaffoldMessenger.of(context).showSnackBar(
ResultSnackbar.error(failure.message),
);
},
success: (result) {
log(
_logger.info(
'Command succeed : ${json.encode(result)}',
);
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
10 changes: 6 additions & 4 deletions example/lib/tab/keychain_derive_keypair.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:convert';
import 'dart:developer';

import 'package:archethic_wallet_client/archethic_wallet_client.dart' as awc;
import 'package:archethic_wallet_client/archethic_wallet_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dapp_example/widgets/snackbar.dart';
import 'package:flutter_dapp_example/widgets/space.dart';
import 'package:logging/logging.dart';

class KeychainDeriveKeypairTab extends StatefulWidget {
const KeychainDeriveKeypairTab({required this.aewalletClient, super.key});
Expand All @@ -29,6 +29,8 @@ class _KeychainDeriveKeypairTabState extends State<KeychainDeriveKeypairTab> {
text: '',
);

final _logger = Logger('Tab-KeychainDeriveKeypair');

@override
void dispose() {
payloadServiceNameController.dispose();
Expand Down Expand Up @@ -87,16 +89,16 @@ class _KeychainDeriveKeypairTabState extends State<KeychainDeriveKeypairTab> {
);
response.when(
failure: (failure) {
log(
_logger.severe(
'Command failed',
error: failure,
failure,
);
ScaffoldMessenger.of(context).showSnackBar(
ResultSnackbar.error(failure.message),
);
},
success: (result) {
log(
_logger.info(
'Command succeed : ${json.encode(result)}',
);
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
10 changes: 6 additions & 4 deletions example/lib/tab/sign_transactions.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:convert';
import 'dart:developer';

import 'package:archethic_wallet_client/archethic_wallet_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dapp_example/widgets/snackbar.dart';
import 'package:flutter_dapp_example/widgets/space.dart';
import 'package:logging/logging.dart';

class SignTransactionsTab extends StatefulWidget {
const SignTransactionsTab({required this.aewalletClient, super.key});
Expand Down Expand Up @@ -67,6 +67,8 @@ class _SignTransactionsTabState extends State<SignTransactionsTab> {
''',
);

final _logger = Logger('Tab-SignTransaction');

@override
void dispose() {
payloadTextController.dispose();
Expand Down Expand Up @@ -104,9 +106,9 @@ class _SignTransactionsTabState extends State<SignTransactionsTab> {
);
response.when(
failure: (failure) {
log(
_logger.severe(
'Command failed',
error: failure,
failure,
);
ScaffoldMessenger.of(context).showSnackBar(
ResultSnackbar.error(
Expand All @@ -115,7 +117,7 @@ class _SignTransactionsTabState extends State<SignTransactionsTab> {
);
},
success: (result) {
log(
_logger.info(
'Command succeed : ${json.encode(result)}',
);
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
10 changes: 6 additions & 4 deletions example/lib/tab/transaction_send.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:convert';
import 'dart:developer';

import 'package:archethic_wallet_client/archethic_wallet_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dapp_example/widgets/snackbar.dart';
import 'package:flutter_dapp_example/widgets/space.dart';
import 'package:logging/logging.dart';

class TransactionSendTab extends StatefulWidget {
const TransactionSendTab({
Expand Down Expand Up @@ -42,6 +42,8 @@ class _TransactionSendTabState extends State<TransactionSendTab> {
''',
);

final _logger = Logger('Tab-TransactionSend');

@override
void dispose() {
payloadTextController.dispose();
Expand Down Expand Up @@ -80,9 +82,9 @@ class _TransactionSendTabState extends State<TransactionSendTab> {

response.when(
failure: (failure) {
log(
_logger.severe(
'Transaction failed',
error: failure,
failure,
);
ScaffoldMessenger.of(context).showSnackBar(
ResultSnackbar.error(
Expand All @@ -91,7 +93,7 @@ class _TransactionSendTabState extends State<TransactionSendTab> {
);
},
success: (result) {
log(
_logger.info(
'Transaction succeed : ${json.encode(result)}',
);
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.0.4"
version: "2.0.5"
archive:
dependency: transitive
description:
Expand Down Expand Up @@ -344,7 +344,7 @@ packages:
source: hosted
version: "3.0.1"
logging:
dependency: transitive
dependency: "direct main"
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
Expand Down
3 changes: 3 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ dependencies:
# An SVG rendering and widget library for Flutter, which allows painting and displaying Scalable Vector Graphics 1.1 files.
flutter_svg: ^2.0.10+1

# Logging lib
logging: ^1.2.0

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
44 changes: 44 additions & 0 deletions lib/src/core/task.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:async';

import 'package:logging/logging.dart';

class SingletonTask<T> {
SingletonTask({
required this.name,
required this.task,
}) {
this._logger = Logger(name);
}

Completer<T>? _completer;
final String name;
final Future<T> Function() task;

late final Logger _logger;

Future<T> run() async {
if (_completer != null) {
_logger.info('Already running. Waiting for result...');
return _completer!.future;
}

_completer = Completer<T>();
unawaited(
Future.sync(() async {
try {
_logger.info('Starting');

_completer?.complete(await task());
_logger.info('Done');
} catch (e) {
_completer?.completeError(e);
_logger.info('Failed');
} finally {
_completer = null;
}
}),
);

return _completer!.future;
}
}
Loading