-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathSettingsViewModel.swift
More file actions
202 lines (177 loc) · 6.96 KB
/
SettingsViewModel.swift
File metadata and controls
202 lines (177 loc) · 6.96 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// SettingsViewModel.swift
// Cryptomator
//
// Created by Tobias Hagemann on 04.06.21.
// Copyright © 2021 Skymatic GmbH. All rights reserved.
//
import Combine
import CryptomatorCommonCore
import CryptomatorFileProvider
import Dependencies
import Foundation
import Promises
import StoreKit
enum SettingsButtonAction: String {
case showAbout
case sendLogFile
case clearCache
case showCloudServices
case showContact
case showRateApp
case showShortcutsGuide
case showUnlockFullVersion
case showManageSubscriptions
case restorePurchase
}
enum SettingsSection: Int {
case purchaseStatusSection = 0
case cloudServiceSection
case cacheSection
case aboutSection
case debugSection
case miscSection
}
class SettingsViewModel: TableViewModel<SettingsSection> {
override var title: String? {
return LocalizedString.getValue("settings.title")
}
override var sections: [Section<SettingsSection>] {
return _sections
}
var showDebugModeWarning: AnyPublisher<Void, Never> {
return showDebugModeWarningPublisher.eraseToAnyPublisher()
}
private var _sections: [Section<SettingsSection>] {
var sections: [Section<SettingsSection>] = []
if !hasFullAccess {
sections.append(Section(id: .purchaseStatusSection, elements: [purchaseStatusCellViewModel]))
}
sections.append(contentsOf: [
Section(id: .cloudServiceSection, elements: [
ButtonCellViewModel.createDisclosureButton(action: SettingsButtonAction.showCloudServices, title: LocalizedString.getValue("settings.cloudServices"))
]),
Section(id: .cacheSection, elements: [
cacheSizeCellViewModel,
clearCacheButtonCellViewModel
]),
Section(id: .aboutSection, elements: aboutSectionElements),
Section(id: .debugSection, elements: [
debugModeViewModel,
ButtonCellViewModel<SettingsButtonAction>(action: .sendLogFile, title: LocalizedString.getValue("settings.sendLogFile"))
]),
Section(id: .miscSection, elements: [
ButtonCellViewModel(action: SettingsButtonAction.showShortcutsGuide, title: LocalizedString.getValue("settings.shortcutsGuide")),
ButtonCellViewModel(action: SettingsButtonAction.showContact, title: LocalizedString.getValue("settings.contact")),
ButtonCellViewModel(action: SettingsButtonAction.showRateApp, title: LocalizedString.getValue("settings.rateApp"))
])
])
return sections
}
override func getFooterTitle(for section: Int) -> String? {
guard sections[section].id == .aboutSection, hasFullAccess else { return nil }
return LocalizedString.getValue("settings.fullVersion.footer")
}
private var hasFullAccess: Bool {
cryptomatorSettings.hasRunningSubscription || cryptomatorSettings.fullVersionUnlocked
}
private var aboutSectionElements: [TableViewCellViewModel] {
var elements: [TableViewCellViewModel] = [
ButtonCellViewModel.createDisclosureButton(action: SettingsButtonAction.showAbout, title: LocalizedString.getValue("settings.aboutCryptomator"))
]
if cryptomatorSettings.hasRunningSubscription {
elements.append(ButtonCellViewModel.createDisclosureButton(action: SettingsButtonAction.showManageSubscriptions, title: LocalizedString.getValue("settings.manageSubscriptions")))
}
return elements
}
private let cacheSizeCellViewModel = LoadingWithLabelCellViewModel(title: LocalizedString.getValue("settings.cacheSize"))
private let clearCacheButtonCellViewModel = ButtonCellViewModel<SettingsButtonAction>(action: .clearCache, title: LocalizedString.getValue("settings.clearCache"), isEnabled: false)
private var cryptomatorSettings: CryptomatorSettings
private var purchaseStatusCellViewModel: PurchaseStatusCellViewModel {
let subtitle: String
if let trialExpirationDate = cryptomatorSettings.trialExpirationDate, trialExpirationDate > Date() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
subtitle = String(format: LocalizedString.getValue("settings.trial.expirationDate"), dateFormatter.string(from: trialExpirationDate))
} else {
subtitle = LocalizedString.getValue("settings.freeTier.subtitle")
}
return PurchaseStatusCellViewModel(
iconName: "checkmark.seal.fill",
title: LocalizedString.getValue("settings.unlockFullVersion"),
subtitle: subtitle
)
}
private lazy var debugModeViewModel: SwitchCellViewModel = {
let viewModel = SwitchCellViewModel(title: LocalizedString.getValue("settings.debugMode"), isOn: cryptomatorSettings.debugModeEnabled)
bindDebugModeViewModel(viewModel)
return viewModel
}()
@Dependency(\.fileProviderConnector) private var fileProviderConnector
private var subscribers = Set<AnyCancellable>()
private lazy var showDebugModeWarningPublisher = PassthroughSubject<Void, Never>()
init(cryptomatorSettings: CryptomatorSettings = CryptomatorUserDefaults.shared) {
self.cryptomatorSettings = cryptomatorSettings
}
func refreshCacheSize() -> Promise<Void> {
var loading = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
if loading {
self.cacheSizeCellViewModel.isLoading.value = true
self.clearCacheButtonCellViewModel.isEnabled.value = false
}
}
let getXPCPromise: Promise<XPC<CacheManaging>> = fileProviderConnector.getXPC(serviceName: .cacheManaging, domain: nil)
return getXPCPromise.then { xpc in
xpc.proxy.getLocalCacheSizeInBytes()
}.then { receivedCacheSizeInBytes -> Void in
let totalCacheSizeInBytes = receivedCacheSizeInBytes?.intValue ?? 0
loading = false
self.cacheSizeCellViewModel.isLoading.value = false
self.clearCacheButtonCellViewModel.isEnabled.value = totalCacheSizeInBytes > 0
let formattedString = ByteCountFormatter().string(fromByteCount: Int64(totalCacheSizeInBytes))
self.cacheSizeCellViewModel.detailTitle.value = formattedString
}
}
func clearCache() -> Promise<Void> {
let getXPCPromise: Promise<XPC<CacheManaging>> = fileProviderConnector.getXPC(serviceName: .cacheManaging, domain: nil)
return getXPCPromise.then { xpc in
xpc.proxy.clearCache()
}.then {
self.refreshCacheSize()
}
}
func restorePurchase() -> Promise<RestoreTransactionsResult> {
return StoreObserver.shared.restore()
}
func enableDebugMode() {
setDebugMode(enabled: true)
}
func disableDebugMode() {
setDebugMode(enabled: false)
debugModeViewModel.isOn.value = false
}
private func setDebugMode(enabled: Bool) {
cryptomatorSettings.debugModeEnabled = enabled
LoggerSetup.setDynamicLogLevel(debugModeEnabled: enabled)
notifyFileProviderAboutLogLevelUpdate()
}
private func bindDebugModeViewModel(_ viewModel: SwitchCellViewModel) {
viewModel.isOnButtonPublisher.sink { [weak self] isOn in
if isOn {
self?.showDebugModeWarningPublisher.send()
} else {
self?.setDebugMode(enabled: false)
}
}.store(in: &subscribers)
}
private func notifyFileProviderAboutLogLevelUpdate() {
let getXPCPromise: Promise<XPC<LogLevelUpdating>> = fileProviderConnector.getXPC(serviceName: .logLevelUpdating, domain: nil)
getXPCPromise.then { xpc in
xpc.proxy.logLevelUpdated()
}.always {
self.fileProviderConnector.invalidateXPC(getXPCPromise)
}
}
}