From fc609000f27d1d7985d6260f7fd3bf99d51ee6aa Mon Sep 17 00:00:00 2001 From: Sergey Dmitriev <51058739+0niel@users.noreply.github.com> Date: Mon, 20 Jan 2025 21:48:11 +0300 Subject: [PATCH] feat: add WatchConnectivityWrapper to handle communication with connected watches --- lib/app/view/app.dart | 32 ++++---- .../widgets/watch_connectivity_wrapper.dart | 76 +++++++++++++++++++ lib/app/widgets/widgets.dart | 1 + pubspec.lock | 16 ++++ pubspec.yaml | 1 + 5 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 lib/app/widgets/watch_connectivity_wrapper.dart diff --git a/lib/app/view/app.dart b/lib/app/view/app.dart index deba1c1b..688087ae 100644 --- a/lib/app/view/app.dart +++ b/lib/app/view/app.dart @@ -194,21 +194,23 @@ class _AppViewState extends State<_AppView> { cupertinoDarkTheme: cupertinoDarkTheme, builder: (context) { return FirebaseInteractedMessageListener( - child: NeonAppProvider( - neonDependencies: widget.neonDependencies, - child: PlatformApp.router( - restorationScopeId: 'app', - localizationsDelegates: [ - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ..._neonLocalizationsDelegates, - ], - supportedLocales: const [Locale('en'), Locale('ru')], - locale: const Locale('ru'), - debugShowCheckedModeBanner: false, - title: 'Приложение РТУ МИРЭА', - routerConfig: _router, + child: WatchConnectivityWrapper( + child: NeonAppProvider( + neonDependencies: widget.neonDependencies, + child: PlatformApp.router( + restorationScopeId: 'app', + localizationsDelegates: [ + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ..._neonLocalizationsDelegates, + ], + supportedLocales: const [Locale('en'), Locale('ru')], + locale: const Locale('ru'), + debugShowCheckedModeBanner: false, + title: 'Приложение РТУ МИРЭА', + routerConfig: _router, + ), ), ), ); diff --git a/lib/app/widgets/watch_connectivity_wrapper.dart b/lib/app/widgets/watch_connectivity_wrapper.dart new file mode 100644 index 00000000..7ad5d0b2 --- /dev/null +++ b/lib/app/widgets/watch_connectivity_wrapper.dart @@ -0,0 +1,76 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:watch_connectivity/watch_connectivity.dart'; +import 'package:rtu_mirea_app/nfc_pass/bloc/nfc_pass_cubit.dart'; + +class WatchConnectivityWrapper extends StatefulWidget { + const WatchConnectivityWrapper({ + super.key, + required this.child, + }); + + final Widget child; + + @override + State createState() => _WatchConnectivityWrapperState(); +} + +class _WatchConnectivityWrapperState extends State { + late final WatchConnectivity _watchConnectivity; + late final NfcPassCubit _nfcPassCubit; + + @override + void initState() { + super.initState(); + + _watchConnectivity = WatchConnectivity(); + + _nfcPassCubit = context.read(); + + _watchConnectivity.messageStream.listen((message) async { + final action = message['action']; + switch (action) { + case 'requestPassId': + _sendPassIdToWatch(); + break; + case 'openPhoneAppForBinding': + debugPrint('Часы запросили открыть экран привязки NFC'); + break; + default: + debugPrint('Неизвестный action: $action'); + } + }); + } + + @override + Widget build(BuildContext context) { + return BlocListener( + listenWhen: (previous, current) => previous.passId != current.passId, + listener: (context, state) { + _sendPassIdToWatch(); + }, + child: widget.child, + ); + } + + void _sendPassIdToWatch() async { + final state = _nfcPassCubit.state; + final passId = state.passId; + + final data = {'passId': passId?.toString() ?? ''}; + + try { + await _watchConnectivity.sendMessage(data); + debugPrint('Отправили passId = ${data['passId']} на часы (sendMessage)'); + } catch (e) { + debugPrint('Не удалось отправить сообщение на часы: $e'); + } + + try { + await _watchConnectivity.updateApplicationContext(data); + debugPrint('Обновили applicationContext passId = ${data['passId']}'); + } catch (e) { + debugPrint('Не удалось обновить applicationContext: $e'); + } + } +} diff --git a/lib/app/widgets/widgets.dart b/lib/app/widgets/widgets.dart index e4090f30..2b54c894 100644 --- a/lib/app/widgets/widgets.dart +++ b/lib/app/widgets/widgets.dart @@ -1 +1,2 @@ export 'firebase_interacted_message_listener.dart'; +export 'watch_connectivity_wrapper.dart'; diff --git a/pubspec.lock b/pubspec.lock index ab956049..23c08e12 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -3182,6 +3182,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.2" + watch_connectivity: + dependency: "direct main" + description: + name: watch_connectivity + sha256: "37a8c3177313525095d15618fdb4043454610f1471de6d6b5c4445982d0ee709" + url: "https://pub.dev" + source: hosted + version: "0.2.1" + watch_connectivity_platform_interface: + dependency: transitive + description: + name: watch_connectivity_platform_interface + sha256: "82e8f165eac779d71bff7f6269a8be530798311cf7f3c33f1594d93468747d11" + url: "https://pub.dev" + source: hosted + version: "0.2.0" watcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 7feb1625..90d9f947 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -152,6 +152,7 @@ dependencies: path: packages/nfc_pass_client nfc_pass_repository: path: packages/nfc_pass_repository + watch_connectivity: ^0.2.1 dependency_overrides: