use assigned and unassigned section for label asiignment modal

This commit is contained in:
Satindar Dhillon 2022-04-06 08:53:18 -07:00
parent c85c244459
commit e059d2d6e7

View file

@ -7,8 +7,8 @@ import Views
final class ApplyLabelsViewModel: ObservableObject { final class ApplyLabelsViewModel: ObservableObject {
private var hasLoadedInitialLabels = false private var hasLoadedInitialLabels = false
@Published var isLoading = true @Published var isLoading = true
@Published var selectedLabels = Set<FeedItemLabel>() @Published var selectedLabels = [FeedItemLabel]()
@Published var labels = [FeedItemLabel]() @Published var unselectedLabels = [FeedItemLabel]()
var subscriptions = Set<AnyCancellable>() var subscriptions = Set<AnyCancellable>()
@ -17,11 +17,11 @@ final class ApplyLabelsViewModel: ObservableObject {
dataService.labelsPublisher().sink( dataService.labelsPublisher().sink(
receiveCompletion: { _ in }, receiveCompletion: { _ in },
receiveValue: { [weak self] result in receiveValue: { [weak self] allLabels in
self?.isLoading = false self?.isLoading = false
self?.labels = result
self?.hasLoadedInitialLabels = true self?.hasLoadedInitialLabels = true
self?.selectedLabels = Set(item.labels) self?.selectedLabels = item.labels
self?.unselectedLabels = allLabels.filter { !item.labels.contains($0) }
} }
) )
.store(in: &subscriptions) .store(in: &subscriptions)
@ -34,6 +34,16 @@ final class ApplyLabelsViewModel: ObservableObject {
) )
.store(in: &subscriptions) .store(in: &subscriptions)
} }
func addLabel(_ label: FeedItemLabel) {
selectedLabels.insert(label, at: 0)
unselectedLabels.removeAll { $0.id == label.id }
}
func removeLabel(_ label: FeedItemLabel) {
unselectedLabels.insert(label, at: 0)
selectedLabels.removeAll { $0.id == label.id }
}
} }
struct ApplyLabelsView: View { struct ApplyLabelsView: View {
@ -49,15 +59,44 @@ struct ApplyLabelsView: View {
if viewModel.isLoading { if viewModel.isLoading {
EmptyView() EmptyView()
} else { } else {
List(viewModel.labels, id: \.self, selection: $viewModel.selectedLabels) { label in List {
if let textChip = TextChip(feedItemLabel: label) { Section(header: Text("Assigned Labels")) {
textChip if viewModel.selectedLabels.isEmpty {
} else { Text("No labels are currently assigned.")
Text(label.name) }
ForEach(viewModel.selectedLabels, id: \.self) { label in
HStack {
TextChip(feedItemLabel: label)
Spacer()
Button(
action: {
withAnimation {
viewModel.removeLabel(label)
}
},
label: { Image(systemName: "trash") }
)
}
}
}
Section(header: Text("Available Labels")) {
ForEach(viewModel.unselectedLabels, id: \.self) { label in
HStack {
TextChip(feedItemLabel: label)
Spacer()
Button(
action: {
withAnimation {
viewModel.addLabel(label)
}
},
label: { Image(systemName: "plus") }
)
}
}
} }
} }
.environment(\.editMode, .constant(EditMode.active)) .navigationTitle("Assign Labels")
.navigationTitle("Apply Labels")
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.toolbar { .toolbar {
ToolbarItem(placement: .navigationBarLeading) { ToolbarItem(placement: .navigationBarLeading) {