From 0fa9526924954d74f78d2e8c5ffbb365b8cee7eb Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Fri, 8 Apr 2022 12:56:08 -0700 Subject: [PATCH] use searchTerm and selected labels to compute searchQuery --- .../App/Views/Home/HomeFeedViewIOS.swift | 6 +++--- .../App/Views/Home/HomeFeedViewMac.swift | 6 +++--- .../App/Views/Home/HomeFeedViewModel.swift | 19 +++++++++++++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index 7c5e60d82..e6c0e494b 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -23,17 +23,17 @@ import Views viewModel.loadItems(dataService: dataService, isRefresh: true) } .searchable( - text: $viewModel.searchQuery, + text: $viewModel.searchTerm, placement: .sidebar ) { - if viewModel.searchQuery.isEmpty { + if viewModel.searchTerm.isEmpty { Text("Inbox").searchCompletion("in:inbox ") Text("All").searchCompletion("in:all ") Text("Archived").searchCompletion("in:archive ") Text("Files").searchCompletion("type:file ") } } - .onChange(of: viewModel.searchQuery) { _ in + .onChange(of: viewModel.searchTerm) { _ in // Maybe we should debounce this, but // it feels like it works ok without viewModel.loadItems(dataService: dataService, isRefresh: true) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift index 91cad42e8..640f6fdc5 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift @@ -77,17 +77,17 @@ import Views .listStyle(PlainListStyle()) .navigationTitle("Home") .searchable( - text: $viewModel.searchQuery, + text: $viewModel.searchTerm, placement: .toolbar ) { - if viewModel.searchQuery.isEmpty { + if viewModel.searchTerm.isEmpty { Text("Inbox").searchCompletion("in:inbox ") Text("All").searchCompletion("in:all ") Text("Archived").searchCompletion("in:archive ") Text("Files").searchCompletion("type:file ") } } - .onChange(of: viewModel.searchQuery) { _ in + .onChange(of: viewModel.searchTerm) { _ in // Maybe we should debounce this, but // it feels like it works ok without viewModel.loadItems(dataService: dataService, isRefresh: true) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index 7a9af7d98..61829eafd 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -15,7 +15,8 @@ final class HomeFeedViewModel: ObservableObject { @Published var isLoading = false @Published var showPushNotificationPrimer = false @Published var itemUnderLabelEdit: FeedItem? - @Published var searchQuery = "" + @Published var searchTerm = "" + @Published var selectedLabels = [FeedItemLabel]() @Published var snoozePresented = false @Published var itemToSnooze: FeedItem? @Published var selectedLinkItem: FeedItem? @@ -68,7 +69,7 @@ final class HomeFeedViewModel: ObservableObject { dataService.libraryItemsPublisher( limit: 10, sortDescending: true, - searchQuery: searchQuery.isEmpty ? nil : searchQuery, + searchQuery: searchQuery, cursor: isRefresh ? nil : cursor ) .sink( @@ -195,4 +196,18 @@ final class HomeFeedViewModel: ObservableObject { items[index].labels = labels } } + + private var searchQuery: String? { + if searchTerm.isEmpty, selectedLabels.isEmpty { + return nil + } + + var query = searchTerm + + for label in selectedLabels { + query.append(" label:\(label.name)") + } + + return query + } }