Fix preferences
This commit is contained in:
@@ -30,6 +30,9 @@ enum Database {
|
|||||||
t.add(column: "autoUpdateInterval", .integer).notNull().defaults(to: 0)
|
t.add(column: "autoUpdateInterval", .integer).notNull().defaults(to: 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
migrator.registerMigration("fix_cellular_typo") { db in
|
||||||
|
try db.execute(sql: "UPDATE preferences SET name = 'exclude_cellular_services' WHERE name = 'exclude_celluar_services'")
|
||||||
|
}
|
||||||
try migrator.migrate(database)
|
try migrator.migrate(database)
|
||||||
return database
|
return database
|
||||||
} catch {
|
} catch {
|
||||||
+21
-4
@@ -4,7 +4,7 @@ import GRDB
|
|||||||
|
|
||||||
extension SharedPreferences {
|
extension SharedPreferences {
|
||||||
public class Preference<T: Codable> {
|
public class Preference<T: Codable> {
|
||||||
private let name: String
|
let name: String
|
||||||
private let defaultValue: T
|
private let defaultValue: T
|
||||||
|
|
||||||
init(_ name: String, defaultValue: T) {
|
init(_ name: String, defaultValue: T) {
|
||||||
@@ -43,7 +43,11 @@ extension SharedPreferences {
|
|||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return try BinaryDecoder().decode(from: item.data)
|
if T.self == String.self {
|
||||||
|
return String(data: item.data, encoding: .utf8) as? T
|
||||||
|
} else {
|
||||||
|
return try BinaryDecoder().decode(from: item.data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private nonisolated static func write(_ name: String, _ value: (some Codable)?) async throws {
|
private nonisolated static func write(_ name: String, _ value: (some Codable)?) async throws {
|
||||||
@@ -52,9 +56,22 @@ extension SharedPreferences {
|
|||||||
try Item.deleteOne(db, id: name)
|
try Item.deleteOne(db, id: name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let data = try BinaryEncoder().encode(value)
|
let data: Data
|
||||||
|
if let stringValue = value as? String {
|
||||||
|
data = stringValue.data(using: .utf8)!
|
||||||
|
} else {
|
||||||
|
data = try BinaryEncoder().encode(value)
|
||||||
|
}
|
||||||
try await Database.sharedWriter.write { db in
|
try await Database.sharedWriter.write { db in
|
||||||
try Item(name: name, data: data).insert(db)
|
try Item(name: name, data: data).save(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nonisolated static func batchDelete(_ names: [String]) async throws {
|
||||||
|
try await Database.sharedWriter.write { db in
|
||||||
|
for name in names {
|
||||||
|
try Item.deleteOne(db, id: name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public enum SharedPreferences {
|
public enum SharedPreferences {
|
||||||
public static let language = Preference<String>("language", defaultValue: "")
|
|
||||||
|
|
||||||
public static let selectedProfileID = Preference<Int64>("selected_profile_id", defaultValue: -1)
|
public static let selectedProfileID = Preference<Int64>("selected_profile_id", defaultValue: -1)
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
@@ -23,20 +21,25 @@ public enum SharedPreferences {
|
|||||||
public static let includeAllNetworks = Preference<Bool>("include_all_networks", defaultValue: false)
|
public static let includeAllNetworks = Preference<Bool>("include_all_networks", defaultValue: false)
|
||||||
public static let excludeAPNs = Preference<Bool>("exclude_apns", defaultValue: true)
|
public static let excludeAPNs = Preference<Bool>("exclude_apns", defaultValue: true)
|
||||||
public static let excludeLocalNetworks = Preference<Bool>("exclude_local_networks", defaultValue: excludeLocalNetworksByDefault)
|
public static let excludeLocalNetworks = Preference<Bool>("exclude_local_networks", defaultValue: excludeLocalNetworksByDefault)
|
||||||
public static let excludeCellularServices = Preference<Bool>("exclude_celluar_services", defaultValue: true)
|
public static let excludeCellularServices = Preference<Bool>("exclude_cellular_services", defaultValue: true)
|
||||||
public static let enforceRoutes = Preference<Bool>("enforce_routes", defaultValue: false)
|
public static let enforceRoutes = Preference<Bool>("enforce_routes", defaultValue: false)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public static func resetPacketTunnel() async {
|
public static func resetPacketTunnel() async {
|
||||||
await ignoreMemoryLimit.set(nil)
|
|
||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
await includeAllNetworks.set(nil)
|
let names = [
|
||||||
await excludeAPNs.set(nil)
|
ignoreMemoryLimit.name,
|
||||||
await excludeLocalNetworks.set(nil)
|
includeAllNetworks.name,
|
||||||
await excludeCellularServices.set(nil)
|
excludeAPNs.name,
|
||||||
await enforceRoutes.set(nil)
|
excludeLocalNetworks.name,
|
||||||
|
excludeCellularServices.name,
|
||||||
|
enforceRoutes.name,
|
||||||
|
]
|
||||||
|
#else
|
||||||
|
let names = [ignoreMemoryLimit.name]
|
||||||
#endif
|
#endif
|
||||||
|
try? await batchDelete(names)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static let maxLogLines = Preference<Int>("max_log_lines", defaultValue: 300)
|
public static let maxLogLines = Preference<Int>("max_log_lines", defaultValue: 300)
|
||||||
@@ -47,8 +50,7 @@ public enum SharedPreferences {
|
|||||||
public static let startedByUser = Preference<Bool>("started_by_user", defaultValue: false)
|
public static let startedByUser = Preference<Bool>("started_by_user", defaultValue: false)
|
||||||
|
|
||||||
public static func resetMacOS() async {
|
public static func resetMacOS() async {
|
||||||
await showMenuBarExtra.set(nil)
|
try? await batchDelete([showMenuBarExtra.name, menuBarExtraInBackground.name])
|
||||||
await menuBarExtraInBackground.set(nil)
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -59,8 +61,8 @@ public enum SharedPreferences {
|
|||||||
public static let systemProxyEnabled = Preference<Bool>("system_proxy_enabled", defaultValue: true)
|
public static let systemProxyEnabled = Preference<Bool>("system_proxy_enabled", defaultValue: true)
|
||||||
|
|
||||||
#if os(tvOS)
|
#if os(tvOS)
|
||||||
public static let commandServerPort = Preference<Int32>("tv_command_server_port", defaultValue: 0)
|
public static let commandServerPort = Preference<Int32>("command_server_port", defaultValue: 0)
|
||||||
public static let commandServerSecret = Preference<String>("tv_command_server_secret", defaultValue: "")
|
public static let commandServerSecret = Preference<String>("command_server_secret", defaultValue: "")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Profile Override
|
// Profile Override
|
||||||
@@ -70,9 +72,7 @@ public enum SharedPreferences {
|
|||||||
public static let excludeAPNsRoute = Preference<Bool>("exclude_apple_push_notification_services", defaultValue: false)
|
public static let excludeAPNsRoute = Preference<Bool>("exclude_apple_push_notification_services", defaultValue: false)
|
||||||
|
|
||||||
public static func resetProfileOverride() async {
|
public static func resetProfileOverride() async {
|
||||||
await excludeDefaultRoute.set(nil)
|
try? await batchDelete([excludeDefaultRoute.name, autoRouteUseSubRangesByDefault.name, excludeAPNsRoute.name])
|
||||||
await autoRouteUseSubRangesByDefault.set(nil)
|
|
||||||
await excludeAPNsRoute.set(nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connections Filter
|
// Connections Filter
|
||||||
@@ -85,7 +85,7 @@ public enum SharedPreferences {
|
|||||||
public static let alwaysOn = Preference<Bool>("always_on", defaultValue: false)
|
public static let alwaysOn = Preference<Bool>("always_on", defaultValue: false)
|
||||||
|
|
||||||
public static func resetOnDemandRules() async {
|
public static func resetOnDemandRules() async {
|
||||||
await alwaysOn.set(nil)
|
try? await batchDelete([alwaysOn.name])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Core
|
// Core
|
||||||
|
|||||||
Reference in New Issue
Block a user