processAction method

Future<NUIMiniProgramMessage?> processAction(
  1. BuildContext context,
  2. String message,
  3. NUIMiniProgramMessage msg, {
  4. Function? initializeListener,
  5. required NUIMiniProgramSessionBuilder sessionBuilder,
  6. required NUIMiniProgramMessageExchangeBuilder messageExchangeBuilder,
})

Processing the action

Implementation

Future<NUIMiniProgramMessage?> processAction(
    BuildContext context,
    String message,
    NUIMiniProgramMessage msg,
    {
      Function? initializeListener,
      required NUIMiniProgramSessionBuilder sessionBuilder, // For session exchange
      required NUIMiniProgramMessageExchangeBuilder messageExchangeBuilder, // For custom message exchange
    }) async{

  try {
    final internalMessage = NUIMiniProgramInternalMessage.empty().fromJson(jsonDecode(message));
    switch(internalMessage.type){
      case NUIMiniProgramEventType.SESSION: {
        // Processing the current session details and return to mini-app
        final session = await _session(sessionBuilder);
        return msg.response(data: session);
      }
      case NUIMiniProgramEventType.POP: {
        // Popping the current screen
        pop(context);
        return null;
      }
      case NUIMiniProgramEventType.TOAST: {
        // Toasting a message
        toast(message, context: context);
        return null;
      }
      case NUIMiniProgramEventType.FILE_SELECT: {
        // Performing file selection
        final selectedFile = await _selectFile();
        return msg.response(data: selectedFile);
      }
      case NUIMiniProgramEventType.HIDE_KEYBOARD: {
        // Hiding the keyboard
        FocusScope.of(context).unfocus();
        return null;
      }
      case NUIMiniProgramEventType.STATUS_BAR_LIGHT: {
        // Changing the status bar style to light
        NUIDisplayUtil.setStatusBarStyleAttr(NUIOverlayStyle(darkStatusBarIcon: false), internal: true);
        return null;
      }
      case NUIMiniProgramEventType.STATUS_BAR_DARK: {
        // Changing the status bar style to dark
        NUIDisplayUtil.setStatusBarStyleAttr(NUIOverlayStyle(darkStatusBarIcon: true), internal: true);
        return null;
      }
      case NUIMiniProgramEventType.INITIALIZED: {
        // Sending the initialized event
        if(initializeListener != null) initializeListener();
        return null;
      }
      case NUIMiniProgramEventType.CAMERA: {
        // Taking a photo
        final selectedFile = await _takePhoto(rear: match(internalMessage.data, "true"));
        return msg.response(data: selectedFile);
      }
      case NUIMiniProgramEventType.VIDEO: {
        // Capturing a video
        final selectedFile = await _captureVideo(rear: match(internalMessage.data, "true"));
        return msg.response(data: selectedFile);
      }
      case NUIMiniProgramEventType.CUSTOM: {
        // Custom message exchange
        final result = await _messageExchange(jsonDecode(internalMessage.data), messageExchangeBuilder);
        return msg.response(data: result);
      }
    }
  }catch(e, s){
    logNUI("NUIMiniProgramActionUtil", "Failed to parse NUIMiniProgramInternalMessage with error : $e, stacktrace: $s");
  }
  return null;
}