Skip to content

Commit

Permalink
Tons of changes for device management & clean up theme code
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Feb 20, 2024
1 parent 79d1e6e commit 17f289e
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 304 deletions.
6 changes: 3 additions & 3 deletions lib/Backend/Bluetooth/BluetoothManager.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:async';
import 'dart:convert';

import 'package:awesome_snackbar_content/awesome_snackbar_content.dart';
import 'package:collection/collection.dart';
import 'package:cross_platform/cross_platform.dart';
import 'package:flutter/material.dart';
import 'package:flutter_foreground_service/flutter_foreground_service.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -23,7 +23,7 @@ part 'BluetoothManager.g.dart';
Stream<DiscoveredDevice> scanForDevices(ScanForDevicesRef ref) {
Flogger.d("Starting scan");
final FlutterReactiveBle bluetoothManagerRef = ref.watch(reactiveBLEProvider);
Stream<DiscoveredDevice> scanStream = bluetoothManagerRef.scanForDevices(withServices: DeviceRegistry.getAllIds()).asBroadcastStream();
Stream<DiscoveredDevice> scanStream = bluetoothManagerRef.scanForDevices(withServices: DeviceRegistry.getAllIds(), requireLocationServicesEnabled: false, scanMode: ScanMode.lowPower).asBroadcastStream();
scanStream.listen((DiscoveredDevice event) {
if (ref.read(knownDevicesProvider).containsKey(event.id) && ref.read(knownDevicesProvider)[event.id]?.deviceConnectionState.value == DeviceConnectionState.disconnected) {
ref.read(knownDevicesProvider.notifier).connect(event);
Expand Down Expand Up @@ -172,7 +172,7 @@ StreamSubscription<ConnectionStateUpdate> btConnectStateHandler(BtConnectStateHa
knownDevices[event.deviceId]?.keepAliveStreamSubscription = null;
knownDevices[event.deviceId]?.battery.value = -1;
knownDevices[event.deviceId]?.rssi.value = -1;
ref.read(snackbarStreamProvider.notifier).add(AwesomeSnackbarContent(title: "Disconnected", message: "Disconnected from ${knownDevices[event.deviceId]?.baseStoredDevice.name}", contentType: ContentType.warning));
ref.read(snackbarStreamProvider.notifier).add(SnackBar(content: Text("Disconnected from ${knownDevices[event.deviceId]?.baseStoredDevice.name}")));
//remove foreground service if no devices connected
if (Platform.isAndroid && knownDevices.values.where((element) => element.deviceConnectionState.value == DeviceConnectionState.connected).isEmpty) {
ForegroundService().stop();
Expand Down
37 changes: 18 additions & 19 deletions lib/Frontend/Widgets/scan_for_new_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ScanForNewDevice extends ConsumerStatefulWidget {
}

class _ScanForNewDevice extends ConsumerState<ScanForNewDevice> {
final ScrollController _controller = ScrollController();
Map<String, DiscoveredDevice> devices = {};

@override
Expand All @@ -40,7 +39,6 @@ class _ScanForNewDevice extends ConsumerState<ScanForNewDevice> {
}
}
}
List<DiscoveredDevice> devicesList = devices.values.toList();
return Column(
children: [
ListTile(
Expand All @@ -55,23 +53,24 @@ class _ScanForNewDevice extends ConsumerState<ScanForNewDevice> {
),
title: Text(scanDevicesAutoConnectTitle()),
),
ListView.builder(
shrinkWrap: true,
itemCount: devices.length,
controller: _controller,
itemBuilder: (context, index) {
return ListTile(
title: Text(getNameFromBTName(devicesList[index].name)),
trailing: Text(devicesList[index].id),
onTap: () {
ref.watch(knownDevicesProvider.notifier).connect(devicesList[index]);
setState(() {
devices.remove(devicesList[index].id);
});
//Navigator.pop(context);
},
);
},
Wrap(
children: devices.values
.map(
(e) => ListTile(
title: Text(getNameFromBTName(e.name)),
trailing: Text(e.id),
onTap: () {
ref.watch(knownDevicesProvider.notifier).connect(e);
setState(
() {
devices.remove(e.id);
},
);
//Navigator.pop(context);
},
),
)
.toList(),
),
Padding(
padding: const EdgeInsets.only(top: 20),
Expand Down
16 changes: 5 additions & 11 deletions lib/Frontend/Widgets/snack_bar_overlay.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';

import 'package:awesome_snackbar_content/awesome_snackbar_content.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
Expand All @@ -9,12 +8,12 @@ part 'snack_bar_overlay.g.dart';

@Riverpod()
class SnackbarStream extends _$SnackbarStream {
StreamController<AwesomeSnackbarContent> streamController = StreamController();
StreamController<SnackBar> streamController = StreamController();

@override
Stream<AwesomeSnackbarContent> build() => streamController.stream;
Stream<SnackBar> build() => streamController.stream;

void add(AwesomeSnackbarContent content) => streamController.add(content);
void add(SnackBar content) => streamController.add(content);
}

class SnackBarOverlay extends ConsumerWidget {
Expand All @@ -24,19 +23,14 @@ class SnackBarOverlay extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
AsyncValue<AwesomeSnackbarContent> value = ref.watch(snackbarStreamProvider);
AsyncValue<SnackBar> value = ref.watch(snackbarStreamProvider);
if (value.hasValue) {
Future(
() {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
elevation: 0,
behavior: SnackBarBehavior.floating,
backgroundColor: Colors.transparent,
content: value.value!,
),
value.value!,
);
},
);
Expand Down
Loading

0 comments on commit 17f289e

Please sign in to comment.