Skip to content

Commit

Permalink
Search: More refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed May 17, 2021
1 parent 8df14f1 commit 5d172c7
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 23 deletions.
16 changes: 10 additions & 6 deletions RedditOs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
9F19B57A26505DCF00FBEEDA /* SidebarMultiView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F19B57926505DCF00FBEEDA /* SidebarMultiView.swift */; };
9F4537862652DF960026C19B /* SearchBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F4537852652DF960026C19B /* SearchBarView.swift */; };
9F4537882652ED550026C19B /* QuickSearchPostsResultView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F4537872652ED550026C19B /* QuickSearchPostsResultView.swift */; };
9F45378B2652F5DC0026C19B /* SubredditSearchState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F45378A2652F5DC0026C19B /* SubredditSearchState.swift */; };
9F4812CC264FC8DB007A719D /* QuickSearchFullResultsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F4812CB264FC8DB007A719D /* QuickSearchFullResultsView.swift */; };
9F8EE49126510BCF00BDE4AC /* MarkdownUI in Frameworks */ = {isa = PBXBuildFile; productRef = 9F8EE49026510BCF00BDE4AC /* MarkdownUI */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -136,6 +137,7 @@
9F19B57926505DCF00FBEEDA /* SidebarMultiView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarMultiView.swift; sourceTree = "<group>"; };
9F4537852652DF960026C19B /* SearchBarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBarView.swift; sourceTree = "<group>"; };
9F4537872652ED550026C19B /* QuickSearchPostsResultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickSearchPostsResultView.swift; sourceTree = "<group>"; };
9F45378A2652F5DC0026C19B /* SubredditSearchState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubredditSearchState.swift; sourceTree = "<group>"; };
9F4812CB264FC8DB007A719D /* QuickSearchFullResultsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickSearchFullResultsView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -267,8 +269,8 @@
6970A0B724B79AF400B11031 /* Search */ = {
isa = PBXGroup;
children = (
9F4537892652F5CD0026C19B /* Subreddit */,
9F4537842652DDDE0026C19B /* Shared */,
9F4537832652DDD50026C19B /* Posts */,
9F4537822652DDCD0026C19B /* Quick */,
693F85D224D06AA700224ADB /* Subreddit Search Popover */,
);
Expand Down Expand Up @@ -401,19 +403,20 @@
path = Quick;
sourceTree = "<group>";
};
9F4537832652DDD50026C19B /* Posts */ = {
9F4537842652DDDE0026C19B /* Shared */ = {
isa = PBXGroup;
children = (
9F4537852652DF960026C19B /* SearchBarView.swift */,
);
path = Posts;
path = Shared;
sourceTree = "<group>";
};
9F4537842652DDDE0026C19B /* Shared */ = {
9F4537892652F5CD0026C19B /* Subreddit */ = {
isa = PBXGroup;
children = (
9F4537852652DF960026C19B /* SearchBarView.swift */,
9F45378A2652F5DC0026C19B /* SubredditSearchState.swift */,
);
path = Shared;
path = Subreddit;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -519,6 +522,7 @@
6924D53E24CD94B0005487CA /* UserViewModel.swift in Sources */,
69222AA724CD6D6C009F31B4 /* SubmittedPostsListView.swift in Sources */,
9F4812CC264FC8DB007A719D /* QuickSearchFullResultsView.swift in Sources */,
9F45378B2652F5DC0026C19B /* SubredditSearchState.swift in Sources */,
697E324724E3ED620006F00F /* CommentViewModel.swift in Sources */,
692566DA24B8A3830014E0D4 /* PostDetailHeader.swift in Sources */,
69CCB3EA24E2BEAC003FAAD7 /* SubredditAboutPopoverView.swift in Sources */,
Expand Down
20 changes: 17 additions & 3 deletions RedditOs/Features/Post/PostDetailToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@ struct PostDetailToolbar: ToolbarContent {
}

struct SearchView: View {
@State private var isExpanded = false
@EnvironmentObject private var uiState: UIState

var body: some View {
if uiState.displayToolbarSearchBar {
QuickSearchBar(showSuggestionPopover: true,
onCommit: {}, onCancel: {})
.frame(width: 300)
if !isExpanded {
Button {
isExpanded.toggle()
} label: {
Image(systemName: "magnifyingglass")
}
} else {
QuickSearchBar(showSuggestionPopover: true,
onCommit: {},
onCancel: {
self.isExpanded = false
})
.frame(width: 300)
.transition(.slide)
.animation(.easeInOut)
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions RedditOs/Features/Profile/SavedPostsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ struct SavedPostsListView: View {
@State private var displayMode = SubredditPostRow.DisplayMode.large

var body: some View {
PostsListView(posts: currentUser.savedPosts,
displayMode: $displayMode) {
PostsListView(header: { EmptyView() },
posts: currentUser.savedPosts,
displayMode: $displayMode) {
currentUser.fetchSaved(after: currentUser.savedPosts?.last)
}.onAppear {
currentUser.fetchSaved(after: nil)
Expand Down
5 changes: 3 additions & 2 deletions RedditOs/Features/Profile/SubmittedPostsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ struct SubmittedPostsListView: View {
@State private var displayMode: SubredditPostRow.DisplayMode = .large

var body: some View {
PostsListView(posts: currentUser.submittedPosts,
displayMode: $displayMode) {
PostsListView(header: { EmptyView() },
posts: currentUser.submittedPosts,
displayMode: $displayMode) {
currentUser.fetchSubmitted(after: currentUser.submittedPosts?.last)
}.onAppear {
currentUser.fetchSubmitted(after: nil)
Expand Down
4 changes: 3 additions & 1 deletion RedditOs/Features/Search/Quick/QuickSearchBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct QuickSearchBar: View {
let onCancel: () -> Void

var body: some View {
SearchBarView(searchText: $searchState.searchText) { editing in
SearchBarView(placeholder: "Search anything",
searchText: $searchState.searchText) { editing in
if showSuggestionPopover {
isPopoverPresented = editing
}
Expand All @@ -27,6 +28,7 @@ struct QuickSearchBar: View {
onCancel()
}
.keyboardShortcut("f", modifiers: .command)
.animation(.easeInOut)
.popover(isPresented: $isPopoverPresented) {
ScrollView {
VStack(alignment: .leading) {
Expand Down
8 changes: 4 additions & 4 deletions RedditOs/Features/Search/Shared/SearchBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import SwiftUI

struct SearchBarView: View {
let placeholder: String
@Binding var searchText: String
@State private var isFocused = false

let onEditingChange: (Bool) -> Void
let onCommit: () -> Void
let onCancel: () -> Void

@State private var isFocused = false

var body: some View {
HStack {
TextField("Search anything", text: $searchText) { editing in
TextField(placeholder, text: $searchText) { editing in
isFocused = editing
onEditingChange(editing)
} onCommit: {
Expand All @@ -28,7 +29,6 @@ struct SearchBarView: View {
.stroke(isFocused ? Color.accentColor : Color.clear)
.background(Color.black.opacity(0.2).cornerRadius(8)))
.textFieldStyle(PlainTextFieldStyle())
.animation(.easeInOut)

if !searchText.isEmpty {
Button {
Expand Down
13 changes: 13 additions & 0 deletions RedditOs/Features/Search/Subreddit/SubredditSearchState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SubredditSearchState.swift
// RedditOs
//
// Created by Thomas Ricouard on 17/05/2021.
//

import SwiftUI
import Combine

class SubredditSearchState: ObservableObject {
@Published var searchText: String = ""
}
16 changes: 14 additions & 2 deletions RedditOs/Features/Subreddit/SubredditPostsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct SubredditPostsListView: View, Equatable {
@EnvironmentObject private var localData: LocalDataStore

@StateObject private var viewModel: SubredditViewModel
@StateObject private var searchState = SubredditSearchState()
@AppStorage(SettingsKey.subreddit_display_mode) private var displayMode = SubredditPostRow.DisplayMode.large

@State private var subredditAboutPopoverShown = false
Expand Down Expand Up @@ -54,8 +55,19 @@ struct SubredditPostsListView: View, Equatable {
}

var body: some View {
PostsListView(posts: viewModel.listings,
displayMode: .constant(displayMode)) {
PostsListView(header: {
SearchBarView(placeholder: "Search",
searchText: $searchState.searchText) { editing in

} onCommit: {

} onCancel: {

}
.keyboardShortcut("f", modifiers: .command)
},
posts: viewModel.listings,
displayMode: .constant(displayMode)) {
viewModel.fetchListings()
}
.equatable()
Expand Down
3 changes: 2 additions & 1 deletion RedditOs/Features/Users/sheet/UserSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct UserSheetView: View {
UserSheetOverviewView(viewModel: viewModel)
} else if sidebarSelection.first == 2 {
if let posts = viewModel.submittedPosts {
PostsListView(posts: posts, displayMode: .constant(.large)) {
PostsListView(header: { EmptyView() },
posts: posts, displayMode: .constant(.large)) {
viewModel.fetchSubmitted(after: posts.last)
}
}
Expand Down
8 changes: 6 additions & 2 deletions RedditOs/Shared/PostsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
import SwiftUI
import Backend

struct PostsListView: View, Equatable {
struct PostsListView<Header: View>: View, Equatable {
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.posts?.count == rhs.posts?.count &&
rhs.displayMode == lhs.displayMode
}

private let loadingPlaceholders = Array(repeating: static_listing, count: 10)

@ViewBuilder var header: () -> Header
let posts: [SubredditPost]?
@Binding var displayMode: SubredditPostRow.DisplayMode
var onNextPage: (() -> Void)

var body: some View {
List {
header()
ForEach(posts ?? loadingPlaceholders) { post in
SubredditPostRow(post: post,
displayMode: $displayMode)
Expand All @@ -41,7 +44,8 @@ struct PostsListView: View, Equatable {

struct PostsListView_Previews: PreviewProvider {
static var previews: some View {
PostsListView(posts: nil,
PostsListView(header: { EmptyView() },
posts: nil,
displayMode: .constant(.large),
onNextPage: {

Expand Down

0 comments on commit 5d172c7

Please sign in to comment.