Giter Club home page Giter Club logo

hoc081098 / flutter_github_search_rx_redux Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 49.02 MB

๐Ÿ”ฐ Flutter github search using rx_redux | Functional & reactive programming with rxdart, rx_redux

Home Page: https://pub.dev/packages/rx_redux

License: Apache License 2.0

Kotlin 0.14% Swift 1.22% Objective-C 0.04% Dart 55.61% HTML 1.12% Ruby 2.57% CMake 17.47% C++ 20.47% C 1.36%
rxdart rxdart-flutter rxdart-bloc rxdart-epic rxdart-redux flutter-bloc flutter-bloc-pattern flutter-bloc-rxdart flutter-bloc-pattern-rxdart flutter-redux

flutter_github_search_rx_redux's Introduction

flutter_github_search_rx_redux

Flutter

Logo

Demo

Android

MacOS desktop

Effects with RxDart

import 'package:flutter_github_search_rx_redux/domain/search_usecase.dart';
import 'package:rx_redux/rx_redux.dart';
import 'package:rxdart_ext/rxdart_ext.dart';

import 'home_action.dart';
import 'home_state.dart';

RxReduxStore<HomeAction, HomeState> createStore(SearchUseCase searchUseCase) =>
    RxReduxStore(
      initialState: HomeState.initial(),
      sideEffects: HomeSideEffects(searchUseCase)(),
      reducer: (state, action) => action.reduce(state),
      logger: rxReduxDefaultLogger,
    );

class HomeSideEffects {
  final SearchUseCase _searchUseCase;

  HomeSideEffects(this._searchUseCase);

  List<SideEffect<HomeAction, HomeState>> call() => [
        searchActionToTextChangedAction,
        search,
        nextPage,
        retry,
      ];

  Stream<HomeAction> searchActionToTextChangedAction(
    Stream<HomeAction> actions,
    GetState<HomeState> getState,
  ) =>
      actions
          .whereType<SearchAction>()
          .debounceTime(const Duration(milliseconds: 500))
          .map((action) => action.term.trim())
          .where((term) => term.isNotEmpty)
          .distinct()
          .map((term) => TextChangedAction((b) => b..term = term));

  Stream<HomeAction> search(
    Stream<HomeAction> actions,
    GetState<HomeState> getState,
  ) =>
      actions
          .whereType<TextChangedAction>()
          .map((action) => action.term)
          .switchMap((term) => _nextPage(term, 1));

  Stream<HomeAction> nextPage(
    Stream<HomeAction> actions,
    GetState<HomeState> getState,
  ) {
    final textChangedAction$ = actions.whereType<TextChangedAction>().debug();

    final performLoadingNextPage = (LoadNextPageAction action) {
      return Stream.value(getState())
          .where((state) => state.canLoadNextPage)
          .exhaustMap((state) => _nextPage(state.term, state.page + 1)
              .takeUntil(textChangedAction$)
              .debug());
    };

    return actions
        .whereType<LoadNextPageAction>()
        .exhaustMap(performLoadingNextPage);
  }

  Stream<HomeAction> retry(
    Stream<HomeAction> actions,
    GetState<HomeState> getState,
  ) {
    final textChangedAction$ = actions.whereType<TextChangedAction>().debug();

    final performRetry = (RetryAction action) {
      return Stream.value(getState())
          .where((state) => state.canRetry)
          .exhaustMap((state) => _nextPage(state.term, state.page + 1)
              .takeUntil(textChangedAction$)
              .debug());
    };

    return actions.whereType<RetryAction>().exhaustMap(performRetry);
  }

  Stream<HomeAction> _nextPage(String term, int nextPage) {
    final loadingAction = SearchLoadingAction((b) => b
      ..term = term
      ..nextPage = nextPage);

    return Rx.fromCallable(() => _searchUseCase(term: term, page: nextPage))
        .map<HomeAction>(
          (items) => SearchSuccessAction((b) => b
            ..term = term
            ..items.replace(items)),
        )
        .startWith(loadingAction)
        .onErrorReturnWith(
          (e, s) => SearchFailureAction((b) => b
            ..error = e
            ..term = term),
        );
  }
}

flutter_github_search_rx_redux's People

Contributors

dependabot[bot] avatar hoc081098 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.