forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattery_widget_service.dart
More file actions
48 lines (43 loc) · 1.56 KB
/
Copy pathbattery_widget_service.dart
File metadata and controls
48 lines (43 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:omi/utils/logger.dart';
/// Service that bridges device state from Flutter to the iOS lock screen WidgetKit extension.
///
/// Writes battery and mute state to shared App Group UserDefaults so the
/// lock screen widget can read it.
class BatteryWidgetService {
static const _channel = MethodChannel('com.omi.battery_widget');
static final BatteryWidgetService _instance = BatteryWidgetService._();
factory BatteryWidgetService() => _instance;
BatteryWidgetService._();
/// Push the latest device battery info to the iOS widget.
/// Note: mute state is managed separately via [updateMuteState] and is never
/// overwritten by this call.
Future<void> updateBatteryInfo({
required String deviceName,
required int batteryLevel,
required String deviceType,
required bool isConnected,
}) async {
if (!Platform.isIOS) return;
try {
await _channel.invokeMethod('updateBatteryInfo', {
'deviceName': deviceName,
'batteryLevel': batteryLevel,
'deviceType': deviceType,
'isConnected': isConnected,
});
} catch (e) {
Logger.debug('BatteryWidgetService.updateBatteryInfo failed: $e');
}
}
/// Update only the mute state without changing other widget data.
Future<void> updateMuteState(bool isMuted) async {
if (!Platform.isIOS) return;
try {
await _channel.invokeMethod('updateMuteState', {'isMuted': isMuted});
} catch (e) {
Logger.debug('BatteryWidgetService.updateMuteState failed: $e');
}
}
}