This commit is contained in:
Daniel 2024-08-12 09:13:43 -04:00
parent d6d590684d
commit 5f92cf977b
5 changed files with 117 additions and 59 deletions

View file

@ -1,20 +1,25 @@
import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/web_socket_channel.dart';
import '../pb/stillbox.pb.dart'; import '../pb/stillbox.pb.dart';
class LiveFeeder { class Client {
late Uri _wsUri; late Uri _wsUri;
late WebSocketChannel channel; late WebSocketChannel channel;
LiveFeeder() { Client() {
String socketUrl = 'ws://xenon:3050/ws'; String socketUrl = 'ws://xenon:3050/ws';
Uri baseUri = Uri.base; Uri baseUri = Uri.base;
if (baseUri.scheme == 'http' || baseUri.scheme == 'https') { if (baseUri.scheme == 'http' || baseUri.scheme == 'https') {
String port = (baseUri.hasPort ? ':${baseUri.port}' : ''); final port = (baseUri.hasPort ? ':${baseUri.port}' : '');
socketUrl = 'ws://${baseUri.host}$port/ws'; socketUrl =
'${baseUri.scheme == 'http' ? 'ws' : 'wss'}://${baseUri.host}$port/ws';
} }
_wsUri = Uri.parse(socketUrl); _wsUri = Uri.parse(socketUrl);
} }
bool isConnected() {
return false;
}
void connect() { void connect() {
channel = WebSocketChannel.connect(_wsUri); channel = WebSocketChannel.connect(_wsUri);
channel.stream.listen((event) => _handleData(event)); channel.stream.listen((event) => _handleData(event));

View file

@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'views/radio.dart'; import 'views/radio.dart';
import 'views/login.dart';
import 'controller/ws.dart';
void main() { void main() {
runApp(const CallsApp()); runApp(const CallsApp());
@ -20,7 +22,50 @@ class CallsApp extends StatelessWidget {
brightness: Brightness.dark, brightness: Brightness.dark,
), ),
themeMode: ThemeMode.dark, themeMode: ThemeMode.dark,
home: const MainRadio(title: 'Stillbox'), home: const CallsHome(),
); );
} }
} }
class CallsHome extends StatefulWidget {
const CallsHome({super.key});
@override
State<StatefulWidget> createState() => CallsHomeState();
}
class CallsHomeState extends State<CallsHome> {
final c = Client();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
loadData();
});
}
Future<void> 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');
}
}

View file

@ -1,9 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class Login extends StatefulWidget { class Login extends StatefulWidget {
const Login({super.key, required this.title}); const Login({super.key});
final String title;
@override @override
State<Login> createState() => _LoginState(); State<Login> createState() => _LoginState();
@ -17,55 +15,66 @@ class _LoginState extends State<Login> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('stillbox login:'),
),
body: Form( body: Form(
key: _formKey, key: _formKey,
child: Padding( child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16), padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
child: Column(children: [ child: Column(children: [
TextFormField( Padding(
controller: userController, padding:
decoration: const InputDecoration( const EdgeInsets.symmetric(horizontal: 8, vertical: 10),
border: OutlineInputBorder(), labelText: "Username"), child: TextFormField(
validator: (value) { controller: userController,
if (value == null || value.isEmpty) { decoration: const InputDecoration(
return 'Please enter your username.'; border: OutlineInputBorder(), labelText: "Username"),
} validator: (value) {
return null; if (value == null || value.isEmpty) {
}, return 'Please enter your username.';
), }
Padding( return null;
padding: },
const EdgeInsets.symmetric(horizontal: 8, vertical: 16), )),
child: TextFormField( Padding(
controller: passwordController, padding:
obscureText: true, const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
decoration: const InputDecoration( child: TextFormField(
border: OutlineInputBorder(), labelText: "Password"), controller: passwordController,
validator: (value) { obscureText: true,
if (value == null || value.isEmpty) { decoration: const InputDecoration(
return 'Please enter your password'; border: OutlineInputBorder(), labelText: "Password"),
} validator: (value) {
return null; if (value == null || value.isEmpty) {
}, return 'Please enter your password.';
)), }
Padding( return null;
padding: },
const EdgeInsets.symmetric(horizontal: 8, vertical: 16.0), )),
child: Center( Padding(
child: ElevatedButton( padding: const EdgeInsets.symmetric(
onPressed: () { horizontal: 8, vertical: 16.0),
if (_formKey.currentState!.validate()) { child: Center(
// TODO do login here child: ElevatedButton(
} else { onPressed: () {
ScaffoldMessenger.of(context).showSnackBar( if (_formKey.currentState!.validate()) {
const SnackBar(content: Text('Please login.')), // TODO do login here
); } else {
} ScaffoldMessenger.of(context).showSnackBar(
}, const SnackBar(content: Text('Please login.')),
child: const Text('Login'), );
), }
)), },
])), style: ElevatedButton.styleFrom(
)); foregroundColor: Colors.white,
backgroundColor: Colors.green,
),
child: const Text('Login'),
),
)),
])),
));
} }
} }

View file

@ -13,7 +13,6 @@ class MainRadio extends StatefulWidget {
} }
class _MainRadioState extends State<MainRadio> { class _MainRadioState extends State<MainRadio> {
LiveFeeder f = LiveFeeder();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const Scaffold( return const Scaffold(

View file

@ -13,7 +13,7 @@ import 'package:calls/main.dart';
void main() { void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async { testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame. // Build our app and trigger a frame.
await tester.pumpWidget(const CallsApp()); await tester.pumpWidget(const CallsHome());
// Verify that our counter starts at 0. // Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget); expect(find.text('0'), findsOneWidget);