launch method

Future<void> launch(
  1. ServiceKeyring? keyring,
  2. Keyring keyringStorage,
  3. Function? onLaunched, {
  4. String? jsCode,
})

Implementation

Future<void> launch(
  ServiceKeyring? keyring,
  Keyring keyringStorage,
  Function? onLaunched, {
  String? jsCode,
}) async {
  /// reset state before webView launch or reload
  _msgHandlers = {};
  _msgCompleters = {};
  _evalJavascriptUID = 0;
  _onLaunched = onLaunched;
  _webViewLoaded = false;

  _jsCode = jsCode ??
      await rootBundle
          .loadString('packages/web3wallet_sdk/js-api/dist/main.js');
  print('js file loaded');

  if (_web == null) {
    await _startLocalServer();

    _web = new HeadlessInAppWebView(
      initialOptions: InAppWebViewGroupOptions(
        crossPlatform: InAppWebViewOptions(),
      ),
      onWebViewCreated: (controller) {
        print('HeadlessInAppWebView created!');
      },
      onConsoleMessage: (controller, message) {
        print("CONSOLE MESSAGE: " + message.message);
        if (message.messageLevel != ConsoleMessageLevel.LOG) return;

        compute(jsonDecode, message.message).then((msg) {
          final String? path = msg['path'];
          if (_msgCompleters[path!] != null) {
            Completer handler = _msgCompleters[path]!;
            handler.complete(msg['data']);
            if (path.contains('uid=')) {
              _msgCompleters.remove(path);
            }
          }
          if (_msgHandlers[path] != null) {
            Function handler = _msgHandlers[path]!;
            handler(msg['data']);
          }
        });
      },
      onLoadStop: (controller, url) async {
        print('webview loaded');
        if (_webViewLoaded) return;

        _handleReloaded();
        await _startJSCode(keyring, keyringStorage);
      },
    );

    await _web!.run();
    _web!.webViewController.loadUrl(
        urlRequest: URLRequest(url: Uri.parse("https://localhost:8080/")));
  } else {
    _tryReload();
  }
}