2020-01-03 17:23:40 +01:00
|
|
|
import 'dart:async';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'dart:convert';
|
2020-01-03 17:23:40 +01:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
import 'package:fluffychat/utils/sqflite_store.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:localstorage/localstorage.dart';
|
|
|
|
import 'package:toast/toast.dart';
|
|
|
|
|
|
|
|
class Matrix extends StatefulWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
final String clientName;
|
|
|
|
|
|
|
|
final Client client;
|
|
|
|
|
|
|
|
Matrix({this.child, this.clientName, this.client, Key key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
MatrixState createState() => MatrixState();
|
|
|
|
|
|
|
|
/// Returns the (nearest) Client instance of your application.
|
|
|
|
static MatrixState of(BuildContext context) {
|
|
|
|
MatrixState newState =
|
|
|
|
(context.dependOnInheritedWidgetOfExactType<_InheritedMatrix>()).data;
|
|
|
|
newState.context = context;
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MatrixState extends State<Matrix> {
|
|
|
|
Client client;
|
|
|
|
BuildContext context;
|
|
|
|
|
2020-01-03 17:23:40 +01:00
|
|
|
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
/// Used to load the old account if there is no store available.
|
|
|
|
void loadAccount() async {
|
|
|
|
final LocalStorage storage = LocalStorage('LocalStorage');
|
|
|
|
await storage.ready;
|
|
|
|
|
|
|
|
final credentialsStr = storage.getItem(widget.clientName);
|
|
|
|
if (credentialsStr == null || credentialsStr.isEmpty) {
|
2020-01-02 15:10:21 +01:00
|
|
|
client.onLoginStateChanged.add(LoginState.loggedOut);
|
2020-01-01 19:10:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
print("[Matrix] Restoring account credentials");
|
|
|
|
final Map<String, dynamic> credentials = json.decode(credentialsStr);
|
2020-01-02 15:10:21 +01:00
|
|
|
client.connect(
|
2020-01-01 19:10:13 +01:00
|
|
|
newDeviceID: credentials["deviceID"],
|
|
|
|
newDeviceName: credentials["deviceName"],
|
|
|
|
newHomeserver: credentials["homeserver"],
|
|
|
|
newLazyLoadMembers: credentials["lazyLoadMembers"],
|
|
|
|
//newMatrixVersions: credentials["matrixVersions"], // FIXME: wrong List type
|
|
|
|
newToken: credentials["token"],
|
|
|
|
newUserID: credentials["userID"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used to save the current account persistently if there is no store available.
|
|
|
|
Future<void> saveAccount() async {
|
|
|
|
if (!kIsWeb) return;
|
|
|
|
print("[Matrix] Save account credentials in crypted preferences");
|
|
|
|
final Map<String, dynamic> credentials = {
|
|
|
|
"deviceID": client.deviceID,
|
|
|
|
"deviceName": client.deviceName,
|
|
|
|
"homeserver": client.homeserver,
|
|
|
|
"lazyLoadMembers": client.lazyLoadMembers,
|
|
|
|
"matrixVersions": client.matrixVersions,
|
|
|
|
"token": client.accessToken,
|
|
|
|
"userID": client.userID,
|
|
|
|
};
|
|
|
|
|
|
|
|
final LocalStorage storage = LocalStorage('LocalStorage');
|
|
|
|
await storage.ready;
|
|
|
|
await storage.setItem(widget.clientName, json.encode(credentials));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clean() async {
|
|
|
|
if (!kIsWeb) return;
|
|
|
|
print("Clear session...");
|
|
|
|
|
|
|
|
final LocalStorage storage = LocalStorage('LocalStorage');
|
|
|
|
await storage.ready;
|
2020-01-02 22:31:39 +01:00
|
|
|
await storage.deleteItem(widget.clientName);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BuildContext _loadingDialogContext;
|
|
|
|
|
|
|
|
Future<dynamic> tryRequestWithLoadingDialog(Future<dynamic> request) async {
|
|
|
|
showLoadingDialog(context);
|
|
|
|
final dynamic = await tryRequestWithErrorToast(request);
|
|
|
|
hideLoadingDialog();
|
|
|
|
return dynamic;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> tryRequestWithErrorToast(Future<dynamic> request) async {
|
|
|
|
try {
|
|
|
|
return await request;
|
|
|
|
} catch (exception) {
|
|
|
|
Toast.show(
|
|
|
|
exception.toString(),
|
|
|
|
context,
|
|
|
|
duration: Toast.LENGTH_LONG,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showLoadingDialog(BuildContext context) {
|
|
|
|
_loadingDialogContext = context;
|
|
|
|
showDialog(
|
|
|
|
context: _loadingDialogContext,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (BuildContext context) => AlertDialog(
|
|
|
|
content: Row(
|
|
|
|
children: <Widget>[
|
|
|
|
CircularProgressIndicator(),
|
|
|
|
SizedBox(width: 16),
|
|
|
|
Text("Loading... Please wait"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
hideLoadingDialog() => Navigator.of(_loadingDialogContext)?.pop();
|
|
|
|
|
2020-01-03 17:23:40 +01:00
|
|
|
StreamSubscription onSetupFirebase;
|
|
|
|
|
2020-01-04 13:53:49 +01:00
|
|
|
void setupFirebase(LoginState login) async {
|
2020-01-03 17:23:40 +01:00
|
|
|
if (login != LoginState.logged) return;
|
|
|
|
if (Platform.isIOS) iOS_Permission();
|
|
|
|
|
2020-01-04 13:53:49 +01:00
|
|
|
final String token = await _firebaseMessaging.getToken();
|
|
|
|
if (token?.isEmpty ?? true) {
|
|
|
|
return Toast.show(
|
|
|
|
"Push notifications disabled.",
|
|
|
|
context,
|
|
|
|
duration: Toast.LENGTH_LONG,
|
2020-01-03 17:23:40 +01:00
|
|
|
);
|
2020-01-04 13:53:49 +01:00
|
|
|
}
|
|
|
|
await client.setPushers(
|
|
|
|
token,
|
|
|
|
"http",
|
|
|
|
"chat.fluffy.fluffychat",
|
|
|
|
widget.clientName,
|
|
|
|
client.deviceName,
|
|
|
|
"en",
|
|
|
|
"https://janian.de:7023/",
|
|
|
|
append: false,
|
|
|
|
format: "event_id_only",
|
|
|
|
);
|
2020-01-03 17:23:40 +01:00
|
|
|
|
|
|
|
_firebaseMessaging.configure(
|
|
|
|
onResume: (Map<String, dynamic> message) async {
|
|
|
|
print('on resume $message');
|
|
|
|
},
|
|
|
|
onLaunch: (Map<String, dynamic> message) async {
|
|
|
|
print('on launch $message');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void iOS_Permission() {
|
|
|
|
_firebaseMessaging.requestNotificationPermissions(
|
|
|
|
IosNotificationSettings(sound: true, badge: true, alert: true));
|
|
|
|
_firebaseMessaging.onIosSettingsRegistered
|
|
|
|
.listen((IosNotificationSettings settings) {
|
|
|
|
print("Settings registered: $settings");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
if (widget.client == null) {
|
2020-01-04 13:53:49 +01:00
|
|
|
client = Client(widget.clientName, debug: false);
|
2020-01-02 22:31:39 +01:00
|
|
|
if (!kIsWeb) {
|
2020-01-01 19:10:13 +01:00
|
|
|
client.store = Store(client);
|
2020-01-02 22:31:39 +01:00
|
|
|
} else {
|
2020-01-01 19:10:13 +01:00
|
|
|
loadAccount();
|
2020-01-02 22:31:39 +01:00
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
} else {
|
|
|
|
client = widget.client;
|
|
|
|
}
|
2020-01-03 17:23:40 +01:00
|
|
|
onSetupFirebase ??= client.onLoginStateChanged.stream.listen(setupFirebase);
|
2020-01-01 19:10:13 +01:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:23:40 +01:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
onSetupFirebase?.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return _InheritedMatrix(
|
|
|
|
data: this,
|
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _InheritedMatrix extends InheritedWidget {
|
|
|
|
final MatrixState data;
|
|
|
|
|
|
|
|
_InheritedMatrix({Key key, this.data, Widget child})
|
|
|
|
: super(key: key, child: child);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotify(_InheritedMatrix old) {
|
|
|
|
bool update = old.data.client.accessToken != this.data.client.accessToken ||
|
|
|
|
old.data.client.userID != this.data.client.userID ||
|
|
|
|
old.data.client.matrixVersions != this.data.client.matrixVersions ||
|
|
|
|
old.data.client.lazyLoadMembers != this.data.client.lazyLoadMembers ||
|
|
|
|
old.data.client.deviceID != this.data.client.deviceID ||
|
|
|
|
old.data.client.deviceName != this.data.client.deviceName ||
|
|
|
|
old.data.client.homeserver != this.data.client.homeserver;
|
|
|
|
return update;
|
|
|
|
}
|
|
|
|
}
|