Protocolized

This commit is contained in:
Max Goedjen 2020-03-15 00:32:56 -07:00
parent 979e5b3e3c
commit 5a75e5440f
No known key found for this signature in database
GPG Key ID: E58C21DD77B9B8E8
4 changed files with 53 additions and 30 deletions

View File

@ -13,7 +13,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
list.add(store: SmartCard.Store())
return list
}()
let updater = Updater()
let updater = PreviewUpdater()
func applicationDidFinishLaunching(_ aNotification: Notification) {

View File

@ -1,10 +1,10 @@
import SwiftUI
import SecretKit
struct ContentView: View {
struct ContentView<UpdaterType: UpdaterProtocol>: View {
@ObservedObject var storeList: SecretStoreList
@ObservedObject var updater: Updater
@ObservedObject var updater: UpdaterType
@State fileprivate var active: AnySecret.ID?
@State fileprivate var showingDeletion = false
@ -92,26 +92,25 @@ struct ContentView: View {
}
extension ContentView {
enum Constants {
static let emptyStoreModifiableTag: AnyHashable = "emptyStoreModifiableTag"
static let emptyStoreTag: AnyHashable = "emptyStoreModifiableTag"
}
fileprivate enum Constants {
static let emptyStoreModifiableTag: AnyHashable = "emptyStoreModifiableTag"
static let emptyStoreTag: AnyHashable = "emptyStoreModifiableTag"
}
//#if DEBUG
//
//struct ContentView_Previews: PreviewProvider {
// static var previews: some View {
// Group {
// ContentView(storeList: Preview.storeList(stores: [Preview.Store(numberOfRandomSecrets: 0)], modifiableStores: [Preview.StoreModifiable(numberOfRandomSecrets: 0)]), updater: PreviewUpdater())
// ContentView(storeList: Preview.storeList(stores: [Preview.Store()], modifiableStores: [Preview.StoreModifiable()]), updater: PreviewUpdater())
// ContentView(storeList: Preview.storeList(stores: [Preview.Store()]), updater: PreviewUpdater())
// ContentView(storeList: Preview.storeList(modifiableStores: [Preview.StoreModifiable()]), updater: PreviewUpdater())
// }
// }
//}
//
//#endif
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView(storeList: Preview.storeList(stores: [Preview.Store(numberOfRandomSecrets: 0)], modifiableStores: [Preview.StoreModifiable(numberOfRandomSecrets: 0)]), updater: PreviewUpdater())
ContentView(storeList: Preview.storeList(stores: [Preview.Store()], modifiableStores: [Preview.StoreModifiable()]), updater: PreviewUpdater())
ContentView(storeList: Preview.storeList(stores: [Preview.Store()]), updater: PreviewUpdater())
ContentView(storeList: Preview.storeList(modifiableStores: [Preview.StoreModifiable()]), updater: PreviewUpdater())
ContentView(storeList: Preview.storeList(stores: [Preview.Store(numberOfRandomSecrets: 0)], modifiableStores: [Preview.StoreModifiable(numberOfRandomSecrets: 0)]), updater: PreviewUpdater(update: .advisory))
ContentView(storeList: Preview.storeList(stores: [Preview.Store(numberOfRandomSecrets: 0)], modifiableStores: [Preview.StoreModifiable(numberOfRandomSecrets: 0)]), updater: PreviewUpdater(update: .critical))
}
}
}
#endif

View File

@ -1,6 +1,27 @@
import Foundation
import Combine
class PreviewUpdater: ObservableObject, UpdaterProtocol {
var update: Release? = nil
class PreviewUpdater: UpdaterProtocol {
let update: Release?
init(update: Update = .none) {
switch update {
case .none:
self.update = nil
case .advisory:
self.update = Release(name: "10.10.10", html_url: URL(string: "https://example.com")!, body: "Some regular update")
case .critical:
self.update = Release(name: "10.10.10", html_url: URL(string: "https://example.com")!, body: "Critical Security Update")
}
}
}
extension PreviewUpdater {
enum Update {
case none, advisory, critical
}
}

View File

@ -2,7 +2,9 @@ import Foundation
import Combine
protocol UpdaterProtocol: ObservableObject {
var update: Release? { get }
}
class Updater: ObservableObject, UpdaterProtocol {
@ -10,9 +12,11 @@ class Updater: ObservableObject, UpdaterProtocol {
@Published var update: Release?
init() {
DispatchQueue.global().asyncAfter(deadline: .now() + 3) {
checkForUpdates()
let timer = Timer.scheduledTimer(withTimeInterval: 60*60*24, repeats: true) { _ in
self.checkForUpdates()
}
timer.tolerance = 60*60
}
func checkForUpdates() {
@ -49,16 +53,15 @@ class Updater: ObservableObject, UpdaterProtocol {
extension Updater {
enum Constants {
static let updateURL = URL(string: "https://api.github.com/repos/rails/rails/releases/latest")!
static let updateURL = URL(string: "https://api.github.com/repos/maxgoedjen/secretive/releases/latest")!
}
}
struct Release: Codable {
let name: String
let html_url: URL
fileprivate let body: String
let body: String
}