mergeWithPaginatedList method

Stream<PaginatedList<T>> mergeWithPaginatedList(
  1. BehaviorSubject<PaginatedList<T>> paginatedList
)

Convenience method that maps the data from the paginated list to a stream, handling error, loading and success states

Implementation

Stream<PaginatedList<T>> mergeWithPaginatedList(
  BehaviorSubject<PaginatedList<T>> paginatedList,
) =>
    map<PaginatedList<T>>((result) {
      // Get the current paginated list data
      final subjectValue = paginatedList.hasValue
          ? paginatedList.value
          : PaginatedList<T>(
              list: [],
              pageSize: 0,
              isLoading: false,
            );

      switch (result) {
        // If the data is still being fetched/loading, respond with isLoading as true
        case ResultLoading<PaginatedList<T>>():
          if (subjectValue._backupList.isNotEmpty) {
            return subjectValue.copyWith(
              isLoading: true,
              list: subjectValue._backupList,
            );
          }

          return subjectValue.copyWith(
            isLoading: true,
            isInitialized: false,
          );

        // If an error occurred, pass this error down and mark loading as false
        case ResultError<PaginatedList<T>>():
          subjectValue._backupList.clear();
          return subjectValue.copyWith(
            isLoading: false,
            isInitialized: false,
            error: result.error,
            list: [],
          );

        // If we got the resulting data successfully, merge and return it
        case ResultSuccess<PaginatedList<T>>():
          // Have we previously reset data. If yes clear the temporary data
          final isReset = subjectValue._backupList.isNotEmpty;
          final listData = isReset
              ? <T>[...result.data.list]
              : <T>[
                  ...subjectValue.list,
                  ...result.data.list,
                ];
          if (isReset) {
            subjectValue.length = 0;
            subjectValue._backupList.clear();
          }

          return subjectValue.copyWith(
            totalCount: result.data.totalCount,
            pageSize: result.data.pageSize,
            list: listData,
            isLoading: false,
            isInitialized: true,
          );
      }
    }).distinct();