2020-12-23 16:53:01 +00:00
|
|
|
import SwiftUI
|
|
|
|
import Sparkle
|
|
|
|
|
|
|
|
struct MacUpdatesView: View {
|
2022-07-28 11:38:19 +00:00
|
|
|
@EnvironmentObject var model: WriteFreelyModel
|
|
|
|
@EnvironmentObject var errorHandling: ErrorHandling
|
|
|
|
|
2022-04-02 12:04:50 +00:00
|
|
|
@ObservedObject var updaterViewModel: MacUpdatesViewModel
|
|
|
|
|
2021-11-05 18:18:36 +00:00
|
|
|
@AppStorage(WFDefaults.automaticallyChecksForUpdates, store: UserDefaults.shared)
|
|
|
|
var automaticallyChecksForUpdates: Bool = false
|
|
|
|
@AppStorage(WFDefaults.subscribeToBetaUpdates, store: UserDefaults.shared)
|
|
|
|
var subscribeToBetaUpdates: Bool = false
|
2022-04-02 12:04:50 +00:00
|
|
|
|
2020-12-23 16:53:01 +00:00
|
|
|
@State private var lastUpdateCheck: Date?
|
|
|
|
|
|
|
|
private let betaWarningString = """
|
2020-12-23 19:38:53 +00:00
|
|
|
To get brand new features before each official release, choose "Test versions." Note that test versions may have bugs \
|
|
|
|
that can cause crashes and data loss.
|
2020-12-23 16:53:01 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
static let lastUpdateFormatter: DateFormatter = {
|
|
|
|
let formatter = DateFormatter()
|
|
|
|
formatter.dateStyle = .short
|
|
|
|
formatter.timeStyle = .short
|
|
|
|
formatter.doesRelativeDateFormatting = true
|
|
|
|
return formatter
|
|
|
|
}()
|
|
|
|
|
|
|
|
var body: some View {
|
2020-12-23 19:38:53 +00:00
|
|
|
VStack(spacing: 24) {
|
|
|
|
Toggle(isOn: $automaticallyChecksForUpdates, label: {
|
|
|
|
Text("Check for updates automatically")
|
2020-12-23 16:53:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
VStack {
|
2020-12-23 19:38:53 +00:00
|
|
|
Button(action: {
|
2022-04-02 12:04:50 +00:00
|
|
|
updaterViewModel.checkForUpdates()
|
|
|
|
// There's a delay between requesting an update, and the timestamp for that update request being
|
|
|
|
// written to user defaults; we therefore delay updating the "Last checked" UI for one second.
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
|
lastUpdateCheck = updaterViewModel.getLastUpdateCheckDate()
|
2020-12-23 19:38:53 +00:00
|
|
|
}
|
|
|
|
}, label: {
|
|
|
|
Text("Check For Updates")
|
2020-12-23 16:53:01 +00:00
|
|
|
})
|
|
|
|
|
2020-12-23 19:38:53 +00:00
|
|
|
HStack {
|
|
|
|
Text("Last checked:")
|
2020-12-23 16:53:01 +00:00
|
|
|
.font(.caption)
|
2020-12-23 19:38:53 +00:00
|
|
|
if let lastUpdateCheck = lastUpdateCheck {
|
|
|
|
Text(lastUpdateCheck, formatter: Self.lastUpdateFormatter)
|
|
|
|
.font(.caption)
|
|
|
|
} else {
|
|
|
|
Text("Never")
|
|
|
|
.font(.caption)
|
|
|
|
}
|
2020-12-23 16:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-23 19:38:53 +00:00
|
|
|
|
|
|
|
VStack(spacing: 16) {
|
|
|
|
HStack(alignment: .top) {
|
|
|
|
Text("Download:")
|
|
|
|
Picker(selection: $subscribeToBetaUpdates, label: Text("Download:"), content: {
|
|
|
|
Text("Release versions").tag(false)
|
|
|
|
Text("Test versions").tag(true)
|
|
|
|
})
|
|
|
|
.pickerStyle(RadioGroupPickerStyle())
|
|
|
|
.labelsHidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
Text(betaWarningString)
|
|
|
|
.frame(width: 350)
|
|
|
|
.foregroundColor(.secondary)
|
2020-12-23 16:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
.onAppear {
|
2022-04-02 12:04:50 +00:00
|
|
|
lastUpdateCheck = updaterViewModel.getLastUpdateCheckDate()
|
2020-12-23 16:53:01 +00:00
|
|
|
}
|
2020-12-23 19:38:53 +00:00
|
|
|
.onChange(of: automaticallyChecksForUpdates) { value in
|
2022-04-02 12:04:50 +00:00
|
|
|
updaterViewModel.automaticallyCheckForUpdates = value
|
2020-12-23 19:38:53 +00:00
|
|
|
}
|
2022-04-02 12:04:50 +00:00
|
|
|
.onChange(of: subscribeToBetaUpdates) { _ in
|
|
|
|
updaterViewModel.toggleAllowedChannels()
|
2020-12-23 19:38:53 +00:00
|
|
|
}
|
2022-07-28 11:38:19 +00:00
|
|
|
.onChange(of: model.hasError) { value in
|
|
|
|
if value {
|
|
|
|
if let error = model.currentError {
|
|
|
|
self.errorHandling.handle(error: error)
|
|
|
|
} else {
|
|
|
|
self.errorHandling.handle(error: AppError.genericError())
|
|
|
|
}
|
|
|
|
model.hasError = false
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 16:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MacUpdatesView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-04-02 12:04:50 +00:00
|
|
|
MacUpdatesView(updaterViewModel: MacUpdatesViewModel())
|
2020-12-23 16:53:01 +00:00
|
|
|
}
|
|
|
|
}
|