summaryrefslogtreecommitdiff
path: root/Sora/Data/Booru/Provider/BooruProviderCredentials.swift
blob: 898201aff09dc8343035e7a945b503820818cebb (plain) (blame)
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
import Foundation

struct BooruProviderCredentials: Codable, Identifiable, Equatable {
  let id: UUID
  let provider: BooruProvider
  var apiKey: String
  var userID: Int

  init(provider: BooruProvider, apiKey: String, userID: Int, id: UUID = UUID()) {
    self.id = id
    self.provider = provider
    self.apiKey = apiKey
    self.userID = userID
  }

  static func from(
    // swiftlint:disable:next large_tuple
    _ rawCredentials: [(provider: BooruProvider, apiKey: String, userID: Int)],
    existingCredentials: [Self]
  ) -> [Self] {
    rawCredentials.map { credentials in
      if let existingKey = existingCredentials.first(
        where: { $0.provider == credentials.provider }
      ) {
        return Self(
          provider: credentials.provider,
          apiKey: credentials.apiKey,
          userID: credentials.userID,
          id: existingKey.id
        )
      }

      return Self(
        provider: credentials.provider,
        apiKey: credentials.apiKey,
        userID: credentials.userID
      )
    }
  }
}