refactor: create search_profiles helper

This commit is contained in:
William Casarin
2023-03-14 17:10:43 -06:00
parent 04bce34297
commit 5d441d3192

View File

@@ -73,10 +73,10 @@ struct SearchResultsView: View {
MainContent MainContent
.frame(maxHeight: .infinity) .frame(maxHeight: .infinity)
.onAppear { .onAppear {
self.result = search_changed(profiles: damus_state.profiles, search) self.result = search_for_string(profiles: damus_state.profiles, search)
} }
.onChange(of: search) { new in .onChange(of: search) { new in
self.result = search_changed(profiles: damus_state.profiles, new) self.result = search_for_string(profiles: damus_state.profiles, new)
} }
} }
} }
@@ -90,7 +90,7 @@ struct SearchResultsView_Previews: PreviewProvider {
*/ */
func search_changed(profiles: Profiles, _ new: String) -> Search? { func search_for_string(profiles: Profiles, _ new: String) -> Search? {
guard new.count != 0 else { guard new.count != 0 else {
return nil return nil
} }
@@ -116,8 +116,11 @@ func search_changed(profiles: Profiles, _ new: String) -> Search? {
} }
} }
let profs = profiles.profiles.enumerated() return .profiles(search_profiles(profiles: profiles, search: new))
let results: [(String, Profile)] = profs.reduce(into: []) { acc, els in }
func search_profiles(profiles: Profiles, search new: String) -> [(String, Profile)] {
return profiles.profiles.enumerated().reduce(into: []) { acc, els in
let pk = els.element.key let pk = els.element.key
let prof = els.element.value.profile let prof = els.element.value.profile
let lowname = prof.name.map { $0.lowercased() } let lowname = prof.name.map { $0.lowercased() }
@@ -134,6 +137,4 @@ func search_changed(profiles: Profiles, _ new: String) -> Search? {
acc.append((pk, prof)) acc.append((pk, prof))
} }
} }
return .profiles(results)
} }