1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-16 10:48:49 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Toolbar/ComposeContentToolbarView+ViewModel.swift
Jed Fox 0a9689c67f
Add support for selecting the post language (#907)
* Basic fake language picker support

* Recognize languages from post text

* Exclude suggested languages from recents

* Load recent languages from Settings object

* Send the language to the API

* Persist the used language to settings

* Always show the currently selected language in the list

* Fix crash

* Add support for picking arbitrary lanuages

* Fix display of 3 letter language codes

* Improve label to include endonym too

* Limit to 3 recent languages

* Reduce lower bound for displaying language suggestions

* Fix saving recent language when publishing

* Fix tint color of language picker button

* Add a badge to prompt users to change language

* Dismiss the badge even if you pick the same language

* Read language names in the language if possible

* Use a compressed font for 3-letter codes

Also use `minimumScaleFactor` to shrink troublesome codes to fit

Co-Authored-By: samhenrigold <49251320+samhenrigold@users.noreply.github.com>

* Remove .vscode/launch.json

* Add message to fatalError()

Co-authored-by: samhenrigold <49251320+samhenrigold@users.noreply.github.com>
2023-01-24 01:50:10 +01:00

156 lines
5.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ComposeContentToolbarView.swift
//
//
// Created by MainasuK on 22/10/18.
//
import SwiftUI
import MastodonCore
import MastodonAsset
import MastodonLocalization
import MastodonSDK
extension ComposeContentToolbarView {
class ViewModel: ObservableObject {
weak var delegate: ComposeContentToolbarViewDelegate?
// input
@Published var backgroundColor = ThemeService.shared.currentTheme.value.composeToolbarBackgroundColor
@Published var suggestedLanguages: [String] = []
@Published var highConfidenceSuggestedLanguage: String?
@Published var visibility: Mastodon.Entity.Status.Visibility = .public
var allVisibilities: [Mastodon.Entity.Status.Visibility] {
return [.public, .private, .direct]
}
@Published var isPollActive = false
@Published var isEmojiActive = false
@Published var isContentWarningActive = false
@Published var isAttachmentButtonEnabled = false
@Published var isPollButtonEnabled = false
@Published var language = Locale.current.languageCode ?? "en"
@Published var recentLanguages: [String] = []
@Published public var maxTextInputLimit = 500
@Published public var contentWeightedLength = 0
@Published public var contentWarningWeightedLength = 0
// output
init(delegate: ComposeContentToolbarViewDelegate) {
self.delegate = delegate
// end init
ThemeService.shared.currentTheme
.map { $0.composeToolbarBackgroundColor }
.assign(to: &$backgroundColor)
}
}
}
extension ComposeContentToolbarView.ViewModel {
enum Action: CaseIterable {
case attachment
case poll
case emoji
case contentWarning
case visibility
case language
var activeImage: UIImage {
switch self {
case .attachment:
return Asset.Scene.Compose.media.image.withRenderingMode(.alwaysTemplate)
case .poll:
return Asset.Scene.Compose.pollFill.image.withRenderingMode(.alwaysTemplate)
case .emoji:
return Asset.Scene.Compose.emojiFill.image.withRenderingMode(.alwaysTemplate)
case .contentWarning:
return Asset.Scene.Compose.chatWarningFill.image.withRenderingMode(.alwaysTemplate)
case .visibility:
return Asset.Scene.Compose.earth.image.withRenderingMode(.alwaysTemplate)
case .language:
fatalError("Languages active image is never accessed")
}
}
var inactiveImage: UIImage {
switch self {
case .attachment:
return Asset.Scene.Compose.media.image.withRenderingMode(.alwaysTemplate)
case .poll:
return Asset.Scene.Compose.poll.image.withRenderingMode(.alwaysTemplate)
case .emoji:
return Asset.Scene.Compose.emoji.image.withRenderingMode(.alwaysTemplate)
case .contentWarning:
return Asset.Scene.Compose.chatWarning.image.withRenderingMode(.alwaysTemplate)
case .visibility:
return Asset.Scene.Compose.earth.image.withRenderingMode(.alwaysTemplate)
case .language:
fatalError("Languages inactive image is never accessed")
}
}
}
enum AttachmentAction: CaseIterable {
case photoLibrary
case camera
case browse
var title: String {
switch self {
case .photoLibrary: return L10n.Scene.Compose.MediaSelection.photoLibrary
case .camera: return L10n.Scene.Compose.MediaSelection.camera
case .browse: return L10n.Scene.Compose.MediaSelection.browse
}
}
var image: UIImage {
switch self {
case .photoLibrary: return UIImage(systemName: "photo.on.rectangle")!
case .camera: return UIImage(systemName: "camera")!
case .browse: return UIImage(systemName: "ellipsis")!
}
}
}
}
extension ComposeContentToolbarView.ViewModel {
func image(for action: Action) -> UIImage {
switch action {
case .poll:
return isPollActive ? action.activeImage : action.inactiveImage
case .emoji:
return isEmojiActive ? action.activeImage : action.inactiveImage
case .contentWarning:
return isContentWarningActive ? action.activeImage : action.inactiveImage
case .language:
fatalError("Languages image is never accessed")
default:
return action.inactiveImage
}
}
func label(for action: Action) -> String {
switch action {
case .attachment:
return L10n.Scene.Compose.Accessibility.appendAttachment
case .poll:
return isPollActive ? L10n.Scene.Compose.Accessibility.removePoll : L10n.Scene.Compose.Accessibility.appendPoll
case .emoji:
return L10n.Scene.Compose.Accessibility.customEmojiPicker
case .contentWarning:
return isContentWarningActive ? L10n.Scene.Compose.Accessibility.disableContentWarning : L10n.Scene.Compose.Accessibility.enableContentWarning
case .visibility:
return L10n.Scene.Compose.Accessibility.postVisibilityMenu
case .language:
return "[[language]]"
}
}
}