2019-12-19 17:32:49 +00:00
|
|
|
/*
|
2023-04-06 09:44:16 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2019-12-19 17:32:49 +00:00
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-12-14 07:01:21 +00:00
|
|
|
*/
|
|
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
import Foundation
|
2016-12-14 07:01:21 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
final class UsersProvider {
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
enum UsersError: Error {
|
|
|
|
|
case invalidData
|
2016-12-14 07:01:21 +00:00
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
let users: [User]
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
init(with file: URL) throws {
|
|
|
|
|
let data = try Data(contentsOf: file)
|
|
|
|
|
let json = try JSONSerialization.jsonObject(with: data, options: [])
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
guard let dicts = json as? [[String: String]] else {
|
|
|
|
|
throw UsersError.invalidData
|
2016-12-14 07:01:21 +00:00
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2018-05-31 00:59:51 +00:00
|
|
|
self.users = dicts.enumerated().compactMap { index, dict in
|
2016-12-19 17:13:21 +00:00
|
|
|
guard let name = dict["name"] else { return nil }
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-19 17:13:21 +00:00
|
|
|
return User(pk: index, name: name.capitalized)
|
|
|
|
|
}.sorted(by: { $0.name < $1.name })
|
2016-12-14 07:01:21 +00:00
|
|
|
}
|
2017-05-16 14:30:08 +00:00
|
|
|
|
2016-12-14 07:01:21 +00:00
|
|
|
}
|