bluetooth_wake_handler.dart 883 Bytes
Newer Older
1 2 3
import 'package:logair_application/services/wake_service.dart';

/// Handler that wraps the [WakeService]
4
// TODO: Relative end / start -> Start and end with guard instead of switch.
5 6 7 8 9 10 11 12 13
class BTWakeHandler {
  factory BTWakeHandler() => _singleton;

  BTWakeHandler._internal();

  static final BTWakeHandler _singleton = BTWakeHandler._internal();

  static bool _wakeServiceActive = false;

14 15
  bool switchState({bool start}) {
    if (_wakeServiceActive || !start)
16
      _end();
17
    else if (!_wakeServiceActive || start)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
      _start();
  
    return _wakeServiceActive;
  }

  void _start() {
    WakeService().modifyWakeService(true).then((value) => print('_device will stay awake $value'));
    _wakeServiceActive = true;
  }

  void _end() {
    WakeService().modifyWakeService(false).then((value) => print('_device will stay awake $value'));
    _wakeServiceActive = false;
  }
}