Files
damus/damus/Views/SearchHomeView.swift
William Casarin f2ce146e98 Revert "Refactor auto-translations and add caching"
There are quite a few issues with this and is causing crashing

This reverts commit ae82114a33.
2023-03-31 10:01:31 -07:00

130 lines
3.8 KiB
Swift

//
// SearchHomeView.swift
// damus
//
// Created by William Casarin on 2022-05-19.
//
import SwiftUI
import CryptoKit
import NaturalLanguage
struct SearchHomeView: View {
let damus_state: DamusState
@StateObject var model: SearchHomeModel
@State var search: String = ""
@FocusState private var isFocused: Bool
let preferredLanguages = Set(Locale.preferredLanguages.map { localeToLanguage($0) })
var SearchInput: some View {
HStack {
HStack{
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
TextField(NSLocalizedString("Search...", comment: "Placeholder text to prompt entry of search query."), text: $search)
.autocorrectionDisabled(true)
.textInputAutocapitalization(.never)
.focused($isFocused)
}
.padding(10)
.background(.secondary.opacity(0.2))
.cornerRadius(20)
if(!search.isEmpty) {
Text("Cancel", comment: "Cancel out of search view.")
.foregroundStyle(LINEAR_GRADIENT)
.padding(EdgeInsets(top: 0.0, leading: 0.0, bottom: 0.0, trailing: 10.0))
.onTapGesture {
self.search = ""
isFocused = false
}
}
}
}
var GlobalContent: some View {
return TimelineView(
events: model.events,
loading: $model.loading,
damus: damus_state,
show_friend_icon: true,
filter: {
if damus_state.settings.show_only_preferred_languages == false {
return true
}
// If we can't determine the note's language with 50%+ confidence, lean on the side of caution and show it anyway.
guard let noteLanguage = $0.note_language(damus_state.keypair.privkey) else {
return true
}
return preferredLanguages.contains(noteLanguage)
}
)
.refreshable {
// Fetch new information by unsubscribing and resubscribing to the relay
model.unsubscribe()
model.subscribe()
}
}
var SearchContent: some View {
SearchResultsView(damus_state: damus_state, search: $search)
.refreshable {
// Fetch new information by unsubscribing and resubscribing to the relay
model.unsubscribe()
model.subscribe()
}
}
var MainContent: some View {
Group {
if search.isEmpty {
GlobalContent
} else {
SearchContent
}
}
}
@Environment(\.colorScheme) var colorScheme
var body: some View {
VStack {
MainContent
}
.safeAreaInset(edge: .top, spacing: 0) {
VStack(spacing: 0) {
SearchInput
//.frame(maxWidth: 275)
.padding()
Divider()
.frame(height: 1)
}
.background(colorScheme == .dark ? Color.black : Color.white)
}
.onReceive(handle_notify(.new_mutes)) { _ in
self.model.filter_muted()
}
.onAppear {
if model.events.events.isEmpty {
model.subscribe()
}
}
.onDisappear {
model.unsubscribe()
}
}
}
struct SearchHomeView_Previews: PreviewProvider {
static var previews: some View {
let state = test_damus_state()
SearchHomeView(
damus_state: state,
model: SearchHomeModel(damus_state: state)
)
}
}