Swiftformat

This commit is contained in:
Thomas Ricouard 2023-02-12 18:24:09 +01:00
parent 35c694b06c
commit ef37db496c
6 changed files with 107 additions and 104 deletions

View file

@ -21,7 +21,7 @@ public struct StatusEditorView: View {
@FocusState private var isSpoilerTextFocused: Bool
@State private var isDismissAlertPresented: Bool = false
@State private var isLanguageConfirmPresented = false
@State private var isLanguageConfirmPresented = false
public init(mode: StatusEditorViewModel.Mode) {
_viewModel = StateObject(wrappedValue: .init(mode: mode))
@ -105,12 +105,12 @@ public struct StatusEditorView: View {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
Task {
viewModel.evaluateLanguages()
if let _ = viewModel.languageConfirmationDialogLanguages {
isLanguageConfirmPresented = true
} else {
await postStatus()
}
viewModel.evaluateLanguages()
if let _ = viewModel.languageConfirmationDialogLanguages {
isLanguageConfirmPresented = true
} else {
await postStatus()
}
}
} label: {
if viewModel.isPosting {
@ -122,7 +122,7 @@ public struct StatusEditorView: View {
.disabled(!viewModel.canPost)
.keyboardShortcut(.return, modifiers: .command)
.confirmationDialog("", isPresented: $isLanguageConfirmPresented, actions: {
languageConfirmationDialog
languageConfirmationDialog
})
}
ToolbarItem(placement: .navigationBarLeading) {
@ -160,41 +160,42 @@ public struct StatusEditorView: View {
.interactiveDismissDisabled(!viewModel.statusText.string.isEmpty)
}
@ViewBuilder
private var languageConfirmationDialog: some View {
if let dialogVals = viewModel.languageConfirmationDialogLanguages,
let detected = dialogVals["detected"],
let detectedLong = Locale.current.localizedString(forLanguageCode: detected),
let selected = dialogVals["selected"],
let selectedLong = Locale.current.localizedString(forLanguageCode: selected){
Button("status.editor.language-select.confirmation.detected-\(detectedLong)") {
viewModel.selectedLanguage = detected
Task {
await postStatus()
}
}
Button("status.editor.language-select.confirmation.selected-\(selectedLong)") {
viewModel.selectedLanguage = selected
Task {
await postStatus()
}
}
Button("action.cancel", role: .cancel) {
viewModel.languageConfirmationDialogLanguages = nil
}
} else {
EmptyView()
@ViewBuilder
private var languageConfirmationDialog: some View {
if let dialogVals = viewModel.languageConfirmationDialogLanguages,
let detected = dialogVals["detected"],
let detectedLong = Locale.current.localizedString(forLanguageCode: detected),
let selected = dialogVals["selected"],
let selectedLong = Locale.current.localizedString(forLanguageCode: selected)
{
Button("status.editor.language-select.confirmation.detected-\(detectedLong)") {
viewModel.selectedLanguage = detected
Task {
await postStatus()
}
}
private func postStatus() async {
let status = await viewModel.postStatus()
if status != nil {
dismiss()
NotificationCenter.default.post(name: NotificationsName.shareSheetClose,
object: nil)
}
Button("status.editor.language-select.confirmation.selected-\(selectedLong)") {
viewModel.selectedLanguage = selected
Task {
await postStatus()
}
}
Button("action.cancel", role: .cancel) {
viewModel.languageConfirmationDialogLanguages = nil
}
} else {
EmptyView()
}
}
private func postStatus() async {
let status = await viewModel.postStatus()
if status != nil {
dismiss()
NotificationCenter.default.post(name: NotificationsName.shareSheetClose,
object: nil)
}
}
@ViewBuilder
private var spoilerTextView: some View {

View file

@ -142,16 +142,17 @@ public class StatusEditorViewModel: NSObject, ObservableObject {
selectedLanguage = selectedLanguage ?? preference ?? currentAccount?.source?.language
}
func evaluateLanguages(){
if let detectedLang = detectLanguage(text: statusText.string),
let selectedLanguage = selectedLanguage,
selectedLanguage != detectedLang {
languageConfirmationDialogLanguages = ["detected": detectedLang,
"selected": selectedLanguage]
} else {
languageConfirmationDialogLanguages = nil;
}
func evaluateLanguages() {
if let detectedLang = detectLanguage(text: statusText.string),
let selectedLanguage = selectedLanguage,
selectedLanguage != detectedLang
{
languageConfirmationDialogLanguages = ["detected": detectedLang,
"selected": selectedLanguage]
} else {
languageConfirmationDialogLanguages = nil
}
}
func postStatus() async -> Status? {
guard let client else { return nil }

View file

@ -2,33 +2,33 @@ import Foundation
import NaturalLanguage
private func stripToPureLanguage(inText: String) -> String {
let hashtagRegex = try! Regex("#[\\w]*")
let emojiRegex = try! Regex(":\\w*:")
let atRegex = try! Regex("@\\w*")
var resultStr = inText
[hashtagRegex, emojiRegex, atRegex].forEach { regex in
let splitArray = resultStr.split(separator: regex, omittingEmptySubsequences: true)
resultStr = splitArray.joined() as String
}
return resultStr.trimmingCharacters(in: .whitespacesAndNewlines)
let hashtagRegex = try! Regex("#[\\w]*")
let emojiRegex = try! Regex(":\\w*:")
let atRegex = try! Regex("@\\w*")
var resultStr = inText
[hashtagRegex, emojiRegex, atRegex].forEach { regex in
let splitArray = resultStr.split(separator: regex, omittingEmptySubsequences: true)
resultStr = splitArray.joined() as String
}
return resultStr.trimmingCharacters(in: .whitespacesAndNewlines)
}
func detectLanguage(text: String) -> String? {
let recognizer = NLLanguageRecognizer()
let strippedText = stripToPureLanguage(inText: text)
recognizer.processString(strippedText)
let hypotheses = recognizer.languageHypotheses(withMaximum: 1)
// Use the detected language only with >= 85 % confidence
if let (lang, confidence) = hypotheses.first, confidence >= 0.85 {
return lang.rawValue
} else {
return nil
}
let recognizer = NLLanguageRecognizer()
let strippedText = stripToPureLanguage(inText: text)
recognizer.processString(strippedText)
let hypotheses = recognizer.languageHypotheses(withMaximum: 1)
// Use the detected language only with >= 85 % confidence
if let (lang, confidence) = hypotheses.first, confidence >= 0.85 {
return lang.rawValue
} else {
return nil
}
}

View file

@ -83,7 +83,8 @@ struct StatusRowContextMenu: View {
}
} label: {
if let statusLang = viewModel.getStatusLang(),
let languageName = Locale.current.localizedString(forLanguageCode: statusLang) {
let languageName = Locale.current.localizedString(forLanguageCode: statusLang)
{
Label("status.action.translate-from-\(languageName)", systemImage: "captions.bubble")
} else {
Label("status.action.translate", systemImage: "captions.bubble")

View file

@ -345,23 +345,23 @@ public struct StatusRowView: View {
.accessibilityHidden(true)
}
private func shouldShowTranslateButton(status: AnyStatus) -> Bool {
let statusLang = viewModel.getStatusLang()
if let userLang = preferences.serverPreferences?.postLanguage,
preferences.showTranslateButton,
!status.content.asRawText.isEmpty,
viewModel.translation == nil
{
return userLang != statusLang
} else {
return false
}
private func shouldShowTranslateButton(status: AnyStatus) -> Bool {
let statusLang = viewModel.getStatusLang()
if let userLang = preferences.serverPreferences?.postLanguage,
preferences.showTranslateButton,
!status.content.asRawText.isEmpty,
viewModel.translation == nil
{
return userLang != statusLang
} else {
return false
}
}
@ViewBuilder
private func makeTranslateView(status: AnyStatus) -> some View {
if let userLang = preferences.serverPreferences?.postLanguage,
if let userLang = preferences.serverPreferences?.postLanguage,
shouldShowTranslateButton(status: status)
{
Button {
@ -372,13 +372,13 @@ public struct StatusRowView: View {
if viewModel.isLoadingTranslation {
ProgressView()
} else {
if let statusLanguage = viewModel.getStatusLang(),
let languageName = Locale.current.localizedString(forLanguageCode: statusLanguage)
{
Text("status.action.translate-from-\(languageName)")
} else {
Text("status.action.translate")
}
if let statusLanguage = viewModel.getStatusLang(),
let languageName = Locale.current.localizedString(forLanguageCode: statusLanguage)
{
Text("status.action.translate-from-\(languageName)")
} else {
Text("status.action.translate")
}
}
}
.buttonStyle(.borderless)

View file

@ -1,8 +1,8 @@
import Env
import Models
import NaturalLanguage
import Network
import SwiftUI
import NaturalLanguage
import DesignSystem
@ -290,16 +290,16 @@ public class StatusRowViewModel: ObservableObject {
reblogsCount = status.reblog?.reblogsCount ?? status.reblogsCount
repliesCount = status.reblog?.repliesCount ?? status.repliesCount
}
func getStatusLang() -> String? {
status.language
}
func getStatusLang() -> String? {
status.language
}
func translate(userLang: String) async {
await translate(userLang: userLang, sourceLang: getStatusLang())
}
private func translate(userLang: String, sourceLang: String?) async {
private func translate(userLang: String, sourceLang _: String?) async {
guard let client else { return }
do {
withAnimation {