-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslateScreenPage.swift
More file actions
132 lines (119 loc) · 5.1 KB
/
TranslateScreenPage.swift
File metadata and controls
132 lines (119 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import SwiftUI
struct TranslateScreenView: View {
@State private var fromLanguage = "English"
@State private var toLanguage = "Turkish"
@State private var inputText = ""
@State private var translatedText = ""
@State private var isTranslating = false
private let languages = ["English", "Spanish", "French", "German", "Italian", "Japanese", "Chinese", "Arabic", "Turkish", "Dutch"]
private let apiManager = APIManager()
@State private var durationTime = 0.3
@Binding var showMainMenu: Bool
var body: some View {
VStack(spacing: 20) {
HStack {
Button(action: {
withAnimation(.easeInOut(duration: durationTime)) {
showMainMenu = true
}
}) {
Image(systemName: "arrow.left.circle.fill")
.resizable()
.frame(width: 40, height: 40)
.foregroundColor(.blue)
}
Spacer()
}
.padding(.leading, 20)
.padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)
Spacer()
.frame(height: 130) // Adjust this value to decrease the space
HStack {
Picker("From", selection: $fromLanguage) {
ForEach(languages, id: \.self) {
Text($0)
}
}
.pickerStyle(MenuPickerStyle())
.frame(width: 150)
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
Button(action: {
swap(&fromLanguage, &toLanguage)
}) {
Image(systemName: "arrow.left.arrow.right")
.foregroundColor(.white)
.padding(.horizontal)
}
Picker("To", selection: $toLanguage) {
ForEach(languages, id: \.self) {
Text($0)
}
}
.pickerStyle(MenuPickerStyle())
.frame(width: 150)
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
}
.padding(.horizontal)
GeometryReader { geometry in
VStack(spacing: 20) {
ZStack(alignment: .leading) {
if inputText.isEmpty {
Text("Enter text to translate")
.foregroundColor(.white.opacity(0.7))
.padding(.horizontal, 36) // Adjusted padding to move text to the right
}
TextField("", text: $inputText)
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
.padding(.horizontal)
.foregroundColor(.white) // Set the text color to white
}
.frame(width: geometry.size.width - 40) // Set width based on parent view's size
Button(action: translate) {
Text(isTranslating ? "Translating..." : "Translate")
.foregroundColor(.white)
.padding()
.background(Color.blue)
.cornerRadius(10)
}
.padding(.top, 20)
VStack(alignment: .leading, spacing: 10) {
Text("Translated Text:")
.font(.custom("AvenirNext-Bold", size: 20))
.foregroundColor(.white)
.padding(.top, 20)
Text(translatedText)
.font(.custom("AvenirNext-Regular", size: 18))
.foregroundColor(.white)
.padding()
.frame(maxWidth: .infinity)
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
.padding(.horizontal)
.frame(width: geometry.size.width - 40) // Set width based on parent view's size
}
.padding(.horizontal, 20)
}
}
Spacer()
}
.background(Color.black.edgesIgnoringSafeArea(.all))
}
private func translate() {
isTranslating = true
apiManager.translateText(from: fromLanguage, to: toLanguage, text: inputText) { result in
DispatchQueue.main.async {
self.translatedText = result ?? "Translation failed"
self.isTranslating = false
}
}
}
}
struct TranslateScreenView_Previews: PreviewProvider {
static var previews: some View {
TranslateScreenView(showMainMenu: .constant(false))
}
}