Files
simplex-chat/apps/ios/Shared/Views/UserSettings/ProtocolServerView.swift
Evgeny Poberezkin f5eea018d9 ios: chat themes and wallpapers (#4376)
* ios: wallpapers (#4304)

* ios: wallpapers

* theme selection

* applied theme colors and preset wallpaper

* more places with background

* one more

* accent color

* defaults

* rename

* background

* no change to cell color

* unneeded

* changes

* no global tint

* defaults

* removed unneeded class

* for merging

* ios: wallpapers types (#4325)

* types and api

* divided types per target

* creating directory for wallpapers

* creating wallpaper dir at launch

* ios: wallpapers appearance (#4335)

* appearance

* changes

* refactor

* scale

* lambda to function

---------

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>

* ios: wallpapers user/chat overrides (#4345)

* ios: wallpapers user/chat overrides

* chat overrides

* color picker updates colors correctly

* fix state update

* labels

* background for light theme

* small optimization

* removed commented code

* ios: enhancements to wallpapers (#4361)

* ios: enhancements to wallpapers

* colors for background

* ios: wallpapers import/export (#4362)

* ios: wallpapers import/export

* comment

* ios: wallpapers theme updates (#4365)

* ios: wallpapers theme updates

* group member background

* colors

* profile picture colors

* unneeded

* optimizations, images, state fixes

* fixes

* no editing of title color

* rename Menus and alerts, refactor

* tint applying fix

* fixes

* migration of accent and themes

* fix updating system theme

* migration changes

* limiting color range

* ios: wallpapers rename enum (#4384)

* ios: wallpapers rename enum2 (#4385)

* ios: wallpapers rename enum2

* change

* colors were commented

* fix build and look

---------

Co-authored-by: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com>
2024-07-03 22:42:13 +01:00

193 lines
6.2 KiB
Swift

//
// SMPServerView.swift
// SimpleX (iOS)
//
// Created by Evgeny on 15/11/2022.
// Copyright © 2022 SimpleX Chat. All rights reserved.
//
import SwiftUI
import SimpleXChat
struct ProtocolServerView: View {
@Environment(\.dismiss) var dismiss: DismissAction
@EnvironmentObject var theme: AppTheme
let serverProtocol: ServerProtocol
@Binding var server: ServerCfg
@State var serverToEdit: ServerCfg
@State var serverEnabled: Bool
@State private var showTestFailure = false
@State private var testing = false
@State private var testFailure: ProtocolTestFailure?
var proto: String { serverProtocol.rawValue.uppercased() }
var body: some View {
ZStack {
if server.preset {
presetServer()
} else {
customServer()
}
if testing {
ProgressView().scaleEffect(2)
}
}
.modifier(BackButton(label: "Your \(proto) servers", disabled: Binding.constant(false)) {
server = serverToEdit
dismiss()
})
.alert(isPresented: $showTestFailure) {
Alert(
title: Text("Server test failed!"),
message: Text(testFailure?.localizedDescription ?? "")
)
}
.onChange(of: serverToEdit.server) { _ in
serverToEdit.tested = serverToEdit.server == server.server ? server.tested : nil
}
}
private func presetServer() -> some View {
return VStack {
List {
Section(header: Text("Preset server address").foregroundColor(theme.colors.secondary)) {
Text(serverToEdit.server)
.textSelection(.enabled)
}
useServerSection(true)
}
}
}
private func customServer() -> some View {
VStack {
let serverAddress = parseServerAddress(serverToEdit.server)
let valid = serverAddress?.valid == true && serverAddress?.serverProtocol == serverProtocol
List {
Section {
TextEditor(text: $serverToEdit.server)
.multilineTextAlignment(.leading)
.autocorrectionDisabled(true)
.autocapitalization(.none)
.allowsTightening(true)
.lineLimit(10)
.frame(height: 144)
.padding(-6)
} header: {
HStack {
Text("Your server address")
.foregroundColor(theme.colors.secondary)
if !valid {
Spacer()
Image(systemName: "exclamationmark.circle").foregroundColor(.red)
}
}
}
useServerSection(valid)
if valid {
Section(header: Text("Add to another device").foregroundColor(theme.colors.secondary)) {
MutableQRCode(uri: $serverToEdit.server)
.listRowInsets(EdgeInsets(top: 12, leading: 12, bottom: 12, trailing: 12))
}
}
}
}
}
private func useServerSection(_ valid: Bool) -> some View {
Section(header: Text("Use server").foregroundColor(theme.colors.secondary)) {
HStack {
Button("Test server") {
testing = true
serverToEdit.tested = nil
Task {
if let f = await testServerConnection(server: $serverToEdit) {
showTestFailure = true
testFailure = f
}
await MainActor.run { testing = false }
}
}
.disabled(!valid || testing)
Spacer()
showTestStatus(server: serverToEdit)
}
Toggle("Use for new connections", isOn: $serverEnabled)
.onChange(of: serverEnabled) { enabled in
serverToEdit.enabled = enabled ? .enabled : .disabled
}
}
}
}
struct BackButton: ViewModifier {
var label: LocalizedStringKey = "Back"
@Binding var disabled: Bool
var action: () -> Void
func body(content: Content) -> some View {
content
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: action) {
HStack {
Image(systemName: "chevron.left")
Text(label)
}
}
.disabled(disabled)
}
}
}
}
@ViewBuilder func showTestStatus(server: ServerCfg) -> some View {
switch server.tested {
case .some(true):
Image(systemName: "checkmark")
.foregroundColor(.green)
case .some(false):
Image(systemName: "multiply")
.foregroundColor(.red)
case .none:
Color.clear
}
}
func testServerConnection(server: Binding<ServerCfg>) async -> ProtocolTestFailure? {
do {
let r = try await testProtoServer(server: server.wrappedValue.server)
switch r {
case .success:
await MainActor.run { server.wrappedValue.tested = true }
return nil
case let .failure(f):
await MainActor.run { server.wrappedValue.tested = false }
return f
}
} catch let error {
logger.error("testServerConnection \(responseError(error))")
await MainActor.run {
server.wrappedValue.tested = false
}
return nil
}
}
func serverHostname(_ srv: String) -> String {
parseServerAddress(srv)?.hostnames.first ?? srv
}
struct ProtocolServerView_Previews: PreviewProvider {
static var previews: some View {
ProtocolServerView(
serverProtocol: .smp,
server: Binding.constant(ServerCfg.sampleData.custom),
serverToEdit: ServerCfg.sampleData.custom,
serverEnabled: true
)
}
}