Skip to content
Snippets Groups Projects
Commit 2c3be5a2 authored by cruxfilm's avatar cruxfilm
Browse files

Simplified code for SearchType and related Picker significantly, keeping functionality identical.

parent 7b4bd92e
No related branches found
No related tags found
1 merge request!76Resolve "InboxView & MailListView improvements"
...@@ -22,8 +22,8 @@ struct MailListView: View { ...@@ -22,8 +22,8 @@ struct MailListView: View {
@State var updating = false @State var updating = false
@State var showUser = false @State var showUser = false
@State var searchText = "" @State var searchText = ""
@State var searchField = 0 @State var searchField = SearchType.All
@State var searchNow = false // @State var searchNow = false
@State var composeMail = false @State var composeMail = false
init(folderpath: String, name: String) { init(folderpath: String, name: String) {
...@@ -44,7 +44,7 @@ struct MailListView: View { ...@@ -44,7 +44,7 @@ struct MailListView: View {
private var mainView: some View { private var mainView: some View {
VStack (alignment: .leading){ VStack (alignment: .leading){
SearchView(searchText: $searchText, searchField: $searchField, searchNow: $searchNow) SearchView(searchText: $searchText, searchType: $searchField)
.padding(6) .padding(6)
mailList mailList
.padding(-12) .padding(-12)
...@@ -113,7 +113,8 @@ struct MailListView: View { ...@@ -113,7 +113,8 @@ struct MailListView: View {
} }
func filterKeyRecord(keyRecord: MailRecord) -> Bool { func filterKeyRecord(keyRecord: MailRecord) -> Bool {
let searchType = SearchType.findType(i: searchField) // let searchType = SearchType.findType(i: searchField)
let searchType = searchField
if self.searchText.isEmpty if self.searchText.isEmpty
|| self.searchText == NSLocalizedString("Searchbar.Title", comment: "Search") { || self.searchText == NSLocalizedString("Searchbar.Title", comment: "Search") {
return true return true
......
...@@ -12,30 +12,11 @@ import Combine ...@@ -12,30 +12,11 @@ import Combine
/** /**
Where are we looking for? Used in the search footer. Where are we looking for? Used in the search footer.
*/ */
enum SearchType: CaseIterable { enum SearchType: LocalizedStringKey, CaseIterable {
case Sender, Subject, Body, All case All = "All"
var text: String { case Sender = "Sender"
get{ case Subject = "Subject"
switch self { case Body = "Body"
case .Sender:
return NSLocalizedString("Sender", comment: "")
case .Subject:
return NSLocalizedString("Subject", comment: "")
case .Body:
return NSLocalizedString("Body", comment: "")
case .All:
return NSLocalizedString("All", comment: "")
}
}
}
static func findType(i: Int) -> SearchType{
if i < SearchType.allCases.count {
return SearchType.allCases[i]
} else {
return SearchType.All
}
}
} }
/** /**
A SearchView for mails with a search segmented Picker to choose the search area. A SearchView for mails with a search segmented Picker to choose the search area.
...@@ -43,29 +24,26 @@ enum SearchType: CaseIterable { ...@@ -43,29 +24,26 @@ enum SearchType: CaseIterable {
*/ */
struct SearchView: View { struct SearchView: View {
@Binding var searchText: String @Binding var searchText: String
@Binding var searchField: Int @Binding var searchType: SearchType
@Binding var searchNow: Bool
@State private var showCancelButton: Bool = false @State private var showCancelButton: Bool = false
var body: some View { var body: some View {
VStack{ VStack {
HStack { HStack {
searchFieldView searchFieldView
if showCancelButton { if showCancelButton {
Button("Cancel") { Button("Cancel") {
UIApplication.shared.endEditing(true) UIApplication.shared.endEditing(true)
self.searchText = "" self.searchText = ""
self.showCancelButton = false self.showCancelButton = false
self.searchNow = false
} }
.foregroundColor(Color(.systemBlue)) .foregroundColor(Color(.systemBlue))
} }
} }
if showCancelButton {
searchSegment if showCancelButton {
searchTypePicker
} }
} }
.padding(.horizontal) .padding(.horizontal)
...@@ -74,16 +52,17 @@ struct SearchView: View { ...@@ -74,16 +52,17 @@ struct SearchView: View {
var searchFieldView: some View { var searchFieldView: some View {
HStack { HStack {
Image(systemName: "magnifyingglass") Image(systemName: "magnifyingglass")
TextField(NSLocalizedString("Searchbar.Title", comment: "Search"), text: $searchText, onEditingChanged: { isEditing in TextField(NSLocalizedString("Searchbar.Title", comment: "Search"),
text: $searchText) { _ in
self.showCancelButton = true self.showCancelButton = true
}, onCommit: { }
self.searchNow = true .foregroundColor(.primary)
})
.foregroundColor(.primary) Button {
Button(action: {
self.searchText = "" self.searchText = ""
}) { } label: {
Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1) Image(systemName: "xmark.circle.fill")
.opacity(searchText == "" ? 0 : 1)
} }
} }
.padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6)) .padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6))
...@@ -92,13 +71,10 @@ struct SearchView: View { ...@@ -92,13 +71,10 @@ struct SearchView: View {
.cornerRadius(10.0) .cornerRadius(10.0)
} }
var searchSegment: some View { var searchTypePicker: some View {
Picker(selection: $searchField, label: EmptyView()) { Picker("Hellooo", selection: $searchType) {
ForEach(0..<SearchType.allCases.count) { index in ForEach(SearchType.allCases, id: \.self) { searchType in
Text(SearchType.allCases[index].text).tag(index) Text(searchType.rawValue)
.onTapGesture {
self.searchField = index
}
} }
} }
.pickerStyle(SegmentedPickerStyle()) .pickerStyle(SegmentedPickerStyle())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment