getImplementationImplementation method

OCClassImplementation getImplementationImplementation()

生成 UniCallback 在实现文件中的类实现

Implementation

OCClassImplementation getImplementationImplementation() {
  return OCClassImplementation(
      className: getName(methodName, paramName),
      injectOCCodeUnit: (depth) {
        return [
          OCFunction(
              functionName: 'onEvent',
              isInstanceMethod: true,
              params: [Variable(paramGeneric, paramName)],
              body: (depth) {
                final ret = <CodeUnit>[];
                ret.add(Comment(depth: depth + 1, comments: ['参数判空检查']));
                ret.add(IfBlock(
                    OneLine(body: '$paramName == NULL', hasNewline: false),
                    (d) => [OneLine(depth: d, body: 'return;')],
                    depth: depth + 1));
                ret.add(OneLine(
                    depth: depth + 1,
                    body: 'FlutterBasicMessageChannel *channel ='));
                ret.add(OneLine(
                    depth: depth + 2, body: '[FlutterBasicMessageChannel'));
                ret.add(OneLine(
                    depth: depth + 3,
                    body:
                        'messageChannelWithName:@"$channelPrefix.UniCallbackManager.callback_channel$channelSuffix"')); // fixme
                ret.add(OneLine(
                    depth: depth + 3,
                    body: 'binaryMessenger:self.binaryMessenger];'));

                if (paramGeneric.realType() is AstCustomType) {
                  ret.add(OneLine(
                      depth: depth + 1,
                      body:
                          'NSDictionary *msg = @{@"callbackName":self.callbackName,@"data":$paramName.toMap};'));
                } else {
                  ret.add(OneLine(
                      depth: depth + 1,
                      body:
                          'NSDictionary *msg = @{@"callbackName":self.callbackName,@"data":$paramName};'));
                }
                ret.add(Comment(depth: depth + 1, comments: ['发送消息']));
                ret.add(OneLine(
                    depth: depth + 1, body: '[channel sendMessage:msg];'));
                return ret;
              })
        ];
      });
}