Skip to content

Commit 0c0bb1b

Browse files
committed
refactor: simplify stream code
1 parent 36d8ab9 commit 0c0bb1b

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

lib/features/pirate_coins/application/coins_service.dart

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "package:riverpod_annotation/riverpod_annotation.dart";
55

66
import "../../auth/application/auth_service.dart";
77
import "../data/coins_repository.dart";
8-
import "../domain/coin_entity.dart";
98
import "../domain/coins_model.dart";
109

1110
part "coins_service.g.dart";
@@ -15,9 +14,9 @@ part "coins_service.g.dart";
1514
base class CoinsService extends _$CoinsService {
1615
@override
1716
FutureOr<CoinsModel> build(int userId) async {
18-
final coin = await ref.watch(coinStreamProvider(userId).future);
17+
final coins = await ref.watch(coinStreamProvider(userId).future);
1918

20-
return CoinsModel(coins: coin);
19+
return CoinsModel(coins: coins);
2120
}
2221

2322
/// Modify coins in the database.
@@ -43,12 +42,10 @@ base class CoinsService extends _$CoinsService {
4342

4443
/// Get the coins of the current user.
4544
@riverpod
46-
Stream<CoinsModel> currentUserCoins(CurrentUserCoinsRef ref) async* {
47-
final userId = await ref.watch(
48-
pirateAuthServiceProvider.selectAsync((data) => data.user.id),
49-
);
45+
Future<CoinsModel> currentUserCoins(CurrentUserCoinsRef ref) async {
46+
final userId = await ref.watch(idProvider.future);
5047

51-
yield await ref.read(coinsServiceProvider(userId).future);
48+
return ref.read(coinsServiceProvider(userId).future);
5249
}
5350

5451
/// Get coins data from data layer.
@@ -68,11 +65,3 @@ base class CurrentStage extends _$CurrentStage {
6865
state = const Stage.pickStudent();
6966
}
7067
}
71-
72-
/// Get the coins stream
73-
@riverpod
74-
Stream<CoinEntity> coinStream(CoinStreamRef ref, int userId) async* {
75-
final coinsDataRepository = ref.read(coinsDataProvider(userId));
76-
77-
yield* coinsDataRepository.coinsData().asBroadcastStream();
78-
}

lib/features/pirate_coins/data/coins_repository.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,12 @@ class _Channel extends _$Channel {
123123
return sub.stream;
124124
}
125125
}
126+
127+
/// Get the coins stream.
128+
/// Used to coerce the stream into a [AsyncValue].
129+
@riverpod
130+
Stream<CoinEntity> coinStream(CoinStreamRef ref, int userId) async* {
131+
final coinsDataRepository = ref.read(coinsDataProvider(userId));
132+
133+
yield* coinsDataRepository.coinsData();
134+
}

0 commit comments

Comments
 (0)