processSpeechRecognitionResult method

void processSpeechRecognitionResult(
  1. SpeechRecognitionResult result
)

Implementation

void processSpeechRecognitionResult(SpeechRecognitionResult result) async {
  if (currentState == ApplicationState.ready) {
    // user has cancelled already, don't process result
    return;
  }

  if (result.finalResult) {
    // on a final result we fetch the wiki content
    _finalResult = result.recognizedWords;
    _partialResult = '';
    _log.fine('Final result: $_finalResult');
    _stopListening();
    // send final query text to Frame line 1 (before we confirm the title)
    if (_finalResult != _prevText) {
      await frame!
          .sendMessage(TxPlainText(msgCode: 0x0a, text: _finalResult));
      _prevText = _finalResult;
    }

    // kick off the http request sequence
    String? error;
    String? title;
    (title, error) = await findBestPage(_finalResult);

    if (title != null) {
      // send page title to Frame on row 1
      if (title != _prevText) {
        await frame!.sendMessage(TxPlainText(msgCode: 0x0a, text: title));
        _prevText = title;
      }

      WikiResult? result;
      String? error;
      (result, error) = await fetchExtract(title);

      if (result != null) {
        _extract =
            TextUtils.wrapText('${result.title}\n${result.extract}', 400, 4);
        _currentPage = 0;
        _finalResult = result.title;
        if (mounted) setState(() {});
        // send first 5 rows of result.extract to Frame ( TODO regex strip non-printable? )
        await frame!.sendMessage(
            TxPlainText(msgCode: 0x0a, text: getCurrentPageText()));
        _prevText = '';

        if (result.thumbUri != null) {
          // first, download the image into an image/image
          Uint8List? imageBytes;
          (imageBytes, error) = await fetchThumbnail(result.thumbUri!);

          if (imageBytes != null) {
            try {
              // Update the UI based on the original image
              setState(() {
                _image = Image.memory(imageBytes!,
                    gaplessPlayback: true, fit: BoxFit.cover);
              });

              // yield here a moment in order to show the first image first
              await Future.delayed(const Duration(milliseconds: 10));

              var sprite = TxSprite.fromImageBytes(
                  msgCode: 0x0d, imageBytes: imageBytes);

              // Update the UI with the modified image
              setState(() {
                _image = Image.memory(img.encodePng(sprite.toImage()),
                    gaplessPlayback: true, fit: BoxFit.cover);
              });

              // create the image sprite block header and its sprite lines
              // based on the sprite
              TxImageSpriteBlock isb = TxImageSpriteBlock(
                  msgCode: 0x0d,
                  image: sprite,
                  spriteLineHeight: 20,
                  progressiveRender: true);

              // and send the block header then the sprite lines to Frame
              await frame!.sendMessage(isb);

              for (var sprite in isb.spriteLines) {
                await frame!.sendMessage(sprite);
              }
            } catch (e) {
              _log.severe('Error processing image: $e');
            }
          } else {
            _log.fine(
                'Error fetching thumbnail for "$_finalResult": "${result.thumbUri!}" - "$error"');
          }
        } else {
          // no thumbnail for this entry
          _image = null;
        }
      }
    } else {
      _log.fine('Error searching for "$_finalResult" - "$error"');
      _extract = [];
      await frame!.sendMessage(TxPlainText(
          msgCode: 0x0a,
          text: TextUtils.wrapText(error!, 400, 4).join('\n')));
      _image = null;
      setState(() {});
    }

    // final result is done
    if (mounted) setState(() {});
  } else {
    // partial result - just display in-progress text
    _partialResult = result.recognizedWords;
    if (mounted) setState(() {});

    _log.fine('Partial result: $_partialResult, ${result.alternates}');
    if (_partialResult != _prevText) {
      // send partial result to Frame line 1
      await frame!
          .sendMessage(TxPlainText(msgCode: 0x0a, text: _partialResult));
      _prevText = _partialResult;
    }
  }
}