Skip to content

Commit

Permalink
Always display filter button in explore search bar (#1007)
Browse files Browse the repository at this point in the history
* Always show filter button in explore search bar
  • Loading branch information
ivan-magda authored Jul 8, 2021
1 parent d258122 commit 2d8c83d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
18 changes: 16 additions & 2 deletions Stepic/Sources/Modules/Explore/ExploreDataFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,22 @@ enum Explore {
struct ViewModel {}
}

/// Present course list filter
enum CourseListFilterPresentation {
/// Present explore course list filter, when search results hidden
enum ExploreCourseListFilterPresentation {
struct Request {}

struct Response {
let currentFilters: [CourseListFilter.Filter]
let defaultCourseLanguage: CourseListFilter.Filter.CourseLanguage
}

struct ViewModel {
let presentationDescription: CourseListFilter.PresentationDescription
}
}

/// Present course list filter when search results visible
enum SearchResultsCourseListFilterPresentation {
struct Request {}

struct Response {
Expand Down
20 changes: 16 additions & 4 deletions Stepic/Sources/Modules/Explore/ExploreInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ protocol ExploreInteractorProtocol: BaseExploreInteractorProtocol {
func doContentLoad(request: Explore.ContentLoad.Request)
func doLanguageSwitchBlockLoad(request: Explore.LanguageSwitchAvailabilityCheck.Request)
func doSearchResultsCourseListFiltersUpdate(request: Explore.SearchResultsCourseListFiltersUpdate.Request)
func doCourseListFilterPresentation(request: Explore.CourseListFilterPresentation.Request)
func doSearchResultsCourseListFilterPresentation(request: Explore.SearchResultsCourseListFilterPresentation.Request)
func doExploreCourseListFilterPresentation(request: Explore.ExploreCourseListFilterPresentation.Request)
}

final class ExploreInteractor: BaseExploreInteractor, ExploreInteractorProtocol {
Expand Down Expand Up @@ -57,20 +58,31 @@ final class ExploreInteractor: BaseExploreInteractor, ExploreInteractorProtocol
self.currentSearchResultsCourseListFilters = request.filters.isEmpty
? self.getDefaultSearchResultsCourseListFilters()
: request.filters
self.explorePresenter?.presentSearchResultsCourseListFilters(
self.explorePresenter?.presentSearchResultsCourseListFiltersUpdateResult(
response: .init(filters: self.currentSearchResultsCourseListFilters)
)
}

func doCourseListFilterPresentation(request: Explore.CourseListFilterPresentation.Request) {
self.explorePresenter?.presentCourseListFilter(
func doSearchResultsCourseListFilterPresentation(
request: Explore.SearchResultsCourseListFilterPresentation.Request
) {
self.explorePresenter?.presentSearchResultsCourseListFilter(
response: .init(
currentFilters: self.currentSearchResultsCourseListFilters,
defaultCourseLanguage: self.getDefaultSearchResultsCourseListFilterLanguage()
)
)
}

func doExploreCourseListFilterPresentation(request: Explore.ExploreCourseListFilterPresentation.Request) {
self.explorePresenter?.presentExploreCourseListFilter(
response: .init(
currentFilters: self.getDefaultSearchResultsCourseListFilters(),
defaultCourseLanguage: self.getDefaultSearchResultsCourseListFilterLanguage()
)
)
}

// MARK: Override BaseExploreInteractor

override func presentEmptyState(sourceModule: CourseListInputProtocol) {
Expand Down
30 changes: 24 additions & 6 deletions Stepic/Sources/Modules/Explore/ExplorePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ protocol ExplorePresenterProtocol: BaseExplorePresenterProtocol {
func presentStoriesBlock(response: Explore.StoriesVisibilityUpdate.Response)
func presentCourseListState(response: Explore.CourseListStateUpdate.Response)
func presentStatusBarStyle(response: Explore.StatusBarStyleUpdate.Response)
func presentCourseListFilter(response: Explore.CourseListFilterPresentation.Response)
func presentSearchResultsCourseListFilters(response: Explore.SearchResultsCourseListFiltersUpdate.Response)
func presentExploreCourseListFilter(response: Explore.ExploreCourseListFilterPresentation.Response)
func presentSearchResultsCourseListFilter(response: Explore.SearchResultsCourseListFilterPresentation.Response)
func presentSearchResultsCourseListFiltersUpdateResult(
response: Explore.SearchResultsCourseListFiltersUpdate.Response
)
}

final class ExplorePresenter: BaseExplorePresenter, ExplorePresenterProtocol {
Expand Down Expand Up @@ -44,18 +47,33 @@ final class ExplorePresenter: BaseExplorePresenter, ExplorePresenterProtocol {
self.exploreViewController?.displayStatusBarStyle(viewModel: .init(statusBarStyle: response.statusBarStyle))
}

func presentCourseListFilter(response: Explore.CourseListFilterPresentation.Response) {
func presentSearchResultsCourseListFilter(response: Explore.SearchResultsCourseListFilterPresentation.Response) {
let presentationDescription = CourseListFilter.PresentationDescription(
availableFilters: .all,
prefilledFilters: response.currentFilters,
defaultCourseLanguage: response.defaultCourseLanguage
)
self.exploreViewController?.displayCourseListFilter(
self.exploreViewController?.displaySearchResultsCourseListFilter(
viewModel: .init(presentationDescription: presentationDescription)
)
}

func presentSearchResultsCourseListFilters(response: Explore.SearchResultsCourseListFiltersUpdate.Response) {
self.exploreViewController?.displaySearchResultsCourseListFilters(viewModel: .init(filters: response.filters))
func presentExploreCourseListFilter(response: Explore.ExploreCourseListFilterPresentation.Response) {
let presentationDescription = CourseListFilter.PresentationDescription(
availableFilters: .all,
prefilledFilters: response.currentFilters,
defaultCourseLanguage: response.defaultCourseLanguage
)
self.exploreViewController?.displayExploreCourseListFilter(
viewModel: .init(presentationDescription: presentationDescription)
)
}

func presentSearchResultsCourseListFiltersUpdateResult(
response: Explore.SearchResultsCourseListFiltersUpdate.Response
) {
self.exploreViewController?.displaySearchResultsCourseListFiltersUpdateResult(
viewModel: .init(filters: response.filters)
)
}
}
59 changes: 53 additions & 6 deletions Stepic/Sources/Modules/Explore/ExploreViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ protocol ExploreViewControllerProtocol: BaseExploreViewControllerProtocol {
func displayModuleErrorState(viewModel: Explore.CourseListStateUpdate.ViewModel)
func displayStatusBarStyle(viewModel: Explore.StatusBarStyleUpdate.ViewModel)
func displaySearchCourses(viewModel: Explore.SearchCourses.ViewModel)
func displayCourseListFilter(viewModel: Explore.CourseListFilterPresentation.ViewModel)
func displaySearchResultsCourseListFilters(viewModel: Explore.SearchResultsCourseListFiltersUpdate.ViewModel)
func displayExploreCourseListFilter(viewModel: Explore.ExploreCourseListFilterPresentation.ViewModel)
func displaySearchResultsCourseListFilter(viewModel: Explore.SearchResultsCourseListFilterPresentation.ViewModel)
func displaySearchResultsCourseListFiltersUpdateResult(
viewModel: Explore.SearchResultsCourseListFiltersUpdate.ViewModel
)
}

final class ExploreViewController: BaseExploreViewController {
Expand Down Expand Up @@ -40,6 +43,26 @@ final class ExploreViewController: BaseExploreViewController {
action: #selector(self.ipadCancelSearchButtonClicked)
)

private lazy var exploreCourseListFilterOutput: CourseListFilterOutputProtocol = {
let output = ExploreCourseListFilterOutput()
output.onFiltersChanged = { [weak self] filters in
guard let strongSelf = self else {
return
}

strongSelf.exploreInteractor?.doSearchResultsCourseListFiltersUpdate(request: .init(filters: filters))

strongSelf.showSearchResults()
strongSelf.searchResultsModuleInput?.searchStarted()
strongSelf.searchResultsModuleInput?.search(query: "")
}
return output
}()

private var isSearchResultsHidden: Bool {
self.searchResultsController?.view.isHidden ?? true
}

init(
interactor: ExploreInteractorProtocol,
analytics: Analytics,
Expand All @@ -64,6 +87,7 @@ final class ExploreViewController: BaseExploreViewController {
self.navigationItem.titleView = self.searchBar
self.exploreInteractor?.doLanguageSwitchBlockLoad(request: .init())

self.searchBar.showsFilterButton = true
self.initSearchResults()

self.updateState(newState: self.state)
Expand Down Expand Up @@ -282,7 +306,6 @@ final class ExploreViewController: BaseExploreViewController {

private func hideSearchResults() {
self.searchResultsController?.view.isHidden = true
self.searchBar.showsFilterButton = false
self.exploreInteractor?.doSearchResultsCourseListFiltersUpdate(request: .init(filters: []))
}

Expand Down Expand Up @@ -358,7 +381,7 @@ extension ExploreViewController: ExploreViewControllerProtocol {
self.searchBarTextDidBeginEditing(self.searchBar)
}

func displayCourseListFilter(viewModel: Explore.CourseListFilterPresentation.ViewModel) {
func displaySearchResultsCourseListFilter(viewModel: Explore.SearchResultsCourseListFilterPresentation.ViewModel) {
let assembly = CourseListFilterAssembly(
presentationDescription: viewModel.presentationDescription,
output: self.interactor as? CourseListFilterOutputProtocol
Expand All @@ -368,7 +391,19 @@ extension ExploreViewController: ExploreViewControllerProtocol {
self.present(module: navigationController, embedInNavigation: false, modalPresentationStyle: .stepikAutomatic)
}

func displaySearchResultsCourseListFilters(viewModel: Explore.SearchResultsCourseListFiltersUpdate.ViewModel) {
func displayExploreCourseListFilter(viewModel: Explore.ExploreCourseListFilterPresentation.ViewModel) {
let assembly = CourseListFilterAssembly(
presentationDescription: viewModel.presentationDescription,
output: self.exploreCourseListFilterOutput
)
let navigationController = StyledNavigationController(rootViewController: assembly.makeModule())

self.present(module: navigationController, embedInNavigation: false, modalPresentationStyle: .stepikAutomatic)
}

func displaySearchResultsCourseListFiltersUpdateResult(
viewModel: Explore.SearchResultsCourseListFiltersUpdate.ViewModel
) {
let newFilterQuery = CourseListFilterQuery(courseListFilters: viewModel.filters)
self.searchResultsModuleInput?.filterQueryChanged(to: newFilterQuery)
}
Expand Down Expand Up @@ -420,7 +455,11 @@ extension ExploreViewController: ExploreSearchBarDelegate {
}

func exploreSearchBarFilterButtonClicked(_ searchBar: ExploreSearchBar) {
self.exploreInteractor?.doCourseListFilterPresentation(request: .init())
if self.isSearchResultsHidden {
self.exploreInteractor?.doExploreCourseListFilterPresentation(request: .init())
} else {
self.exploreInteractor?.doSearchResultsCourseListFilterPresentation(request: .init())
}
}
}

Expand All @@ -432,3 +471,11 @@ extension ExploreViewController: BaseExploreViewDelegate {
}
}
}

private class ExploreCourseListFilterOutput: CourseListFilterOutputProtocol {
var onFiltersChanged: (([CourseListFilter.Filter]) -> Void)?

func handleCourseListFilterDidFinishWithFilters(_ filters: [CourseListFilter.Filter]) {
self.onFiltersChanged?(filters)
}
}

0 comments on commit 2d8c83d

Please sign in to comment.