-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuriaWebViewDelegate.swift
More file actions
173 lines (142 loc) · 5.24 KB
/
EuriaWebViewDelegate.swift
File metadata and controls
173 lines (142 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Infomaniak Euria - iOS App
Copyright (C) 2025 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 EuriaCore
import EuriaOnboardingView
import InfomaniakCore
import InfomaniakDI
import InfomaniakLogin
import OSLog
import Sentry
import SwiftUI
import UIKit
import WebKit
@MainActor
final class EuriaWebViewDelegate: NSObject, WebViewCoordinator, ObservableObject {
@Published var isLoaded = false
@Published var isShowingRegisterView = false
@Published var isPresentingDocument: URL?
@Published var error: ErrorDomain?
@ObservedObject var loginHandler = LoginHandler()
let host: String
let webConfiguration: WKWebViewConfiguration
var downloads = [WKDownload: URL]()
var isReadyToReceiveEvents = false
private var pendingDestination: String?
weak var webView: WKWebView?
enum Cookie: String {
case userToken = "USER-TOKEN"
case userLanguage = "USER-LANGUAGE"
}
enum ErrorDomain: LocalizedError, Equatable {
case urlGenerationFailed(error: Error)
case downloadFailed(error: Error)
var errorDescription: String? {
switch self {
case .urlGenerationFailed(let error):
return error.localizedDescription
case .downloadFailed(let error):
return error.localizedDescription
}
}
static func == (lhs: ErrorDomain, rhs: ErrorDomain) -> Bool {
switch (lhs, rhs) {
case (.urlGenerationFailed, .urlGenerationFailed):
return true
case (.downloadFailed, .downloadFailed):
return true
default:
return false
}
}
}
init(host: String, session: any UserSessionable) {
self.host = host
webConfiguration = WKWebViewConfiguration()
super.init()
setupWebViewConfiguration(token: session.apiFetcher.currentToken)
}
deinit {
Task {
await EuriaWebViewDelegate.cleanTemporaryFolder()
}
}
private func setupWebViewConfiguration(token: ApiToken?) {
addCookies(token: token)
addUserContentControllers()
}
private func addCookies(token: ApiToken?) {
let cookieStore = webConfiguration.websiteDataStore.httpCookieStore
if let token, let tokenCookie = createCookie(cookie: .userToken, value: "\(token.accessToken)") {
cookieStore.setCookie(tokenCookie)
}
let language = Locale.current.language.languageCode?.identifier ?? "en"
if let languageCookie = createCookie(cookie: .userLanguage, value: language) {
cookieStore.setCookie(languageCookie)
}
}
private func addUserContentControllers() {
for topic in EuriaWebViewDelegate.MessageTopic.allCases {
webConfiguration.userContentController.add(self, name: topic.rawValue)
}
}
private func createCookie(cookie: EuriaWebViewDelegate.Cookie, value: String) -> HTTPCookie? {
return HTTPCookie(
properties: [
.name: cookie.rawValue,
.value: value,
.path: "/",
.domain: host,
.maximumAge: TimeInterval.sixMonths
]
)
}
private nonisolated static func cleanTemporaryFolder() async {
do {
try FileManager.default.removeItem(at: URL.temporaryDownloadsDirectory())
} catch {
Logger.general.error("Error while cleaning temporary folder: \(error)")
}
}
func enqueueNavigation(destination: String) {
pendingDestination = destination
navigateIfPossible()
}
func navigateIfPossible() {
guard isReadyToReceiveEvents, let destination = pendingDestination else {
return
}
pendingDestination = nil
Task {
// Sometimes, when navigating from a universal link, Euria can’t access the local storage right away,
// which causes the user to be logged out.
// To avoid this situation, we wait a few milliseconds.
if destination == NavigationConstants.ephemeralRoute || destination == NavigationConstants.speechRoute {
try? await Task.sleep(for: .milliseconds(400))
}
try await webView?.evaluateJavaScript(JSBridge.goTo(destination))
}
}
func updateSessionToken(_ session: any UserSessionable) {
if let token = session.apiFetcher.currentToken {
addCookies(token: token)
reloadWebView()
}
}
func reloadWebView() {
isLoaded = false
isReadyToReceiveEvents = false
webView?.reload()
}
}