Skip to content

feat: Force sync if not done yet #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions Mail/Views/SplitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import MailCoreUI
import MailResources
import MyKSuite
import NavigationBackport
import OSLog
import RealmSwift
import SwiftModalPresentation
import SwiftUI
Expand Down Expand Up @@ -134,7 +135,11 @@ struct SplitView: View {
}
.discoveryPresenter(isPresented: $mainViewState.isShowingSyncDiscovery) {
DiscoveryView(item: .syncDiscovery) {
UserDefaults.shared.shouldPresentSyncDiscovery = false
guard UserDefaults.shared.showSyncCounter < 3 else {
UserDefaults.shared.shouldPresentSyncDiscovery = false
return
}
UserDefaults.shared.nextShowSync = appLaunchCounter.value + Constants.nextOpeningBeforeSync
} completionHandler: { willSync in
guard willSync else { return }
if willSync {
Expand Down Expand Up @@ -250,7 +255,7 @@ struct SplitView: View {
try await mailboxManager.refreshAllSignatures()
}
guard !platformDetector.isDebug else { return }
mainViewState.isShowingSyncDiscovery = platformDetector.isMac ? false : shouldShowSync()
mainViewState.isShowingSyncDiscovery = platformDetector.isMac ? false : await shouldShowSync()
}
}

Expand Down Expand Up @@ -296,17 +301,31 @@ struct SplitView: View {
}
}

private func shouldShowSync() -> Bool {
private func shouldShowSync() async -> Bool {
guard UserDefaults.shared.shouldPresentSyncDiscovery else { return false }

guard !mainViewState.isShowingUpdateAvailable else {
// We don't want to show both DiscoveryView at the same time
return false
}
guard UserDefaults.shared.shouldPresentSyncDiscovery,
!appLaunchCounter.isFirstLaunch else {

guard UserDefaults.shared.nextShowSync <= appLaunchCounter.value else {
return false
}

return appLaunchCounter.value > Constants.minimumOpeningBeforeSync
do {
let syncDate = try await mailboxManager.apiFetcher.lastSyncDate()
if syncDate == nil {
UserDefaults.shared.nextShowSync = appLaunchCounter.value + Constants.nextOpeningBeforeSync
UserDefaults.shared.showSyncCounter += 1
return true
} else {
UserDefaults.shared.shouldPresentSyncDiscovery = false
}
} catch {
Logger.general.error("Error while fetching last sync date: \(error)")
}
return false
}

private func handleOpenUrl(_ url: URL) {
Expand Down
3 changes: 0 additions & 3 deletions Mail/Views/Sync Profile/SyncInstallProfileTutorialView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ struct SyncInstallProfileTutorialView: View {
}
}
}
.onAppear {
UserDefaults.shared.shouldPresentSyncDiscovery = false
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions MailCore/API/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public extension ApiEnvironment {
var mailHost: String {
return "mail.\(host)"
}

var calendarHost: String {
return "calendar.\(host)"
}
}

// MARK: - Endpoints
Expand Down Expand Up @@ -297,4 +301,12 @@ public extension Endpoint {
static func share(messageResource: String) -> Endpoint {
return .resource(messageResource).appending(path: "/share")
}

static func lastSyncDate() -> Endpoint {
return Endpoint(
hostKeypath: \.calendarHost,
path: "/api/sync-connection",
queryItems: [URLQueryItem(name: "os", value: "ios")]
)
}
}
4 changes: 4 additions & 0 deletions MailCore/API/MailApiFetcher/MailApiFetcher+Extended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,8 @@ public extension MailApiFetcher {
method: .delete
))
}

func lastSyncDate() async throws -> String? {
return try await perform(request: authenticatedRequest(.lastSyncDate()))
}
}
1 change: 1 addition & 0 deletions MailCore/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public enum Constants {
]

public static let minimumOpeningBeforeSync = 2
public static let nextOpeningBeforeSync = 50

/// A count limit for the Contact cache in Extension mode, where we have strong memory constraints.
public static let contactCacheExtensionMaxCount = 50
Expand Down
23 changes: 23 additions & 0 deletions MailCore/Utils/MailUserDefaults+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public extension UserDefaults.Keys {
static let sentryAuthorized = UserDefaults.Keys(rawValue: "sentryAuthorized")
static let lastScheduleInterval = UserDefaults.Keys(rawValue: "lastCustomSchedule")
static let nextShowQuotasAlert = UserDefaults.Keys(rawValue: "nextShowQuotasAlert")
static let nextShowSync = UserDefaults.Keys(rawValue: "nextShowSync")
static let showSyncCounter = UserDefaults.Keys(rawValue: "showSyncCounter")
}

public extension UserDefaults {
Expand Down Expand Up @@ -326,4 +328,25 @@ public extension UserDefaults {
set(newValue, forKey: key(.nextShowQuotasAlert))
}
}

var nextShowSync: Int {
get {
if object(forKey: key(.nextShowSync)) == nil {
set(Constants.minimumOpeningBeforeSync, forKey: key(.nextShowSync))
}
return integer(forKey: key(.nextShowSync))
}
set {
set(newValue, forKey: key(.nextShowSync))
}
}

var showSyncCounter: Int {
get {
return integer(forKey: key(.showSyncCounter))
}
set {
set(newValue, forKey: key(.showSyncCounter))
}
}
}
8 changes: 4 additions & 4 deletions Tuist/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-core",
"state" : {
"revision" : "ac5eb96798ebb687f814d681f66fb3d163e69d21",
"version" : "15.0.1"
"revision" : "17a022ee4c2342ebaa54bfae7509dd459df0b380",
"version" : "15.1.0"
}
},
{
Expand Down Expand Up @@ -77,8 +77,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-features",
"state" : {
"revision" : "697e75d70a5039cf8a86fef75b9e237b17e6cae4",
"version" : "1.0.5"
"revision" : "ecce060d82d76bc99c24678050a0ee04a996dbf5",
"version" : "1.0.6"
}
},
{
Expand Down
Loading