mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43c356b9c2 | |||
| 9b29afef3e | |||
| 6eab3aeb62 | |||
| 2209a467bb |
@@ -33,6 +33,29 @@ enum UserPickerSheet: Identifiable {
|
||||
|
||||
class SaveableSettings: ObservableObject {
|
||||
@Published var servers: ServerSettings = ServerSettings(currUserServers: [], userServers: [], serverErrors: [])
|
||||
@Published var networkSettings: NetworkSettings = NetworkSettings.defaults
|
||||
|
||||
public func saveNetCfg() -> Bool {
|
||||
do {
|
||||
let netCfg = networkSettings.netCfg
|
||||
let netProxy = networkSettings.netProxy
|
||||
try setNetworkConfig(netCfg)
|
||||
networkSettings.currentNetCfg = netCfg
|
||||
setNetCfg(netCfg, networkProxy: netCfg.socksProxy != nil ? netProxy : nil)
|
||||
networkSettings.currentNetProxy = netProxy
|
||||
networkProxyDefault.set(netProxy)
|
||||
return true
|
||||
} catch let error {
|
||||
let err = responseError(error)
|
||||
showAlert(
|
||||
NSLocalizedString("Error updating settings", comment: "alert title"),
|
||||
message: responseError(error)
|
||||
)
|
||||
|
||||
logger.error("\(err)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ServerSettings {
|
||||
@@ -41,6 +64,20 @@ struct ServerSettings {
|
||||
public var serverErrors: [UserServersError]
|
||||
}
|
||||
|
||||
struct NetworkSettings {
|
||||
public var currentNetCfg: NetCfg
|
||||
public var netCfg: NetCfg
|
||||
public var currentNetProxy: NetworkProxy
|
||||
public var netProxy: NetworkProxy
|
||||
|
||||
static let defaults = NetworkSettings(
|
||||
currentNetCfg: NetCfg.defaults,
|
||||
netCfg: NetCfg.defaults,
|
||||
currentNetProxy: networkProxyDefault.get(),
|
||||
netProxy: networkProxyDefault.get()
|
||||
)
|
||||
}
|
||||
|
||||
struct UserPickerSheetView: View {
|
||||
let sheet: UserPickerSheet
|
||||
@EnvironmentObject var chatModel: ChatModel
|
||||
@@ -89,15 +126,30 @@ struct UserPickerSheetView: View {
|
||||
)
|
||||
}
|
||||
.onDisappear {
|
||||
let advancedNetworkCanBeSaved = advancedNetworkSettingsCanBeSaved(ss.networkSettings)
|
||||
let advancedNetworkSaveText = NSLocalizedString("Save and reconnect", comment: "alert button")
|
||||
|
||||
if serversCanBeSaved(
|
||||
ss.servers.currUserServers,
|
||||
ss.servers.userServers,
|
||||
ss.servers.serverErrors
|
||||
) {
|
||||
showAlert(
|
||||
title: NSLocalizedString("Save servers?", comment: "alert title"),
|
||||
buttonTitle: NSLocalizedString("Save", comment: "alert button"),
|
||||
buttonAction: { saveServers($ss.servers.currUserServers, $ss.servers.userServers) },
|
||||
title: NSLocalizedString(advancedNetworkCanBeSaved ? "Save servers and network settings?" : "Save servers?", comment: "alert title"),
|
||||
buttonTitle: advancedNetworkCanBeSaved ? NSLocalizedString("Save", comment: "alert button"): advancedNetworkSaveText,
|
||||
buttonAction: {
|
||||
saveServers($ss.servers.currUserServers, $ss.servers.userServers)
|
||||
if advancedNetworkCanBeSaved {
|
||||
_ = ss.saveNetCfg()
|
||||
}
|
||||
},
|
||||
cancelButton: true
|
||||
)
|
||||
} else if (advancedNetworkCanBeSaved) {
|
||||
showAlert(
|
||||
title: NSLocalizedString("Update network settings?", comment: "alert title"),
|
||||
buttonTitle: advancedNetworkSaveText,
|
||||
buttonAction: { _ = ss.saveNetCfg() },
|
||||
cancelButton: true
|
||||
)
|
||||
}
|
||||
|
||||
+14
-22
@@ -28,19 +28,20 @@ struct AdvancedNetworkSettings: View {
|
||||
@EnvironmentObject var theme: AppTheme
|
||||
@AppStorage(DEFAULT_DEVELOPER_TOOLS) private var developerTools = false
|
||||
@AppStorage(DEFAULT_SHOW_SENT_VIA_RPOXY) private var showSentViaProxy = false
|
||||
@State private var netCfg = NetCfg.defaults
|
||||
@State private var currentNetCfg = NetCfg.defaults
|
||||
@State private var cfgLoaded = false
|
||||
@State private var enableKeepAlive = true
|
||||
@State private var keepAliveOpts = KeepAliveOpts.defaults
|
||||
@State private var showSettingsAlert: NetworkSettingsAlert?
|
||||
@State private var onionHosts: OnionHosts = .no
|
||||
@State private var showSaveDialog = false
|
||||
@State private var netProxy = networkProxyDefault.get()
|
||||
@State private var currentNetProxy = networkProxyDefault.get()
|
||||
@State private var useNetProxy = false
|
||||
@State private var netProxyAuth = false
|
||||
|
||||
@Binding public var currentNetCfg: NetCfg
|
||||
@Binding public var netCfg: NetCfg
|
||||
@Binding public var currentNetProxy: NetworkProxy
|
||||
@Binding public var netProxy: NetworkProxy
|
||||
let saveNetCfg: () -> Bool
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
List {
|
||||
@@ -312,22 +313,6 @@ struct AdvancedNetworkSettings: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func saveNetCfg() -> Bool {
|
||||
do {
|
||||
try setNetworkConfig(netCfg)
|
||||
currentNetCfg = netCfg
|
||||
setNetCfg(netCfg, networkProxy: useNetProxy ? netProxy : nil)
|
||||
currentNetProxy = netProxy
|
||||
networkProxyDefault.set(netProxy)
|
||||
return true
|
||||
} catch let error {
|
||||
let err = responseError(error)
|
||||
showSettingsAlert = .error(err: err)
|
||||
logger.error("\(err)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func intSettingPicker(_ title: LocalizedStringKey, selection: Binding<Int>, values: [Int], label: String) -> some View {
|
||||
Picker(title, selection: selection) {
|
||||
ForEach(values, id: \.self) { value in
|
||||
@@ -386,6 +371,13 @@ struct AdvancedNetworkSettings: View {
|
||||
|
||||
struct AdvancedNetworkSettings_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
AdvancedNetworkSettings()
|
||||
let defaultSettings = NetworkSettings.defaults
|
||||
AdvancedNetworkSettings(
|
||||
currentNetCfg: Binding.constant(defaultSettings.currentNetCfg),
|
||||
netCfg: Binding.constant(defaultSettings.netCfg),
|
||||
currentNetProxy: Binding.constant(defaultSettings.currentNetProxy),
|
||||
netProxy: Binding.constant(defaultSettings.netProxy),
|
||||
saveNetCfg: { true }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,9 +94,15 @@ struct NetworkAndServers: View {
|
||||
}
|
||||
|
||||
NavigationLink {
|
||||
AdvancedNetworkSettings()
|
||||
.navigationTitle("Advanced settings")
|
||||
.modifier(ThemedBackground(grouped: true))
|
||||
AdvancedNetworkSettings(
|
||||
currentNetCfg: $ss.networkSettings.currentNetCfg,
|
||||
netCfg: $ss.networkSettings.netCfg,
|
||||
currentNetProxy: $ss.networkSettings.currentNetProxy,
|
||||
netProxy: $ss.networkSettings.netProxy,
|
||||
saveNetCfg: ss.saveNetCfg
|
||||
)
|
||||
.navigationTitle("Advanced settings")
|
||||
.modifier(ThemedBackground(grouped: true))
|
||||
} label: {
|
||||
Text("Advanced network settings")
|
||||
}
|
||||
@@ -336,6 +342,13 @@ func serversCanBeSaved(
|
||||
return userServers != currUserServers && serverErrors.isEmpty
|
||||
}
|
||||
|
||||
func advancedNetworkSettingsCanBeSaved(
|
||||
_ config: NetworkSettings
|
||||
) -> Bool {
|
||||
let useNetProxy = config.netCfg.socksProxy != nil
|
||||
return (config.currentNetCfg != config.netCfg || config.currentNetProxy != config.netProxy) && (useNetProxy ? config.netProxy.valid : true)
|
||||
}
|
||||
|
||||
struct ServersErrorView: View {
|
||||
@EnvironmentObject var theme: AppTheme
|
||||
var errStr: String
|
||||
|
||||
Reference in New Issue
Block a user