updateBlocEventAndStates method

Future<void> updateBlocEventAndStates(
  1. {required String featureName,
  2. required String requestModelName,
  3. required String responseModelName,
  4. required String apiName,
  5. required String apiMethodName,
  6. required String shimmerChoice}
)

Implementation

Future<void> updateBlocEventAndStates(
    {required String featureName,
    required String requestModelName,
    required String responseModelName,
    required String apiName,
    required String apiMethodName,
    required String shimmerChoice}) async {
  String featureNameString = featureName.toLowerCase();
  String pascalCaseApiName = Utility.toPascalCase(input: apiName);
  String name = Utility.getPubspecInfo() ?? '';
  String mockEndPoint = "$apiName$kJson";
  String featureNamePascalCase =
      Utility.toPascalCase(input: featureNameString);
  String destinationPathEvent =
      PathHandler().getEventPath(featureName: featureName);
  List<String> eventImportStatements = [];
  if (requestModelName.isNotEmpty) {
    eventImportStatements.add(PathHandler().getRequestModelImportFile(
        featureName: featureName, requestModelName: requestModelName));
  }
  if (Utility.doesFileExist(filePath: destinationPathEvent)) {
    List<String> eventAppendLines = [];
    if (requestModelName.isNotEmpty) {
      eventAppendLines.add(CodeHandler().getEventStatementWithResponseModel(
          pascalCaseApiName: pascalCaseApiName,
          featureNamePascalCase: featureNamePascalCase,
          requestModelName: requestModelName));
    } else {
      eventAppendLines.add(CodeHandler()
          .getEventStatementWithoutResponseModel(
              pascalCaseApiName: pascalCaseApiName,
              featureNamePascalCase: featureNamePascalCase));
    }

    await fileUpdate(
        destinationPathEvent, eventImportStatements, eventAppendLines, true);
    Utility.successPrinter(text: '$kApiCodeUpdate $kEvent $kFile');
  }

  /// update state.dart to add imports statement and function method
  String destinationPathState =
      PathHandler().getStatePath(featureName: featureName);
  if (Utility.doesFileExist(filePath: destinationPathState)) {
    List<String> importStateStatements = [];
    if (responseModelName.isNotEmpty) {
      importStateStatements = [
        PathHandler().getResponseModelImport(
            featureName: featureName, responseModelName: responseModelName)
      ];
    }

    List<String> stateSourceAppendLines = [];

    if (responseModelName.isNotEmpty) {
      stateSourceAppendLines = CodeHandler()
          .getStateStatementsWithRequestModel(
              pascalCaseApiName: pascalCaseApiName,
              responseModelName: responseModelName,
              featureNamePascalCase: featureNamePascalCase);
    } else {
      stateSourceAppendLines = CodeHandler()
          .getStateStatementsWithoutRequestModel(
              pascalCaseApiName: pascalCaseApiName,
              featureNamePascalCase: featureNamePascalCase);
    }

    if (shimmerChoice == kYes.toLowerCase()) {
      stateSourceAppendLines.add(CodeHandler()
          .getStateStatementShimmerChoiceY(
              pascalCaseApiName: pascalCaseApiName,
              featureNamePascalCase: featureNamePascalCase));
    }
    await fileUpdate(destinationPathState, importStateStatements,
        stateSourceAppendLines, true);
    Utility.successPrinter(text: '$kApiCodeUpdate $kState $kFile');
  }

  /// update bloc.dart to add imports statement and function signature
  String useCaseName = "$apiName$kUseCasePascal";
  String useCaseFileName = "${apiName}$kUseCasesPrefixUnderscore";
  String destinationPathBloc =
      PathHandler().getBlocPath(featureName: featureName);
  if (Utility.doesFileExist(filePath: destinationPathBloc)) {
    List<String> importBlocImplStatements = [
      "",
      PathHandler().getDartzImport(featureName: featureName),
      PathHandler().getFailureImport(featureName: featureName),
      PathHandler().getSuccessImport(featureName: featureName),
      PathHandler().getFlutterBlocPath(featureName: featureName),
      PathHandler().getUseCaseFileImport(
          featureName: featureName, useCaseFileName: useCaseFileName)
    ];

    if (responseModelName.isNotEmpty) {
      importBlocImplStatements.add(PathHandler().getResponseModelImport(
          featureName: featureName, responseModelName: responseModelName));
    } else {
      importBlocImplStatements
          .add(PathHandler().getNoRequestImport(featureName: featureName));
    }

    List<String> blockAppendLines = [];

    String usCaseEvent =
        CodeHandler().getUseCaseEventNoRequest(useCaseName: useCaseName);
    if (requestModelName.isNotEmpty) {
      usCaseEvent = CodeHandler()
          .getUseCaseEventWithRequestModel(useCaseName: useCaseName);
    }

    if (shimmerChoice == kYes.toLowerCase()) {
      blockAppendLines = CodeHandler().getBlocStatementsShimmerChoiceY(
          pascalCaseApiName: pascalCaseApiName,
          apiName: apiName,
          usCaseEvent: usCaseEvent);
    } else {
      importBlocImplStatements.add(
          PathHandler().getApiBaseBlocEventImport(featureName: featureName));
      blockAppendLines = CodeHandler().getBlocStatementsShimmerChoiceN(
          pascalCaseApiName: pascalCaseApiName,
          apiName: apiName,
          usCaseEvent: usCaseEvent);
    }
    if (responseModelName.isNotEmpty) {
      String responseModel = camelCase(responseModelName);
      String responseName =
          responseModel[0].toLowerCase() + responseModel.substring(1);
      blockAppendLines.add(CodeHandler().getBlocStatementWithResponseModel(
          responseModelName: responseModelName,
          responseName: responseName,
          pascalCaseApiName: pascalCaseApiName));
    } else {
      blockAppendLines.add(CodeHandler().getBlocStatementWithoutResponseModel(
          pascalCaseApiName: pascalCaseApiName));
    }

    await fileUpdate(
        destinationPathBloc, importBlocImplStatements, blockAppendLines);

    List<String> linesToAddAfterSuperApiBaseBloc = CodeHandler()
        .getAddBlocAfterSuperApiBaseBloc(
            pascalCaseApiName: pascalCaseApiName, apiName: apiName);

    await appendLine(
        destinationPathBloc,
        linesToAddAfterSuperApiBaseBloc,
        CodeHandler().getBlocAppendAfterContainString(
            pascalCaseApiName: pascalCaseApiName));

    List<String> linesToAddBeforeSuperApiBaseBloc = CodeHandler()
        .getAddBlocBeforeSuperApiBaseBloc(useCaseName: useCaseName);

    await appendLine(
        destinationPathBloc,
        linesToAddBeforeSuperApiBaseBloc,
        CodeHandler()
            .getBlocSuperApiBaseBloc(pascalCaseApiName: pascalCaseApiName),
        isAfter: false);

    List<String> linesToAddUseCaseDeclaration = [
      "    $kFinal ${camelCase(useCaseName)} $useCaseName;",
    ];

    await appendLine(
        destinationPathBloc,
        linesToAddUseCaseDeclaration,
        CodeHandler()
            .getExtendsBaseBloc(pascalCaseApiName: pascalCaseApiName));
    Utility.successPrinter(text: '$kApiCodeUpdate $kBloc $kFile');
  }
}