summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/ChangeLog/ChangeLogReader.swift
blob: 6e99ea4ba8d57398270ad2ebd9c81cfac0b6245b (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
//
//  ChangeLogReader.swift
//  MullvadVPN
//
//  Created by Mojgan on 2025-01-10.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Foundation

protocol ChangeLogReaderProtocol {
    func read() throws -> [String]
}

struct ChangeLogReader: ChangeLogReaderProtocol {
    /**
     Reads change log file from bundle and returns its contents as a string.
     */
    func read() throws -> [String] {
        try String(contentsOfFile: try getPathToChangesFile())
            .split(whereSeparator: { $0.isNewline })
            .compactMap { line in
                let trimmedString = line.trimmingCharacters(in: .whitespaces)

                return trimmedString.isEmpty ? nil : trimmedString
            }
    }

    /**
     Returns path to change log file in bundle.
     */
    private func getPathToChangesFile() throws -> String {
        if let filePath = Bundle.main.path(forResource: "changes", ofType: "txt") {
            return filePath
        } else {
            throw CocoaError(.fileNoSuchFile)
        }
    }
}