Skip to content
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
1 change: 1 addition & 0 deletions EuriaCore/WebView Bridge/WebViewBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum JSMessageTopic: String, CaseIterable {
case signUp
case cancelFileUpload
case openReview
case upgrade
}

public protocol WebViewBridge: AnyObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ extension EuriaWebViewDelegate: WKScriptMessageHandler, WebViewMessageSubscriber
isShowingRegisterView = true
case .openReview:
isShowingReviewAlert = true
case .upgrade:
upgradeUserOffer()
default:
break
}
Expand Down Expand Up @@ -87,4 +89,15 @@ extension EuriaWebViewDelegate: WKScriptMessageHandler, WebViewMessageSubscriber
private func keepDeviceAwake(_ shouldKeepDeviceAwake: Bool) {
UIApplication.shared.isIdleTimerDisabled = shouldKeepDeviceAwake
}

private func upgradeUserOffer() {
Task {
@InjectService var accountManager: AccountManagerable
guard let token = await accountManager.currentSession?.apiFetcher.currentToken else {
return
}

upgradeViewToken = UpgradeTokenItem(token: token)
}
}
}
7 changes: 7 additions & 0 deletions EuriaFeatures/MainView/Delegate/EuriaWebViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class EuriaWebViewDelegate: NSObject, WebViewCoordinator, WebViewBridge, O
@Published var isLoaded = false
@Published var isShowingRegisterView = false
@Published var isShowingReviewAlert = false
@Published var upgradeViewToken: UpgradeTokenItem?

@Published var isPresentingDocument: URL?
@Published var error: ErrorDomain?
Expand Down Expand Up @@ -101,6 +102,7 @@ final class EuriaWebViewDelegate: NSObject, WebViewCoordinator, WebViewBridge, O
addSubscriber(self, topic: .signUp)
addSubscriber(self, topic: .unauthenticated)
addSubscriber(self, topic: .openReview)
addSubscriber(self, topic: .upgrade)
}

private func addCookies(token: ApiToken?) {
Expand Down Expand Up @@ -212,3 +214,8 @@ final class EuriaWebViewDelegate: NSObject, WebViewCoordinator, WebViewBridge, O
}
}
}

struct UpgradeTokenItem: Identifiable {
var id: String { return token.accessToken }
let token: ApiToken
}
5 changes: 5 additions & 0 deletions EuriaFeatures/MainView/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public struct MainView: View {
loginHandler.loginAfterAccountCreation(from: viewController)
}
}
.sheet(item: $webViewDelegate.upgradeViewToken) { accessToken in
UpgradeAccountView(accessToken: accessToken.token) {
webViewDelegate.reloadWebView()
}
}
.onReceive(accountManager.objectWillChange) { _ in
Task {
guard let session = await accountManager.currentSession else {
Expand Down
88 changes: 88 additions & 0 deletions EuriaFeatures/MainView/UpgradeAccountView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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 InfomaniakCore
import InfomaniakCreateAccount
import InfomaniakLogin
import SwiftUI
import WebKit

struct UpgradeAccountView: UIViewRepresentable {
@Environment(\.dismiss) private var dismiss

let accessToken: ApiToken
let onUpgradeCompleted: (() -> Void)?

static let welcomeRoute = URL(string: "https://welcome.infomaniak.com/signup/euria")!
static let managerRoute = Constants.autologinUrl(to: welcomeRoute.absoluteString)!

class Coordinator: NSObject, WKNavigationDelegate {
let dismissAction: () -> Void
let onUpgradeCompleted: (() -> Void)?

init(dismissAction: @escaping () -> Void, onUpgradeCompleted: (() -> Void)?) {
self.dismissAction = dismissAction
self.onUpgradeCompleted = onUpgradeCompleted
}

func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @MainActor (WKNavigationActionPolicy) -> Void
) {
guard let url = navigationAction.request.url else {
decisionHandler(.cancel)
return
}

if url.host == ApiEnvironment.current.euriaHost {
decisionHandler(.cancel)
onUpgradeCompleted?()
dismissAction()
} else {
guard url.host == managerRoute.host() ||
url.host == welcomeRoute.host() else {
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
}
}

func makeCoordinator() -> Coordinator {
Coordinator(
dismissAction: { dismiss() },
onUpgradeCompleted: onUpgradeCompleted
)
}

func makeUIView(context: Context) -> WKWebView {
let configuration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = context.coordinator

var request = URLRequest(url: UpgradeAccountView.managerRoute)
request.setValue("Bearer \(accessToken.accessToken)", forHTTPHeaderField: "Authorization")
webView.load(request)

return webView
}

func updateUIView(_ webView: WKWebView, context: Context) {}
}
Loading