feat: support push for multi accounts

Signed-off-by: The one with the braid <the-one@with-the-braid.cf>
This commit is contained in:
The one with the braid 2023-02-12 14:33:01 +01:00
parent 78a5206ba7
commit 49e3beb39e
1 changed files with 15 additions and 5 deletions

View File

@ -23,6 +23,7 @@ import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:ui'; import 'dart:ui';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
@ -46,7 +47,7 @@ class NoTokenException implements Exception {
} }
class BackgroundPush { class BackgroundPush {
static BackgroundPush? _instance; static final Map<int, BackgroundPush?> _instance = {};
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin();
Client client; Client client;
@ -56,7 +57,9 @@ class BackgroundPush {
void Function(String errorMsg, {Uri? link})? onFcmError; void Function(String errorMsg, {Uri? link})? onFcmError;
L10n? l10n; L10n? l10n;
Store? _store; Store? _store;
Store get store => _store ??= Store(); Store get store => _store ??= Store();
Future<void> loadLocale() async { Future<void> loadLocale() async {
// inspired by _lookupL10n in .dart_tool/flutter_gen/gen_l10n/l10n.dart // inspired by _lookupL10n in .dart_tool/flutter_gen/gen_l10n/l10n.dart
l10n ??= (context != null ? L10n.of(context!) : null) ?? l10n ??= (context != null ? L10n.of(context!) : null) ??
@ -96,8 +99,8 @@ class BackgroundPush {
} }
factory BackgroundPush.clientOnly(Client client) { factory BackgroundPush.clientOnly(Client client) {
_instance ??= BackgroundPush._(client); _instance[client.id!] ??= BackgroundPush._(client);
return _instance!; return _instance[client.id!]!;
} }
factory BackgroundPush( factory BackgroundPush(
@ -236,7 +239,7 @@ class BackgroundPush {
return; return;
} }
_wentToRoomOnStartup = true; _wentToRoomOnStartup = true;
goToRoom(details.notificationResponse); goToRoom(details.notificationResponse, client.id);
}); });
} }
@ -280,8 +283,13 @@ class BackgroundPush {
); );
} }
Future<void> goToRoom(NotificationResponse? response) async { Future<void> goToRoom(NotificationResponse? response, [int? clientId]) async {
try { try {
final context = this.context;
if (context != null) {
Matrix.of(context).setActiveClient(client);
}
final roomId = response?.payload; final roomId = response?.payload;
Logs().v('[Push] Attempting to go to room $roomId...'); Logs().v('[Push] Attempting to go to room $roomId...');
if (router == null || roomId == null) { if (router == null || roomId == null) {
@ -381,6 +389,7 @@ class BackgroundPush {
/// sort by [roomId] which is a String. To make sure that we don't have duplicated /// sort by [roomId] which is a String. To make sure that we don't have duplicated
/// IDs we map the [roomId] to a number and store this number. /// IDs we map the [roomId] to a number and store this number.
late Map<String, int> idMap; late Map<String, int> idMap;
Future<void> _loadIdMap() async { Future<void> _loadIdMap() async {
idMap = Map<String, int>.from(json.decode( idMap = Map<String, int>.from(json.decode(
(await store.getItem(SettingKeys.notificationCurrentIds)) ?? '{}')); (await store.getItem(SettingKeys.notificationCurrentIds)) ?? '{}'));
@ -407,6 +416,7 @@ class BackgroundPush {
} }
bool _clearingPushLock = false; bool _clearingPushLock = false;
Future<void> _onClearingPush({bool getFromServer = true}) async { Future<void> _onClearingPush({bool getFromServer = true}) async {
if (_clearingPushLock) { if (_clearingPushLock) {
return; return;