|
| 1 | +/// |
| 2 | +/// Copyright (c) 2021 Tjek. All rights reserved. |
| 3 | +/// |
| 4 | + |
| 5 | +import UIKit |
| 6 | +import PlaygroundSupport |
| 7 | +import Incito |
| 8 | + |
| 9 | +class MyIncitoViewController: IncitoLoaderViewController { |
| 10 | + |
| 11 | + override func viewDidAppear(_ animated: Bool) { |
| 12 | + super.viewDidAppear(animated) |
| 13 | + |
| 14 | + // Use the IncitoLoaderViewControllerDelegate to respond to events and customize the error screen, for example. |
| 15 | + self.delegate = self |
| 16 | + |
| 17 | + // The `IncitoLoader` is used to asyncronously fetch the incito document. It's up to you how you fetch that data. |
| 18 | + let loader: IncitoLoader |
| 19 | + |
| 20 | + // This is simply a utility function that generates a loader using a local file. |
| 21 | + loader = IncitoJSONFileLoader(filename: "elgiganten-nov19-375.json") |
| 22 | + |
| 23 | + // … otherwise you could use the callback version to asyncrously fetch the document's json data |
| 24 | + //loader = IncitoLoader { loadedDocCallback in |
| 25 | + // ... async work to fetch the data |
| 26 | + // let result = .success(IncitoDocument(jsonData: data)) |
| 27 | + // loadedCallback(result) |
| 28 | + //} |
| 29 | + |
| 30 | + // Passing the loader to the load function starts the fetching & parsing of the IncitoDocument, and showing a spinner while loading, an error screen if it fails, and the rendered incito on success. |
| 31 | + self.load(loader) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +extension MyIncitoViewController: IncitoLoaderViewControllerDelegate { |
| 36 | + func stateDidChange(from oldState: IncitoLoaderViewController.State, to newState: IncitoLoaderViewController.State, in viewController: IncitoLoaderViewController) { |
| 37 | + // The state changes when the incito changes between loading, success, or error. |
| 38 | + |
| 39 | + switch newState { |
| 40 | + case .success(let incitoVC): |
| 41 | + // When an incito is successfully loaded, you get access to the underlying `IncitoViewController` that renders the incito here (or via the failable `incitoViewController` property on the `IncitoLoaderViewController`). |
| 42 | + |
| 43 | + // If you set yourself as delegate now, you can listen for interaction events etc. |
| 44 | + incitoVC.delegate = self |
| 45 | + case .loading: |
| 46 | + // The incito started loading |
| 47 | + break |
| 48 | + case .error(let error): |
| 49 | + // something bad happened while trying to load the incito |
| 50 | + print(error) |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +extension MyIncitoViewController: IncitoViewControllerDelegate { |
| 56 | + func incitoDidReceiveTap(at point: CGPoint, in viewController: IncitoViewController) { |
| 57 | + // This event is triggered when the user taps on the incito content itself. |
| 58 | + // You can find out what is at the interaction location using a number of utility functions on the content viewcontroller itself. |
| 59 | + |
| 60 | + // for example, here we are getting the first element in the view hierarchy at the tap location that meets the `where` criteria. This is an async request, so has a completion handler. |
| 61 | + viewController.getFirstElement( |
| 62 | + at: point, |
| 63 | + where: { $0.role == "offer" }, |
| 64 | + completion: { foundElement in |
| 65 | + // if an element matching the criteria it is returned here. |
| 66 | + print("Tapped on an offer @ \(point)") |
| 67 | + } |
| 68 | + ) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +let incitoVC = MyIncitoViewController() |
| 73 | +incitoVC.view.frame = CGRect(x: 0, y: 0, width: 375, height: 800) |
| 74 | + |
| 75 | +PlaygroundPage.current.liveView = incitoVC |
0 commit comments