Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

🎯
Focusing
View GitHub Profile
import Foundation
// TODO: - Move to the separated file FlightData.swift
struct FlightData: Codable {
let Airline: String
let Flight: String
let ActualTime: String
}
final class Repository {
import UIKit
final class ViewController: UIViewController {
let repository = Repository(apiClient: APIClient())
override func viewDidLoad() {
super.viewDidLoad()
repository.getFlights { (result) in
// JsonCoddableUrlSession.FlightData(Airline: "BELAVIA", Flight: "B2734", ActualTime: "05:37"), JsonCoddableUrlSession.FlightData(Airline: "BELAVIA", Flight: "B2736", ActualTime: "05:46"), JsonCoddableUrlSession.FlightData(Airline: "BELAVIA", Flight: "B2848", ActualTime: "06:58"), JsonCoddableUrlSession.FlightData(Airline: "BELAVIA", Flight: "B2944", ActualTime: "06:51"),
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'JsonCoddableUrlSession' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for JsonCoddableUrlSession
pod 'RealmSwift'
# Add this line
import Foundation
import RealmSwift
// TODO: - Move to the separated file Flight.swift (Realm folder)
class Flight: Object {
@objc dynamic var airline: String = ""
@objc dynamic var flight: String = ""
@objc dynamic var actualTime: String = ""
override static func primaryKey() -> String? {
enum RepositoryType {
case local, remote
}
...
private let database: Database!
init(apiClient: APIClient, database: Database) {
self.apiClient = apiClient
self.database = database
}
...
...
func getFlights(_ type: RepositoryType, _ completion: @escaping ((Result<[FlightData]>) -> Void)) {
switch type {
case .local:
completion(.success(database.get()))
case .remote:
let resource = Resource(url: URL(string: "https://desolate-beyond-86929.herokuapp.com/arrival")!)
apiClient.load(resource) { [weak self] (result) in
switch result {
case .success(let data):
...
let repository = Repository(apiClient: APIClient(), database: Database())
...
...
repository.getFlights(.remote) { (result) in
switch result {
...