From ef31239655d52f3e02906e4a9f59aae57f6cbcf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Fri, 27 Sep 2024 04:32:53 +0700 Subject: [PATCH] Clean imports --- app/lib/backend/schema/bt_device.dart | 6 +- app/lib/pages/capture/widgets/widgets.dart | 253 ------------------ app/lib/pages/home/page.dart | 3 +- app/lib/pages/memories/widgets/capture.dart | 1 - app/lib/providers/auth_provider.dart | 2 +- app/lib/providers/device_provider.dart | 2 +- app/lib/providers/onboarding_provider.dart | 2 +- .../providers/speech_profile_provider.dart | 3 +- app/lib/services/devices.dart | 4 +- .../services/devices/device_connection.dart | 13 +- .../services/devices/frame_connection.dart | 74 ++--- .../services/devices/friend_connection.dart | 11 +- app/lib/services/devices/models.dart | 3 +- app/lib/services/sockets.dart | 2 +- app/lib/services/sockets/pure_socket.dart | 2 +- .../sockets/transcription_connection.dart | 2 +- app/lib/utils/memories/integrations.dart | 2 +- app/lib/utils/memories/process.dart | 2 +- 18 files changed, 51 insertions(+), 336 deletions(-) diff --git a/app/lib/backend/schema/bt_device.dart b/app/lib/backend/schema/bt_device.dart index 25baf075f..967b40317 100644 --- a/app/lib/backend/schema/bt_device.dart +++ b/app/lib/backend/schema/bt_device.dart @@ -1,7 +1,7 @@ import 'package:flutter_blue_plus/flutter_blue_plus.dart'; -import 'package:friend_private/services/device_connections.dart'; -import 'package:friend_private/services/frame_connection.dart'; -import 'package:friend_private/utils/ble/gatt_utils.dart'; +import 'package:friend_private/services/devices/device_connection.dart'; +import 'package:friend_private/services/devices/frame_connection.dart'; +import 'package:friend_private/services/devices/models.dart'; enum BleAudioCodec { pcm16, diff --git a/app/lib/pages/capture/widgets/widgets.dart b/app/lib/pages/capture/widgets/widgets.dart index d131f5ae7..a9666a1e3 100644 --- a/app/lib/pages/capture/widgets/widgets.dart +++ b/app/lib/pages/capture/widgets/widgets.dart @@ -2,212 +2,16 @@ import 'package:flutter/material.dart'; import 'package:friend_private/backend/preferences.dart'; import 'package:friend_private/backend/schema/bt_device.dart'; import 'package:friend_private/backend/schema/transcript_segment.dart'; -import 'package:friend_private/pages/capture/connect.dart'; import 'package:friend_private/pages/speech_profile/page.dart'; import 'package:friend_private/providers/capture_provider.dart'; -import 'package:friend_private/providers/connectivity_provider.dart'; -import 'package:friend_private/providers/device_provider.dart'; import 'package:friend_private/providers/home_provider.dart'; import 'package:friend_private/utils/analytics/mixpanel.dart'; import 'package:friend_private/utils/enums.dart'; import 'package:friend_private/utils/other/temp.dart'; -import 'package:friend_private/utils/websockets.dart'; -import 'package:friend_private/widgets/device_widget.dart'; -import 'package:friend_private/widgets/dialog.dart'; import 'package:friend_private/widgets/photos_grid.dart'; -import 'package:friend_private/widgets/scanning_ui.dart'; import 'package:friend_private/widgets/transcript.dart'; -import 'package:gradient_borders/gradient_borders.dart'; -import 'package:lottie/lottie.dart'; import 'package:provider/provider.dart'; import 'package:tuple/tuple.dart'; -import 'package:url_launcher/url_launcher.dart'; - -getConnectionStateWidgets( - BuildContext context, - bool hasTranscripts, - BTDeviceStruct? device, - WebsocketConnectionStatus wsConnectionState, -) { - if (hasTranscripts) return []; - if (device == null) { - return [ - const DeviceAnimationWidget(sizeMultiplier: 0.7), - SharedPreferencesUtil().btDeviceStruct.id == '' - ? _getNoFriendConnectedYet(context) - : const ScanningUI( - string1: 'Looking for Friend wearable', - string2: 'Locating your Friend device. Keep it near your phone for pairing', - ), - ]; - } - - bool isWebsocketError = - wsConnectionState == WebsocketConnectionStatus.failed || wsConnectionState == WebsocketConnectionStatus.error; - - return [ - const Center(child: DeviceAnimationWidget(sizeMultiplier: 0.7)), - Consumer(builder: (context, connectivityProvider, child) { - return GestureDetector( - onTap: !connectivityProvider.isConnected || isWebsocketError - ? () { - showDialog( - context: context, - builder: (c) => getDialog( - context, - () => Navigator.pop(context), - () => Navigator.pop(context), - !connectivityProvider.isConnected ? 'Internet Connection Lost' : 'Connection Issue', - !connectivityProvider.isConnected - ? 'Your device is offline. Transcription is paused until connection is restored.' - : 'Unable to connect to the transcript service. Please restart the app or contact support if the problem persists.', - okButtonText: 'Ok', - singleButton: true, - ), - ); - } - : null, - child: Center( - child: Row( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const SizedBox(width: 24), - Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Text( - !connectivityProvider.isConnected - ? 'No Internet' - : (connectivityProvider.isConnected && isWebsocketError) - ? 'Server Issue' - : 'Listening', - style: TextStyle( - color: Colors.white, - fontSize: !connectivityProvider.isConnected - ? 29 - : isWebsocketError - ? 29 - : 29, - letterSpacing: 0.0, - fontWeight: FontWeight.w700, - height: 1.2), - textAlign: TextAlign.center, - ), - Text( - '${device.name} (${device.getShortId()})', - style: const TextStyle( - color: Colors.white, - fontSize: 16.0, - fontWeight: FontWeight.w500, - height: 1.5, - ), - textAlign: TextAlign.center, - ) - ], - ), - const SizedBox(width: 24), - !connectivityProvider.isConnected - ? Lottie.asset('assets/lottie_animations/no_internet.json', height: 56, width: 56) - : isWebsocketError - // ? Lottie.network('https://lottie.host/8223dbf8-8a50-4d48-8e37-0b845b1f1094/TQcT5w5Mn4.json', height: 48, width: 48) - ? Lottie.asset('assets/lottie_animations/no_internet.json', height: 56, width: 56) - // TODO: find a better animation for server - : Container( - width: 10, - height: 10, - decoration: const BoxDecoration( - color: Color.fromARGB(255, 0, 255, 9), - shape: BoxShape.circle, - ), - ), - ], - )), - ); - }), - const SizedBox(height: 8), - // const Row( - // crossAxisAlignment: CrossAxisAlignment.center, - // mainAxisAlignment: MainAxisAlignment.center, - // children: [], - // ), - ]; -} - -_getNoFriendConnectedYet(BuildContext context) { - return Column( - children: [ - const SizedBox(height: 24), - Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - // const Padding( - // padding: EdgeInsets.symmetric(horizontal: 32), - // child: Text( - // 'Get a Friend wearable to start capturing your memories.', - // textAlign: TextAlign.center, - // style: TextStyle(color: Colors.white, fontSize: 18), - // )), - // const SizedBox(height: 32), - Container( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 0), - decoration: BoxDecoration( - border: const GradientBoxBorder( - gradient: LinearGradient(colors: [ - Color.fromARGB(127, 208, 208, 208), - Color.fromARGB(127, 188, 99, 121), - Color.fromARGB(127, 86, 101, 182), - Color.fromARGB(127, 126, 190, 236) - ]), - width: 2, - ), - borderRadius: BorderRadius.circular(12), - ), - child: TextButton( - onPressed: () { - launchUrl(Uri.parse('https://omi.me')); - MixpanelManager().getFriendClicked(); - }, - child: const Text( - 'Get a Friend', - style: TextStyle(color: Colors.white, fontSize: 16), - ), - ), - ), - const SizedBox(height: 4), - TextButton( - onPressed: () async { - Navigator.of(context).push(MaterialPageRoute(builder: (c) => const ConnectDevicePage())); - MixpanelManager().connectFriendClicked(); - }, - style: ElevatedButton.styleFrom( - backgroundColor: Colors.transparent, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(12), - ), - ), - child: const Text( - 'Connect', - style: TextStyle( - fontWeight: FontWeight.w400, - fontSize: 18, - color: Colors.white, - decoration: TextDecoration.underline, - ), - ), - ), - const SizedBox(width: 16), - ], - ), - // const Text( - // 'Or you can use your phone as\nthe audio source 👇', - // style: TextStyle(color: Colors.white, fontSize: 18), - // textAlign: TextAlign.center, - // ), - ], - ); -} class SpeechProfileCardWidget extends StatelessWidget { const SpeechProfileCardWidget({super.key}); @@ -313,63 +117,6 @@ getLiteTranscriptWidget( ); } -connectionStatusWidgets( - BuildContext context, - List segments, - WebsocketConnectionStatus wsConnectionState, -) { - if (segments.isEmpty) return []; - - bool isWifiDisconnected = !Provider.of(context, listen: false).isConnected; - bool isWebsocketError = - wsConnectionState == WebsocketConnectionStatus.failed || wsConnectionState == WebsocketConnectionStatus.error; - if (!isWifiDisconnected && !isWebsocketError) return []; - return [ - GestureDetector( - onTap: () { - showDialog( - context: context, - builder: (c) => getDialog( - context, - () => Navigator.pop(context), - () => Navigator.pop(context), - isWifiDisconnected ? 'Internet Connection Lost' : 'Connection Issue', - isWifiDisconnected - ? 'Your device is offline. Transcription is paused until connection is restored.' - : 'Unable to connect to the transcript service. Please restart the app or contact support if the problem persists.', - okButtonText: 'Ok', - singleButton: true, - ), - ); - }, - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 16), - child: Row( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Text( - isWifiDisconnected ? 'No Internet' : 'Server Issue', - style: TextStyle( - color: Colors.grey.shade300, - fontSize: 20, - ), - textAlign: TextAlign.center, - ), - const SizedBox(width: 16), - Padding( - padding: const EdgeInsets.only(bottom: 6), - child: isWifiDisconnected - ? Lottie.asset('assets/lottie_animations/no_internet.json', height: 48, width: 48) - : Lottie.asset('assets/lottie_animations/no_internet.json', height: 48, width: 48), - ) - ], - ), - ), - ) - ]; -} - getPhoneMicRecordingButton(VoidCallback recordingToggled, RecordingState state) { if (SharedPreferencesUtil().btDeviceStruct.id.isNotEmpty) return const SizedBox.shrink(); return Visibility( diff --git a/app/lib/pages/home/page.dart b/app/lib/pages/home/page.dart index dde45dcb6..19c017ee9 100644 --- a/app/lib/pages/home/page.dart +++ b/app/lib/pages/home/page.dart @@ -21,8 +21,7 @@ import 'package:friend_private/providers/memory_provider.dart' as mp; import 'package:friend_private/providers/memory_provider.dart'; import 'package:friend_private/providers/message_provider.dart'; import 'package:friend_private/providers/plugin_provider.dart'; -import 'package:friend_private/services/notification_service.dart'; -import 'package:friend_private/services/services.dart'; +import 'package:friend_private/services/notifications.dart'; import 'package:friend_private/utils/analytics/mixpanel.dart'; import 'package:friend_private/utils/audio/foreground.dart'; import 'package:friend_private/utils/other/temp.dart'; diff --git a/app/lib/pages/memories/widgets/capture.dart b/app/lib/pages/memories/widgets/capture.dart index 76196a8ef..7b2190ed3 100644 --- a/app/lib/pages/memories/widgets/capture.dart +++ b/app/lib/pages/memories/widgets/capture.dart @@ -9,7 +9,6 @@ import 'package:friend_private/providers/capture_provider.dart'; import 'package:friend_private/providers/connectivity_provider.dart'; import 'package:friend_private/providers/device_provider.dart'; import 'package:friend_private/providers/onboarding_provider.dart'; -import 'package:friend_private/providers/websocket_provider.dart'; import 'package:friend_private/services/services.dart'; import 'package:friend_private/utils/audio/wav_bytes.dart'; import 'package:friend_private/widgets/dialog.dart'; diff --git a/app/lib/providers/auth_provider.dart b/app/lib/providers/auth_provider.dart index 0f74843e2..f7c300319 100644 --- a/app/lib/providers/auth_provider.dart +++ b/app/lib/providers/auth_provider.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; import 'package:friend_private/backend/auth.dart'; import 'package:friend_private/backend/preferences.dart'; import 'package:friend_private/providers/base_provider.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/notifications.dart'; import 'package:friend_private/utils/alerts/app_snackbar.dart'; import 'package:friend_private/utils/analytics/gleap.dart'; import 'package:friend_private/utils/analytics/mixpanel.dart'; diff --git a/app/lib/providers/device_provider.dart b/app/lib/providers/device_provider.dart index fef982dcd..ee4b1a82a 100644 --- a/app/lib/providers/device_provider.dart +++ b/app/lib/providers/device_provider.dart @@ -5,7 +5,7 @@ import 'package:friend_private/backend/preferences.dart'; import 'package:friend_private/backend/schema/bt_device.dart'; import 'package:friend_private/providers/capture_provider.dart'; import 'package:friend_private/services/devices.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/notifications.dart'; import 'package:friend_private/services/services.dart'; import 'package:friend_private/utils/analytics/mixpanel.dart'; import 'package:instabug_flutter/instabug_flutter.dart'; diff --git a/app/lib/providers/onboarding_provider.dart b/app/lib/providers/onboarding_provider.dart index 0c89d0619..25ddbbd25 100644 --- a/app/lib/providers/onboarding_provider.dart +++ b/app/lib/providers/onboarding_provider.dart @@ -12,7 +12,7 @@ import 'package:friend_private/backend/schema/bt_device.dart'; import 'package:friend_private/providers/base_provider.dart'; import 'package:friend_private/providers/device_provider.dart'; import 'package:friend_private/services/devices.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/notifications.dart'; import 'package:friend_private/services/services.dart'; import 'package:friend_private/utils/analytics/mixpanel.dart'; import 'package:friend_private/utils/audio/foreground.dart'; diff --git a/app/lib/providers/speech_profile_provider.dart b/app/lib/providers/speech_profile_provider.dart index 8efec243b..c00334aed 100644 --- a/app/lib/providers/speech_profile_provider.dart +++ b/app/lib/providers/speech_profile_provider.dart @@ -13,13 +13,12 @@ import 'package:friend_private/backend/schema/memory.dart'; import 'package:friend_private/backend/schema/message_event.dart'; import 'package:friend_private/backend/schema/structured.dart'; import 'package:friend_private/backend/schema/transcript_segment.dart'; -import 'package:friend_private/providers/capture_provider.dart'; import 'package:friend_private/providers/device_provider.dart'; import 'package:friend_private/services/devices.dart'; import 'package:friend_private/services/services.dart'; +import 'package:friend_private/services/sockets/transcription_connection.dart'; import 'package:friend_private/utils/audio/wav_bytes.dart'; import 'package:friend_private/utils/memories/process.dart'; -import 'package:friend_private/utils/pure_socket.dart'; import 'package:uuid/uuid.dart'; class SpeechProfileProvider extends ChangeNotifier diff --git a/app/lib/services/devices.dart b/app/lib/services/devices.dart index a4088a6c7..9bc5a4811 100644 --- a/app/lib/services/devices.dart +++ b/app/lib/services/devices.dart @@ -4,8 +4,8 @@ import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:friend_private/backend/schema/bt_device.dart'; -import 'package:friend_private/services/device_connections.dart'; -import 'package:friend_private/utils/ble/gatt_utils.dart'; +import 'package:friend_private/services/devices/device_connection.dart'; +import 'package:friend_private/services/devices/models.dart'; abstract class IDeviceService { void start(); diff --git a/app/lib/services/devices/device_connection.dart b/app/lib/services/devices/device_connection.dart index aab1fef8f..aea17630e 100644 --- a/app/lib/services/devices/device_connection.dart +++ b/app/lib/services/devices/device_connection.dart @@ -3,13 +3,12 @@ import 'dart:io'; import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:friend_private/backend/schema/bt_device.dart'; import 'package:friend_private/services/devices.dart'; -import 'package:friend_private/services/frame_connection.dart'; -import 'package:friend_private/services/friend_connection.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/devices/frame_connection.dart'; +import 'package:friend_private/services/devices/friend_connection.dart'; +import 'package:friend_private/services/notifications.dart'; class DeviceConnectionFactory { static DeviceConnection? create( @@ -200,11 +199,11 @@ abstract class DeviceConnection { Future> performGetStorageList(); - Future performWriteToStorage(int numFile); + Future performWriteToStorage(int numFile, int command); - Future writeToStorage(int numFile) async { + Future writeToStorage(int numFile, int command) async { if (await isConnected()) { - return await performWriteToStorage(numFile); + return await performWriteToStorage(numFile, command); } _showDeviceDisconnectedNotification(); return Future.value(false); diff --git a/app/lib/services/devices/frame_connection.dart b/app/lib/services/devices/frame_connection.dart index 2b14021b1..94c33968b 100644 --- a/app/lib/services/devices/frame_connection.dart +++ b/app/lib/services/devices/frame_connection.dart @@ -7,8 +7,8 @@ import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:frame_sdk/bluetooth.dart'; import 'package:frame_sdk/frame_sdk.dart'; import 'package:friend_private/backend/schema/bt_device.dart'; -import 'package:friend_private/services/device_connections.dart'; import 'package:friend_private/services/devices.dart'; +import 'package:friend_private/services/devices/device_connection.dart'; const String _photoHeader = "/9j/4AAQSkZJRgABAgAAZABkAAD/2wBDACAWGBwYFCAcGhwkIiAmMFA0MCwsMGJGSjpQdGZ6eHJmcG6AkLicgIiuim5woNqirr7EztDOfJri8uDI8LjKzsb/2wBDASIkJDAqMF40NF7GhHCExsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsb/wAARCAIAAgADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwA="; @@ -19,9 +19,7 @@ class FrameDeviceConnection extends DeviceConnection { get deviceId => device.id; @override - Future connect( - {Function(String deviceId, DeviceConnectionState state)? - onConnectionStateChanged}) async { + Future connect({Function(String deviceId, DeviceConnectionState state)? onConnectionStateChanged}) async { await super.connect(onConnectionStateChanged: onConnectionStateChanged); await init(); } @@ -68,8 +66,7 @@ class FrameDeviceConnection extends DeviceConnection { await _frame?.bluetooth.sendData(heartbeatBytes); } - Future getFromLoop(String key, - {Duration timeout = const Duration(seconds: 5)}) async { + Future getFromLoop(String key, {Duration timeout = const Duration(seconds: 5)}) async { int prefix = switch (key) { "loopStatus" => 0xE1, "micState" => 0xE2, @@ -78,8 +75,7 @@ class FrameDeviceConnection extends DeviceConnection { _ => throw Exception("Invalid key: $key"), }; - final futureResult = - _frame!.bluetooth.getDataWithPrefix(prefix).first.timeout(timeout); + final futureResult = _frame!.bluetooth.getDataWithPrefix(prefix).first.timeout(timeout); if (!await sendUntilEchoed("GET $key", maxAttempts: 1, timeout: timeout)) { return null; @@ -98,10 +94,8 @@ class FrameDeviceConnection extends DeviceConnection { Future setTimeOnFrame() async { if (_isLooping == true) { - String utcUnixEpochTime = - (DateTime.now().toUtc().millisecondsSinceEpoch ~/ 1000).toString(); - String timeZoneOffset = - DateTime.now().timeZoneOffset.inMinutes > 0 ? '+' : '-'; + String utcUnixEpochTime = (DateTime.now().toUtc().millisecondsSinceEpoch ~/ 1000).toString(); + String timeZoneOffset = DateTime.now().timeZoneOffset.inMinutes > 0 ? '+' : '-'; timeZoneOffset += '${DateTime.now().timeZoneOffset.inHours.abs().toString().padLeft(2, '0')}:${(DateTime.now().timeZoneOffset.inMinutes.abs() % 60).toString().padLeft(2, '0')}'; await sendUntilEchoed("timeUtc=$utcUnixEpochTime"); @@ -128,10 +122,9 @@ class FrameDeviceConnection extends DeviceConnection { await setTimeOnFrame(); bool isLoaded = false; bool isRunning = false; - final String mainLuaContent = - (await rootBundle.loadString('assets/device_assets/frame_lib.lua')) - .replaceAll("\t", "") - .replaceAll("\n\n", "\n"); + final String mainLuaContent = (await rootBundle.loadString('assets/device_assets/frame_lib.lua')) + .replaceAll("\t", "") + .replaceAll("\n\n", "\n"); final int frameLibHash = mainLuaContent.hashCode; if (_isLooping == false) { @@ -175,13 +168,9 @@ class FrameDeviceConnection extends DeviceConnection { await sendUntilEchoed("CAMERA START"); } else if (!isLoaded) { await _frame!.bluetooth.sendBreakSignal(); - print( - "About to send main.lua to frame, length = ${mainLuaContent.length}"); + print("About to send main.lua to frame, length = ${mainLuaContent.length}"); try { - await _frame!.files.writeFile( - "main.lua", - utf8.encode( - "$mainLuaContent\nframeLibHash = $frameLibHash\nstart()"), + await _frame!.files.writeFile("main.lua", utf8.encode("$mainLuaContent\nframeLibHash = $frameLibHash\nstart()"), checked: true); print("Sent main.lua to frame"); await _frame!.bluetooth.sendResetSignal(); @@ -211,26 +200,21 @@ class FrameDeviceConnection extends DeviceConnection { } Future sendUntilEchoed(String data, - {int maxAttempts = 3, - Duration timeout = const Duration(seconds: 10)}) async { + {int maxAttempts = 3, Duration timeout = const Duration(seconds: 10)}) async { Uint8List bytesToSend = Uint8List.fromList(utf8.encode(data)); //print("Sending $data to frame"); for (int attempt = 1; attempt <= maxAttempts; attempt++) { try { - var future = _frame!.bluetooth.stringResponse - .firstWhere((element) => element == "ECHO:$data") - .timeout(timeout); + var future = _frame!.bluetooth.stringResponse.firstWhere((element) => element == "ECHO:$data").timeout(timeout); await _frame!.bluetooth.sendData(bytesToSend); await future; //print("Received ECHO:$data from frame"); return true; } catch (e) { if (e is TimeoutException) { - print( - "Timeout occurred while waiting for echo of $data. Attempt $attempt of $maxAttempts"); + print("Timeout occurred while waiting for echo of $data. Attempt $attempt of $maxAttempts"); if (attempt == maxAttempts) { - print( - "Failed to receive echo for $data after $maxAttempts attempts"); + print("Failed to receive echo for $data after $maxAttempts attempts"); //await disconnectDevice(); return false; } @@ -284,8 +268,7 @@ class FrameDeviceConnection extends DeviceConnection { }); } - StreamSubscription subscription = - _frame!.bluetooth.getDataWithPrefix(0xEE).listen((value) { + StreamSubscription subscription = _frame!.bluetooth.getDataWithPrefix(0xEE).listen((value) { _isLooping = true; if (value.isNotEmpty) onAudioBytesReceived(value); }, onDone: () async { @@ -329,8 +312,7 @@ class FrameDeviceConnection extends DeviceConnection { } } - StreamSubscription subscription = - _frame!.bluetooth.getDataWithPrefix(0xCC).listen((value) { + StreamSubscription subscription = _frame!.bluetooth.getDataWithPrefix(0xCC).listen((value) { _isLooping = true; if (value.isNotEmpty) checkBatteryLevel(value); }); @@ -349,10 +331,7 @@ class FrameDeviceConnection extends DeviceConnection { Future init() async { print("Initialising Frame Device"); var device = bleDevice; - if (_frame != null && - device != null && - _frame!.isConnected && - device!.isConnected) { + if (_frame != null && device != null && _frame!.isConnected && device!.isConnected) { print("Device is already connected in init...?"); //await afterConnect(); //return; @@ -371,15 +350,13 @@ class FrameDeviceConnection extends DeviceConnection { if (_isLooping == null || _isLooping == false) { Future.microtask(() async { try { - _firmwareRevision = - await _frame!.evaluate("frame.FIRMWARE_VERSION"); + _firmwareRevision = await _frame!.evaluate("frame.FIRMWARE_VERSION"); _isLooping = false; } catch (e) { // Ignore error } try { - _batteryLevel = - int.parse(await _frame!.evaluate("frame.battery_level()")); + _batteryLevel = int.parse(await _frame!.evaluate("frame.battery_level()")); _isLooping = false; } catch (e) { // Ignore error @@ -412,8 +389,7 @@ class FrameDeviceConnection extends DeviceConnection { } @override - Future performGetImageListener( - {required void Function(Uint8List p1) onImageReceived}) async { + Future performGetImageListener({required void Function(Uint8List p1) onImageReceived}) async { if (_frame == null || _frame!.isConnected == false) { await Future.doWhile(() async { await Future.delayed(Duration(milliseconds: 100)); @@ -421,15 +397,13 @@ class FrameDeviceConnection extends DeviceConnection { }); } - StreamSubscription subscription = _frame!.bluetooth - .getDataOfType(FrameDataTypePrefixes.photoData) - .listen((value) { + StreamSubscription subscription = + _frame!.bluetooth.getDataOfType(FrameDataTypePrefixes.photoData).listen((value) { if (value.isNotEmpty) { print("Received photo data from frame, length = ${value.length}"); final header = base64.decode(_photoHeader); final combinedData = Uint8List.fromList([...header, ...value]); - print( - "Processed photo data from frame, length = ${combinedData.length}"); + print("Processed photo data from frame, length = ${combinedData.length}"); onImageReceived(combinedData); } }); diff --git a/app/lib/services/devices/friend_connection.dart b/app/lib/services/devices/friend_connection.dart index 3ec1488c9..986ce0032 100644 --- a/app/lib/services/devices/friend_connection.dart +++ b/app/lib/services/devices/friend_connection.dart @@ -6,11 +6,11 @@ import 'package:awesome_notifications/awesome_notifications.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:friend_private/backend/schema/bt_device.dart'; -import 'package:friend_private/services/device_connections.dart'; import 'package:friend_private/services/devices.dart'; +import 'package:friend_private/services/devices/device_connection.dart'; +import 'package:friend_private/services/devices/errors.dart'; +import 'package:friend_private/services/devices/models.dart'; import 'package:friend_private/utils/audio/wav_bytes.dart'; -import 'package:friend_private/utils/ble/errors.dart'; -import 'package:friend_private/utils/ble/gatt_utils.dart'; import 'package:friend_private/utils/logger.dart'; class FriendDeviceConnection extends DeviceConnection { @@ -294,7 +294,7 @@ class FriendDeviceConnection extends DeviceConnection { return listener; } - Future performWriteToStorage(int numFile) async { + Future performWriteToStorage(int numFile, int command) async { if (_storageService == null) { logServiceNotFoundError('Storage Write', deviceId); return false; @@ -307,8 +307,7 @@ class FriendDeviceConnection extends DeviceConnection { } debugPrint('About to write to storage bytes'); debugPrint('about to send $numFile'); - await storageDataStreamCharacteristic.write([0x00, numFile & 0xFF]); - + await storageDataStreamCharacteristic.write([command & 0xFF, numFile & 0xFF]); return true; } // Future> performGetStorageList(); diff --git a/app/lib/services/devices/models.dart b/app/lib/services/devices/models.dart index f25f17d58..4b52c316a 100644 --- a/app/lib/services/devices/models.dart +++ b/app/lib/services/devices/models.dart @@ -1,8 +1,7 @@ import 'dart:async'; -import 'dart:io'; import 'package:collection/collection.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; -import 'package:friend_private/utils/ble/errors.dart'; +import 'package:friend_private/services/devices/errors.dart'; import 'package:friend_private/utils/logger.dart'; const String friendServiceUuid = '19b10000-e8f2-537e-4f6c-d104768a1214'; diff --git a/app/lib/services/sockets.dart b/app/lib/services/sockets.dart index 4cf3e0a34..92ac5fb17 100644 --- a/app/lib/services/sockets.dart +++ b/app/lib/services/sockets.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:friend_private/backend/schema/bt_device.dart'; -import 'package:friend_private/utils/pure_socket.dart'; +import 'package:friend_private/services/sockets/transcription_connection.dart'; abstract class ISocketService { void start(); diff --git a/app/lib/services/sockets/pure_socket.dart b/app/lib/services/sockets/pure_socket.dart index bf6adfbb5..8b7c9a5ed 100644 --- a/app/lib/services/sockets/pure_socket.dart +++ b/app/lib/services/sockets/pure_socket.dart @@ -9,7 +9,7 @@ import 'package:friend_private/backend/schema/bt_device.dart'; import 'package:friend_private/backend/schema/message_event.dart'; import 'package:friend_private/backend/schema/transcript_segment.dart'; import 'package:friend_private/env/env.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/notifications.dart'; import 'package:instabug_flutter/instabug_flutter.dart'; import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart'; import 'package:web_socket_channel/io.dart'; diff --git a/app/lib/services/sockets/transcription_connection.dart b/app/lib/services/sockets/transcription_connection.dart index bf6adfbb5..8b7c9a5ed 100644 --- a/app/lib/services/sockets/transcription_connection.dart +++ b/app/lib/services/sockets/transcription_connection.dart @@ -9,7 +9,7 @@ import 'package:friend_private/backend/schema/bt_device.dart'; import 'package:friend_private/backend/schema/message_event.dart'; import 'package:friend_private/backend/schema/transcript_segment.dart'; import 'package:friend_private/env/env.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/notifications.dart'; import 'package:instabug_flutter/instabug_flutter.dart'; import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart'; import 'package:web_socket_channel/io.dart'; diff --git a/app/lib/utils/memories/integrations.dart b/app/lib/utils/memories/integrations.dart index c4c012c58..6483ef72c 100644 --- a/app/lib/utils/memories/integrations.dart +++ b/app/lib/utils/memories/integrations.dart @@ -1,7 +1,7 @@ import 'package:friend_private/backend/schema/transcript_segment.dart'; import 'package:friend_private/backend/http/webhooks.dart'; import 'package:friend_private/backend/schema/message.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/notifications.dart'; triggerTranscriptSegmentReceivedEvents( List segments, diff --git a/app/lib/utils/memories/process.dart b/app/lib/utils/memories/process.dart index 96237afd6..0e245895c 100644 --- a/app/lib/utils/memories/process.dart +++ b/app/lib/utils/memories/process.dart @@ -7,7 +7,7 @@ import 'package:friend_private/backend/http/api/memories.dart'; import 'package:friend_private/backend/http/webhooks.dart'; import 'package:friend_private/backend/schema/memory.dart'; import 'package:friend_private/backend/schema/message.dart'; -import 'package:friend_private/services/notification_service.dart'; +import 'package:friend_private/services/notifications.dart'; import 'package:tuple/tuple.dart'; Future processTranscriptContent({