Files
m8chat-app2/lib/features/spaces/presentation/spaces_screen.dart
help4bis b941cdfe4b refactor: /simplify — 22 fixes from 3-agent code review
Critical:
- Fix MXC URI resolution: all avatars/images now resolve mxc:// to HTTP
- Sync persistence: only write changed rooms, batch message upserts
- lastActivityAt uses room.lastEvent.originServerTs, not creation time

High:
- Shared MatrixAvatar widget replaces 6 duplicate implementations
- CallScreen decodes roomId before LiveKit JWT fetch
- Decline button actually dismisses incoming call overlay
- EventTypes constants replace raw string literals
- LiveKitService uses lazy auth reads, onDispose disconnects

Medium:
- CallController is keepAlive with timer/room cleanup
- authRepository is keepAlive (used from keepAlive notifier)
- StreamController not closed in stopListening (crash fix)
- Index on messages.roomId for query performance
- 400ms debounce on user search
- Static DateFormat in MessageBubble
- Hardcoded strings replaced with AppConfig refs
- Duplicate isDirectMessage field removed from RoomModel
- E2EE profile claim corrected to Phase 3

Shared utilities:
- lib/shared/widgets/matrix_avatar.dart
- lib/shared/utils/mxc_url.dart
- lib/shared/utils/room_preview.dart
- lib/shared/utils/matrix_id.dart

rawJson column removed (unused, caused main-thread jsonEncode)
Schema migrated to v2 with roomId index.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 13:19:22 +10:00

225 lines
6.6 KiB
Dart

// Version: 1.2.0 | Created: 2026-04-01 | Updated: 2026-04-02
// Spaces screen — list of Matrix spaces with expandable child room lists.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../../shared/widgets/matrix_avatar.dart';
import '../data/spaces_repository.dart';
import '../domain/space_model.dart';
part 'spaces_screen.g.dart';
// ---------------------------------------------------------------------------
// Providers
// ---------------------------------------------------------------------------
@riverpod
Stream<List<SpaceModel>> spacesStream(Ref ref) {
return ref.watch(spacesRepositoryProvider).watchSpaces();
}
@riverpod
List<SpaceRoomModel> spaceRooms(Ref ref, String spaceId) {
return ref.watch(spacesRepositoryProvider).getRoomsInSpace(spaceId);
}
// ---------------------------------------------------------------------------
// Screen
// ---------------------------------------------------------------------------
class SpacesScreen extends ConsumerWidget {
const SpacesScreen({super.key, this.embedded = false});
final bool embedded;
@override
Widget build(BuildContext context, WidgetRef ref) {
final spacesAsync = ref.watch(spacesStreamProvider);
final body = spacesAsync.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (err, _) => Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Text('Could not load spaces: $err'),
),
),
data: (spaces) {
if (spaces.isEmpty) {
return _EmptySpacesState();
}
return ListView.builder(
itemCount: spaces.length,
itemBuilder: (context, index) {
return _SpaceTile(space: spaces[index]);
},
);
},
);
if (embedded) return body;
return Scaffold(
appBar: AppBar(title: const Text('Spaces')),
body: body,
);
}
}
// ---------------------------------------------------------------------------
// Space tile with expandable child room list
// ---------------------------------------------------------------------------
class _SpaceTile extends ConsumerStatefulWidget {
const _SpaceTile({required this.space});
final SpaceModel space;
@override
ConsumerState<_SpaceTile> createState() => _SpaceTileState();
}
class _SpaceTileState extends ConsumerState<_SpaceTile> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final space = widget.space;
return Column(
children: [
// Space header row
ListTile(
leading: MatrixAvatar(
name: space.displayName,
avatarUrl: space.avatarUrl,
radius: 22,
),
title: Text(
space.displayName,
style: theme.textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.w600,
),
),
subtitle: Text(
'${space.roomCount} ${space.roomCount == 1 ? 'room' : 'rooms'}',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurface.withAlpha(153),
),
),
trailing: space.roomCount > 0
? IconButton(
icon: Icon(
_expanded ? Icons.expand_less : Icons.expand_more,
color: theme.colorScheme.onSurface.withAlpha(153),
),
onPressed: () => setState(() => _expanded = !_expanded),
)
: null,
onTap: () => setState(() => _expanded = !_expanded),
),
// Child rooms — shown when expanded
if (_expanded) _ChildRoomList(spaceId: space.id),
const Divider(height: 1),
],
);
}
}
// ---------------------------------------------------------------------------
// Child room list
// ---------------------------------------------------------------------------
class _ChildRoomList extends ConsumerWidget {
const _ChildRoomList({required this.spaceId});
final String spaceId;
@override
Widget build(BuildContext context, WidgetRef ref) {
final rooms = ref.watch(spaceRoomsProvider(spaceId));
final theme = Theme.of(context);
if (rooms.isEmpty) {
return Padding(
padding: const EdgeInsets.only(left: 56, right: 16, bottom: 8),
child: Text(
'No joined rooms in this space.',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurface.withAlpha(102),
),
),
);
}
return Column(
children: rooms.map((room) {
return ListTile(
contentPadding: const EdgeInsets.only(left: 56, right: 16),
leading: MatrixAvatar(
name: room.displayName,
avatarUrl: room.avatarUrl,
radius: 18,
),
title: Text(room.displayName, style: theme.textTheme.bodyMedium),
trailing: room.isDirect
? Icon(
Icons.person_outline,
size: 16,
color: theme.colorScheme.onSurface.withAlpha(102),
)
: null,
onTap: () => context.push('/rooms/${Uri.encodeComponent(room.id)}'),
);
}).toList(),
);
}
}
// ---------------------------------------------------------------------------
// Empty state
// ---------------------------------------------------------------------------
class _EmptySpacesState extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.dashboard_outlined,
size: 64,
color: theme.colorScheme.onSurface.withAlpha(77),
),
const SizedBox(height: 16),
Text(
'No spaces yet',
style: theme.textTheme.titleMedium?.copyWith(
color: theme.colorScheme.onSurface.withAlpha(153),
),
),
const SizedBox(height: 8),
Text(
'Spaces you join will appear here.',
textAlign: TextAlign.center,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurface.withAlpha(102),
),
),
],
),
),
);
}
}