View messages for a room

This commit is contained in:
Brandon Presley 2023-12-29 10:40:09 +13:00
parent a6306ceddc
commit 3352939009
4 changed files with 143 additions and 57 deletions

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:zenith/chats.dart'; import 'package:zenith/rooms.dart';
import 'package:zenith/zenith_client_provider.dart'; import 'package:zenith/zenith_client_provider.dart';
class LoginPage extends StatefulWidget { class LoginPage extends StatefulWidget {
@ -29,7 +29,7 @@ class _LoginPageState extends State<LoginPage> {
if (!mounted) return; if (!mounted) return;
Navigator.pushAndRemoveUntil( Navigator.pushAndRemoveUntil(
context, context,
MaterialPageRoute(builder: (context) => const ChatsPage()), MaterialPageRoute(builder: (context) => const RoomsPage()),
(route) => false); (route) => false);
} }

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:zenith/chats.dart'; import 'package:zenith/rooms.dart';
import 'package:zenith/login.dart'; import 'package:zenith/login.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:zenith/zenith_client_provider.dart'; import 'package:zenith/zenith_client_provider.dart';
@ -66,7 +66,7 @@ class _MyHomePageState extends State<MyHomePage> {
if (!savedCreds) { if (!savedCreds) {
return const LoginPage(); return const LoginPage();
} else { } else {
return const ChatsPage(); return const RoomsPage();
} }
} }
} }

77
lib/room.dart Normal file
View File

@ -0,0 +1,77 @@
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
class RoomPage extends StatefulWidget {
const RoomPage({super.key, required this.room});
final Room room;
@override
State<RoomPage> createState() => _RoomPageState();
}
class _RoomPageState extends State<RoomPage> {
Timeline? timeline;
void updateTimeline() async {
final newTimeline = await widget.room.getTimeline(
eventContextId: widget.room.fullyRead, onUpdate: () => setState(() {}));
setState(() {
timeline = newTimeline;
});
}
@override
void initState() {
super.initState();
updateTimeline();
widget.room.onUpdate.stream.listen((event) {
updateTimeline();
});
}
@override
void dispose() {
super.dispose();
timeline?.cancelSubscriptions();
widget.room.onUpdate.close();
}
void sendMessage() {}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.room.getLocalizedDisplayname()),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: timeline == null
? [const CircularProgressIndicator()]
: [
Expanded(
child: ListView.builder(
itemCount: timeline?.events.length,
reverse: true,
itemBuilder: (context, index) => ListTile(
title: Text(timeline!
.events[index]
.senderFromMemoryOrFallback
.displayName ??
""),
subtitle: Text(timeline!.events[index].body),
)),
)
],
)),
floatingActionButton: FloatingActionButton(
onPressed: sendMessage,
tooltip: 'Send message',
child: const Icon(Icons.send),
),
);
}
}

View File

@ -1,53 +1,62 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:matrix/matrix.dart';
import 'package:zenith/zenith_client_provider.dart'; import 'package:provider/provider.dart';
import 'package:zenith/room.dart';
class ChatsPage extends StatefulWidget { import 'package:zenith/zenith_client_provider.dart';
const ChatsPage({super.key});
class RoomsPage extends StatefulWidget {
@override const RoomsPage({super.key});
State<ChatsPage> createState() => _ChatsPageState();
} @override
State<RoomsPage> createState() => _RoomsPageState();
class _ChatsPageState extends State<ChatsPage> { }
final serverController = TextEditingController();
final usernameController = TextEditingController(); class _RoomsPageState extends State<RoomsPage> {
final passwordController = TextEditingController(); final serverController = TextEditingController();
final usernameController = TextEditingController();
@override final passwordController = TextEditingController();
void initState() {
super.initState(); @override
} void initState() {
super.initState();
void sendMessage() {} }
@override void sendMessage() {}
Widget build(BuildContext context) {
return Scaffold( void viewRoom(Room room) {
appBar: AppBar( Navigator.push(
backgroundColor: Theme.of(context).colorScheme.inversePrimary, context, MaterialPageRoute(builder: (context) => RoomPage(room: room)));
title: const Text("Zenith"), }
),
body: Padding( @override
padding: const EdgeInsets.all(16.0), Widget build(BuildContext context) {
child: Center(child: Consumer<ZenithClientProvider>( return Scaffold(
builder: (context, provider, child) { appBar: AppBar(
if (provider.loading) backgroundColor: Theme.of(context).colorScheme.inversePrimary,
return const CircularProgressIndicator(); title: const Text("Zenith"),
else ),
return ListView.builder( body: Padding(
itemBuilder: (context, index) => ListTile( padding: const EdgeInsets.all(16.0),
title: Text(provider.client.rooms[index] child: Center(child: Consumer<ZenithClientProvider>(
.getLocalizedDisplayname()), builder: (context, provider, child) {
)); if (provider.loading)
}, return const CircularProgressIndicator();
)), else
), return ListView.builder(
floatingActionButton: FloatingActionButton( itemCount: provider.client.rooms.length,
onPressed: sendMessage, itemBuilder: (context, index) => ListTile(
tooltip: 'Send message', title: Text(provider.client.rooms[index]
child: const Icon(Icons.send), .getLocalizedDisplayname()),
), onTap: () => viewRoom(provider.client.rooms[index]),
); ));
} },
} )),
),
floatingActionButton: FloatingActionButton(
onPressed: sendMessage,
tooltip: 'Send message',
child: const Icon(Icons.send),
),
);
}
}