safeCallObjectStatus<T> method

Future<void> safeCallObjectStatus<T>({
  1. required Either<FetchFailure, BaseObjectResponse<T?>> response,
  2. required dynamic funError(
    1. FetchFailure
    ),
  3. required dynamic funDataServer(
    1. FetchData<T>
    ),
  4. dynamic funSuccess(
    1. FetchData<T>
    )?,
  5. String keyCache = "",
  6. dynamic funDataCache(
    1. T?
    )?,
  7. bool isShowError = true,
})

Implementation

Future<void> safeCallObjectStatus<T>(
    {required Either<FetchFailure, BaseObjectResponse<T?>> response,
    required Function(FetchFailure) funError,
    required Function(FetchData<T>) funDataServer,
    Function(FetchData<T>)? funSuccess,
    String keyCache = "",
    Function(T?)? funDataCache,
    bool isShowError = true}) async {
  try {
    if (funDataCache != null && keyCache.isNotEmpty == true) {
      await safeResponseDataCache<T>(
          keyCache: keyCache,
          funCallBack: (data) {
            isShowError = false;
            funDataCache(data);
          });
    }
    if (response.get != null) {
      if (response.get?.success == true) {
        safeResponseDataServer<T>(
            dataServer: response.get?.data,
            funCallBack: (data) async {
              if (data != null) {
                safeDataCache(
                    isDataCache: keyCache.isNotEmpty == true,
                    funCallBack: () {
                      saveCacheData(key: keyCache, data: data);
                    });
                final msg = response.get?.message;
                final codeName = response.get?.code_name;
                if (funSuccess != null) {
                  funSuccess(
                      FetchData<T>(data: data, msg: msg, codeName: codeName));
                }
                return funDataServer(
                    FetchData<T>(data: data, msg: msg, codeName: codeName));
              } else {
                return funError(await safeResponseError(
                    typeError: TypeError.ERROR_NO_DATA,
                    isShowError: isShowError));
              }
            });
      } else {
        final msg = response.get?.message.toString() ?? "Error";
        final data = response.get?.data.toString() ?? "Error";
        return funError(await safeResponseError(
            typeError: TypeError.ERROR_WARNING,
            errorServer: FetchFailure.warningError(200, [msg], data),
            isShowError: isShowError));
      }
    } else {
      return funError(await safeResponseError(
          typeError: TypeError.ERROR_SERVER,
          errorServer: response.leftOrCrash,
          isShowError: isShowError));
    }
  } catch (e) {
    final failure = returnFailure(e);
    return funError(await safeResponseError(
        typeError: TypeError.ERROR_UNKNOWN,
        errorServer: FetchFailure.crashError(100, failure, "Error"),
        isShowError: isShowError));
  }
}