Skip to content

Commit

Permalink
feat: Fast action before API call (#1407)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann authored May 1, 2024
2 parents 81ceef6 + d088185 commit 409bc9d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 22 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
}
}
}
}
}
29 changes: 21 additions & 8 deletions MailCore/Cache/MailboxManager/MailboxManager+Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,18 @@ public extension MailboxManager {
// MARK: Private

func markAsSeen(messages: [Message], seen: Bool) async throws {
if seen {
try await apiFetcher.markAsSeen(mailbox: mailbox, messages: messages)
} else {
try await apiFetcher.markAsUnseen(mailbox: mailbox, messages: messages)
await updateLocally(.seen, value: seen, messages: messages)

do {
if seen {
try await apiFetcher.markAsSeen(mailbox: mailbox, messages: messages)
} else {
try await apiFetcher.markAsUnseen(mailbox: mailbox, messages: messages)
}
} catch {
await updateLocally(.seen, value: !seen, messages: messages)
}

try await refreshFolder(from: messages, additionalFolder: nil)

// TODO: Remove after fix
Expand All @@ -119,10 +126,16 @@ public extension MailboxManager {
/// 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 {
if starred {
_ = try await star(messages: messages)
} else {
_ = try await unstar(messages: messages)
await updateLocally(.star, value: starred, messages: messages)

do {
if starred {
_ = try await star(messages: messages)
} else {
_ = try await unstar(messages: messages)
}
} catch {
await updateLocally(.star, value: !starred, messages: messages)
}
}

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 409bc9d

Please sign in to comment.