From 22c94d14cb4ea8ea8c0b7b936cf77eb74d969730 Mon Sep 17 00:00:00 2001
From: kkostiuk <kateryna.kostiuk@savoirfairelinux.com>
Date: Wed, 26 May 2021 12:31:57 -0400
Subject: [PATCH] presence: unsubscribe when app is in background

Change-Id: Ie15efebd03ea57e1ba9b7c33c406ac4e940a75ae
---
 Ring/Ring/AppDelegate.swift | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/Ring/Ring/AppDelegate.swift b/Ring/Ring/AppDelegate.swift
index b0597d335..a924a8b8e 100644
--- a/Ring/Ring/AppDelegate.swift
+++ b/Ring/Ring/AppDelegate.swift
@@ -240,16 +240,21 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     }
 
     func reloadDataFor(account: AccountModel) {
-        self.contactsService.loadContacts(withAccount: account)
-        self.contactsService.loadContactRequests(withAccount: account.id)
-        self.presenceService.subscribeBuddies(withAccount: account.id, withContacts: self.contactsService.contacts.value, subscribe: true)
-        self.conversationManager?
-                .prepareConversationsForAccount(accountId: account.id)
+        let state = UIApplication.shared.applicationState
+        if state == .active {
+            self.contactsService.loadContacts(withAccount: account)
+            self.contactsService.loadContactRequests(withAccount: account.id)
+            self.presenceService.subscribeBuddies(withAccount: account.id, withContacts: self.contactsService.contacts.value, subscribe: true)
+            self.conversationManager?
+                    .prepareConversationsForAccount(accountId: account.id)
+        }
     }
 
     func applicationDidEnterBackground(_ application: UIApplication) {
         self.log.warning("entering background")
         self.callService.muteCurrentCallVideoVideo( mute: true)
+        guard let account = self.accountService.currentAccount else { return }
+        self.presenceService.subscribeBuddies(withAccount: account.id, withContacts: self.contactsService.contacts.value, subscribe: false)
     }
 
     func applicationWillEnterForeground(_ application: UIApplication) {
@@ -257,6 +262,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         self.daemonService.connectivityChanged()
         self.updateNotificationAvailability()
         self.callService.muteCurrentCallVideoVideo( mute: false)
+        guard let account = self.accountService.currentAccount else { return }
+        self.presenceService.subscribeBuddies(withAccount: account.id, withContacts: self.contactsService.contacts.value, subscribe: true)
     }
 
     func applicationWillTerminate(_ application: UIApplication) {
-- 
GitLab