mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-04-11 23:05:49 +00:00
* wip
* refactor, fix bindings
* wip
* wip
* fixes
* wip
* information map, logos
* global conditions hack
* restructure
* restructure
* texts
* text
* restructure
* wip
* restructure
* rename
* wip
* conditions for all
* comment
* onboarding wip
* onboarding wip
* fix paddings
* fix paddings
* wip
* fix padding
* onboarding wip
* nav link instead of sheet
* pretty button
* large titles
* notifications mode button style
* reenable demo operator
* Revert "reenable demo operator"
This reverts commit 42111eb333.
* padding
* reenable demo operator
* refactor (removes additional model api)
* style
* bold
* bold
* light/dark
* fix button
* comment
* wip
* remove preset
* new types
* api types
* apis
* smp and xftp servers in single view
* test operator servers, refactor
* save in main view
* better progress
* better in progress
* remove shadow
* update
* apis
* conditions view wip
* load text
* remove custom servers button from onboarding, open already conditions in nav link
* allow to continue with simplex on onboarding
* footer
* existing users notice
* fix to not show nothing on no action
* disable notice
* review later
* disable notice
* wip
* wip
* wip
* wip
* optional tag
* fix
* fix tags
* fix
* wip
* remove coding keys
* fix onboarding
* rename
* rework model wip
* wip
* wip
* wip
* fix
* wip
* wip
* delete
* simplify
* wip
* fix delete
* ios: server operators ui wip
* refactor
* edited
* save servers on dismiss/back
* ios: add address card and remove address from onboarding (#5181)
* ios: add address card and remove address from onboarding
* allow for address creation in info when open via card
* conditions interactions wip
* conditions interactions wip
* fix
* wip
* wip
* wip
* wip
* rename
* wip
* fix
* remove operator binding
* fix set enabled
* rename
* cleanup
* text
* fix info view dark mode
* update lib
* ios: operators & servers validation
* fix
* ios: align onboarding style
* ios: align onboarding style
* ios: operators info (#5207)
* ios: operators info
* update
* update texts
* texts
---------
Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>
---------
Co-authored-by: Diogo <diogofncunha@gmail.com>
Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
157 lines
5.8 KiB
Swift
157 lines
5.8 KiB
Swift
//
|
|
// NewServerView.swift
|
|
// SimpleX (iOS)
|
|
//
|
|
// Created by spaced4ndy on 13.11.2024.
|
|
// Copyright © 2024 SimpleX Chat. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SimpleXChat
|
|
|
|
struct NewServerView: View {
|
|
@Environment(\.dismiss) var dismiss: DismissAction
|
|
@EnvironmentObject var theme: AppTheme
|
|
@Binding var userServers: [UserOperatorServers]
|
|
@Binding var serverErrors: [UserServersError]
|
|
@State private var serverToEdit: UserServer = .empty
|
|
@State private var showTestFailure = false
|
|
@State private var testing = false
|
|
@State private var testFailure: ProtocolTestFailure?
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
customServer()
|
|
if testing {
|
|
ProgressView().scaleEffect(2)
|
|
}
|
|
}
|
|
.modifier(BackButton(disabled: Binding.constant(false)) {
|
|
addServer(serverToEdit, $userServers, $serverErrors, dismiss)
|
|
})
|
|
.alert(isPresented: $showTestFailure) {
|
|
Alert(
|
|
title: Text("Server test failed!"),
|
|
message: Text(testFailure?.localizedDescription ?? "")
|
|
)
|
|
}
|
|
}
|
|
|
|
// TODO Possibly refactor - similar functions in ProtocolServerView
|
|
private func customServer() -> some View {
|
|
VStack {
|
|
let serverAddress = parseServerAddress(serverToEdit.server)
|
|
let valid = serverAddress?.valid == true
|
|
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: $serverToEdit.enabled)
|
|
}
|
|
}
|
|
}
|
|
|
|
func serverProtocolAndOperator(_ server: UserServer, _ userServers: [UserOperatorServers]) -> (ServerProtocol, ServerOperator?)? {
|
|
if let serverAddress = parseServerAddress(server.server) {
|
|
let serverProtocol = serverAddress.serverProtocol
|
|
let hostnames = serverAddress.hostnames
|
|
let matchingOperator = userServers.compactMap { $0.operator }.first { op in
|
|
op.serverDomains.contains { domain in
|
|
hostnames.contains { hostname in
|
|
hostname.hasSuffix(domain)
|
|
}
|
|
}
|
|
}
|
|
return (serverProtocol, matchingOperator)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func addServer(
|
|
_ server: UserServer,
|
|
_ userServers: Binding<[UserOperatorServers]>,
|
|
_ serverErrors: Binding<[UserServersError]>,
|
|
_ dismiss: DismissAction
|
|
) {
|
|
if let (serverProtocol, matchingOperator) = serverProtocolAndOperator(server, userServers.wrappedValue) {
|
|
if let i = userServers.wrappedValue.firstIndex(where: { $0.operator?.operatorId == matchingOperator?.operatorId }) {
|
|
switch serverProtocol {
|
|
case .smp: userServers[i].wrappedValue.smpServers.append(server)
|
|
case .xftp: userServers[i].wrappedValue.xftpServers.append(server)
|
|
}
|
|
validateServers_(userServers, serverErrors)
|
|
dismiss()
|
|
if let op = matchingOperator {
|
|
showAlert(
|
|
NSLocalizedString("Operator server", comment: "alert title"),
|
|
message: String.localizedStringWithFormat(NSLocalizedString("Server added to operator %@.", comment: "alert message"), op.tradeName)
|
|
)
|
|
}
|
|
} else { // Shouldn't happen
|
|
dismiss()
|
|
showAlert(NSLocalizedString("Error adding server", comment: "alert title"))
|
|
}
|
|
} else {
|
|
dismiss()
|
|
if server.server.trimmingCharacters(in: .whitespaces) != "" {
|
|
showAlert(
|
|
NSLocalizedString("Invalid server address!", comment: "alert title"),
|
|
message: NSLocalizedString("Check server address and try again.", comment: "alert title")
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
NewServerView(
|
|
userServers: Binding.constant([UserOperatorServers.sampleDataNilOperator]),
|
|
serverErrors: Binding.constant([])
|
|
)
|
|
}
|