launch method

Future<void> launch(
  1. Function? onLaunched, {
  2. String? jsCode,
  3. Function? socketDisconnectedAction,
})

Implementation

Future<void> launch(
  Function? onLaunched, {
  String? jsCode,
  Function? socketDisconnectedAction,
}) async {
  /// reset state before webView launch or reload
  _msgHandlers = {};
  _msgCompleters = {};
  _reloadHandlers = {};
  _evalJavascriptUID = 0;
  _onLaunched = onLaunched;
  webViewLoaded = false;
  jsCodeStarted = -1;

  _jsCode = jsCode;

  if (_web == null) {
    _web = new HeadlessInAppWebView(
      windowId: 2,
      initialOptions: InAppWebViewGroupOptions(
        crossPlatform: InAppWebViewOptions(clearCache: true),
        android: AndroidInAppWebViewOptions(useOnRenderProcessGone: true),
      ),
      androidOnRenderProcessGone: (webView, detail) async {
        if (_web?.webViewController == webView) {
          webViewLoaded = false;
          await _web?.webViewController.clearCache();
          await _web?.webViewController.reload();
        }
      },
      initialUrlRequest: URLRequest(
          url: Uri.parse(
              "http://localhost:8080/packages/polkawallet_sdk/assets/bridge.html")),
      onWebViewCreated: (controller) async {
        print('Bridge HeadlessInAppWebView created!');
        controller.loadUrl(
            urlRequest: URLRequest(
                url: Uri.parse(
                    "http://localhost:8080/packages/polkawallet_sdk/assets/bridge.html")));
      },
      onConsoleMessage: (controller, message) {
        print("CONSOLE MESSAGE: " + message.message);
        if (jsCodeStarted < 0) {
          try {
            final msg = jsonDecode(message.message);
            if (msg['path'] == 'log') {
              if (message.message.contains('js loaded')) {
                jsCodeStarted = 1;
              } else {
                jsCodeStarted = 0;
              }
            }
          } catch (err) {
            // ignore
          }
        }
        if (message.message.contains("WebSocket is not connected") &&
            socketDisconnectedAction != null) {
          socketDisconnectedAction();
        }
        if (message.messageLevel != ConsoleMessageLevel.LOG) return;

        try {
          var msg = jsonDecode(message.message);

          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']);
          }
        } catch (_) {
          // ignore
        }
      },
      onLoadStop: (controller, url) async {
        print('webview loaded $url');
        if (webViewLoaded) return;

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

    await _web?.dispose();
    await _web?.run();
  } else {
    _webViewReloadTimer = Timer.periodic(Duration(seconds: 3), (timer) {
      _tryReload();
    });
  }
}