safeCallListStatus<T> method

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

Implementation

Future<void> safeCallListStatus<T>(
    {required Either<FetchFailure, BaseListResponse<T>?> response,
    required Function(FetchFailure) funError,
    required Function(FetchData<List<T>>) funDataServer,
    Function(FetchData<List<T>>)? funSuccess,
    bool isLoadCache = true,
    String keyCache = "",
    Function(List<T>?)? funDataCache,
    bool isShowError = true}) async {
  try {
    if (funDataCache != null && keyCache.isNotEmpty == true && isLoadCache) {
      await safeResponseDataCache<List<T>>(
          keyCache: keyCache,
          funCallBack: (data) {
            isShowError = false;
            funDataCache(data);
          });
    }
    if (response.get != null) {
      if (response.get?.success == true) {
        await safeResponseDataServer<List<T>>(
            dataServer: response.get?.data,
            funCallBack: (data) async {
              if (data?.isNotEmpty == true) {
                safeDataCache(
                    isDataCache: keyCache.isNotEmpty == true && isLoadCache,
                    funCallBack: () {
                      saveCacheData(key: keyCache, data: data);
                    });
                final msg = response.get?.message;
                if (funSuccess != null) {
                  funSuccess(FetchData<List<T>>(
                      data: data, paging: response.get?.meta_data, msg: msg));
                }
                return funDataServer(FetchData<List<T>>(
                    data: data, paging: response.get?.meta_data, msg: msg));
              } else {
                return funDataServer(FetchData<List<T>>(data: []));
              }
            });
      } 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));
  }
}