xmartlabs / stock

Dart package for Async Data Loading and Caching. Combine local (DB, cache) and network data simply and safely.
https://pub.dev/packages/stock
Apache License 2.0
74 stars 6 forks source link

Add `map` and `when` extension methods over `StockResponse` #21

Closed leynier closed 1 year ago

leynier commented 2 years ago

Description

The goal is to use the StockResponse in a more declarative way by using extension methods on StockResponse.

Methods

Tests

Example

// Create a stream to listen tweet changes of the user `xmartlabs`
stock
    .stream('xmartlabs', refresh: true)
    .listen((StockResponse<List<Tweet>> stockResponse) {
  if (stockResponse is StockResponseLoading) {
    _displayLoadingIndicator();
  } else if (stockResponse is StockResponseData) {
    _displayTweetsInUI(stockResponse.requireData());
  } else {
    _displayErrorInUi((stockResponse as StockResponseError).error);
  }
});

With when:

// Create a stream to listen tweet changes of the user `xmartlabs`
stock
  .stream('xmartlabs', refresh: true)
  .listen((StockResponse<List<Tweet>> stockResponse) => stockResponse.when(
        onLoading: _displayLoadingIndicator,
        onData: _displayTweetsInUI,
        onError: (error, stackTrace) => _displayErrorInUi(error),
      ));
mirland commented 1 year ago

Hey @leynier, how are you doing? Could you check my comment? Ping me if you have some question/comment.

We’re planning to release the v1.0 and it would be great to include your feature!

leynier commented 1 year ago

Hi @mirland, sorry for the late response, I had a blackout for several days due to hurricane Ian.

I could have the tests and the origin parameter addition ready by early next week.

Best regards.

mirland commented 1 year ago

Cool, that great! Again thanks for your contribution!

codecov-commenter commented 1 year ago

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage :thumbsup:

Coverage data is based on head (aef9386) compared to base (4befd40). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #21 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 14 14 Lines 199 199 ========================================= Hits 199 199 ``` | [Impacted Files](https://codecov.io/gh/xmartlabs/stock/pull/21?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xmartlabs) | Coverage Δ | | |---|---|---| | [lib/src/stock\_response\_extensions.dart](https://codecov.io/gh/xmartlabs/stock/pull/21/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xmartlabs#diff-bGliL3NyYy9zdG9ja19yZXNwb25zZV9leHRlbnNpb25zLmRhcnQ=) | `100.00% <100.00%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xmartlabs). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xmartlabs)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

leynier commented 1 year ago

@mirland ready for review ✅