init: rough companion app stub
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../data/sources/localiser/realtime_data_client.dart';
|
||||
import '../../providers.dart';
|
||||
|
||||
class LoginScreen extends ConsumerStatefulWidget {
|
||||
const LoginScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<LoginScreen> createState() => _LoginScreenState();
|
||||
}
|
||||
|
||||
class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
final _usernameController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
bool _loading = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tryAutoLogin();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_usernameController.dispose();
|
||||
_passwordController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _tryAutoLogin() async {
|
||||
final store = ref.read(credentialStoreProvider);
|
||||
final saved = await store.load();
|
||||
if (saved == null || !mounted) return;
|
||||
|
||||
_usernameController.text = saved.username;
|
||||
_passwordController.text = saved.password;
|
||||
await _login(saved.username, saved.password, saveCredentials: false);
|
||||
}
|
||||
|
||||
Future<void> _login(String username, String password,
|
||||
{bool saveCredentials = true}) async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
final client = ref.read(sessionClientProvider);
|
||||
final tokenResponse = await client.login(username, password);
|
||||
final token = tokenResponse.token;
|
||||
|
||||
ref.read(authTokenProvider.notifier).state = token;
|
||||
|
||||
final config = ref.read(serverConfigProvider)!;
|
||||
final realtime = RealtimeDataClient(config: config, token: token);
|
||||
await realtime.connect();
|
||||
ref.read(realtimeDataClientProvider.notifier).state = realtime;
|
||||
|
||||
if (saveCredentials) {
|
||||
await ref
|
||||
.read(credentialStoreProvider)
|
||||
.save((username: username, password: password));
|
||||
}
|
||||
|
||||
if (mounted) context.go('/floorplan');
|
||||
} on Exception catch (e) {
|
||||
setState(() => _error = e.toString());
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Sign In')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
TextField(
|
||||
controller: _usernameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Username',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
textInputAction: TextInputAction.next,
|
||||
autofillHints: const [AutofillHints.username],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _passwordController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Password',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
obscureText: true,
|
||||
textInputAction: TextInputAction.done,
|
||||
autofillHints: const [AutofillHints.password],
|
||||
onSubmitted: _loading
|
||||
? null
|
||||
: (_) => _login(
|
||||
_usernameController.text.trim(),
|
||||
_passwordController.text,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (_error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: Text(
|
||||
_error!,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: _loading
|
||||
? null
|
||||
: () => _login(
|
||||
_usernameController.text.trim(),
|
||||
_passwordController.text,
|
||||
),
|
||||
child: _loading
|
||||
? const SizedBox.square(
|
||||
dimension: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('Sign in'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../data/sources/localiser/onboarding_client.dart';
|
||||
import '../../data/sources/mdns/mdns_discovery.dart';
|
||||
import '../../domain/models/server_config.dart';
|
||||
import '../../providers.dart';
|
||||
|
||||
class ServerDiscoveryScreen extends ConsumerStatefulWidget {
|
||||
const ServerDiscoveryScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<ServerDiscoveryScreen> createState() =>
|
||||
_ServerDiscoveryScreenState();
|
||||
}
|
||||
|
||||
class _ServerDiscoveryScreenState extends ConsumerState<ServerDiscoveryScreen> {
|
||||
final _hostController = TextEditingController();
|
||||
final _portController = TextEditingController(text: '4000');
|
||||
bool _connecting = false;
|
||||
String? _error;
|
||||
|
||||
final _discovery = MdnsDiscovery();
|
||||
final _discoveredServers = <ServerConfig>[];
|
||||
StreamSubscription<ServerConfig>? _discoverySub;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_discoverySub = _discovery.discover().listen(
|
||||
(server) {
|
||||
if (!_discoveredServers.contains(server)) {
|
||||
setState(() => _discoveredServers.add(server));
|
||||
}
|
||||
},
|
||||
onError: (_) {},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_discoverySub?.cancel();
|
||||
_hostController.dispose();
|
||||
_portController.dispose();
|
||||
_discovery.stop();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _connect(ServerConfig config) async {
|
||||
setState(() {
|
||||
_connecting = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
final checklist =
|
||||
await OnboardingClient(config: config).getChecklist();
|
||||
|
||||
ref.read(serverConfigProvider.notifier).state = config;
|
||||
|
||||
if (!mounted) return;
|
||||
if (!checklist.hasAdmin) {
|
||||
context.go('/onboarding');
|
||||
} else {
|
||||
context.go('/login');
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() => _error = e.toString());
|
||||
} finally {
|
||||
if (mounted) setState(() => _connecting = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Connect to Server')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text('Discovered servers'),
|
||||
const SizedBox(height: 8),
|
||||
if (_discoveredServers.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 32),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Scanning…',
|
||||
style: TextStyle(color: Colors.grey),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 200),
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: _discoveredServers.length,
|
||||
itemBuilder: (context, i) {
|
||||
final server = _discoveredServers[i];
|
||||
return ListTile(
|
||||
title: Text(server.host),
|
||||
subtitle: Text('Port ${server.port}'),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: _connecting ? null : () => _connect(server),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
const SizedBox(height: 16),
|
||||
const Text('Manual entry'),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _hostController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Host / IP',
|
||||
hintText: '192.168.1.100',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.url,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _portController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Port',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (_error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: Text(
|
||||
_error!,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: _connecting
|
||||
? null
|
||||
: () => _connect(ServerConfig(
|
||||
host: _hostController.text.trim(),
|
||||
port: int.tryParse(_portController.text) ?? 4000,
|
||||
)),
|
||||
child: _connecting
|
||||
? const SizedBox.square(
|
||||
dimension: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('Connect'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user