Compare commits

..

No commits in common. "3728172844e1e16e4983f6192a96786c75aebc48" and "5fbc8bee42ddd9072442aa0af86d450b445d0793" have entirely different histories.

5 changed files with 47 additions and 74 deletions

View File

@ -1,5 +1,4 @@
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';
@ -19,7 +18,7 @@ class _LoginPageState extends State<LoginPage> {
bool loggingIn = false; bool loggingIn = false;
String failedMessage = ''; String failedMessage = '';
void login() async { void connectMatrix() async {
setState(() { setState(() {
loggingIn = true; loggingIn = true;
}); });
@ -27,22 +26,10 @@ class _LoginPageState extends State<LoginPage> {
try { try {
final provider = final provider =
Provider.of<ZenithClientProvider>(context, listen: false); Provider.of<ZenithClientProvider>(context, listen: false);
final client = Client("zenith"); await provider.initialize(serverController.text, usernameController.text,
await client.checkHomeserver(Uri.parse(serverController.text)); passwordController.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); print("Error signing in $error");
setState(() { setState(() {
failedMessage = error.toString(); failedMessage = error.toString();
loggingIn = false; loggingIn = false;
@ -60,6 +47,11 @@ 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,
@ -100,7 +92,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) => login(), onFieldSubmitted: (value) => connectMatrix(),
), ),
Text(failedMessage, Text(failedMessage,
style: Theme.of(context).textTheme.headlineSmall) style: Theme.of(context).textTheme.headlineSmall)
@ -109,7 +101,7 @@ class _LoginPageState extends State<LoginPage> {
), ),
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: loggingIn ? null : login, onPressed: loggingIn ? null : connectMatrix,
tooltip: 'Log in', tooltip: 'Log in',
child: loggingIn child: loggingIn
? const CircularProgressIndicator() ? const CircularProgressIndicator()

View File

@ -37,35 +37,22 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
bool showRooms = false; bool savedCreds = 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 accessToken = prefs.getString("accessToken"); final password = prefs.getString("password");
final refreshToken = prefs.getString("refreshToken");
if (homeserver == null || username == null || accessToken == null) return; if (homeserver == null || username == null || password == 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);
final client = Client("zenith"); await provider.initialize(homeserver, username, password);
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
@ -76,9 +63,10 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (showRooms) if (!savedCreds) {
return const RoomsPage();
else
return const LoginPage(); return const LoginPage();
} else {
return const RoomsPage();
}
} }
} }

View File

@ -42,34 +42,18 @@ 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: messages?.length, itemCount: timeline?.events.length,
reverse: true, reverse: true,
itemBuilder: (context, index) => ListTile( itemBuilder: (context, index) => ListTile(
title: Text(messages![index] title: Text(timeline!.events[index].senderFromMemoryOrFallback
.senderFromMemoryOrFallback .displayName ??
.displayName ""),
.toString()), subtitle: Text(timeline!.events[index].body),
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,8 +14,6 @@ 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();
@ -81,13 +79,7 @@ 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) {
provider.client.roomsLoading?.then((value) { if (provider.loading)
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,11 +3,28 @@ 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;
void setClient(Client value) { bool get loading => _loading;
_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();
} }
} }