Skip to content

Commit a40ada0

Browse files
committed
refactor: simplify IsAdvertising to a StreamProvider and handle AsyncValue in UI
1 parent c6a3811 commit a40ada0

3 files changed

Lines changed: 5 additions & 14 deletions

File tree

lib/src/features/discovery/presentation/advertising_state.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,9 @@ class CanAdvertise extends _$CanAdvertise {
2222
}
2323

2424
@riverpod
25-
class IsAdvertising extends _$IsAdvertising {
26-
final FlutterBackgroundService _service = FlutterBackgroundService();
27-
28-
@override
29-
bool build() {
30-
_service.on("advertisingChange").listen((event) {
31-
final bool? active = event?["active"];
32-
if (active != null) state = active;
33-
});
34-
35-
return false;
36-
}
25+
Stream<bool> isAdvertising(Ref ref) {
26+
final service = FlutterBackgroundService();
27+
return service.on("advertisingChange").map((event) => event?["active"] ?? false);
3728
}
3829

3930
@riverpod

lib/src/features/discovery/presentation/discovery_screen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class _DiscoveryScreenState extends ConsumerState<DiscoveryScreen> {
146146
@override
147147
Widget build(BuildContext context) {
148148
final devicesAsync = ref.watch(discoveryControllerProvider);
149-
final isAdvertising = ref.watch(isAdvertisingProvider);
149+
final isAdvertising = ref.watch(isAdvertisingProvider).value ?? false;
150150
final scanProgress = ref.watch(scanProgressProvider).value ?? 0.0;
151151

152152
return Scaffold(

lib/src/features/profile/presentation/profile_screen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class _ProfileScreenState extends ConsumerState<ProfileScreen> {
4747
@override
4848
Widget build(BuildContext context) {
4949
final profileAsync = ref.watch(profileControllerProvider);
50-
final isAdvertising = ref.watch(isAdvertisingProvider);
50+
final isAdvertising = ref.watch(isAdvertisingProvider).value ?? false;
5151

5252
return Scaffold(
5353
body: profileAsync.when(

0 commit comments

Comments
 (0)