-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPreviewViewController.swift
41 lines (35 loc) · 1.28 KB
/
PreviewViewController.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
//
// PreviewViewController.swift
// GamebookPreviewExtension
//
// Created by Brad Root on 9/14/19.
// Copyright © 2019 Brad Root. All rights reserved.
//
import QuickLook
import UIKit
class PreviewViewController: UIViewController, QLPreviewingController {
@IBOutlet var titleLabel: UILabel!
@IBOutlet var authorLabel: UILabel!
@IBOutlet var synopsisLabel: UILabel!
@IBOutlet var totalPagesLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func preparePreviewOfFile(at url: URL, completionHandler handler: @escaping (Error?) -> Void) {
_ = url.startAccessingSecurityScopedResource()
guard let jsonData = try? Data(contentsOf: url) else { handler(nil)
return
}
url.stopAccessingSecurityScopedResource()
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
if let gameStruct = try? decoder.decode(GameStruct.self, from: jsonData) {
titleLabel.text = gameStruct.name
authorLabel.text = gameStruct.author ?? "Anonymous"
synopsisLabel.text = gameStruct.about
totalPagesLabel.text = "\(gameStruct.pages.count)"
}
handler(nil)
}
}