summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadSettings/RecentConnectionsRepository.swift
blob: b655fd615632872332c7dfd6e84be808cf0522d9 (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
79
//
//  RecentConnectionsRepository.swift
//  MullvadVPN
//
//  Created by Mojgan on 2025-10-15.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import MullvadTypes

public enum RecentConnectionsRepositoryError: LocalizedError, Hashable {
    case recentsDisabled

    public var errorDescription: String? {
        switch self {
        case .recentsDisabled:
            "To add the location to the recents, first enable it in the settings."
        }
    }
}

final class RecentConnectionsRepository: RecentConnectionsRepositoryProtocol {
    private let store: SettingsStore
    private let maxLimit: UInt

    private let settingsParser: SettingsParser = {
        SettingsParser(decoder: JSONDecoder(), encoder: JSONEncoder())
    }()

    init(store: SettingsStore, maxLimit: UInt = 50) {
        self.store = store
        self.maxLimit = maxLimit
    }

    func setRecentsEnabled(_ isEnabled: Bool) throws {
        // Clear all recents whenever the recents feature status changes.
        try write(RecentConnections(isEnabled: isEnabled, entryLocations: [], exitLocations: []))
    }

    func add(_ location: UserSelectedRelays, as type: RecentLocationType) throws {
        let current = try read()
        guard current.isEnabled else { throw RecentConnectionsRepositoryError.recentsDisabled }
        var currentList = current[keyPath: keyPath(for: type)]
        if let idx = currentList.firstIndex(of: location) { currentList.remove(at: idx) }
        currentList.insert(location, at: 0)
        currentList = Array(currentList.prefix(Int(maxLimit)))

        let new =
            (type == .entry)
            ? RecentConnections(
                isEnabled: current.isEnabled, entryLocations: currentList, exitLocations: current.exitLocations)
            : RecentConnections(
                isEnabled: current.isEnabled, entryLocations: current.entryLocations, exitLocations: currentList)

        try write(new)
    }

    func all() throws -> RecentConnections {
        try read()
    }
}

private extension RecentConnectionsRepository {
    private func keyPath(for type: RecentLocationType) -> KeyPath<RecentConnections, [UserSelectedRelays]> {
        switch type {
        case .entry: return \.entryLocations
        case .exit: return \.exitLocations
        }
    }

    private func read() throws -> RecentConnections {
        let data = try store.read(key: .recentConnections)
        return try settingsParser.parseUnversionedPayload(as: RecentConnections.self, from: data)
    }

    private func write(_ value: RecentConnections) throws {
        let data = try settingsParser.produceUnversionedPayload(value)
        try store.write(data, for: .recentConnections)
    }
}