From 49e3beb39e1c7bd951c2ef26736d083213522ce4 Mon Sep 17 00:00:00 2001 From: The one with the braid Date: Sun, 12 Feb 2023 14:33:01 +0100 Subject: [PATCH] feat: support push for multi accounts Signed-off-by: The one with the braid --- lib/utils/background_push.dart | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index 49d8178b..0eead6a3 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -23,6 +23,7 @@ import 'dart:io'; import 'dart:typed_data'; import 'dart:ui'; +import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; @@ -46,7 +47,7 @@ class NoTokenException implements Exception { } class BackgroundPush { - static BackgroundPush? _instance; + static final Map _instance = {}; final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); Client client; @@ -56,7 +57,9 @@ class BackgroundPush { void Function(String errorMsg, {Uri? link})? onFcmError; L10n? l10n; Store? _store; + Store get store => _store ??= Store(); + Future loadLocale() async { // inspired by _lookupL10n in .dart_tool/flutter_gen/gen_l10n/l10n.dart l10n ??= (context != null ? L10n.of(context!) : null) ?? @@ -96,8 +99,8 @@ class BackgroundPush { } factory BackgroundPush.clientOnly(Client client) { - _instance ??= BackgroundPush._(client); - return _instance!; + _instance[client.id!] ??= BackgroundPush._(client); + return _instance[client.id!]!; } factory BackgroundPush( @@ -236,7 +239,7 @@ class BackgroundPush { return; } _wentToRoomOnStartup = true; - goToRoom(details.notificationResponse); + goToRoom(details.notificationResponse, client.id); }); } @@ -280,8 +283,13 @@ class BackgroundPush { ); } - Future goToRoom(NotificationResponse? response) async { + Future goToRoom(NotificationResponse? response, [int? clientId]) async { try { + final context = this.context; + if (context != null) { + Matrix.of(context).setActiveClient(client); + } + final roomId = response?.payload; Logs().v('[Push] Attempting to go to room $roomId...'); 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 /// IDs we map the [roomId] to a number and store this number. late Map idMap; + Future _loadIdMap() async { idMap = Map.from(json.decode( (await store.getItem(SettingKeys.notificationCurrentIds)) ?? '{}')); @@ -407,6 +416,7 @@ class BackgroundPush { } bool _clearingPushLock = false; + Future _onClearingPush({bool getFromServer = true}) async { if (_clearingPushLock) { return;