Files
Docwell/Phone-App/lib/screens/space_screen.dart
T
tajniak81andClaude Opus 5.5 1f580ca6f8 Add Mobile 1.0 and UnifiedPush notifications
Mobile app (Android):
- Sign-in to any server, with authenticator codes. The server address and
  token are kept in secure storage; a dropped connection never signs out.
- Workspaces, recent pages, spaces and a foldable page tree; search; the
  inbox with mark-read.
- Pages open read-only in the shared editor from their saved content and
  join the live document to edit (with the native toolbar). Page links in
  the text open in the app (new "navigate" bridge message).
- Comments: threads, replies, resolve.
- "Save to Docwell" in the Android share sheet makes a page from shared
  text, with web addresses as links.
- Offline reading: every answer the app reads is kept on the phone, and
  used when the server can't be reached.
- Push through UnifiedPush (ntfy, NextPush, ...). The app uses the Android
  connector directly; the umbrella package pulls in a native crypto build
  for Linux that breaks `dart run` and tests.

Server:
- /me/devices registers a phone's UnifiedPush endpoint and Web Push keys
  (new push_devices collection). Each notification is also pushed,
  encrypted with RFC 8291 aes128gcm so the distributor can't read it; the
  app drops messages that don't decrypt. Endpoints answering 404/410 are
  forgotten. Endpoints follow the webhook address rules.
- /collab accepts the apps' own origins ("null" for the mobile editor page,
  wails:// for the desktop app). Sign-in is a token in the first message,
  not a cookie, so this opens no cross-site hijacking.

Tried on an Android 15 emulator: sign-in, browsing, reading, live editing,
sharing and offline reading. Not covered: push end to end (no distributor
on the emulator), iOS, and YouTube embeds, which refuse to play from the
app's local editor page (error 153).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 14:52:20 +01:00

159 lines
4.7 KiB
Dart

// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:flutter/material.dart';
import '../api/docwell_api.dart';
import '../app.dart';
import 'page_screen.dart';
import 'widgets.dart';
/// One page of the tree with how deep it sits.
typedef TreeRow = ({PageNode page, int depth});
/// The space's pages in tree order with their depth. The server lists them
/// depth-first; a page whose parent isn't listed sits at the top.
List<TreeRow> treeRows(List<PageNode> pages) {
final depth = <String, int>{};
return [
for (final p in pages)
(
page: p,
depth: depth[p.id] = p.parent.isEmpty ? 0 : (depth[p.parent] ?? -1) + 1,
),
];
}
/// A space's page tree. Branches start folded; tap the arrow to open them.
class SpaceScreen extends StatefulWidget {
const SpaceScreen({super.key, required this.session, required this.space});
final AppSession session;
final Space space;
@override
State<SpaceScreen> createState() => _SpaceScreenState();
}
class _SpaceScreenState extends State<SpaceScreen> {
List<PageNode>? _pages;
final _open = <String>{};
String? _error;
bool _offline = false;
bool get _canEdit =>
widget.space.role == SpaceCallerRole.admin ||
widget.space.role == SpaceCallerRole.editor;
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
final api = widget.session.api;
try {
final pages = await api.pages(widget.space.id);
if (mounted) {
setState(() {
_pages = pages;
_offline = api.offline;
_error = null;
});
}
} catch (e) {
if (mounted) setState(() => _error = describeError(e));
}
}
Future<void> _openPage(String id, {bool edit = false}) async {
await Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) =>
PageScreen(session: widget.session, pageId: id, startEditing: edit),
),
);
_load();
}
Future<void> _newPage() async {
try {
final p = await widget.session.api.createPage(widget.space.id);
await _openPage(p.id, edit: true);
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(describeError(e))));
}
}
}
@override
Widget build(BuildContext context) {
final pages = _pages;
final rows = pages == null ? <TreeRow>[] : treeRows(pages);
final hasChildren = {for (final p in pages ?? <PageNode>[]) p.parent};
// A row shows when every ancestor is open.
final visible = <TreeRow>[];
final shown = <String>{''};
for (final r in rows) {
if (shown.contains(r.page.parent) &&
(r.page.parent.isEmpty || _open.contains(r.page.parent))) {
visible.add(r);
shown.add(r.page.id);
}
}
return Scaffold(
appBar: AppBar(title: Text(widget.space.name)),
floatingActionButton: _canEdit && !_offline
? FloatingActionButton(
tooltip: 'New page',
onPressed: _newPage,
child: const Icon(Icons.add),
)
: null,
body: RefreshIndicator(
onRefresh: _load,
child: ListView(
children: [
if (_offline) const OfflineBanner(),
if (_error != null)
ErrorTile(message: _error!, onRetry: _load)
else if (pages == null)
const Padding(
padding: EdgeInsets.all(32),
child: Center(child: CircularProgressIndicator()),
)
else if (pages.isEmpty)
const ListTile(title: Text('No pages yet.')),
for (final r in visible)
ListTile(
key: Key('page-${r.page.id}'),
contentPadding: EdgeInsets.only(
left: 8.0 + r.depth * 20,
right: 16,
),
leading: hasChildren.contains(r.page.id)
? IconButton(
tooltip: _open.contains(r.page.id) ? 'Fold' : 'Unfold',
icon: Icon(
_open.contains(r.page.id)
? Icons.expand_more
: Icons.chevron_right,
),
onPressed: () => setState(() {
if (!_open.remove(r.page.id)) _open.add(r.page.id);
}),
)
: SizedBox(width: 48, child: PageIcon(r.page.icon)),
title: Text(r.page.title.isEmpty ? 'Untitled' : r.page.title),
onTap: () => _openPage(r.page.id),
),
],
),
),
);
}
}