style: refactored esense_input into global singleton
This commit is contained in:
		
							parent
							
								
									947570755e
								
							
						
					
					
						commit
						c5cee0cb9d
					
				
							
								
								
									
										83
									
								
								lib/esense_input.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								lib/esense_input.dart
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,83 @@
 | 
			
		|||
import 'dart:async';
 | 
			
		||||
import 'dart:io';
 | 
			
		||||
 | 
			
		||||
import 'package:esense_flutter/esense.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:permission_handler/permission_handler.dart';
 | 
			
		||||
 | 
			
		||||
class ESenseInput {
 | 
			
		||||
  static final instance = ESenseInput._();
 | 
			
		||||
 | 
			
		||||
  ESenseManager eSenseManager = ESenseManager('unknown');
 | 
			
		||||
  ValueNotifier<String> deviceStatus = ValueNotifier('');
 | 
			
		||||
 | 
			
		||||
  String eSenseDeviceName = '';
 | 
			
		||||
  bool connected = false;
 | 
			
		||||
  bool sampling = false;
 | 
			
		||||
 | 
			
		||||
  ESenseInput._() {
 | 
			
		||||
    _listenToESense();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _askForPermissions() async {
 | 
			
		||||
    if (!Platform.isAndroid && !Platform.isIOS) return;
 | 
			
		||||
    if (!(await Permission.bluetoothScan.request().isGranted &&
 | 
			
		||||
        await Permission.bluetoothConnect.request().isGranted)) {
 | 
			
		||||
      print(
 | 
			
		||||
          'WARNING - no permission to use Bluetooth granted. Cannot access eSense device.');
 | 
			
		||||
    }
 | 
			
		||||
    // for some strange reason, Android requires permission to location for Bluetooth to work.....?
 | 
			
		||||
    if (Platform.isAndroid) {
 | 
			
		||||
      if (!(await Permission.locationWhenInUse.request().isGranted)) {
 | 
			
		||||
        print(
 | 
			
		||||
            'WARNING - no permission to access location granted. Cannot access eSense device.');
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  StreamSubscription _listenToESense() {
 | 
			
		||||
    // if you want to get the connection events when connecting,
 | 
			
		||||
    // set up the listener BEFORE connecting...
 | 
			
		||||
    return eSenseManager.connectionEvents.listen((event) {
 | 
			
		||||
      print('CONNECTION event: $event');
 | 
			
		||||
 | 
			
		||||
      // when we're connected to the eSense device, we can start listening to events from it
 | 
			
		||||
      // if (event.type == ConnectionType.connected) _listenToESenseEvents();
 | 
			
		||||
 | 
			
		||||
      connected = false;
 | 
			
		||||
      switch (event.type) {
 | 
			
		||||
        case ConnectionType.connected:
 | 
			
		||||
          deviceStatus.value = 'connected';
 | 
			
		||||
          connected = true;
 | 
			
		||||
          break;
 | 
			
		||||
        case ConnectionType.unknown:
 | 
			
		||||
          deviceStatus.value = 'unknown';
 | 
			
		||||
          break;
 | 
			
		||||
        case ConnectionType.disconnected:
 | 
			
		||||
          deviceStatus.value = 'disconnected';
 | 
			
		||||
          sampling = false;
 | 
			
		||||
          break;
 | 
			
		||||
        case ConnectionType.device_found:
 | 
			
		||||
          deviceStatus.value = 'device_found';
 | 
			
		||||
          break;
 | 
			
		||||
        case ConnectionType.device_not_found:
 | 
			
		||||
          deviceStatus.value = 'device_not_found';
 | 
			
		||||
          break;
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> connectToESense(String deviceName) async {
 | 
			
		||||
    if (!connected) {
 | 
			
		||||
      await _askForPermissions();
 | 
			
		||||
      print('Trying to connect to eSense device namend \'$deviceName\'');
 | 
			
		||||
      eSenseDeviceName = deviceName;
 | 
			
		||||
      eSenseManager.deviceName = deviceName;
 | 
			
		||||
      connected = await eSenseManager.connect();
 | 
			
		||||
      print('Trying to connect to eSense device namend \'${eSenseManager.deviceName}\'');
 | 
			
		||||
 | 
			
		||||
      deviceStatus.value = connected ? 'connecting...' : 'connection failed';
 | 
			
		||||
      print(deviceStatus.value);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,10 +1,9 @@
 | 
			
		|||
import 'dart:io';
 | 
			
		||||
 | 
			
		||||
import 'package:esense_flutter/esense.dart';
 | 
			
		||||
import 'package:file_picker/file_picker.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:permission_handler/permission_handler.dart';
 | 
			
		||||
import 'package:sense_the_rhythm/esense_connect_dialog.dart';
 | 
			
		||||
import 'package:sense_the_rhythm/esense_input.dart';
 | 
			
		||||
import 'package:sense_the_rhythm/simfile.dart';
 | 
			
		||||
import 'package:shared_preferences/shared_preferences.dart';
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -21,88 +20,12 @@ class _LevelSelectionState extends State<LevelSelection> {
 | 
			
		|||
  String? stepmaniaCoursesPath;
 | 
			
		||||
  List<Simfile> stepmaniaCoursesFolders = [];
 | 
			
		||||
 | 
			
		||||
  String eSenseDeviceName = '';
 | 
			
		||||
  ESenseManager? eSenseManager;
 | 
			
		||||
  ValueNotifier<String> _deviceStatus = ValueNotifier('');
 | 
			
		||||
  // String _deviceStatus = '';
 | 
			
		||||
  bool connected = false;
 | 
			
		||||
  bool sampling = false;
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void initState() {
 | 
			
		||||
    super.initState();
 | 
			
		||||
    _listenToESense();
 | 
			
		||||
    loadFolderPath();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _askForPermissions() async {
 | 
			
		||||
    if (!(await Permission.bluetoothScan.request().isGranted &&
 | 
			
		||||
        await Permission.bluetoothConnect.request().isGranted)) {
 | 
			
		||||
      print(
 | 
			
		||||
          'WARNING - no permission to use Bluetooth granted. Cannot access eSense device.');
 | 
			
		||||
    }
 | 
			
		||||
    // for some strange reason, Android requires permission to location for Bluetooth to work.....?
 | 
			
		||||
    if (Platform.isAndroid) {
 | 
			
		||||
      if (!(await Permission.locationWhenInUse.request().isGranted)) {
 | 
			
		||||
        print(
 | 
			
		||||
            'WARNING - no permission to access location granted. Cannot access eSense device.');
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _listenToESense() async {
 | 
			
		||||
    await _askForPermissions();
 | 
			
		||||
 | 
			
		||||
    // if you want to get the connection events when connecting,
 | 
			
		||||
    // set up the listener BEFORE connecting...
 | 
			
		||||
    eSenseManager!.connectionEvents.listen((event) {
 | 
			
		||||
      print('CONNECTION event: $event');
 | 
			
		||||
 | 
			
		||||
      // when we're connected to the eSense device, we can start listening to events from it
 | 
			
		||||
      // if (event.type == ConnectionType.connected) _listenToESenseEvents();
 | 
			
		||||
 | 
			
		||||
      setState(() {
 | 
			
		||||
        connected = false;
 | 
			
		||||
        switch (event.type) {
 | 
			
		||||
          case ConnectionType.connected:
 | 
			
		||||
            _deviceStatus.value = 'connected';
 | 
			
		||||
            connected = true;
 | 
			
		||||
            break;
 | 
			
		||||
          case ConnectionType.unknown:
 | 
			
		||||
            _deviceStatus.value = 'unknown';
 | 
			
		||||
            break;
 | 
			
		||||
          case ConnectionType.disconnected:
 | 
			
		||||
            _deviceStatus.value = 'disconnected';
 | 
			
		||||
            sampling = false;
 | 
			
		||||
            break;
 | 
			
		||||
          case ConnectionType.device_found:
 | 
			
		||||
            _deviceStatus.value = 'device_found';
 | 
			
		||||
            break;
 | 
			
		||||
          case ConnectionType.device_not_found:
 | 
			
		||||
            _deviceStatus.value = 'device_not_found';
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _connectToESense(String deviceName) async {
 | 
			
		||||
    if (!connected) {
 | 
			
		||||
      await _askForPermissions();
 | 
			
		||||
      print('Trying to connect to eSense device...');
 | 
			
		||||
      setState(() {
 | 
			
		||||
        eSenseDeviceName = deviceName;
 | 
			
		||||
      });
 | 
			
		||||
      print(eSenseDeviceName);
 | 
			
		||||
      eSenseManager = ESenseManager(eSenseDeviceName);
 | 
			
		||||
      connected = await eSenseManager!.connect();
 | 
			
		||||
      print('success!');
 | 
			
		||||
 | 
			
		||||
      setState(() {
 | 
			
		||||
        _deviceStatus.value = connected ? 'connecting...' : 'connection failed';
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> loadFolderPath() async {
 | 
			
		||||
    SharedPreferences prefs = await SharedPreferences.getInstance();
 | 
			
		||||
| 
						 | 
				
			
			@ -163,9 +86,9 @@ class _LevelSelectionState extends State<LevelSelection> {
 | 
			
		|||
                    context: context,
 | 
			
		||||
                    builder: (BuildContext context) {
 | 
			
		||||
                      return ESenseConnectDialog(
 | 
			
		||||
                          deviceStatus: _deviceStatus,
 | 
			
		||||
                          deviceStatus: ESenseInput.instance.deviceStatus,
 | 
			
		||||
                          connect: (String name) {
 | 
			
		||||
                            _connectToESense(name);
 | 
			
		||||
                            ESenseInput.instance.connectToESense(name);
 | 
			
		||||
                          });
 | 
			
		||||
                    },
 | 
			
		||||
                  ),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue