diff --git a/lib/controller/ws.dart b/lib/controller/ws.dart index 400d94b..a4e3d98 100644 --- a/lib/controller/ws.dart +++ b/lib/controller/ws.dart @@ -1,20 +1,25 @@ import 'package:web_socket_channel/web_socket_channel.dart'; import '../pb/stillbox.pb.dart'; -class LiveFeeder { +class Client { late Uri _wsUri; late WebSocketChannel channel; - LiveFeeder() { + Client() { String socketUrl = 'ws://xenon:3050/ws'; Uri baseUri = Uri.base; if (baseUri.scheme == 'http' || baseUri.scheme == 'https') { - String port = (baseUri.hasPort ? ':${baseUri.port}' : ''); - socketUrl = 'ws://${baseUri.host}$port/ws'; + final port = (baseUri.hasPort ? ':${baseUri.port}' : ''); + socketUrl = + '${baseUri.scheme == 'http' ? 'ws' : 'wss'}://${baseUri.host}$port/ws'; } _wsUri = Uri.parse(socketUrl); } + bool isConnected() { + return false; + } + void connect() { channel = WebSocketChannel.connect(_wsUri); channel.stream.listen((event) => _handleData(event)); diff --git a/lib/main.dart b/lib/main.dart index 94a24e8..33a0e99 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'views/radio.dart'; +import 'views/login.dart'; +import 'controller/ws.dart'; void main() { runApp(const CallsApp()); @@ -20,7 +22,50 @@ class CallsApp extends StatelessWidget { brightness: Brightness.dark, ), themeMode: ThemeMode.dark, - home: const MainRadio(title: 'Stillbox'), + home: const CallsHome(), ); } } + +class CallsHome extends StatefulWidget { + const CallsHome({super.key}); + + @override + State createState() => CallsHomeState(); +} + +class CallsHomeState extends State { + final c = Client(); + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) { + loadData(); + }); + } + + 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), + ), + ); + } + } + + @override + Widget build(BuildContext context) { + return const MainRadio(title: 'Stillbox'); + } +} diff --git a/lib/views/login.dart b/lib/views/login.dart index 88d0665..bf8eb79 100644 --- a/lib/views/login.dart +++ b/lib/views/login.dart @@ -1,9 +1,7 @@ import 'package:flutter/material.dart'; class Login extends StatefulWidget { - const Login({super.key, required this.title}); - - final String title; + const Login({super.key}); @override State createState() => _LoginState(); @@ -17,55 +15,66 @@ class _LoginState extends State { @override Widget build(BuildContext context) { return Scaffold( + appBar: AppBar( + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + title: const Text('stillbox login:'), + ), body: Form( - key: _formKey, - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16), - child: Column(children: [ - TextFormField( - controller: userController, - decoration: const InputDecoration( - border: OutlineInputBorder(), labelText: "Username"), - validator: (value) { - if (value == null || value.isEmpty) { - return 'Please enter your username.'; - } - return null; - }, - ), - Padding( - padding: - const EdgeInsets.symmetric(horizontal: 8, vertical: 16), - child: TextFormField( - controller: passwordController, - obscureText: true, - decoration: const InputDecoration( - border: OutlineInputBorder(), labelText: "Password"), - validator: (value) { - if (value == null || value.isEmpty) { - return 'Please enter your password'; - } - return null; - }, - )), - Padding( - padding: - const EdgeInsets.symmetric(horizontal: 8, vertical: 16.0), - child: Center( - child: ElevatedButton( - onPressed: () { - if (_formKey.currentState!.validate()) { - // TODO do login here - } else { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Please login.')), - ); - } - }, - child: const Text('Login'), - ), - )), - ])), - )); + key: _formKey, + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16), + child: Column(children: [ + Padding( + padding: + const EdgeInsets.symmetric(horizontal: 8, vertical: 10), + child: TextFormField( + controller: userController, + decoration: const InputDecoration( + border: OutlineInputBorder(), labelText: "Username"), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter your username.'; + } + return null; + }, + )), + Padding( + padding: + const EdgeInsets.symmetric(horizontal: 8, vertical: 16), + child: TextFormField( + controller: passwordController, + obscureText: true, + decoration: const InputDecoration( + border: OutlineInputBorder(), labelText: "Password"), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter your password.'; + } + return null; + }, + )), + Padding( + padding: const EdgeInsets.symmetric( + horizontal: 8, vertical: 16.0), + child: Center( + child: ElevatedButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + // TODO do login here + } else { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Please login.')), + ); + } + }, + style: ElevatedButton.styleFrom( + foregroundColor: Colors.white, + backgroundColor: Colors.green, + ), + child: const Text('Login'), + ), + )), + ])), + )); } } diff --git a/lib/views/radio.dart b/lib/views/radio.dart index d69dbcb..dc5b18f 100644 --- a/lib/views/radio.dart +++ b/lib/views/radio.dart @@ -13,7 +13,6 @@ class MainRadio extends StatefulWidget { } class _MainRadioState extends State { - LiveFeeder f = LiveFeeder(); @override Widget build(BuildContext context) { return const Scaffold( diff --git a/test/widget_test.dart b/test/widget_test.dart index 16287a1..a954be8 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -13,7 +13,7 @@ import 'package:calls/main.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { // Build our app and trigger a frame. - await tester.pumpWidget(const CallsApp()); + await tester.pumpWidget(const CallsHome()); // Verify that our counter starts at 0. expect(find.text('0'), findsOneWidget);