Break out empty view.

This commit is contained in:
Max Goedjen 2020-09-13 23:11:13 -06:00
parent cc9a23a3e8
commit 794de4e132
No known key found for this signature in database
GPG Key ID: E58C21DD77B9B8E8
2 changed files with 32 additions and 17 deletions

View File

@ -1,7 +1,35 @@
import SwiftUI import SwiftUI
import SecretKit
struct EmptyStoreView: View { struct EmptyStoreView: View {
@ObservedObject var store: AnySecretStore
@Binding var activeSecret: AnySecret.ID?
var body: some View {
if store is AnySecretStoreModifiable {
NavigationLink(destination: EmptyStoreModifiableView(), tag: Constants.emptyStoreModifiableTag, selection: $activeSecret) {
Text("No Secrets")
}
} else {
NavigationLink(destination: EmptyStoreImmutableView(), tag: Constants.emptyStoreTag, selection: $activeSecret) {
Text("No Secrets")
}
}
}
}
extension EmptyStoreView {
enum Constants {
static let emptyStoreModifiableTag: AnyHashable = "emptyStoreModifiableTag"
static let emptyStoreTag: AnyHashable = "emptyStoreModifiableTag"
}
}
struct EmptyStoreImmutableView: View {
var body: some View { var body: some View {
VStack { VStack {
Text("No Secrets").bold() Text("No Secrets").bold()
@ -48,7 +76,7 @@ struct EmptyStoreModifiableView: View {
struct EmptyStoreModifiableView_Previews: PreviewProvider { struct EmptyStoreModifiableView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
Group { Group {
EmptyStoreView() EmptyStoreImmutableView()
EmptyStoreModifiableView() EmptyStoreModifiableView()
} }
} }

View File

@ -15,15 +15,7 @@ struct StoreListView: View {
if store.isAvailable { if store.isAvailable {
Section(header: Text(store.name)) { Section(header: Text(store.name)) {
if store.secrets.isEmpty { if store.secrets.isEmpty {
if store is AnySecretStoreModifiable { EmptyStoreView(store: store, activeSecret: $activeSecret)
NavigationLink(destination: EmptyStoreModifiableView(), tag: Constants.emptyStoreModifiableTag, selection: $activeSecret) {
Text("No Secrets")
}
} else {
NavigationLink(destination: EmptyStoreView(), tag: Constants.emptyStoreTag, selection: $activeSecret) {
Text("No Secrets")
}
}
} else { } else {
SecretListView(store: store, activeSecret: $activeSecret, deletingSecret: $deletingSecret, deletedSecret: { _ in SecretListView(store: store, activeSecret: $activeSecret, deletingSecret: $deletingSecret, deletedSecret: { _ in
activeSecret = nextDefaultSecret activeSecret = nextDefaultSecret
@ -48,16 +40,11 @@ extension StoreListView {
var nextDefaultSecret: AnyHashable? { var nextDefaultSecret: AnyHashable? {
let fallback: AnyHashable let fallback: AnyHashable
if storeList.modifiableStore?.isAvailable ?? false { if storeList.modifiableStore?.isAvailable ?? false {
fallback = Constants.emptyStoreModifiableTag fallback = EmptyStoreView.Constants.emptyStoreModifiableTag
} else { } else {
fallback = Constants.emptyStoreTag fallback = EmptyStoreView.Constants.emptyStoreTag
} }
return storeList.stores.compactMap(\.secrets.first).first?.id ?? fallback return storeList.stores.compactMap(\.secrets.first).first?.id ?? fallback
} }
} }
private enum Constants {
static let emptyStoreModifiableTag: AnyHashable = "emptyStoreModifiableTag"
static let emptyStoreTag: AnyHashable = "emptyStoreModifiableTag"
}