Files
Docwell/Phone-App/lib/main.dart
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

281 lines
8.0 KiB
Dart

// 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<DocwellApp> createState() => _DocwellAppState();
}
class _DocwellAppState extends State<DocwellApp> {
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<User> onSignedIn;
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
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<void> _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<HomeScreen> 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<HomeScreen> {
late Future<String> _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<String>(
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<void>(
builder: (_) => const EditorScreen(
title: 'Scratch page',
content: _scratchPage,
),
),
),
),
],
),
),
);
}
}