secretive/Secretive/ContentView.swift

98 lines
3.9 KiB
Swift
Raw Normal View History

2020-02-19 03:36:41 +00:00
import SwiftUI
2020-02-19 04:52:00 +00:00
import SecretKit
struct ContentView: View {
2020-03-09 03:03:40 +00:00
@ObservedObject var storeList: SecretStoreList
@State fileprivate var active: AnySecret.ID?
@State fileprivate var showingDeletion = false
@State fileprivate var deletingSecret: AnySecret?
2020-02-19 03:36:41 +00:00
var body: some View {
2020-03-04 07:14:38 +00:00
NavigationView {
2020-03-07 21:43:57 +00:00
List(selection: $active) {
2020-03-09 03:03:40 +00:00
ForEach(storeList.stores) { store in
if store.isAvailable {
Section(header: Text(store.name)) {
if store.secrets.isEmpty {
if store is AnySecretStoreModifiable {
2020-03-11 08:53:31 +00:00
NavigationLink(destination: EmptyStoreModifiableView(), tag: Constants.emptyStoreModifiableTag, selection: self.$active) {
Text("No Secrets")
}
} else {
2020-03-11 08:53:31 +00:00
NavigationLink(destination: EmptyStoreView(), tag: Constants.emptyStoreTag, selection: self.$active) {
Text("No Secrets")
}
}
} else {
ForEach(store.secrets) { secret in
NavigationLink(destination: SecretDetailView(secret: secret), tag: secret.id, selection: self.$active) {
Text(secret.name)
}.contextMenu {
if store is AnySecretStoreModifiable {
Button(action: { self.delete(secret: secret) }) {
Text("Delete")
}
2020-03-09 03:03:40 +00:00
}
}
2020-03-07 23:42:40 +00:00
}
2020-03-04 07:14:38 +00:00
}
}
}
2020-02-19 04:52:00 +00:00
}
2020-03-07 06:21:10 +00:00
}.onAppear {
2020-03-11 08:53:31 +00:00
let fallback: AnyHashable
if self.storeList.modifiableStore?.isAvailable ?? false {
fallback = Constants.emptyStoreModifiableTag
} else {
fallback = Constants.emptyStoreTag
}
self.active = self.storeList.stores.compactMap { $0.secrets.first }.first?.id ?? fallback
2020-02-19 04:52:00 +00:00
}
2020-03-04 07:14:38 +00:00
.listStyle(SidebarListStyle())
.frame(minWidth: 100, idealWidth: 240)
2020-03-07 06:21:10 +00:00
}
.navigationViewStyle(DoubleColumnNavigationViewStyle())
.sheet(isPresented: $showingDeletion) {
2020-03-09 03:03:40 +00:00
if self.storeList.modifiableStore != nil {
DeleteSecretView(secret: self.deletingSecret!, store: self.storeList.modifiableStore!) {
self.showingDeletion = false
}
}
}
2020-02-19 03:36:41 +00:00
}
2020-03-09 03:03:40 +00:00
func delete<SecretType: Secret>(secret: SecretType) {
deletingSecret = AnySecret(secret)
self.showingDeletion = true
2020-03-04 07:14:38 +00:00
}
2020-03-04 07:14:38 +00:00
}
2020-03-11 08:53:31 +00:00
extension ContentView {
enum Constants {
static let emptyStoreModifiableTag: AnyHashable = "emptyStoreModifiableTag"
static let emptyStoreTag: AnyHashable = "emptyStoreModifiableTag"
}
}
2020-03-12 06:07:22 +00:00
#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)]))
ContentView(storeList: Preview.storeList(stores: [Preview.Store()], modifiableStores: [Preview.StoreModifiable()]))
ContentView(storeList: Preview.storeList(stores: [Preview.Store()]))
ContentView(storeList: Preview.storeList(modifiableStores: [Preview.StoreModifiable()]))
}
}
}
2020-03-12 06:07:22 +00:00
#endif