forked from scribe-org/Scribe-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslationLanguagePickerView.swift
More file actions
197 lines (170 loc) · 7.41 KB
/
Copy pathTranslationLanguagePickerView.swift
File metadata and controls
197 lines (170 loc) · 7.41 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
struct TranslationLanguagePickerView: View {
// MARK: Properties
let tableData: [ParentTableCellModel]
let parentSection: Scribe.Section?
let langCode: String
@AppStorage(
"increaseTextSize", store: UserDefaults(suiteName: "group.be.scri.userDefaultsContainer")
)
var increaseTextSize: Bool = false
private var textSizeMultiplier: CGFloat {
increaseTextSize ? 1.25 : 1.0
}
private let userDefaults = UserDefaults(suiteName: "group.be.scri.userDefaultsContainer")!
@State private var selectedLang: String = "en"
@State private var showConfirmation = false
@State private var pendingNewLang: String = ""
// MARK: Body
var body: some View {
List {
ForEach(Array(tableData.enumerated()), id: \.offset) { sectionIndex, sectionModel in
SwiftUI.Section {
ForEach(Array(sectionModel.section.enumerated()), id: \.offset) { _, item in
let itemLang = extractLangCode(from: item)
languageRow(item: item, itemLang: itemLang)
}
} header: {
if !sectionModel.headingTitle.isEmpty {
Text(sectionModel.headingTitle)
.font(.system(size: (DeviceType.isPad ? 18 : 14) * textSizeMultiplier, weight: .bold))
.foregroundColor(Color(UIColor(ScribeColor.keyChar)))
.textCase(nil)
}
}
}
}
.listStyle(.insetGrouped)
.onAppear {
selectedLang = userDefaults.string(forKey: langCode + "TranslateLanguage") ?? "en"
}
.fullScreenCover(isPresented: $showConfirmation) {
confirmationPopup()
}
.navigationTitle(NSLocalizedString("i18n.app.settings.keyboard.translation.select_source.title", value: "Translation language", comment: ""))
}
// MARK: Language Row
@ViewBuilder
private func languageRow(item: Scribe.Section, itemLang: String) -> some View {
Button {
handleSelection(newLang: itemLang)
} label: {
HStack {
Image(selectedLang == itemLang ? "radioButtonSelected" : "radioButton")
.resizable()
.frame(width: DeviceType.isPad ? 28 : 22, height: DeviceType.isPad ? 28 : 22)
Text(item.sectionTitle)
.font(.system(size: (DeviceType.isPad ? fontSize * 1.5 : fontSize) * textSizeMultiplier))
.foregroundColor(Color(UIColor(ScribeColor.keyChar)))
Spacer()
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.listRowBackground(Color(UIColor(ScribeColor.lightWhiteDarkBlack)))
}
// MARK: Selection Handler
private func handleSelection(newLang: String) {
let oldLang = selectedLang
if newLang != oldLang {
pendingNewLang = newLang
// We temporarily don't update selectedLang so we can revert if cancelled.
showConfirmation = true
}
}
// MARK: Confirmation Popup
@ViewBuilder
private func confirmationPopup() -> some View {
let oldLang = userDefaults.string(forKey: langCode + "TranslateLanguage") ?? "en"
let oldSourceLanguage = getKeyInDict(givenValue: oldLang, dict: languagesAbbrDict)
let newSourceLanguage = getKeyInDict(givenValue: pendingNewLang, dict: languagesAbbrDict)
let localizedOldSourceLanguage = NSLocalizedString(
"i18n.app._global." + oldSourceLanguage.lowercased(),
value: oldSourceLanguage,
comment: ""
)
let localizedNewSourceLanguage = NSLocalizedString(
"i18n.app._global." + newSourceLanguage.lowercased(),
value: newSourceLanguage,
comment: ""
)
let infoText = NSLocalizedString(
"i18n.app.settings.keyboard.translation.change_source_tooltip.download_warning",
value: "You've changed your source translation language. Would you like to download new data so that you can translate from {source_language}?",
comment: ""
).replacingOccurrences(of: "{source_language}", with: localizedNewSourceLanguage)
let changeButtonText = NSLocalizedString(
"i18n.app.settings.keyboard.translation.change_source_tooltip.keep_source_language",
value: "Keep {source_language}", comment: ""
).replacingOccurrences(of: "{source_language}", with: localizedOldSourceLanguage)
let confirmButtonText = NSLocalizedString(
"i18n.app._global.download_data", value: "Download data", comment: ""
)
ConfirmTranslationSource(
infoText: infoText,
changeButtonText: changeButtonText,
confirmButtonText: confirmButtonText,
onDismiss: { showConfirmation = false },
onChange: { showConfirmation = false },
onConfirm: { confirmDownload() }
)
.background(BackgroundClearView())
}
// MARK: Actions
private func confirmDownload() {
showConfirmation = false
let dictionaryKey = langCode + "TranslateLanguage"
userDefaults.setValue(pendingNewLang, forKey: dictionaryKey)
selectedLang = pendingNewLang
DownloadStateManager.shared.handleDownloadAction(key: langCode, forceDownload: true)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
navigateToDownloadScreen()
}
}
private func navigateToDownloadScreen() {
guard
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.windows.first,
let tabBarController = window.rootViewController as? UITabBarController,
let installationNavController = tabBarController.viewControllers?[0] as? UINavigationController
else { return }
if let downloadScreen = installationNavController.viewControllers.first(where: {
$0 is UIHostingController<DownloadDataScreen>
}) {
tabBarController.selectedIndex = 0
installationNavController.popToViewController(downloadScreen, animated: true)
} else {
tabBarController.selectedIndex = 0
installationNavController.popToRootViewController(animated: false)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
NotificationCenter.default.post(
name: NSNotification.Name("NavigateToDownloadScreen"),
object: nil
)
}
}
}
private func extractLangCode(from section: Scribe.Section) -> String {
if case let .specificLang(lang) = section.sectionState {
return lang
}
return "n/a"
}
}
private struct BackgroundClearView: UIViewRepresentable {
func makeUIView(context _: Context) -> UIView {
let view = InnerView()
DispatchQueue.main.async {
view.superview?.superview?.backgroundColor = .clear
}
return view
}
func updateUIView(_: UIView, context _: Context) {}
private class InnerView: UIView {
override func didMoveToWindow() {
super.didMoveToWindow()
superview?.superview?.backgroundColor = .clear
}
}
}