Last active
November 14, 2018 20:03
-
-
Save sharedRoutine/84db3ae254315b76818f963d95e79ec9 to your computer and use it in GitHub Desktop.
Pod License Display in App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PodAck.swift | |
// | |
// Created by Janosch Hübner on 03.11.18. | |
// Copyright © 2018 Janosch Hübner. All rights reserved. | |
// | |
import Foundation | |
struct PodAck: Decodable { | |
let licenses: [PodLicense] | |
enum CodingKeys: String, CodingKey { | |
case licenses = "PreferenceSpecifiers" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PodLicense.swift | |
// | |
// Created by Janosch Hübner on 03.11.18. | |
// Copyright © 2018 Janosch Hübner. All rights reserved. | |
// | |
import Foundation | |
struct PodLicense: Decodable { | |
let title: String | |
let content: String | |
enum CodingKeys: String, CodingKey { | |
case title = "Title" | |
case content = "FooterText" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PodLicensingViewController.swift | |
// | |
// Created by Janosch Hübner on 03.11.18. | |
// Copyright © 2018 Janosch Hübner. All rights reserved. | |
// | |
import UIKit | |
class PodLicensingViewController: UITableViewController { | |
private func loadAcknowledgements(fromFile file: String) -> PodAck? { | |
guard let ackData = try? Data(contentsOf: URL(fileURLWithPath: file)) else { | |
return nil | |
} | |
guard let podAck: PodAck = try? PropertyListDecoder().decode(PodAck.self, from: ackData) else { | |
return nil | |
} | |
return podAck | |
} | |
private var licenses: [PodLicense] { | |
get { | |
if let ackPath = Bundle.main.path(forResource: "Acknowledgements", ofType: "plist"), | |
let podAck = self.loadAcknowledgements(fromFile: ackPath) { | |
let licenses = podAck.licenses.filter({ (license: PodLicense) -> Bool in | |
return license.title.count > 0 && license.content.count > 0 | |
}) | |
return licenses.sorted(by: { (license1: PodLicense, license2: PodLicense) -> Bool in | |
return license1.title < license2.title | |
}) | |
} | |
return [] | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.tableView.separatorStyle = .none | |
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LicensesCell") | |
self.title = "Acknowledgements" | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return self.licenses.count | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "LicensesCell", for: indexPath) | |
let license: PodLicense = self.licenses[indexPath.section] | |
cell.textLabel?.text = license.content | |
cell.textLabel?.numberOfLines = 0 | |
cell.textLabel?.lineBreakMode = .byWordWrapping | |
cell.selectionStyle = .none | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
let license: PodLicense = self.licenses[section] | |
return license.title | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
tableView.deselectRow(at: indexPath, animated: true) | |
} | |
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
return UITableView.automaticDimension | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment