Skip to content

Commit

Permalink
added search reselection
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBAndrews committed Dec 28, 2023
1 parent ae535ec commit 3753677
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions Mlem/Views/Tabs/Search/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ struct SearchView: View {

// environment
@Environment(\.scrollViewProxy) private var scrollProxy
@Environment(\.navigationPathWithRoutes) private var navigationPath
@Environment(\.tabSelectionHashValue) var tabSelectionHashValue
@EnvironmentObject var appState: AppState
@EnvironmentObject private var recentSearchesTracker: RecentSearchesTracker
@StateObject var searchModel: SearchModel

@StateObject var homeSearchModel: SearchModel
@StateObject var homeContentTracker: ContentTracker<AnyContentModel> = .init()

@State var searchBarFocused: Bool = false
@State var isSearching: Bool = false
@State var page: Page = .home

Expand Down Expand Up @@ -66,6 +69,7 @@ struct SearchView: View {
resultsScrollToTopSignal += 1
recentsScrollToTopSignal += 1
}
.focused($searchBarFocused)
}
.navigationSearchBarHiddenWhenScrolling(false)
.autocorrectionDisabled(true)
Expand All @@ -85,6 +89,24 @@ struct SearchView: View {
resultsScrollToTopSignal += 1
}
}
.onChange(of: tabSelectionHashValue) { newValue in
// due to the hack in SearchBar:136, this is required to move focus off of the search bar when changing tabs
// because the keyboard hides the tab bar and so users are required to cancel a search to switch tabs, this
// probably isn't needed in most cases, but since it's possible to disable the on-screen keyboard I've included it
if tabSelectionHashValue == TabSelection.search.hashValue, newValue != TabSelection.search.hashValue {
searchBarFocused = false
}
}
.hoistNavigation {
if scrollToTopAppeared {
searchBarFocused = true
} else {
withAnimation {
scrollToTop(page: page)
}
}
return true
}
}

@ViewBuilder
Expand Down Expand Up @@ -146,12 +168,6 @@ struct SearchView: View {
}
}
}
.hoistNavigation {
withAnimation {
scrollToTop(page: page)
}
return true
}
}

private func scrollToTop(page: Page) {
Expand Down

0 comments on commit 3753677

Please sign in to comment.