fix: separate server setup page, fix already-logged-in precondition error
This commit is contained in:
@@ -1,6 +1,20 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:pyramid/core/matrix_client.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
const _prefHomeserver = 'homeserver_url';
|
||||
|
||||
// Gespeicherter Homeserver aus SharedPreferences
|
||||
final homeserverProvider = FutureProvider<String?>((ref) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getString(_prefHomeserver);
|
||||
});
|
||||
|
||||
Future<void> saveHomeserver(String url) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_prefHomeserver, url);
|
||||
}
|
||||
|
||||
enum LoginStatus { idle, loading, error }
|
||||
|
||||
@@ -19,18 +33,17 @@ class LoginState {
|
||||
|
||||
class LoginNotifier extends StateNotifier<LoginState> {
|
||||
final Ref _ref;
|
||||
|
||||
LoginNotifier(this._ref) : super(const LoginState());
|
||||
|
||||
Future<void> login({
|
||||
required String userId,
|
||||
required String username,
|
||||
required String password,
|
||||
required String homeserver,
|
||||
String? deviceName,
|
||||
}) async {
|
||||
state = state.copyWith(status: LoginStatus.loading, errorMessage: null);
|
||||
|
||||
final clientAsync = _ref.read(matrixClientProvider);
|
||||
final client = clientAsync.valueOrNull;
|
||||
final client = _ref.read(matrixClientProvider).valueOrNull;
|
||||
if (client == null) {
|
||||
state = state.copyWith(
|
||||
status: LoginStatus.error,
|
||||
@@ -39,20 +52,21 @@ class LoginNotifier extends StateNotifier<LoginState> {
|
||||
return;
|
||||
}
|
||||
|
||||
if (client.isLogged()) {
|
||||
state = state.copyWith(status: LoginStatus.idle);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Auto-discover homeserver from MXID
|
||||
if (userId.isValidMatrixId) {
|
||||
final domain = Uri.https(userId.domain!, '');
|
||||
try {
|
||||
await client.checkHomeserver(domain);
|
||||
} catch (_) {
|
||||
client.homeserver = domain;
|
||||
}
|
||||
}
|
||||
final serverUri = Uri.https(
|
||||
homeserver.replaceAll(RegExp(r'^https?://'), ''),
|
||||
'',
|
||||
);
|
||||
await client.checkHomeserver(serverUri);
|
||||
|
||||
await client.login(
|
||||
LoginType.mLoginPassword,
|
||||
identifier: AuthenticationUserIdentifier(user: userId),
|
||||
identifier: AuthenticationUserIdentifier(user: username),
|
||||
password: password,
|
||||
initialDeviceDisplayName: deviceName ?? 'Pyramid',
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user