2020-09-22 06:12:50 +00:00
|
|
|
import SwiftUI
|
2022-12-23 04:29:51 +00:00
|
|
|
import Combine
|
2020-09-22 06:12:50 +00:00
|
|
|
import SecretKit
|
|
|
|
|
|
|
|
struct StoreListView: View {
|
|
|
|
|
2022-12-23 04:29:51 +00:00
|
|
|
@Binding var activeSecret: AnySecret.ID?
|
2020-09-22 06:12:50 +00:00
|
|
|
|
|
|
|
@EnvironmentObject private var storeList: SecretStoreList
|
|
|
|
|
2021-06-01 06:20:38 +00:00
|
|
|
private func secretDeleted(secret: AnySecret) {
|
|
|
|
activeSecret = nextDefaultSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
private func secretRenamed(secret: AnySecret) {
|
2022-12-23 04:29:51 +00:00
|
|
|
activeSecret = secret.id
|
2021-06-01 06:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 06:12:50 +00:00
|
|
|
var body: some View {
|
|
|
|
NavigationView {
|
|
|
|
List(selection: $activeSecret) {
|
|
|
|
ForEach(storeList.stores) { store in
|
|
|
|
if store.isAvailable {
|
|
|
|
Section(header: Text(store.name)) {
|
|
|
|
if store.secrets.isEmpty {
|
|
|
|
EmptyStoreView(store: store, activeSecret: $activeSecret)
|
|
|
|
} else {
|
2023-08-24 07:08:46 +00:00
|
|
|
ForEach(store.secrets.sorted(by: { $0.name < $1.name })) { secret in
|
2021-06-01 06:20:38 +00:00
|
|
|
SecretListItemView(
|
|
|
|
store: store,
|
|
|
|
secret: secret,
|
|
|
|
activeSecret: $activeSecret,
|
|
|
|
deletedSecret: self.secretDeleted,
|
|
|
|
renamedSecret: self.secretRenamed
|
|
|
|
)
|
|
|
|
}
|
2020-09-22 06:12:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.listStyle(SidebarListStyle())
|
|
|
|
.onAppear {
|
|
|
|
activeSecret = nextDefaultSecret
|
|
|
|
}
|
|
|
|
.frame(minWidth: 100, idealWidth: 240)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension StoreListView {
|
|
|
|
|
|
|
|
var nextDefaultSecret: AnyHashable? {
|
|
|
|
let fallback: AnyHashable
|
|
|
|
if storeList.modifiableStore?.isAvailable ?? false {
|
|
|
|
fallback = EmptyStoreView.Constants.emptyStoreModifiableTag
|
|
|
|
} else {
|
|
|
|
fallback = EmptyStoreView.Constants.emptyStoreTag
|
|
|
|
}
|
|
|
|
return storeList.stores.compactMap(\.secrets.first).first?.id ?? fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|