-
Notifications
You must be signed in to change notification settings - Fork 406
/
SubscriptionITPViewModel.swift
144 lines (121 loc) · 5.19 KB
/
SubscriptionITPViewModel.swift
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
//
// SubscriptionITPViewModel.swift
// DuckDuckGo
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UserScript
import Combine
import Core
#if SUBSCRIPTION
@available(iOS 15.0, *)
final class SubscriptionITPViewModel: ObservableObject {
let userScript: IdentityTheftRestorationPagesUserScript
let subFeature: IdentityTheftRestorationPagesFeature
var manageITPURL = URL.manageITP
var viewTitle = UserText.settingsPProITRTitle
enum Constants {
static let navigationBarHideThreshold = 40.0
static let downloadableContent = ["application/pdf"]
}
// State variables
var itpURL = URL.manageITP
@Published var webViewModel: AsyncHeadlessWebViewViewModel
@Published var shouldShowNavigationBar: Bool = false
@Published var canNavigateBack: Bool = false
@Published var isDownloadableContent: Bool = false
@Published var activityItems: [Any] = []
@Published var attachmentURL: URL?
private var currentURL: URL?
private var cancellables = Set<AnyCancellable>()
private var canGoBackCancellable: AnyCancellable?
init(userScript: IdentityTheftRestorationPagesUserScript = IdentityTheftRestorationPagesUserScript(),
subFeature: IdentityTheftRestorationPagesFeature = IdentityTheftRestorationPagesFeature()) {
self.userScript = userScript
self.subFeature = subFeature
self.webViewModel = AsyncHeadlessWebViewViewModel(userScript: userScript,
subFeature: subFeature,
settings: AsyncHeadlessWebViewSettings(bounces: false))
}
// Observe transaction status
private func setupSubscribers() async {
webViewModel.$scrollPosition
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
self?.shouldShowNavigationBar = value.y > Constants.navigationBarHideThreshold
}
.store(in: &cancellables)
webViewModel.$contentType
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
guard let strongSelf = self else { return }
if Constants.downloadableContent.contains(value) {
strongSelf.isDownloadableContent = true
guard let url = strongSelf.currentURL else { return }
Task {
// We are using a dummy PDF for testing, as the real PDF's are behind the internal user login
if let downloadURL = URL(string: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf") {
await strongSelf.downloadAttachment(from: downloadURL)
}
// if let downloadURL = url {
// await strongSelf.downloadAttachment(from: downloadURL)
}
}
}
.store(in: &cancellables)
webViewModel.$url
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
self?.isDownloadableContent = false
self?.currentURL = value
}
.store(in: &cancellables)
canGoBackCancellable = webViewModel.$canGoBack
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
self?.canNavigateBack = value
}
}
func initializeView() {
webViewModel.navigationCoordinator.navigateTo(url: manageITPURL )
Task { await setupSubscribers() }
}
private func downloadAttachment(from url: URL) async {
if let (temporaryURL, _) = try? await URLSession.shared.download(from: url) {
let fileManager = FileManager.default
let fileName = url.lastPathComponent
let tempDirectory = fileManager.temporaryDirectory
let tempFileURL = tempDirectory.appendingPathComponent(fileName)
if fileManager.fileExists(atPath: tempFileURL.path) {
try? fileManager.removeItem(at: tempFileURL)
}
try? fileManager.moveItem(at: temporaryURL, to: tempFileURL)
DispatchQueue.main.async {
self.attachmentURL = tempFileURL
}
}
}
@MainActor
private func disableGoBack() {
canGoBackCancellable?.cancel()
canNavigateBack = false
}
@MainActor
func navigateBack() async {
await webViewModel.navigationCoordinator.goBack()
}
}
#endif