Skip to content

Commit

Permalink
fix: Conditionally display views in subviews (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed May 24, 2024
2 parents 3e80c30 + e67d34b commit ce31400
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 91 deletions.
44 changes: 23 additions & 21 deletions Mail/Views/Search/SearchContactsSectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@ struct SearchContactsSectionView: View {
let viewModel: SearchViewModel

var body: some View {
Section {
ForEach(viewModel.frozenContacts) { contact in
RecipientCell(recipient: contact)
.onTapGesture {
viewModel.matomo.track(eventWithCategory: .search, name: "selectContact")
viewModel.addToHistoryIfNeeded()
Constants.globallyResignFirstResponder()
viewModel.searchThreadsForContact(contact)
}
}
.padding(.vertical, threadDensity.cellVerticalPadding)
.padding(.leading, UIPadding.small + UIConstants.unreadIconSize + UIPadding.small)
.padding(.trailing, value: .regular)
} header: {
if !viewModel.frozenContacts.isEmpty {
Text(MailResourcesStrings.Localizable.contactsSearch)
.textStyle(.bodySmallSecondary)
.padding(.horizontal, value: .regular)
if viewModel.searchState == .results {
Section {
ForEach(viewModel.frozenContacts) { contact in
RecipientCell(recipient: contact)
.onTapGesture {
viewModel.matomo.track(eventWithCategory: .search, name: "selectContact")
viewModel.addToHistoryIfNeeded()
Constants.globallyResignFirstResponder()
viewModel.searchThreadsForContact(contact)
}
}
.padding(.vertical, threadDensity.cellVerticalPadding)
.padding(.leading, UIPadding.small + UIConstants.unreadIconSize + UIPadding.small)
.padding(.trailing, value: .regular)
} header: {
if !viewModel.frozenContacts.isEmpty {
Text(MailResourcesStrings.Localizable.contactsSearch)
.textStyle(.bodySmallSecondary)
.padding(.horizontal, value: .regular)
}
}
.listRowInsets(.init())
.listRowSeparator(.hidden)
.listRowBackground(MailResourcesAsset.backgroundColor.swiftUIColor)
}
.listRowInsets(.init())
.listRowSeparator(.hidden)
.listRowBackground(MailResourcesAsset.backgroundColor.swiftUIColor)
}
}
72 changes: 37 additions & 35 deletions Mail/Views/Search/SearchHistorySectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,55 @@ struct SearchHistorySectionView: View {
let viewModel: SearchViewModel

var body: some View {
Section {
if let history = searchHistory?.history {
if history.isEmpty {
if viewModel.searchState == .history {
Section {
if searchHistory == nil || searchHistory?.history.isEmpty == true {
SearchNoHistoryView()
}

ForEach(history, id: \.self) { searchItem in
HStack(spacing: UIPadding.regular) {
IKIcon(MailResourcesAsset.clock, size: .large)
.foregroundStyle(.tint)
if let history = searchHistory?.history {
ForEach(history, id: \.self) { searchItem in
HStack(spacing: UIPadding.regular) {
IKIcon(MailResourcesAsset.clock, size: .large)
.foregroundStyle(.tint)

Text(searchItem)
.textStyle(.bodyMedium)
Text(searchItem)
.textStyle(.bodyMedium)

Spacer()
Spacer()

Button {
deleteSearchTapped(searchItem: searchItem)
} label: {
IKIcon(MailResourcesAsset.close)
.foregroundStyle(MailResourcesAsset.textSecondaryColor)
Button {
deleteSearchTapped(searchItem: searchItem)
} label: {
IKIcon(MailResourcesAsset.close)
.foregroundStyle(MailResourcesAsset.textSecondaryColor)
}
.buttonStyle(BorderlessButtonStyle())
.accessibilityLabel(MailResourcesStrings.Localizable.contentDescriptionButtonDeleteHistory)
}
.buttonStyle(BorderlessButtonStyle())
.accessibilityLabel(MailResourcesStrings.Localizable.contentDescriptionButtonDeleteHistory)
}
.contentShape(Rectangle())
.onTapGesture {
viewModel.matomo.track(eventWithCategory: .search, name: "fromHistory")
Constants.globallyResignFirstResponder()
viewModel.searchValue = searchItem
Task {
await viewModel.fetchThreads()
.contentShape(Rectangle())
.onTapGesture {
viewModel.matomo.track(eventWithCategory: .search, name: "fromHistory")
Constants.globallyResignFirstResponder()
viewModel.searchValue = searchItem
Task {
await viewModel.fetchThreads()
}
}
.padding(value: .regular)
}
.padding(value: .regular)
}
} header: {
if viewModel.searchState == .history && searchHistory?.history.isEmpty == false {
Text(MailResourcesStrings.Localizable.recentSearchesTitle)
.textStyle(.bodySmallSecondary)
.padding(.horizontal, value: .regular)
}
}
} header: {
if searchHistory?.history.isEmpty == false {
Text(MailResourcesStrings.Localizable.recentSearchesTitle)
.textStyle(.bodySmallSecondary)
.padding(.horizontal, value: .regular)
}
.listRowSeparator(.hidden)
.listRowBackground(MailResourcesAsset.backgroundColor.swiftUIColor)
.listRowInsets(.init())
}
.listRowSeparator(.hidden)
.listRowBackground(MailResourcesAsset.backgroundColor.swiftUIColor)
.listRowInsets(.init())
}

@MainActor
Expand Down
60 changes: 31 additions & 29 deletions Mail/Views/Search/SearchThreadsSectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,39 @@ struct SearchThreadsSectionView: View {
let viewModel: SearchViewModel

var body: some View {
Section {
ForEach(viewModel.frozenThreads) { thread in
ThreadCell(thread: thread, density: threadDensity, accentColor: accentColor)
.onTapGesture {
didTapCell(thread: thread)
}
.background(SelectionBackground(
selectionType: viewModel.selectedThread == thread ? .single : .none,
paddingLeading: 4,
withAnimation: false,
accentColor: accentColor
))
.onAppear {
viewModel.loadNextPageIfNeeded(currentItem: thread)
}
}
} header: {
if !viewModel.frozenThreads.isEmpty {
Text(MailResourcesStrings.Localizable.searchAllMessages)
.textStyle(.bodySmallSecondary)
.padding(.horizontal, value: .regular)
}
} footer: {
if viewModel.isLoading {
ProgressView()
.frame(maxWidth: .infinity)
.id(UUID())
.threadListCellAppearance()
if viewModel.searchState == .results {
Section {
ForEach(viewModel.frozenThreads) { thread in
ThreadCell(thread: thread, density: threadDensity, accentColor: accentColor)
.onTapGesture {
didTapCell(thread: thread)
}
.background(SelectionBackground(
selectionType: viewModel.selectedThread == thread ? .single : .none,
paddingLeading: 4,
withAnimation: false,
accentColor: accentColor
))
.onAppear {
viewModel.loadNextPageIfNeeded(currentItem: thread)
}
}
} header: {
if !viewModel.frozenThreads.isEmpty {
Text(MailResourcesStrings.Localizable.searchAllMessages)
.textStyle(.bodySmallSecondary)
.padding(.horizontal, value: .regular)
}
} footer: {
if viewModel.isLoading {
ProgressView()
.frame(maxWidth: .infinity)
.id(UUID())
.threadListCellAppearance()
}
}
.threadListCellAppearance()
}
.threadListCellAppearance()
}

private func didTapCell(thread: Thread) {
Expand Down
9 changes: 3 additions & 6 deletions Mail/Views/Search/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,9 @@ struct SearchView: View {
}

List {
if viewModel.searchState == .history {
SearchHistorySectionView(viewModel: viewModel)
} else if viewModel.searchState == .results {
SearchContactsSectionView(viewModel: viewModel)
SearchThreadsSectionView(viewModel: viewModel)
}
SearchHistorySectionView(viewModel: viewModel)
SearchContactsSectionView(viewModel: viewModel)
SearchThreadsSectionView(viewModel: viewModel)
}
.listStyle(.plain)
}
Expand Down

0 comments on commit ce31400

Please sign in to comment.