import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:wake_alarm_sync/services/alarm_scheduler.dart'; import 'package:wake_alarm_sync/services/enforcement_state.dart'; /// Dart side of the wake-enforcement methods (`EnforcementChannel.kt`). /// /// The same single alarm channel as [AlarmScheduler] -- a separate class /// only so that file stays under the line cap and this one stays about one /// thing: reading the native stick and the event log, and the escape. class EnforcementBridge { /// Creates a bridge; [channel] is injectable so tests need no platform. EnforcementBridge({MethodChannel? channel}) : _channel = channel ?? const MethodChannel(AlarmScheduler.channelName); final MethodChannel _channel; /// Appends a Dart-side event to the native log, stamped natively. Future appendEvent( String kind, [ Map data = const {}, ]) => _channel.invokeMethod('appendEvent', { 'kind': kind, 'data': data, }); /// The whole event log, newest first; empty when the platform has none. Future>> readEvents() async { final text = await _channel.invokeMethod('readEvents'); if (text == null) return const >[]; final decoded = jsonDecode(text); if (decoded is! List) return const >[]; return >[ for (final entry in decoded) if (entry is Map) Map.from(entry), ]; } /// The native stick; null when the platform answers nothing usable. Future state() async => EnforcementState.fromMap( await _channel.invokeMapMethod('enforcementState'), ); /// Marks today escaped natively, clears the streak and lifts the /// lockdown; false when the hatch has closed. Future escapeToday() async => await _channel.invokeMethod('escapeToday') ?? false; }