Skip to content

Commit

Permalink
replace riverpod scan method with direct one
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Apr 6, 2024
1 parent 57cb3ee commit e3263ec
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
3 changes: 3 additions & 0 deletions lib/Backend/Bluetooth/bluetooth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ final log.Logger bluetoothLog = log.Logger('Bluetooth');

@riverpod
Stream<DiscoveredDevice> scanForDevices(ScanForDevicesRef ref) {
ref.onDispose(() {
bluetoothLog.fine("Stopping scan");
});
bluetoothLog.fine("Starting scan");
final FlutterReactiveBle bluetoothManagerRef = ref.watch(reactiveBLEProvider);
Stream<DiscoveredDevice> scanStream = bluetoothManagerRef.scanForDevices(withServices: DeviceRegistry.getAllIds(), requireLocationServicesEnabled: false, scanMode: ScanMode.lowPower).asBroadcastStream();
Expand Down
71 changes: 47 additions & 24 deletions lib/Frontend/Widgets/known_gear_scan_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:tail_app/Backend/Definitions/Device/device_definition.dart';
import 'package:tail_app/constants.dart';

import '../../Backend/Bluetooth/bluetooth_manager.dart';
import '../../Backend/device_registry.dart';

final knownGearScanControllerLogger = log.Logger('KnownGearScanController');

Expand All @@ -27,6 +28,7 @@ class _KnownGearScanControllerState extends ConsumerState<KnownGearScanControlle
bool shouldScan = false;
final Duration scanDurationTimeout = const Duration(seconds: 30);
Timer? scanTimeout;
StreamSubscription<DiscoveredDevice>? scanStream;

@override
Widget build(BuildContext context) {
Expand All @@ -45,44 +47,62 @@ class _KnownGearScanControllerState extends ConsumerState<KnownGearScanControlle
if (ref.watch(btStatusProvider).valueOrNull == BleStatus.ready) {
//when running, automatically reconnects to devices
knownGearScanControllerLogger.info("Scanning for gear");
ref.listen(
scanForDevicesProvider,
(previous, next) {},
scanStream ??= ref
.read(reactiveBLEProvider)
.scanForDevices(withServices: DeviceRegistry.getAllIds(), requireLocationServicesEnabled: false, scanMode: ScanMode.lowPower)
.where(
(event) => knownDevices.containsKey(event.id) && knownDevices[event.id]?.deviceConnectionState.value == DeviceConnectionState.disconnected && !knownDevices[event.id]!.disableAutoConnect,
)
.listen(
(DiscoveredDevice event) {
Future(() => ref.read(knownDevicesProvider.notifier).connect(event));
},
);
}
} else {
knownGearScanControllerLogger.info("All devices connected");
if (shouldScan) {
setState(
() {
shouldScan = false;
},
Future(
() => setState(
() {
shouldScan = false;
scanTimeout?.cancel();
scanTimeout = null;
},
),
);
}
scanTimeout?.cancel();
scanTimeout = null;
scanStream?.cancel();
scanStream = null;
}
return Stack(
children: [
AnimatedCrossFade(firstChild: Container(), secondChild: const LinearProgressIndicator(), crossFadeState: shouldScan ? CrossFadeState.showSecond : CrossFadeState.showFirst, duration: animationTransitionDuration),
NotificationListener<OverscrollNotification>(
onNotification: (OverscrollNotification notification) {
knownGearScanControllerLogger.info('Overscroll ${notification.overscroll}');
if (notification.overscroll < 2 && notification.overscroll > -2) {
// ignore, don't do anything
return false;
}
startScanTimer();
return true;
},
child: child!),
],
);
return child!;
},
child: widget.child,
child: Stack(
children: [
AnimatedCrossFade(firstChild: Container(), secondChild: const LinearProgressIndicator(), crossFadeState: shouldScan ? CrossFadeState.showSecond : CrossFadeState.showFirst, duration: animationTransitionDuration),
NotificationListener<OverscrollNotification>(
onNotification: (OverscrollNotification notification) {
knownGearScanControllerLogger.info('Overscroll ${notification.overscroll}');
if (notification.overscroll < 2 && notification.overscroll > -2) {
// ignore, don't do anything
return false;
}
startScanTimer();
return true;
},
child: widget.child),
],
),
);
},
valueListenable: SentryHive.box(settings).listenable(keys: [alwaysScanning]),
child: widget.child,
);
}
scanStream?.cancel();
scanStream = null;
return widget.child;
}

Expand All @@ -102,6 +122,7 @@ class _KnownGearScanControllerState extends ConsumerState<KnownGearScanControlle
},
);
}
scanTimeout = null;
},
);
if (mounted) {
Expand All @@ -110,6 +131,8 @@ class _KnownGearScanControllerState extends ConsumerState<KnownGearScanControlle
shouldScan = true;
},
);
} else {
shouldScan = true;
}
}
}
13 changes: 6 additions & 7 deletions lib/Frontend/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart' as log;
import 'package:permission_handler/permission_handler.dart';
import 'package:pod_player/pod_player.dart';
import 'package:tail_app/constants.dart';
import 'package:url_launcher/url_launcher.dart';

Expand All @@ -21,11 +20,11 @@ class Home extends ConsumerStatefulWidget {
}

class _HomeState extends ConsumerState<Home> {
late final PodPlayerController controller;
//late final PodPlayerController controller;

@override
void initState() {
controller = PodPlayerController(
/*controller = PodPlayerController(
playVideoFrom: PlayVideoFrom.vimeo(
'913642606',
),
Expand All @@ -36,13 +35,13 @@ class _HomeState extends ConsumerState<Home> {
wakelockEnabled: false,
forcedVideoFocus: false,
),
)..initialise();
)..initialise();*/
super.initState();
}

@override
void dispose() {
controller.dispose();
//controller.dispose();
super.dispose();
}

Expand Down Expand Up @@ -148,7 +147,7 @@ class _HomeState extends ConsumerState<Home> {
secondChild: Container(),
crossFadeState: btStatus.valueOrNull == BleStatus.unauthorized ? CrossFadeState.showFirst : CrossFadeState.showSecond,
duration: animationTransitionDuration),
Padding(
/*Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
clipBehavior: Clip.antiAlias,
Expand All @@ -169,7 +168,7 @@ class _HomeState extends ConsumerState<Home> {
),
),
),
),
),*/
],
);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ class RiverpodProviderObserver extends ProviderObserver {
Object? value,
ProviderContainer container,
) {
riverpodLogger.fine('Provider $provider was initialized with $value');
riverpodLogger.info('Provider $provider was initialized with $value');
}

@override
void didDisposeProvider(
ProviderBase<Object?> provider,
ProviderContainer container,
) {
riverpodLogger.fine('Provider $provider was disposed');
riverpodLogger.info('Provider $provider was disposed');
}

@override
Expand All @@ -231,7 +231,7 @@ class RiverpodProviderObserver extends ProviderObserver {
Object? newValue,
ProviderContainer container,
) {
riverpodLogger.fine('Provider $provider updated from $previousValue to $newValue');
riverpodLogger.info('Provider $provider updated from $previousValue to $newValue');
}

@override
Expand Down Expand Up @@ -260,6 +260,8 @@ class _EagerInitialization extends ConsumerWidget {
ref.watch(btConnectStateHandlerProvider);
ref.watch(triggerListProvider);
ref.watch(moveListsProvider);
ref.watch(favoriteActionsProvider);

return child;
}
}

0 comments on commit e3263ec

Please sign in to comment.