From f34631525b0f7fd986bca2e2ad101ca94bb7bd19 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Tue, 5 Apr 2022 11:49:45 -0700 Subject: [PATCH] create apply labels modal --- .../Sources/App/Views/ApplyLabelsView.swift | 70 +++++++++++++++++++ .../App/Views/Home/HomeFeedViewIOS.swift | 2 +- .../App/Views/Profile/LabelsView.swift | 1 + 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 apple/OmnivoreKit/Sources/App/Views/ApplyLabelsView.swift diff --git a/apple/OmnivoreKit/Sources/App/Views/ApplyLabelsView.swift b/apple/OmnivoreKit/Sources/App/Views/ApplyLabelsView.swift new file mode 100644 index 000000000..a24826f8f --- /dev/null +++ b/apple/OmnivoreKit/Sources/App/Views/ApplyLabelsView.swift @@ -0,0 +1,70 @@ +import Combine +import Models +import Services +import SwiftUI + +final class ApplyLabelsViewModel: ObservableObject { + private var hasLoadedInitialLabels = false + @Published var isLoading = true + @Published var selectedLabels = Set() + @Published var labels = [FeedItemLabel]() + + var subscriptions = Set() + + func load(item: FeedItem, dataService: DataService) { + guard !hasLoadedInitialLabels else { return } + + dataService.labelsPublisher().sink( + receiveCompletion: { _ in }, + receiveValue: { [weak self] result in + self?.isLoading = false + self?.labels = result + self?.hasLoadedInitialLabels = true + self?.selectedLabels = Set(item.labels) + } + ) + .store(in: &subscriptions) + } +} + +struct ApplyLabelsView: View { + let item: FeedItem + @EnvironmentObject var dataService: DataService + @Environment(\.presentationMode) private var presentationMode + @StateObject var viewModel = ApplyLabelsViewModel() + + var body: some View { + NavigationView { + if viewModel.isLoading { + EmptyView() + } else { + List(viewModel.labels, id: \.self, selection: $viewModel.selectedLabels) { label in + Text(label.name) + } + .environment(\.editMode, .constant(EditMode.active)) + .navigationTitle("Apply Labels") + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .navigationBarLeading) { + Button( + action: { presentationMode.wrappedValue.dismiss() }, + label: { Text("Cancel") } + ) + } + ToolbarItem(placement: .navigationBarTrailing) { + Button( + action: { + print("saving") + presentationMode.wrappedValue.dismiss() + }, + label: { Text("Save") } + ) + } + } + } + } + .onAppear { + viewModel.load(item: item, dataService: dataService) + } + } +} diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index c621e4f12..1f908cbe6 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -168,7 +168,7 @@ import Views } } .sheet(item: $viewModel.itemUnderLabelEdit, onDismiss: { print("edit label modal dismissed") }) { item in - Text("editing item with id: \(item.id)") + ApplyLabelsView(item: item) } } } diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/LabelsView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/LabelsView.swift index b2f95acda..9853cb993 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Profile/LabelsView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Profile/LabelsView.swift @@ -13,6 +13,7 @@ final class LabelsViewModel: ObservableObject { var subscriptions = Set() func loadLabels(dataService: DataService) { + guard !hasLoadedInitialLabels else { return } isLoading = true dataService.labelsPublisher().sink(