createPresentationBloc method

void createPresentationBloc(
  1. String pathPage,
  2. String pageName,
  3. String apiName,
  4. bool bodyList,
  5. bool responseList,
)

Implementation

void createPresentationBloc(
  String pathPage,
  String pageName,
  String apiName,
  bool bodyList,
  bool responseList,
) {
  final apiClassName = apiName.pascalCase;

  final path = join(pathPage, 'presentation', 'bloc', apiName);
  DirectoryHelper.createDir(path);

  final bodyClass = getBodyClass(apiClassName, bodyList);
  final entityClass = getEntityClass(apiClassName, responseList);

  join(path, '${apiName}_state.dart').write('''part of '${apiName}_bloc.dart';

@immutable
abstract class ${apiClassName}State extends Equatable {
bool get isInitial => this is ${apiClassName}Initial;
bool get isLoading => this is ${apiClassName}Loading;
bool get isFailed => this is ${apiClassName}Failed;
bool get isSuccess => this is ${apiClassName}Success;

bool get isNotInitial => this is! ${apiClassName}Initial;
bool get isNotLoading => this is! ${apiClassName}Loading;
bool get isNotFailed => this is! ${apiClassName}Failed;
bool get isNotSuccess => this is! ${apiClassName}Success;

void when({
  void Function(${apiClassName}Initial state)? onInitial,
  void Function(${apiClassName}Loading state)? onLoading,
  void Function(${apiClassName}Failed state)? onFailed,
  void Function(${apiClassName}Success state)? onSuccess,
}) {
  final state = this;
  if (state is ${apiClassName}Initial) {
    onInitial?.call(state);
  } else if (state is ${apiClassName}Loading) {
    onLoading?.call(state);
  } else if (state is ${apiClassName}Failed) {
    onFailed?.call(state);
  } else if (state is ${apiClassName}Success) {
    onSuccess?.call(state);
  }
}

Widget builder({
  Widget Function(${apiClassName}Initial state)? onInitial,
  Widget Function(${apiClassName}Loading state)? onLoading,
  Widget Function(${apiClassName}Failed state)? onFailed,
  Widget Function(${apiClassName}Success state)? onSuccess,
  Widget Function(${apiClassName}State state)? onStateBuilder,
}) {
  final state = this;
  final defaultWidget = onStateBuilder?.call(this) ?? const SizedBox.shrink();

  if (state is ${apiClassName}Initial) {
    return onInitial?.call(state) ?? defaultWidget;
  } else if (state is ${apiClassName}Loading) {
    return onLoading?.call(state) ?? defaultWidget;
  } else if (state is ${apiClassName}Failed) {
    return onFailed?.call(state) ?? defaultWidget;
  } else if (state is ${apiClassName}Success) {
    return onSuccess?.call(state) ?? defaultWidget;
  } else {
    return defaultWidget;
  }
}
}

class ${apiClassName}Initial extends ${apiClassName}State {
@override
List<Object?> get props => [];
}

class ${apiClassName}Loading extends ${apiClassName}State {
 ${apiClassName}Loading(this.body, this.headers, this.extra,);

final $bodyClass body;
final Map<String, String>? headers;
final dynamic extra;

@override
List<Object?> get props => [body, headers, extra,];
}

class ${apiClassName}Success extends ${apiClassName}State {
${apiClassName}Success(this.body, this.headers, this.data, this.extra,);

final $bodyClass body;
final Map<String, String>? headers;
final $entityClass data;
final dynamic extra;

@override
List<Object?> get props => [body, headers, data, extra,];
}

class ${apiClassName}Failed extends ${apiClassName}State {
${apiClassName}Failed(this.body, this.headers, this.failure, this.extra,);

final $bodyClass body;
final Map<String, String>? headers;
final MorphemeFailure failure;
final dynamic extra;

@override
List<Object?> get props => [body, headers, failure, extra,];
}''');

  join(path, '${apiName}_event.dart').write('''part of '${apiName}_bloc.dart';

@immutable
abstract class ${apiClassName}Event extends Equatable {}

class Fetch$apiClassName extends ${apiClassName}Event {
Fetch$apiClassName(this.body, {this.headers, this.extra,});

final $bodyClass body;
final Map<String, String>? headers;
final dynamic extra;

@override
List<Object?> get props => [body, headers, extra,];
}''');

  join(path, '${apiName}_bloc.dart').write('''import 'package:core/core.dart';
import 'package:flutter/material.dart';

import '../../../data/models/body/${apiName}_body.dart';
import '../../../domain/entities/${apiName}_entity.dart';
import '../../../domain/usecases/${apiName}_use_case.dart';

part '${apiName}_event.dart';
part '${apiName}_state.dart';

class ${apiClassName}Bloc extends Bloc<${apiClassName}Event, ${apiClassName}State> {
${apiClassName}Bloc({
  required this.useCase,
}) : super(${apiClassName}Initial()) {
  on<Fetch$apiClassName>((event, emit) async {
    emit(${apiClassName}Loading(event.body, event.headers, event.extra,));
    final result = await useCase(event.body, headers: event.headers,);
    emit(
      result.fold(
        (failure) => ${apiClassName}Failed(event.body, event.headers, failure, event.extra,),
        (success) => ${apiClassName}Success(event.body, event.headers, success, event.extra,),
      ),
    );
  });
}

final ${apiClassName}UseCase useCase;
}''');

  StatusHelper.generated(join(path, '${apiName}_state.dart'));
  StatusHelper.generated(join(path, '${apiName}_event.dart'));
  StatusHelper.generated(join(path, '${apiName}_bloc.dart'));
}