diff --git a/lib/controller/play.dart b/lib/controller/play.dart index bf2ac3e..3207326 100644 --- a/lib/controller/play.dart +++ b/lib/controller/play.dart @@ -1,6 +1,12 @@ +import 'dart:typed_data'; + import 'package:audioplayers/audioplayers.dart'; +import '../pb/stillbox.pb.dart'; class Player { final player = AudioPlayer(); Player(); + Future play(Call call) { + return player.play(BytesSource(Uint8List.fromList(call.audio))); + } } diff --git a/lib/controller/stillbox.dart b/lib/controller/stillbox.dart index d34deda..3a17096 100644 --- a/lib/controller/stillbox.dart +++ b/lib/controller/stillbox.dart @@ -1,9 +1,13 @@ import 'package:flutter/material.dart'; import 'package:web_socket_channel/io.dart'; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import '../pb/stillbox.pb.dart'; import 'play.dart'; +class BadAuthException implements Exception {} + class Stillbox extends ChangeNotifier { + final storage = FlutterSecureStorage(); Player player = Player(); late IOWebSocketChannel channel; bool connected = false; @@ -11,6 +15,8 @@ class Stillbox extends ChangeNotifier { LiveState _state = LiveState.LS_LIVE; Filter? currentFilter; Call? _currentCall; + Map headers = {}; + set state(LiveState newState) { channel.sink.add(Live(state: newState, filter: currentFilter)); _state = newState; @@ -38,8 +44,9 @@ class Stillbox extends ChangeNotifier { _wsUri = Uri.parse(socketUrl); } - void connect() { - channel = IOWebSocketChannel.connect(_wsUri); + Future connect() async { + await setBearer(); + channel = IOWebSocketChannel.connect(_wsUri, headers: headers); channel.stream.listen((event) => _handleData(event), onDone: () { connected = false; }, onError: (error) { @@ -49,10 +56,18 @@ class Stillbox extends ChangeNotifier { notifyListeners(); } + Future setBearer() async { + String? storedToken = await storage.read(key: 'token'); + if (storedToken == null) { + throw (BadAuthException); + } + } + void _handleData(dynamic event) { final msg = Message.fromBuffer(event); switch (msg.whichToClientMessage()) { case Message_ToClientMessage.call: + player.play(msg.call); case Message_ToClientMessage.notification: case Message_ToClientMessage.popup: case Message_ToClientMessage.error: diff --git a/lib/main.dart b/lib/main.dart index 8402b08..61e88c1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -48,21 +48,24 @@ class CallsHomeState extends State { } Future loadData() async { - await Future.delayed(const Duration(seconds: 1)); // Simulate some delay - // Ensure the navigation happens in the context of this widget's subtree - if (mounted) { - Navigator.pushReplacement( - context, - PageRouteBuilder( - pageBuilder: (context, animation, secondaryAnimation) => - const Login(), - transitionsBuilder: (context, animation, secondaryAnimation, child) { - return child; - }, - transitionDuration: const Duration(milliseconds: 0), - ), - ); + try { + await Provider.of(context, listen: false).connect(); + } catch (e) { + if (mounted) { + Navigator.pushReplacement( + context, + PageRouteBuilder( + pageBuilder: (context, animation, secondaryAnimation) => + const Login(), + transitionsBuilder: + (context, animation, secondaryAnimation, child) { + return child; + }, + transitionDuration: const Duration(milliseconds: 0), + ), + ); + } } } diff --git a/lib/views/radio.dart b/lib/views/radio.dart index dc5b18f..40416ef 100644 --- a/lib/views/radio.dart +++ b/lib/views/radio.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import '../../views/lcd.dart'; import '../../views/keypad.dart'; -import '../controller/ws.dart'; class MainRadio extends StatefulWidget { const MainRadio({super.key, required this.title}); diff --git a/linux/flutter/generated_plugin_registrant.cc b/linux/flutter/generated_plugin_registrant.cc index 1830e5c..14a06d2 100644 --- a/linux/flutter/generated_plugin_registrant.cc +++ b/linux/flutter/generated_plugin_registrant.cc @@ -7,9 +7,13 @@ #include "generated_plugin_registrant.h" #include +#include void fl_register_plugins(FlPluginRegistry* registry) { g_autoptr(FlPluginRegistrar) audioplayers_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "AudioplayersLinuxPlugin"); audioplayers_linux_plugin_register_with_registrar(audioplayers_linux_registrar); + g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin"); + flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar); } diff --git a/linux/flutter/generated_plugins.cmake b/linux/flutter/generated_plugins.cmake index e9abb91..188af9f 100644 --- a/linux/flutter/generated_plugins.cmake +++ b/linux/flutter/generated_plugins.cmake @@ -4,6 +4,7 @@ list(APPEND FLUTTER_PLUGIN_LIST audioplayers_linux + flutter_secure_storage_linux ) list(APPEND FLUTTER_FFI_PLUGIN_LIST diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index a9f2f23..ae73647 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -6,9 +6,11 @@ import FlutterMacOS import Foundation import audioplayers_darwin +import flutter_secure_storage_macos import path_provider_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin")) + FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index 4932418..e37280f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -158,6 +158,54 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.0" + flutter_secure_storage: + dependency: "direct main" + description: + name: flutter_secure_storage + sha256: "165164745e6afb5c0e3e3fcc72a012fb9e58496fb26ffb92cf22e16a821e85d0" + url: "https://pub.dev" + source: hosted + version: "9.2.2" + flutter_secure_storage_linux: + dependency: transitive + description: + name: flutter_secure_storage_linux + sha256: "4d91bfc23047422cbcd73ac684bc169859ee766482517c22172c86596bf1464b" + url: "https://pub.dev" + source: hosted + version: "1.2.1" + flutter_secure_storage_macos: + dependency: transitive + description: + name: flutter_secure_storage_macos + sha256: "1693ab11121a5f925bbea0be725abfcfbbcf36c1e29e571f84a0c0f436147a81" + url: "https://pub.dev" + source: hosted + version: "3.1.2" + flutter_secure_storage_platform_interface: + dependency: transitive + description: + name: flutter_secure_storage_platform_interface + sha256: cf91ad32ce5adef6fba4d736a542baca9daf3beac4db2d04be350b87f69ac4a8 + url: "https://pub.dev" + source: hosted + version: "1.1.2" + flutter_secure_storage_web: + dependency: transitive + description: + name: flutter_secure_storage_web + sha256: f4ebff989b4f07b2656fb16b47852c0aab9fed9b4ec1c70103368337bc1886a9 + url: "https://pub.dev" + source: hosted + version: "1.2.1" + flutter_secure_storage_windows: + dependency: transitive + description: + name: flutter_secure_storage_windows + sha256: b20b07cb5ed4ed74fc567b78a72936203f587eba460af1df11281c9326cd3709 + url: "https://pub.dev" + source: hosted + version: "3.1.2" flutter_test: dependency: "direct dev" description: flutter @@ -477,6 +525,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.1" + win32: + dependency: transitive + description: + name: win32 + sha256: "68d1e89a91ed61ad9c370f9f8b6effed9ae5e0ede22a270bdfa6daf79fc2290a" + url: "https://pub.dev" + source: hosted + version: "5.5.4" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 69dd879..a3c57ed 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: web_socket_channel: ^3.0.1 provider: ^6.1.2 audioplayers: ^5.2.1 + flutter_secure_storage: ^9.2.2 dev_dependencies: flutter_test: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 09e8e2c..5f23188 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -7,8 +7,11 @@ #include "generated_plugin_registrant.h" #include +#include void RegisterPlugins(flutter::PluginRegistry* registry) { AudioplayersWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin")); + FlutterSecureStorageWindowsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin")); } diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 375535c..1164d9f 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -4,6 +4,7 @@ list(APPEND FLUTTER_PLUGIN_LIST audioplayers_windows + flutter_secure_storage_windows ) list(APPEND FLUTTER_FFI_PLUGIN_LIST