Skip to content

Commit

Permalink
fix: UpdateLocally in MailboxManager+Local
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambrdctr committed May 1, 2024
1 parent 0345e5b commit d088185
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 65 deletions.
86 changes: 86 additions & 0 deletions MailCore/Cache/MailboxManager/MailboxManager+Local.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2024 Infomaniak Network SA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Foundation

public extension MailboxManager {
enum UpdateType {
case seen
case star

func update(message: Message, with value: Bool) {
switch self {
case .seen:
message.seen = value
case .star:
message.flagged = value
}
}

func update(thread: Thread) {
switch self {
case .seen:
thread.updateUnseenMessages()
case .star:
thread.updateFlagged()
}
}
}

func updateLocally(_ type: UpdateType, value: Bool, messages: [Message]) async {
await backgroundRealm.execute { realm in
var updateThreads = Set<Thread>()

try? realm.write {
for message in messages {
guard let liveMessage = realm.object(ofType: Message.self, forPrimaryKey: message.uid) else {
continue
}

type.update(message: liveMessage, with: value)

for thread in liveMessage.threads {
updateThreads.insert(thread)
}
}

for thread in updateThreads {
guard let liveThread = realm.object(ofType: Thread.self, forPrimaryKey: thread.uid) else {
continue
}

type.update(thread: liveThread)
}
}
}
}

func markMovedLocally(_ movedLocally: Bool, threads: [Thread]) async {
await backgroundRealm.execute { realm in
try? realm.write {
for thread in threads {
guard let liveThread = realm.object(ofType: Thread.self, forPrimaryKey: thread.uid) else {
continue
}

liveThread.isMovedOutLocally = movedLocally
}
}
}
}
}
51 changes: 0 additions & 51 deletions MailCore/Cache/MailboxManager/MailboxManager+Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,57 +123,6 @@ public extension MailboxManager {
SentryDebug.listIncoherentMessageUpdate(messages: messages, actualSeen: seen)
}

enum UpdateType {
case seen
case star

func update(message: Message, with value: Bool) {
switch self {
case .seen:
message.seen = value
case .star:
message.flagged = value
}
}

func update(thread: Thread) {
switch self {
case .seen:
thread.updateUnseenMessages()
case .star:
thread.updateFlagged()
}
}
}

func updateLocally(_ type: UpdateType, value: Bool, messages: [Message]) async {
await backgroundRealm.execute { realm in
var updateThreads = Set<Thread>()

try? realm.write {
for message in messages {
guard let liveMessage = realm.object(ofType: Message.self, forPrimaryKey: message.uid) else {
continue
}

type.update(message: liveMessage, with: value)

for thread in liveMessage.threads {
updateThreads.insert(thread)
}
}

for thread in updateThreads {
guard let liveThread = realm.object(ofType: Thread.self, forPrimaryKey: thread.uid) else {
continue
}

type.update(thread: liveThread)
}
}
}
}

/// Set starred the given messages.
/// - Important: This methods stars only the messages you passes, no processing is done to add duplicates or remove drafts
func star(messages: [Message], starred: Bool) async throws {
Expand Down
14 changes: 0 additions & 14 deletions MailCore/Cache/MailboxManager/MailboxManager+Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -707,18 +707,4 @@ public extension MailboxManager {
}
}
}

func markMovedLocally(_ movedLocally: Bool, threads: [Thread]) async {
await backgroundRealm.execute { realm in
try? realm.write {
for thread in threads {
guard let liveThread = realm.object(ofType: Thread.self, forPrimaryKey: thread.uid) else {
continue
}

liveThread.isMovedOutLocally = movedLocally
}
}
}
}
}

0 comments on commit d088185

Please sign in to comment.