Add persistence to matrix Client

This commit is contained in:
Brandon Presley 2023-12-30 16:11:20 +13:00
parent a552c30ba0
commit 128a1bf35f
6 changed files with 41 additions and 18 deletions

View File

@ -25,11 +25,10 @@ class _LoginPageState extends State<LoginPage> {
});
final provider = Provider.of<ZenithClientProvider>(context, listen: false);
final client = Client("zenith");
try {
await client.checkHomeserver(Uri.parse(serverController.text));
await client.login(
await provider.client.checkHomeserver(Uri.parse(serverController.text));
await provider.client.login(
LoginType.mLoginPassword,
password: passwordController.text,
identifier: AuthenticationUserIdentifier(user: usernameController.text),
@ -54,7 +53,6 @@ class _LoginPageState extends State<LoginPage> {
});
}
provider.setClient(client);
final prefs = await SharedPreferences.getInstance();
prefs.setString("homeserver", serverController.text);
prefs.setString("username", usernameController.text);

View File

@ -50,16 +50,14 @@ class _MyHomePageState extends State<MyHomePage> {
if (!mounted) return;
final provider = Provider.of<ZenithClientProvider>(context, listen: false);
final client = Client("zenith");
provider.setClient(client);
setState(() {
showRooms = true;
});
try {
await client.checkHomeserver(Uri.parse(homeserver));
if (!client.isLogged())
await client.login(LoginType.mLoginPassword,
await provider.client.checkHomeserver(Uri.parse(homeserver));
if (!provider.client.isLogged())
await provider.client.login(LoginType.mLoginPassword,
password: password,
identifier: AuthenticationUserIdentifier(user: username));
} catch (error) {

View File

@ -1,13 +1,13 @@
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
class ZenithClientProvider extends ChangeNotifier {
late Client _client;
Client get client => _client;
void setClient(Client value) {
_client = value;
notifyListeners();
}
final client = Client("zenith", databaseBuilder: (_) async {
final dir =
await getApplicationSupportDirectory(); // Recommend path_provider package
final db = HiveCollectionsDatabase('matrix_example_chat', dir.path);
await db.open();
return db;
});
}

View File

@ -5,8 +5,10 @@
import FlutterMacOS
import Foundation
import path_provider_foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}

View File

@ -344,6 +344,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
path_provider:
dependency: "direct main"
description:
name: path_provider
sha256: a1aa8aaa2542a6bc57e381f132af822420216c80d4781f7aa085ca3229208aaa
url: "https://pub.dev"
source: hosted
version: "2.1.1"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
sha256: "477184d672607c0a3bf68fbbf601805f92ef79c82b64b4d6eb318cbca4c48668"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
path_provider_foundation:
dependency: transitive
description:
name: path_provider_foundation
sha256: "19314d595120f82aca0ba62787d58dde2cc6b5df7d2f0daf72489e38d1b57f2d"
url: "https://pub.dev"
source: hosted
version: "2.3.1"
path_provider_linux:
dependency: transitive
description:
@ -623,4 +647,4 @@ packages:
version: "6.3.0"
sdks:
dart: ">=3.1.5 <4.0.0"
flutter: ">=3.7.0"
flutter: ">=3.10.0"

View File

@ -41,6 +41,7 @@ dependencies:
provider: ^6.1.1
shared_preferences: ^2.2.2
file_picker: ^6.1.1
path_provider: ^2.1.1
dev_dependencies:
flutter_test: