Compare commits

...

3 Commits

Author SHA1 Message Date
3728172844 Tried to set up auth tokens
I always get invalid token error
when I try to login with mLoginToken type
2023-12-29 19:31:35 +13:00
ea7587fe2d Only show messages in room view 2023-12-29 17:19:40 +13:00
1376580e7e Add profile images to chats 2023-12-29 17:10:47 +13:00
5 changed files with 74 additions and 47 deletions

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:matrix/matrix.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/rooms.dart'; import 'package:zenith/rooms.dart';
@ -18,7 +19,7 @@ class _LoginPageState extends State<LoginPage> {
bool loggingIn = false; bool loggingIn = false;
String failedMessage = ''; String failedMessage = '';
void connectMatrix() async { void login() async {
setState(() { setState(() {
loggingIn = true; loggingIn = true;
}); });
@ -26,10 +27,22 @@ class _LoginPageState extends State<LoginPage> {
try { try {
final provider = final provider =
Provider.of<ZenithClientProvider>(context, listen: false); Provider.of<ZenithClientProvider>(context, listen: false);
await provider.initialize(serverController.text, usernameController.text, final client = Client("zenith");
passwordController.text); await client.checkHomeserver(Uri.parse(serverController.text));
final result = await client.login(
LoginType.mLoginPassword,
password: passwordController.text,
identifier: AuthenticationUserIdentifier(user: usernameController.text),
);
provider.setClient(client);
final prefs = await SharedPreferences.getInstance();
prefs.setString("homeserver", serverController.text);
prefs.setString("username", usernameController.text);
prefs.setString("accessToken", result.accessToken);
prefs.setString("refreshToken", result.refreshToken ?? "");
} catch (error) { } catch (error) {
print("Error signing in $error"); print(error);
setState(() { setState(() {
failedMessage = error.toString(); failedMessage = error.toString();
loggingIn = false; loggingIn = false;
@ -47,11 +60,6 @@ class _LoginPageState extends State<LoginPage> {
}); });
} }
final prefs = await SharedPreferences.getInstance();
prefs.setString("homeserver", serverController.text);
prefs.setString("username", usernameController.text);
prefs.setString("password", passwordController.text);
if (!mounted) return; if (!mounted) return;
Navigator.pushAndRemoveUntil( Navigator.pushAndRemoveUntil(
context, context,
@ -92,7 +100,7 @@ class _LoginPageState extends State<LoginPage> {
controller: passwordController, controller: passwordController,
decoration: const InputDecoration(labelText: 'Password'), decoration: const InputDecoration(labelText: 'Password'),
obscureText: true, obscureText: true,
onFieldSubmitted: (value) => connectMatrix(), onFieldSubmitted: (value) => login(),
), ),
Text(failedMessage, Text(failedMessage,
style: Theme.of(context).textTheme.headlineSmall) style: Theme.of(context).textTheme.headlineSmall)
@ -101,7 +109,7 @@ class _LoginPageState extends State<LoginPage> {
), ),
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: loggingIn ? null : connectMatrix, onPressed: loggingIn ? null : login,
tooltip: 'Log in', tooltip: 'Log in',
child: loggingIn child: loggingIn
? const CircularProgressIndicator() ? const CircularProgressIndicator()

View File

@ -37,22 +37,35 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
bool savedCreds = false; bool showRooms = false;
String errorMessage = "";
void getCreds() async { void getCreds() async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
final homeserver = prefs.getString("homeserver"); final homeserver = prefs.getString("homeserver");
final username = prefs.getString("username"); final username = prefs.getString("username");
final password = prefs.getString("password"); final accessToken = prefs.getString("accessToken");
final refreshToken = prefs.getString("refreshToken");
if (homeserver == null || username == null || password == null) return; if (homeserver == null || username == null || accessToken == null) return;
setState(() {
savedCreds = true;
});
if (!mounted) return; if (!mounted) return;
final provider = Provider.of<ZenithClientProvider>(context, listen: false); final provider = Provider.of<ZenithClientProvider>(context, listen: false);
await provider.initialize(homeserver, username, password); final client = Client("zenith");
try {
await client.checkHomeserver(Uri.parse(homeserver));
await client.login(LoginType.mLoginToken, token: accessToken);
provider.setClient(client);
setState(() {
showRooms = true;
});
} catch (error) {
print(error);
setState(() {
errorMessage = error.toString();
});
}
} }
@override @override
@ -63,10 +76,9 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (!savedCreds) { if (showRooms)
return const LoginPage();
} else {
return const RoomsPage(); return const RoomsPage();
} else
return const LoginPage();
} }
} }

View File

@ -42,18 +42,34 @@ class _RoomPageState extends State<RoomPage> {
void sendMessage() {} void sendMessage() {}
List<Event>? get messages => timeline?.events
.where((element) => element.type == EventTypes.Message)
.toList();
List<Widget> getChildren() { List<Widget> getChildren() {
if (timeline == null) return [const CircularProgressIndicator()]; if (timeline == null) return [const CircularProgressIndicator()];
return [ return [
Expanded( Expanded(
child: ListView.builder( child: ListView.builder(
itemCount: timeline?.events.length, itemCount: messages?.length,
reverse: true, reverse: true,
itemBuilder: (context, index) => ListTile( itemBuilder: (context, index) => ListTile(
title: Text(timeline!.events[index].senderFromMemoryOrFallback title: Text(messages![index]
.displayName ?? .senderFromMemoryOrFallback
""), .displayName
subtitle: Text(timeline!.events[index].body), .toString()),
subtitle: Text(messages![index].body),
leading:
messages![index].senderFromMemoryOrFallback.avatarUrl !=
null
? CircleAvatar(
foregroundImage: NetworkImage(messages![index]
.senderFromMemoryOrFallback
.avatarUrl!
.getThumbnail(widget.room.client,
width: 50, height: 50)
.toString()))
: null,
)), )),
), ),
TextFormField( TextFormField(

View File

@ -14,6 +14,8 @@ class RoomsPage extends StatefulWidget {
} }
class _RoomsPageState extends State<RoomsPage> { class _RoomsPageState extends State<RoomsPage> {
bool loadingRooms = true;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -79,7 +81,13 @@ class _RoomsPageState extends State<RoomsPage> {
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
child: Center(child: Consumer<ZenithClientProvider>( child: Center(child: Consumer<ZenithClientProvider>(
builder: (context, provider, child) { builder: (context, provider, child) {
if (provider.loading) provider.client.roomsLoading?.then((value) {
setState(() {
loadingRooms = false;
});
});
if (provider.client.roomsLoading != null && loadingRooms)
return const CircularProgressIndicator(); return const CircularProgressIndicator();
else else
return ListView.builder( return ListView.builder(

View File

@ -3,28 +3,11 @@ import 'package:matrix/matrix.dart';
class ZenithClientProvider extends ChangeNotifier { class ZenithClientProvider extends ChangeNotifier {
late Client _client; late Client _client;
bool _loading = true;
Client get client => _client; Client get client => _client;
bool get loading => _loading; void setClient(Client value) {
_client = value;
Future<void> initialize(
String homeserver, String username, String password) async {
_loading = true;
_client = Client("zenith");
try {
print("Checking homeserver...");
await client.checkHomeserver(Uri.parse(homeserver));
print("Logging in...");
await client.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: username),
password: password);
await client.roomsLoading;
await client.accountDataLoading;
} finally {
_loading = false;
}
notifyListeners(); notifyListeners();
} }
} }