Skip to content

Commit

Permalink
feat: add WatchConnectivityWrapper to handle communication with conne…
Browse files Browse the repository at this point in the history
…cted watches
  • Loading branch information
0niel committed Jan 20, 2025
1 parent 9a27e43 commit fc60900
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
),
),
);
Expand Down
76 changes: 76 additions & 0 deletions lib/app/widgets/watch_connectivity_wrapper.dart
Original file line number Diff line number Diff line change
@@ -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<WatchConnectivityWrapper> createState() => _WatchConnectivityWrapperState();
}

class _WatchConnectivityWrapperState extends State<WatchConnectivityWrapper> {
late final WatchConnectivity _watchConnectivity;
late final NfcPassCubit _nfcPassCubit;

@override
void initState() {
super.initState();

_watchConnectivity = WatchConnectivity();

_nfcPassCubit = context.read<NfcPassCubit>();

_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<NfcPassCubit, NfcPassState>(
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');
}
}
}
1 change: 1 addition & 0 deletions lib/app/widgets/widgets.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'firebase_interacted_message_listener.dart';
export 'watch_connectivity_wrapper.dart';
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit fc60900

Please sign in to comment.