choose_image_handler.dart 1.32 KB
import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/services/dispatcher.dart';

class ChooseImageHandler extends MessageHandler {
  late WebCubit? _webCubit;
  late String? _message;

  @override
  void setCubit(WebCubit cubit) {
    this._webCubit = cubit;
  }

  void _unfollowCubit() {
    this._webCubit = null;
  }

  @override
  void setMessage(String message) {
    this._message = message;
  }

  @override
  Future<dynamic> handleMessage(params) async {
    try {
      if (params is! Map<String, dynamic>) {
        throw Exception('参数错误');
      }
      var sourceType = params['sourceType'] as String;
      if (sourceType != 'album' && sourceType != 'camera') {
        throw Exception('参数错误');
      }
      // 暂时忽略对此参数的处理
      List<dynamic>? sizeType;
      if (params.containsKey('sizeType')) {
        sizeType = params['sizeType'] as List<dynamic>;
        if (sizeType.isEmpty || sizeType.length > 2) {
          throw Exception('参数错误');
        }
      }

      int count = 9;
      if (params.containsKey('count')) {
        count = params['count'] as int;
        if (count < 1 || count > 9) {
          throw Exception('参数错误');
        }
      }

      _webCubit!.setChooseImageCmdFlag(true, _message!);
    } finally {
      _unfollowCubit();
    }
  }
}