|
| 1 | +// |
| 2 | +// ViewController.swift |
| 3 | +// SMLinkPreview |
| 4 | +// |
| 5 | +// Created by crspybits on 04/20/2019. |
| 6 | +// Copyright (c) 2019 Spastic Muffin, LLC. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import SMLinkPreview |
| 11 | + |
| 12 | +class ViewController: UIViewController { |
| 13 | + @IBOutlet weak var tableView: UITableView! |
| 14 | + var rowData = [LinkData]() |
| 15 | + var rowViews = [LinkPreview]() |
| 16 | + let reuseId = "ReuseId" |
| 17 | + |
| 18 | + override func viewDidLoad() { |
| 19 | + super.viewDidLoad() |
| 20 | + |
| 21 | + tableView.dataSource = self |
| 22 | + tableView.delegate = self |
| 23 | + tableView.register(UINib(nibName: "LinkPreviewCell", bundle: nil), forCellReuseIdentifier: reuseId) |
| 24 | + tableView.allowsSelection = false |
| 25 | + |
| 26 | + func add(datum: LinkData, proportion: CGFloat = 1.0) { |
| 27 | + let preview = LinkPreview.create(with: datum) |
| 28 | + preview.heightAnchor.constraint(equalToConstant: preview.frame.height*proportion).isActive = true |
| 29 | + preview.widthAnchor.constraint(equalToConstant: preview.frame.width*proportion).isActive = true |
| 30 | + preview.translatesAutoresizingMaskIntoConstraints = false |
| 31 | + rowViews += [preview] |
| 32 | + } |
| 33 | + |
| 34 | + let data0 = LinkData(url: URL(string: "http://www.cprince.com")!, title: "Home sweet home", description: "Welcome to my web page. Web pages are funny (they make me laugh). A store front for a person. An Internet presence. Information about yourself that every-internet-navigating person on the planet above age two can access. Is it the truth? Perhaps it’s Google-true?", image: URL(string: "http://cprince.com/WordPress/wp-content/uploads/2013/10/tree-225x300.jpg"), icon: URL(string: "https://cprince.com/WordPress/wp-content/uploads/2013/11/IMG_09254.jpg")) |
| 35 | + add(datum: data0, proportion: 0.75) |
| 36 | + |
| 37 | + let data1 = LinkData(url: URL(string: "http://www.cprince.com")!, title: "Home sweet home", description: "Welcome to my web page. Web pages are funny (they make me laugh). A store front for a person. An Internet presence. Information about yourself that every-internet-navigating person on the planet above age two can access. Is it the truth? Perhaps it’s Google-true?", image: URL(string: "http://cprince.com/WordPress/wp-content/uploads/2013/10/tree-225x300.jpg"), icon: URL(string: "https://cprince.com/WordPress/wp-content/uploads/2013/11/IMG_09254.jpg")) |
| 38 | + rowData += [data1] |
| 39 | + |
| 40 | + let data1b = LinkData(url: URL(string: "http://www.cprince.com")!, title: "Home sweet home and more text for the title", description: "Welcome to my web page. Web pages are funny (they make me laugh). A store front for a person. An Internet presence. Information about yourself that every-internet-navigating person on the planet above age two can access. Is it the truth? Perhaps it’s Google-true?", image: URL(string: "https://cprince.com/WordPress/wp-content/uploads/2013/10/tree-225x300.jpg"), icon: URL(string: "https://cprince.com/WordPress/wp-content/uploads/2013/11/IMG_09254.jpg")) |
| 41 | + rowData += [data1b] |
| 42 | + |
| 43 | + let data2 = LinkData(url: URL(string: "http://www.cprince.com")!, title: nil, description: nil, image: nil, icon: nil) |
| 44 | + rowData += [data2] |
| 45 | + |
| 46 | + let data3 = LinkData(url: URL(string: "http://www.cprince.com")!, title: nil, description: nil, image: nil, icon: URL(string: "https://cprince.com/WordPress/wp-content/uploads/2013/11/IMG_09254.jpg")) |
| 47 | + rowData += [data3] |
| 48 | + |
| 49 | + let data4 = LinkData(url: URL(string: "http://www.cprince.com")!, title: "Home sweet home and more text for the title", description: nil, image: nil, icon: URL(string: "https://cprince.com/WordPress/wp-content/uploads/2013/11/IMG_09254.jpg")) |
| 50 | + rowData += [data4] |
| 51 | + |
| 52 | + let data5 = LinkData(url: URL(string: "http://www.cprince.com")!, title: "Home sweet home and more text for the title", description: nil, image: nil, icon: nil) |
| 53 | + rowData += [data5] |
| 54 | + |
| 55 | + for datum in rowData { |
| 56 | + add(datum: datum) |
| 57 | + } |
| 58 | + |
| 59 | + PreviewManager.session.config = PreviewConfiguration(maxNumberTitleLines: 1) |
| 60 | + let data6 = LinkData(url: URL(string: "http://www.cprince.com")!, title: "Only a single line despite how much text I type because I forced it to be so.", description: nil, image: nil, icon: nil) |
| 61 | + add(datum: data6) |
| 62 | + PreviewManager.session.config = PreviewConfiguration() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +extension ViewController : UITableViewDataSource, UITableViewDelegate { |
| 67 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 68 | + return rowViews.count |
| 69 | + } |
| 70 | + |
| 71 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 72 | + |
| 73 | + let cell = tableView.dequeueReusableCell(withIdentifier: reuseId, for: indexPath) as! LinkPreviewCell |
| 74 | + let preview = rowViews[indexPath.row] |
| 75 | + cell.setup(with: preview) |
| 76 | + |
| 77 | + return cell |
| 78 | + } |
| 79 | + |
| 80 | + func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { |
| 81 | + let preview = rowViews[indexPath.row] |
| 82 | + let height = preview.frame.height + LinkPreviewCell.verticalPadding |
| 83 | + return height |
| 84 | + } |
| 85 | +} |
0 commit comments