summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadSettings/DAITASettings.swift
blob: 3cea842db026249a61e5484329bbeda060415a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
//  DAITASettings.swift
//  MullvadSettings
//
//  Created by Mojgan on 2024-08-08.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation

/// Whether DAITA is enabled.
public enum DAITAState: Codable, Sendable {
    case on
    case off

    public var isEnabled: Bool {
        get {
            self == .on
        }
        set {
            self = newValue ? .on : .off
        }
    }
}

/// Whether "direct only" is enabled, meaning no automatic routing to DAITA relays.
public enum DirectOnlyState: Codable, Sendable {
    case on
    case off

    public var isEnabled: Bool {
        get {
            self == .on
        }
        set {
            self = newValue ? .on : .off
        }
    }
}

/// Selected relay is incompatible with DAITA, either through singlehop or multihop.
public enum DAITASettingsCompatibilityError {
    case singlehop, multihop
}

public struct DAITASettings: Codable, Equatable, Sendable {
    @available(*, deprecated, renamed: "daitaState")
    public let state: DAITAState = .off

    public var daitaState: DAITAState
    public var directOnlyState: DirectOnlyState

    public var isAutomaticRouting: Bool {
        daitaState.isEnabled && !directOnlyState.isEnabled
    }

    public var isDirectOnly: Bool {
        daitaState.isEnabled && directOnlyState.isEnabled
    }

    public init(daitaState: DAITAState = .off, directOnlyState: DirectOnlyState = .off) {
        self.daitaState = daitaState
        self.directOnlyState = directOnlyState
    }

    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        daitaState =
            try container.decodeIfPresent(DAITAState.self, forKey: .daitaState)
            ?? container.decodeIfPresent(DAITAState.self, forKey: .state)
            ?? .off

        directOnlyState =
            try container.decodeIfPresent(DirectOnlyState.self, forKey: .directOnlyState)
            ?? .off
    }
}