Skip to content

Commit

Permalink
Fix as reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
shp7724 committed Dec 27, 2023
1 parent 26f1b01 commit e371bbe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions SNUTT-2022/SNUTT/AppState/States/SearchState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SearchState {
@Published var isFilterOpen = false
@Published var searchTagList: SearchTagList?
@Published var selectedTagList: [SearchTag] = []
@Published var displayMode: SearchDisplayMode = .search

/// If `nil`, the user had never started searching.
/// If empty, the server returned an empty search result.
Expand Down
7 changes: 7 additions & 0 deletions SNUTT-2022/SNUTT/Services/SearchService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protocol SearchServiceProtocol: Sendable {
func setSelectedLecture(_ value: Lecture?)
func initializeSearchState()
func getBookmark() async throws
func setSearchDisplayMode(_ mode: SearchDisplayMode)
}

struct SearchService: SearchServiceProtocol {
Expand Down Expand Up @@ -47,6 +48,7 @@ struct SearchService: SearchServiceProtocol {
searchState.selectedTagList = []
searchState.searchResult = nil
searchState.searchText = ""
searchState.displayMode = .search
}

func fetchTags(quarter: Quarter) async throws {
Expand Down Expand Up @@ -108,6 +110,10 @@ struct SearchService: SearchServiceProtocol {
searchState.isFilterOpen = value
}

func setSearchDisplayMode(_ mode: SearchDisplayMode) {
searchState.displayMode = mode
}

func setSelectedLecture(_ value: Lecture?) {
searchState.selectedLecture = value
}
Expand Down Expand Up @@ -144,4 +150,5 @@ class FakeSearchService: SearchServiceProtocol {
func setSelectedLecture(_: Lecture?) {}
func initializeSearchState() {}
func getBookmark() async throws {}
func setSearchDisplayMode(_ mode: SearchDisplayMode) {}
}
13 changes: 6 additions & 7 deletions SNUTT-2022/SNUTT/ViewModels/SearchLectureSceneViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SearchLectureSceneViewModel: BaseViewModel, ObservableObject {
@Published private var _timetableConfig: TimetableConfiguration = .init()
@Published private var _searchText: String = ""
@Published private var _isFilterOpen: Bool = false
@Published private var _displayMode: SearchDisplayMode = .search

@Published var searchResult: [Lecture]? = nil
@Published var selectedTagList: [SearchTag] = []
Expand All @@ -28,12 +29,9 @@ class SearchLectureSceneViewModel: BaseViewModel, ObservableObject {
set { services.searchService.setIsFilterOpen(newValue) }
}

private var searchState: SearchState {
appState.search
}

private var timetableState: TimetableState {
appState.timetable
var displayMode: SearchDisplayMode {
get { _displayMode }
set { services.searchService.setSearchDisplayMode(newValue) }
}

override init(container: DIContainer) {
Expand All @@ -47,6 +45,7 @@ class SearchLectureSceneViewModel: BaseViewModel, ObservableObject {
appState.search.$isLoading.assign(to: &$isLoading)
appState.search.$searchResult.assign(to: &$searchResult)
appState.search.$selectedTagList.assign(to: &$selectedTagList)
appState.search.$displayMode.assign(to: &$_displayMode)
}

var selectedLecture: Lecture? {
Expand All @@ -66,7 +65,7 @@ class SearchLectureSceneViewModel: BaseViewModel, ObservableObject {
if appState.search.searchTagList != nil {
return
}
guard let currentTimetable = timetableState.current else { return }
guard let currentTimetable = appState.timetable.current else { return }
do {
try await services.searchService.fetchTags(quarter: currentTimetable.quarter)
} catch {
Expand Down
7 changes: 3 additions & 4 deletions SNUTT-2022/SNUTT/Views/Scenes/SearchLectureScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ struct SearchLectureScene: View {
static let searchBarHeight = 44.0
}

@State private var displayMode: SearchDisplayMode = .search
@State private var reloadSearchList: Int = 0

var body: some View {
ZStack {
backgroundTimetableView

VStack(spacing: 0) {
switch displayMode {
switch viewModel.displayMode {
case .search:
searchContentView
.transition(.move(edge: .leading))
Expand All @@ -35,7 +34,7 @@ struct SearchLectureScene: View {
.safeAreaInset(edge: .top, alignment: .center, spacing: 0) {
SearchBar(text: $viewModel.searchText,
isFilterOpen: $viewModel.isFilterOpen,
displayMode: $displayMode,
displayMode: $viewModel.displayMode,
action: viewModel.fetchInitialSearchResult)
.frame(height: Design.searchBarHeight)
}
Expand All @@ -49,7 +48,7 @@ struct SearchLectureScene: View {
.animation(.customSpring, value: viewModel.searchResult?.count)
.animation(.customSpring, value: viewModel.isLoading)
.animation(.customSpring, value: viewModel.selectedTagList.count)
.animation(.customSpring, value: displayMode)
.animation(.customSpring, value: viewModel.displayMode)
.onChange(of: viewModel.isLoading) { _ in
withAnimation(.customSpring) {
reloadSearchList += 1
Expand Down

0 comments on commit e371bbe

Please sign in to comment.