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:matrix/matrix.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zenith/rooms.dart';
@ -19,7 +18,7 @@ class _LoginPageState extends State<LoginPage> {
bool loggingIn = false;
String failedMessage = '';
void login() async {
void connectMatrix() async {
setState(() {
loggingIn = true;
});
@ -27,22 +26,10 @@ class _LoginPageState extends State<LoginPage> {
try {
final provider =
Provider.of<ZenithClientProvider>(context, listen: false);
final client = Client("zenith");
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 ?? "");
await provider.initialize(serverController.text, usernameController.text,
passwordController.text);
} catch (error) {
print(error);
print("Error signing in $error");
setState(() {
failedMessage = error.toString();
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;
Navigator.pushAndRemoveUntil(
context,
@ -100,7 +92,7 @@ class _LoginPageState extends State<LoginPage> {
controller: passwordController,
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
onFieldSubmitted: (value) => login(),
onFieldSubmitted: (value) => connectMatrix(),
),
Text(failedMessage,
style: Theme.of(context).textTheme.headlineSmall)
@ -109,7 +101,7 @@ class _LoginPageState extends State<LoginPage> {
),
),
floatingActionButton: FloatingActionButton(
onPressed: loggingIn ? null : login,
onPressed: loggingIn ? null : connectMatrix,
tooltip: 'Log in',
child: loggingIn
? const CircularProgressIndicator()

View File

@ -37,35 +37,22 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
bool showRooms = false;
String errorMessage = "";
bool savedCreds = false;
void getCreds() async {
final prefs = await SharedPreferences.getInstance();
final homeserver = prefs.getString("homeserver");
final username = prefs.getString("username");
final accessToken = prefs.getString("accessToken");
final refreshToken = prefs.getString("refreshToken");
final password = prefs.getString("password");
if (homeserver == null || username == null || accessToken == null) return;
if (homeserver == null || username == null || password == null) return;
setState(() {
savedCreds = true;
});
if (!mounted) return;
final provider = Provider.of<ZenithClientProvider>(context, listen: false);
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();
});
}
await provider.initialize(homeserver, username, password);
}
@override
@ -76,9 +63,10 @@ class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
if (showRooms)
return const RoomsPage();
else
if (!savedCreds) {
return const LoginPage();
} else {
return const RoomsPage();
}
}
}

View File

@ -42,34 +42,18 @@ class _RoomPageState extends State<RoomPage> {
void sendMessage() {}
List<Event>? get messages => timeline?.events
.where((element) => element.type == EventTypes.Message)
.toList();
List<Widget> getChildren() {
if (timeline == null) return [const CircularProgressIndicator()];
return [
Expanded(
child: ListView.builder(
itemCount: messages?.length,
itemCount: timeline?.events.length,
reverse: true,
itemBuilder: (context, index) => ListTile(
title: Text(messages![index]
.senderFromMemoryOrFallback
.displayName
.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,
title: Text(timeline!.events[index].senderFromMemoryOrFallback
.displayName ??
""),
subtitle: Text(timeline!.events[index].body),
)),
),
TextFormField(

View File

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

View File

@ -3,11 +3,28 @@ import 'package:matrix/matrix.dart';
class ZenithClientProvider extends ChangeNotifier {
late Client _client;
bool _loading = true;
Client get client => _client;
void setClient(Client value) {
_client = value;
bool get loading => _loading;
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();
}
}