// SPDX-License-Identifier: AGPL-3.0-or-later import 'package:flutter/material.dart'; import 'api/docwell_api.dart'; import 'editor/editor_screen.dart'; void main() { runApp(const DocwellApp()); } // Android emulators reach the host machine at 10.0.2.2. const defaultServerUrl = 'http://10.0.2.2:8080'; class DocwellApp extends StatefulWidget { const DocwellApp({super.key, this.api}); final DocwellApi? api; @override State createState() => _DocwellAppState(); } class _DocwellAppState extends State { late final DocwellApi _api = widget.api ?? DocwellApi(defaultServerUrl); // Kept in memory only for now; secure storage comes with the full mobile app. User? _user; @override Widget build(BuildContext context) { const brand = Color(0xFF2F5FD0); return MaterialApp( title: 'Docwell', theme: ThemeData(colorSchemeSeed: brand, useMaterial3: true), darkTheme: ThemeData( colorSchemeSeed: brand, brightness: Brightness.dark, useMaterial3: true, ), home: _user == null ? LoginScreen(api: _api, onSignedIn: (u) => setState(() => _user = u)) : HomeScreen( api: _api, user: _user!, onSignOut: () => setState(() { _api.logout(); _user = null; }), ), ); } } class LoginScreen extends StatefulWidget { const LoginScreen({super.key, required this.api, required this.onSignedIn}); final DocwellApi api; final ValueChanged onSignedIn; @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _email = TextEditingController(); final _password = TextEditingController(); bool _busy = false; String? _error; static const _messages = { 'invalid_credentials': 'Wrong email or password.', 'account_disabled': 'This account is disabled.', 'mfa_required': 'Two-step sign-in is not supported in the app yet.', }; Future _submit() async { setState(() { _busy = true; _error = null; }); try { widget.onSignedIn( await widget.api.login(_email.text.trim(), _password.text), ); } on DocwellApiException catch (e) { setState(() => _error = _messages[e.code] ?? 'Sign-in failed.'); } catch (_) { setState(() => _error = "Can't reach the server."); } finally { if (mounted) setState(() => _busy = false); } } @override void dispose() { _email.dispose(); _password.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 400), child: AutofillGroup( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text( 'Sign in to Docwell', style: Theme.of(context).textTheme.headlineSmall, ), const SizedBox(height: 24), TextField( key: const Key('email'), controller: _email, keyboardType: TextInputType.emailAddress, autofillHints: const [AutofillHints.email], decoration: const InputDecoration( labelText: 'Email', border: OutlineInputBorder(), ), ), const SizedBox(height: 12), TextField( key: const Key('password'), controller: _password, obscureText: true, autofillHints: const [AutofillHints.password], onSubmitted: (_) => _submit(), decoration: const InputDecoration( labelText: 'Password', border: OutlineInputBorder(), ), ), if (_error != null) ...[ const SizedBox(height: 12), Text( _error!, style: TextStyle( color: Theme.of(context).colorScheme.error, ), ), ], const SizedBox(height: 20), FilledButton( onPressed: _busy ? null : _submit, child: const Text('Sign in'), ), ], ), ), ), ), ), ), ); } } class HomeScreen extends StatefulWidget { const HomeScreen({ super.key, required this.api, required this.user, required this.onSignOut, }); final DocwellApi api; final User user; final VoidCallback onSignOut; @override State createState() => _HomeScreenState(); } String _roleLabel(Role role) => switch (role) { Role.superadmin => 'Superadmin', Role.admin => 'Admin', Role.user => 'User', }; /// A local page for trying the WebView editor before pages sync to mobile. const _scratchPage = { 'type': 'doc', 'content': [ { 'type': 'heading', 'attrs': {'level': 1}, 'content': [ {'type': 'text', 'text': 'Scratch page'}, ], }, { 'type': 'paragraph', 'content': [ {'type': 'text', 'text': 'This is the same editor as the web app.'}, ], }, ], }; class _HomeScreenState extends State { late Future _version = widget.api.version(); void _retry() => setState(() => _version = widget.api.version()); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Docwell'), actions: [ TextButton( onPressed: widget.onSignOut, child: const Text('Sign out'), ), ], ), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Text( 'Hello, ${widget.user.name}', style: Theme.of(context).textTheme.titleLarge, ), const SizedBox(height: 4), Text( widget.user.organizationName.isEmpty ? _roleLabel(widget.user.role) : '${_roleLabel(widget.user.role)} · ${widget.user.organizationName}', ), const SizedBox(height: 16), FutureBuilder( future: _version, builder: (context, snap) { if (snap.connectionState != ConnectionState.done) { return const CircularProgressIndicator(); } if (snap.hasError) { return Column( mainAxisSize: MainAxisSize.min, children: [ const Text('Server unreachable'), const SizedBox(height: 12), FilledButton( onPressed: _retry, child: const Text('Retry'), ), ], ); } return Text('Connected · v${snap.data}'); }, ), const SizedBox(height: 24), OutlinedButton.icon( icon: const Icon(Icons.edit_note), label: const Text('Try the editor'), onPressed: () => Navigator.of(context).push( MaterialPageRoute( builder: (_) => const EditorScreen( title: 'Scratch page', content: _scratchPage, ), ), ), ), ], ), ), ); } }