-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStore.swift
executable file
·50 lines (43 loc) · 1.37 KB
/
DataStore.swift
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
//
// DataStore.swift
// Movie Hub
//
// Created by Rashmi Ranjan Rout on 8/2/18.
// Copyright © 2018 Rashmi Ranjan Rout. All rights reserved.
//
import Foundation
import UIKit
final class DataStore {
static let sharedInstance = DataStore()
fileprivate init() {}
var audiobooks: [Audiobook] = []
var images: [UIImage] = []
func getBooks(completion: @escaping () -> Void) {
APIClient.getAudiobooksAPI { (json) in
if let results = json?["results"] as?[AudiobookJSON] {
for i in 0..<results.count{
let newAudiobook = Audiobook(dictionary: results[i])
self.audiobooks.append(newAudiobook)
}
completion()
}
}
}
func getBookImages(completion: @escaping () -> Void) {
getBooks {
for book in self.audiobooks {
let resultStr = "https://image.tmdb.org/t/p/w92\(book.coverImage)"
// print(resultStr)
let url = URL(string: resultStr)
let data = try? Data(contentsOf: url!)
if let imageData = data {
let image = UIImage(data: imageData)
self.images.append(image!)
}
}
OperationQueue.main.addOperation {
completion()
}
}
}
}