Skip to content

Commit

Permalink
update card_repository: uses Single
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Jun 18, 2021
1 parent cdea06a commit 53009bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
51 changes: 26 additions & 25 deletions MobileApp/datn/lib/data/repository/card_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,39 @@ class CardRepositoryImpl implements CardRepository {
CardRepositoryImpl(this._authClient, this._cardResponseToCard);

@override
Stream<BuiltList<Card>> getCards() async* {
final json = await _authClient.getJson(buildUrl('/cards')) as List;
yield json
.map((e) => CardResponse.fromJson(e))
.map(_cardResponseToCard)
.toBuiltList();
}
Single<BuiltList<Card>> getCards() =>
Single.fromCallable(() => _authClient.getJson(buildUrl('/cards')))
.cast<List>()
.map((json) => json
.map((e) => CardResponse.fromJson(e))
.map(_cardResponseToCard)
.toBuiltList());

@override
Stream<Card> removeCard(Card card) =>
Rx.fromCallable(() => _authClient.delete(buildUrl('/cards/${card.id}')))
.mapTo(card);
Single<Card> removeCard(Card card) => Single.fromCallable(
() => _authClient.delete(buildUrl('/cards/${card.id}')))
.mapTo(card)
.singleOrError();

@override
Stream<Card> addCard({
Single<Card> addCard({
required String cardHolderName,
required String number,
required int cvc,
required int expYear,
required int expMonth,
}) async* {
final body = {
'card_holder_name': cardHolderName,
'number': number,
'cvc': cvc.toString(),
'exp_month': expMonth,
'exp_year': expYear,
};
final json = await _authClient.postJson(
buildUrl('/cards'),
body: body,
);
yield _cardResponseToCard(CardResponse.fromJson(json));
}
}) =>
Single.fromCallable(() {
final body = {
'card_holder_name': cardHolderName,
'number': number,
'cvc': cvc.toString(),
'exp_month': expMonth,
'exp_year': expYear,
};
return _authClient.postJson(
buildUrl('/cards'),
body: body,
);
}).map((json) => CardResponse.fromJson(json)).map(_cardResponseToCard);
}
7 changes: 4 additions & 3 deletions MobileApp/datn/lib/domain/repository/card_repository.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:built_collection/built_collection.dart';
import 'package:rxdart_ext/rxdart_ext.dart';

import '../model/card.dart';

abstract class CardRepository {
Stream<BuiltList<Card>> getCards();
Single<BuiltList<Card>> getCards();

Stream<Card> removeCard(Card card);
Single<Card> removeCard(Card card);

Stream<Card> addCard({
Single<Card> addCard({
required String cardHolderName,
required String number,
required int cvc,
Expand Down
12 changes: 6 additions & 6 deletions MobileApp/datn/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -355,7 +355,7 @@ packages:
name: flutter_bloc_pattern
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
flutter_blurhash:
dependency: transitive
description:
Expand Down Expand Up @@ -811,7 +811,7 @@ packages:
name: rxdart_ext
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1-dev.0"
version: "0.1.1-dev.1"
shared_preferences:
dependency: "direct main"
description:
Expand Down Expand Up @@ -900,7 +900,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sqflite:
dependency: transitive
description:
Expand Down Expand Up @@ -935,7 +935,7 @@ packages:
name: stream_loader
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
stream_transform:
dependency: transitive
description:
Expand Down Expand Up @@ -970,7 +970,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
timezone:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions MobileApp/datn/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ dependencies:
rx_shared_preferences: ^2.1.0
disposebag: ^1.5.0
flutter_disposebag: ^1.1.0
flutter_bloc_pattern: ^2.1.0
rxdart_ext: ^0.1.1-dev.0
flutter_bloc_pattern: ^2.1.1
rxdart_ext: ^0.1.1-dev.1
listenable_stream: ^1.1.0
stream_loader: ^1.2.0
stream_loader: ^1.2.1
flutter_google_places_hoc081098: ^1.0.0-nullsafety.2
utils:
path: ../utils
Expand Down

0 comments on commit 53009bd

Please sign in to comment.