refactor: move translate button into its own view

This commit is contained in:
William Casarin
2023-02-02 13:53:57 -08:00
parent 7d406fd75f
commit 1fd4d4d950

View File

@@ -87,29 +87,21 @@ struct NoteContentView: View {
@EnvironmentObject var user_settings: UserSettingsStore
func MainContent() -> some View {
return VStack(alignment: .leading) {
Text(artifacts.content)
.font(eventviewsize_to_font(size))
.fixedSize(horizontal: false, vertical: true)
if size == .selected && noteLanguage != nil && noteLanguage != currentLanguage {
var TranslateButton: some View {
Group {
let languageName = Locale.current.localizedString(forLanguageCode: noteLanguage!)
if show_translated_note {
if checkingTranslationStatus {
Button(NSLocalizedString("Translating from \(languageName!)...", comment: "Button to indicate that the note is in the process of being translated from a different language.")) {
show_translated_note = false
}
.font(.footnote)
.contentShape(Rectangle())
.padding(.top, 10)
.translate_button_style()
} else if translated_artifacts != nil {
Button(NSLocalizedString("Translated from \(languageName!)", comment: "Button to indicate that the note has been translated from a different language.")) {
show_translated_note = false
}
.font(.footnote)
.contentShape(Rectangle())
.padding(.top, 10)
.translate_button_style()
Text(translated_artifacts!.content)
.font(eventviewsize_to_font(size))
@@ -119,11 +111,20 @@ struct NoteContentView: View {
Button(NSLocalizedString("Translate Note", comment: "Button to translate note from different language.")) {
show_translated_note = true
}
.font(.footnote)
.contentShape(Rectangle())
.padding(.top, 10)
.translate_button_style()
}
}
}
func MainContent() -> some View {
return VStack(alignment: .leading) {
Text(artifacts.content)
.font(eventviewsize_to_font(size))
.fixedSize(horizontal: false, vertical: true)
if size == .selected && noteLanguage != nil && noteLanguage != currentLanguage {
TranslateButton
}
if show_images && artifacts.images.count > 0 {
ImageCarousel(urls: artifacts.images)
@@ -374,3 +375,13 @@ struct NoteContentView_Previews: PreviewProvider {
NoteContentView(privkey: "", event: NostrEvent(content: content, pubkey: "pk"), profiles: state.profiles, previews: PreviewCache(), show_images: true, artifacts: artifacts, size: .normal)
}
}
extension View {
func translate_button_style() -> some View {
return self
.font(.footnote)
.contentShape(Rectangle())
.padding([.top, .bottom], 10)
}
}