IceCubesApp/IceCubesApp/App/Tabs/Settings/DisplaySettingsView.swift

230 lines
8.3 KiB
Swift
Raw Normal View History

2023-03-13 12:38:28 +00:00
import Combine
2023-01-06 16:14:34 +00:00
import DesignSystem
2023-01-22 05:38:30 +00:00
import Env
2023-01-17 10:36:01 +00:00
import Models
2023-02-18 06:26:48 +00:00
import Network
2023-01-06 16:14:34 +00:00
import Status
2023-01-17 10:36:01 +00:00
import SwiftUI
class DisplaySettingsLocalColors: ObservableObject {
@Published var tintColor = Theme.shared.tintColor
@Published var primaryBackgroundColor = Theme.shared.primaryBackgroundColor
@Published var secondaryBackgroundColor = Theme.shared.secondaryBackgroundColor
@Published var labelColor = Theme.shared.labelColor
2023-03-13 12:38:28 +00:00
private var subscriptions = Set<AnyCancellable>()
2023-03-13 12:38:28 +00:00
init() {
$tintColor
.debounce(for: .seconds(0.5), scheduler: DispatchQueue.main)
2023-03-13 12:38:28 +00:00
.sink(receiveValue: { newColor in Theme.shared.tintColor = newColor })
.store(in: &subscriptions)
$primaryBackgroundColor
.debounce(for: .seconds(0.5), scheduler: DispatchQueue.main)
2023-03-13 12:38:28 +00:00
.sink(receiveValue: { newColor in Theme.shared.primaryBackgroundColor = newColor })
.store(in: &subscriptions)
$secondaryBackgroundColor
.debounce(for: .seconds(0.5), scheduler: DispatchQueue.main)
2023-03-13 12:38:28 +00:00
.sink(receiveValue: { newColor in Theme.shared.secondaryBackgroundColor = newColor })
.store(in: &subscriptions)
$labelColor
.debounce(for: .seconds(0.5), scheduler: DispatchQueue.main)
2023-03-13 12:38:28 +00:00
.sink(receiveValue: { newColor in Theme.shared.labelColor = newColor })
.store(in: &subscriptions)
}
}
2023-01-06 16:14:34 +00:00
struct DisplaySettingsView: View {
2023-01-30 06:27:06 +00:00
typealias FontState = Theme.FontState
2023-01-22 15:59:56 +00:00
@Environment(\.colorScheme) private var colorScheme
2023-01-06 16:14:34 +00:00
@EnvironmentObject private var theme: Theme
2023-01-17 20:08:05 +00:00
@EnvironmentObject private var userPreferences: UserPreferences
2023-03-13 12:38:28 +00:00
@StateObject private var localColors = DisplaySettingsLocalColors()
2023-01-30 06:27:06 +00:00
@State private var isFontSelectorPresented = false
2023-03-13 12:38:28 +00:00
private let previewStatusViewModel = StatusRowViewModel(status: Status.placeholder(forSettings: true, language: "la"),
client: Client(server: ""),
routerPath: RouterPath()) // translate from latin button
2023-03-13 12:38:28 +00:00
2023-01-06 16:14:34 +00:00
var body: some View {
Form {
2023-03-03 07:29:45 +00:00
exampleSection
themeSection
fontSection
layoutSection
platformsSection
resetSection
}
.navigationTitle("settings.display.navigation-title")
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
}
2023-03-13 12:38:28 +00:00
2023-03-03 07:29:45 +00:00
private var exampleSection: some View {
Section("settings.display.example-toot") {
StatusRowView(viewModel: { previewStatusViewModel })
.allowsHitTesting(false)
}
}
2023-03-13 12:38:28 +00:00
2023-03-03 07:29:45 +00:00
private var themeSection: some View {
Section {
Toggle("settings.display.theme.systemColor", isOn: $theme.followSystemColorScheme)
themeSelectorButton
Group {
ColorPicker("settings.display.theme.tint", selection: $localColors.tintColor)
ColorPicker("settings.display.theme.background", selection: $localColors.primaryBackgroundColor)
ColorPicker("settings.display.theme.secondary-background", selection: $localColors.secondaryBackgroundColor)
ColorPicker("settings.display.theme.text-color", selection: $localColors.labelColor)
}
2023-03-03 07:29:45 +00:00
.disabled(theme.followSystemColorScheme)
.opacity(theme.followSystemColorScheme ? 0.5 : 1.0)
.onChange(of: theme.selectedSet) { _ in
localColors.tintColor = theme.tintColor
localColors.primaryBackgroundColor = theme.primaryBackgroundColor
localColors.secondaryBackgroundColor = theme.secondaryBackgroundColor
localColors.labelColor = theme.labelColor
2023-03-03 07:29:45 +00:00
}
} header: {
Text("settings.display.section.theme")
} footer: {
if theme.followSystemColorScheme {
Text("settings.display.section.theme.footer")
}
}
.listRowBackground(theme.primaryBackgroundColor)
}
2023-03-13 12:38:28 +00:00
2023-03-03 07:29:45 +00:00
private var fontSection: some View {
Section("settings.display.section.font") {
Picker("settings.display.font", selection: .init(get: { () -> FontState in
if theme.chosenFont?.fontName == "OpenDyslexic-Regular" {
return FontState.openDyslexic
} else if theme.chosenFont?.fontName == "AtkinsonHyperlegible-Regular" {
return FontState.hyperLegible
} else if theme.chosenFont?.fontName == ".AppleSystemUIFontRounded-Regular" {
return FontState.SFRounded
}
return theme.chosenFontData != nil ? FontState.custom : FontState.system
}, set: { newValue in
switch newValue {
case .system:
theme.chosenFont = nil
case .openDyslexic:
theme.chosenFont = UIFont(name: "OpenDyslexic", size: 1)
case .hyperLegible:
theme.chosenFont = UIFont(name: "Atkinson Hyperlegible", size: 1)
case .SFRounded:
theme.chosenFont = UIFont.systemFont(ofSize: 1).rounded()
case .custom:
isFontSelectorPresented = true
}
})) {
ForEach(FontState.allCases, id: \.rawValue) { fontState in
Text(fontState.title).tag(fontState)
}
}
2023-03-03 07:29:45 +00:00
.navigationDestination(isPresented: $isFontSelectorPresented, destination: { FontPicker() })
2023-02-18 06:26:48 +00:00
2023-03-03 07:29:45 +00:00
VStack {
Slider(value: $theme.fontSizeScale, in: 0.5 ... 1.5, step: 0.1)
Text("settings.display.font.scaling-\(String(format: "%.1f", theme.fontSizeScale))")
.font(.scaledBody)
}
.alignmentGuide(.listRowSeparatorLeading) { d in
d[.leading]
}
VStack {
Slider(value: $theme.lineSpacing, in: 0.4 ... 10.0, step: 0.2)
Text("settings.display.font.line-spacing-\(String(format: "%.1f", theme.lineSpacing))")
.font(.scaledBody)
}
.alignmentGuide(.listRowSeparatorLeading) { d in
d[.leading]
}
2023-03-03 07:29:45 +00:00
}
.listRowBackground(theme.primaryBackgroundColor)
}
2023-03-13 12:38:28 +00:00
2023-03-03 07:29:45 +00:00
private var layoutSection: some View {
Section("settings.display.section.display") {
Picker("settings.display.avatar.position", selection: $theme.avatarPosition) {
ForEach(Theme.AvatarPosition.allCases, id: \.rawValue) { position in
Text(position.description).tag(position)
2023-01-06 16:14:34 +00:00
}
2023-03-03 07:29:45 +00:00
}
Picker("settings.display.avatar.shape", selection: $theme.avatarShape) {
ForEach(Theme.AvatarShape.allCases, id: \.rawValue) { shape in
Text(shape.description).tag(shape)
2023-01-07 16:44:25 +00:00
}
2023-01-06 16:14:34 +00:00
}
2023-03-03 07:29:45 +00:00
Toggle("settings.display.full-username", isOn: $theme.displayFullUsername)
Picker("settings.display.status.action-buttons", selection: $theme.statusActionsDisplay) {
ForEach(Theme.StatusActionsDisplay.allCases, id: \.rawValue) { buttonStyle in
Text(buttonStyle.description).tag(buttonStyle)
}
}
2023-03-03 07:29:45 +00:00
Picker("settings.display.status.media-style", selection: $theme.statusDisplayStyle) {
ForEach(Theme.StatusDisplayStyle.allCases, id: \.rawValue) { buttonStyle in
Text(buttonStyle.description).tag(buttonStyle)
}
}
2023-03-03 07:29:45 +00:00
Toggle("settings.display.translate-button", isOn: $userPreferences.showTranslateButton)
}
.listRowBackground(theme.primaryBackgroundColor)
}
2023-03-13 12:38:28 +00:00
2023-03-03 07:29:45 +00:00
@ViewBuilder
private var platformsSection: some View {
if UIDevice.current.userInterfaceIdiom == .phone {
Section("iPhone") {
Toggle("settings.display.show-tab-label", isOn: $userPreferences.showiPhoneTabLabel)
}
.listRowBackground(theme.primaryBackgroundColor)
}
2023-03-03 07:29:45 +00:00
if UIDevice.current.userInterfaceIdiom == .pad {
Section("iPad") {
Toggle("settings.display.show-ipad-column", isOn: $userPreferences.showiPadSecondaryColumn)
2023-01-06 16:14:34 +00:00
}
.listRowBackground(theme.primaryBackgroundColor)
}
2023-03-03 07:29:45 +00:00
}
2023-03-13 12:38:28 +00:00
2023-03-03 07:29:45 +00:00
private var resetSection: some View {
Section {
Button {
theme.followSystemColorScheme = true
theme.selectedSet = colorScheme == .dark ? .iceCubeDark : .iceCubeLight
theme.avatarShape = .rounded
theme.avatarPosition = .top
theme.statusActionsDisplay = .full
2023-03-13 12:38:28 +00:00
localColors.tintColor = theme.tintColor
localColors.primaryBackgroundColor = theme.primaryBackgroundColor
localColors.secondaryBackgroundColor = theme.secondaryBackgroundColor
localColors.labelColor = theme.labelColor
2023-03-13 12:38:28 +00:00
2023-03-03 07:29:45 +00:00
} label: {
Text("settings.display.restore")
}
}
.listRowBackground(theme.primaryBackgroundColor)
2023-01-06 16:14:34 +00:00
}
2023-01-06 16:14:34 +00:00
private var themeSelectorButton: some View {
NavigationLink(destination: ThemePreviewView()) {
HStack {
Text("settings.display.section.theme")
2023-01-06 16:14:34 +00:00
Spacer()
Text(theme.selectedSet.rawValue)
}
}
}
}