Files
meshcore-open/lib/services/home_widget_bridge.dart
T
tthandClaude Opus 5.5 36806e289d
build / build (push) Successful in 16m14s
Home-screen widget 1x1: unread DMs, unread channels, companion battery
A terminal-style Android widget (UnreadWidget.kt):

    dm 3
    ch 12
    bt 87%

Counts turn green when non-zero, the battery red at 20 % and grey when
the companion is disconnected (last known level kept). Tapping opens the
app. HomeWidgetBridge pushes the values over a method channel only when
they change; they are kept in SharedPreferences so the widget survives
the app being closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 22:53:07 +02:00

48 lines
1.5 KiB
Dart

import 'package:flutter/services.dart';
import '../connector/meshcore_connector.dart';
import '../utils/app_logger.dart';
import '../utils/platform_info.dart';
/// tth fork: feeds the 1x1 Android home-screen widget (UnreadWidget.kt) with
/// unread direct messages, unread channel messages and the companion's
/// battery. Pushes only when one of those changes - the connector notifies
/// on every frame.
class HomeWidgetBridge {
static const _channel = MethodChannel('meshcore_open/home_widget');
final MeshCoreConnector _connector;
({int dm, int channels, int? battery, bool connected})? _last;
HomeWidgetBridge(this._connector);
void start() {
if (!PlatformInfo.isAndroid) return;
_connector.addListener(_onChanged);
_onChanged();
}
void _onChanged() {
final connected = _connector.isConnected;
final state = (
dm: _connector.getTotalContactsUnreadCount(),
channels: _connector.getTotalChannelsUnreadCount(),
// Keep the last known level while disconnected; the widget dims it.
battery: _connector.batteryPercent ?? _last?.battery,
connected: connected,
);
if (state == _last) return;
_last = state;
_channel
.invokeMethod<void>('update', {
'dm': state.dm,
'channels': state.channels,
'battery': state.battery ?? -1,
'connected': state.connected,
})
.catchError((Object e) {
appLogger.warn('home widget update failed: $e', noNotify: true);
});
}
}