Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search bar #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added NYTimes Screenshots/add-search-bar-After.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added NYTimes Screenshots/add-search-bar-Before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
8 changes: 8 additions & 0 deletions NYTimes/ViewModel/ArticleViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ class ArticleViewModel: ObservableObject {

@Published var articles = [Article]()
@Published var isArticlesLoading = false
@Published var searchText = ""

var cancellableTask: AnyCancellable? = nil
var searchResults : [Article] {
if searchText.isEmpty {
return articles
} else {
return articles.filter { $0.title.lowercased().contains(searchText.lowercased()) || $0.subtitle.lowercased().contains(searchText.lowercased()) || $0.author.lowercased().contains(searchText.lowercased()) }
}
}

func loadArticles(for category: Category) {
guard let url = URL(string: category.url) else { return }
Expand Down
24 changes: 22 additions & 2 deletions NYTimes/Views/RootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct RootView: View {
@Environment(\.managedObjectContext) var managedObjectContext

@State var categories: [Category] = Category.allCases

var initialCategory: Int

@ObservedObject var articlesViewModel = ArticleViewModel()
Expand All @@ -33,6 +33,7 @@ struct RootView: View {
var body: some View {
NavigationView {
if networkReachability.isNetworkConnected {

ArticleView()
.edgesIgnoringSafeArea(.all)
.navigationViewStyle(StackNavigationViewStyle())
Expand Down Expand Up @@ -87,8 +88,11 @@ struct RootView: View {

fileprivate func ArticleView() -> some View {
return VStack {

NavigationLink(destination: BookmarksView(), isActive: $shouldShowBookmarks) {}
NavigationLink(destination: CategoriesView(viewModel: CategoriesViewModel(updateCategories)), isActive: $openCategories) {}


if articlesViewModel.isArticlesLoading {
VStack{
Spacer()
Expand All @@ -98,9 +102,23 @@ struct RootView: View {
Spacer()
}
} else {

ArticleListView(articlesViewModel: articlesViewModel, bookmarkViewModel: bookmarkViewModel)
}
Spacer()

HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
.frame(width: 10, alignment: .leading)
.padding(.leading, 8)

TextField("Search title, subtitle, or author", text: $articlesViewModel.searchText)
.padding(7)
.cornerRadius(8)

}

CategorySelector(
categories: categories,
articleViewModel: articlesViewModel,
Expand All @@ -118,7 +136,9 @@ struct ArticleListView: View {
@ObservedObject var bookmarkViewModel: BookmarkViewModel

var body: some View {
List(articlesViewModel.articles, id: \.id){ article in

List(articlesViewModel.searchResults, id: \.id){ article in

NavigationLink(destination: WebViewHolder(url: URL(string: article.url)!, article: article)){
NewsFeedView(article: article)
.contextMenu(menuItems: {
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Adding SearchBar

![](https://raw.githubusercontent.com/TheCodeMonks/NYTimes-iOS/master/NYTimes%20Screenshots/Banner%400.25x.png)
![SwiftUI](https://img.shields.io/badge/Interface-SwfitUI-red)
![Architecture](https://img.shields.io/badge/Architecture-MVVM-green)
Expand Down