Skip to content

Commit

Permalink
feat: Advertise NSUserActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jul 11, 2023
1 parent 8fcd227 commit 6af5c31
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Mail/MailApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public struct EarlyDIHook {
let draftManager = Factory(type: DraftManager.self) { _, _ in
DraftManager()
}
let userActivityController = Factory(type: UserActivityController.self) { _, _ in
UserActivityController()
}

SimpleResolver.sharedResolver.store(factory: networkLoginService)
SimpleResolver.sharedResolver.store(factory: loginService)
Expand All @@ -72,6 +75,7 @@ public struct EarlyDIHook {
SimpleResolver.sharedResolver.store(factory: matomoUtils)
SimpleResolver.sharedResolver.store(factory: avoider)
SimpleResolver.sharedResolver.store(factory: draftManager)
SimpleResolver.sharedResolver.store(factory: userActivityController)
}
}

Expand Down
4 changes: 4 additions & 0 deletions Mail/Views/Thread List/ThreadListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class FlushAlertState: Identifiable {

struct ThreadListView: View {
@LazyInjectService private var matomo: MatomoUtils
@LazyInjectService private var userActivityController: UserActivityController

@EnvironmentObject var splitViewManager: SplitViewManager
@EnvironmentObject var navigationState: NavigationState
Expand Down Expand Up @@ -217,9 +218,12 @@ struct ThreadListView: View {
if viewModel.isCompact {
viewModel.selectedThread = nil
}
userActivityController.setCurrentActivity(mailbox: viewModel.mailboxManager.mailbox,
folder: splitViewManager.selectedFolder)
}
.onChange(of: splitViewManager.selectedFolder) { newFolder in
changeFolder(newFolder: newFolder)
userActivityController.setCurrentActivity(mailbox: viewModel.mailboxManager.mailbox, folder: newFolder)
}
.onChange(of: viewModel.selectedThread) { newThread in
if let newThread {
Expand Down
55 changes: 55 additions & 0 deletions MailCore/Utils/UserActivityController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2022 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
import InfomaniakCore

public class UserActivityController {
var currentActivity: NSUserActivity?

public init() {}

public func setCurrentActivity(
_ activity: NSUserActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb),
mailbox: Mailbox,
folder: Folder?
) {
guard let folder,
let mailboxIndex = getMailboxIndexForCustomOrder(mailbox),
let remoteURL = URL(string: "https://\(ApiEnvironment.current.mailHost)/\(mailboxIndex);fid=\(folder.id)") else {
return
}
currentActivity?.invalidate()
currentActivity = activity
currentActivity?.webpageURL = remoteURL
currentActivity?.becomeCurrent()
}

private func getMailboxIndexForCustomOrder(_ mailbox: Mailbox) -> Int? {
let sortedUserMailboxes = MailboxInfosManager.instance.getMailboxes(for: mailbox.userId).sorted {
if $0.isPrimary {
return true
} else if $1.isPrimary {
return false
} else {
return $0.email < $1.email
}
}
return sortedUserMailboxes.firstIndex { $0.objectId == mailbox.objectId }
}
}

0 comments on commit 6af5c31

Please sign in to comment.