From ec4d296b9f7fef891472752b6d2ae9e546358d82 Mon Sep 17 00:00:00 2001 From: Florian Gabach Date: Wed, 8 Mar 2017 16:46:23 +0100 Subject: [PATCH 1/4] Add public customisation --- .../OpenSourceController/ViewController.swift | 43 ++++++++++++++++--- .../Controller/AppearenceManager.swift | 17 -------- .../Controller/OpenSourceController.swift | 10 ++++- .../Controller/OpenSourceViewController.swift | 24 ++++++++--- .../Model/OpenSourceControllerConfig.swift | 38 ++++++++++++++++ .../View/OpenSourceTableViewCell.swift | 7 ++- README.md | 28 +++++++++++- 7 files changed, 134 insertions(+), 33 deletions(-) delete mode 100644 OpenSourceController/Classes/Controller/AppearenceManager.swift create mode 100644 OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift diff --git a/Example/OpenSourceController/ViewController.swift b/Example/OpenSourceController/ViewController.swift index 3a9900b..db6db7a 100644 --- a/Example/OpenSourceController/ViewController.swift +++ b/Example/OpenSourceController/ViewController.swift @@ -11,6 +11,8 @@ import OpenSourceController class ViewController: UIViewController { + // MARK: - Lifecycle + override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib @@ -21,21 +23,50 @@ class ViewController: UIViewController { // Dispose of any resources that can be recreated. } + // MARK: - Action + + /// Handle button click & show the controller with licences @IBAction func showOpenSource(_ sender: Any) { // Create controller let openSourceVC = OpenSourceController() + // Apply somre customisation if you want ! + self.controllerCustomUI(controller: openSourceVC) + // Init with LicenceFile object openSourceVC.licences = [LicenceFile(title: "FacebookImagePicker", url: "https://raw.githubusercontent.com/terflogag/FacebookImagePicker/master/LICENSE"), -// LicenceFile(title: "JSQMessagesViewController", -// url: "https://raw.githubusercontent.com/jessesquires/JSQMessagesViewController/develop/LICENSE"), -// LicenceFile(title: "OpenSourceController", -// url: "https://raw.githubusercontent.com/terflogag/OpenSourceController/master/LICENSE"), - LicenceFile(title: "Twitter & Fabric", - url: " http://get.fabric.io/terms/opensource.txt")] + LicenceFile(title: "JSQMessagesViewController", + url: "https://raw.githubusercontent.com/jessesquires/JSQMessagesViewController/develop/LICENSE"), + LicenceFile(title: "OpenSourceController", + url: "https://raw.githubusercontent.com/terflogag/OpenSourceController/master/LICENSE")] // Present controller openSourceVC.presentOpenSourceController(from: self) } + + // Customization + + fileprivate func controllerCustomUI(controller: OpenSourceController) { + // Navigation bar title + controller.config.title = "MyCustomTitle" + + // Navigation bar tint color + controller.config.uiConfig.barTintColor = UIColor.red + + // Close button color + controller.config.uiConfig.closeButtonColor = UIColor.white + + // BackgroundColor + controller.config.uiConfig.backgroundColor = UIColor.red.withAlphaComponent(0.6) + + // Licence text color + controller.config.uiConfig.licenceTextColor = UIColor.white + + // Navigation bar title color + controller.config.uiConfig.titleColor = UIColor.white + + // Licence cell background color + controller.config.uiConfig.licenceBackgroundColor = UIColor.red + } } diff --git a/OpenSourceController/Classes/Controller/AppearenceManager.swift b/OpenSourceController/Classes/Controller/AppearenceManager.swift deleted file mode 100644 index 7627a5a..0000000 --- a/OpenSourceController/Classes/Controller/AppearenceManager.swift +++ /dev/null @@ -1,17 +0,0 @@ -// -// AppearenceManager.swift -// Pods -// -// Created by Florian Gabach on 01/03/2017. -// -// - -import UIKit - -class AppearenceManager: NSObject { - // White color : http://www.color-hex.com/color/ffffff - static let whiteCustom = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1.0) - - // Black color : http://www.color-hex.com/color/000000 - static let black = UIColor.black -} diff --git a/OpenSourceController/Classes/Controller/OpenSourceController.swift b/OpenSourceController/Classes/Controller/OpenSourceController.swift index efcb9cf..dc82e64 100644 --- a/OpenSourceController/Classes/Controller/OpenSourceController.swift +++ b/OpenSourceController/Classes/Controller/OpenSourceController.swift @@ -11,18 +11,24 @@ import UIKit public class OpenSourceController: NSObject { // Contains list of licence object - public var licences: [LicenceFile]? + open var licences: [LicenceFile]? + + // Client-side authorization options. + open var config = OpenSourceControllerConfig() /// Present OpenSourceViewController embbeded in navigation controller /// /// - Parameter from: the source controller - public final func presentOpenSourceController(from: UIViewController) { + open func presentOpenSourceController(from: UIViewController) { // Create open source controller let licenceController = OpenSourceViewController() // Init licence licenceController.licences = self.licences + // Init config + licenceController.config = self.config + // Embed in navigation controller let navController = UINavigationController(rootViewController: licenceController) diff --git a/OpenSourceController/Classes/Controller/OpenSourceViewController.swift b/OpenSourceController/Classes/Controller/OpenSourceViewController.swift index 48f8465..0daebca 100644 --- a/OpenSourceController/Classes/Controller/OpenSourceViewController.swift +++ b/OpenSourceController/Classes/Controller/OpenSourceViewController.swift @@ -19,7 +19,10 @@ class OpenSourceViewController: UITableViewController { private let reuseIdentifier = "openSourceCell" /// Contains all licence object before downloaded - var licences: [LicenceFile]? + internal var licences: [LicenceFile]? + + // Controller configuration for customization + internal var config = OpenSourceControllerConfig() /// Contains all licences object after downloaded fileprivate var downloadedLicence: [LicenceFile]? { @@ -65,18 +68,24 @@ class OpenSourceViewController: UITableViewController { fileprivate func prepareTableView() { // Common init self.tableView.tableFooterView = UIView() - self.title = NSLocalizedString("Tiers library", comment: "") - self.tableView.register(OpenSourceTableViewCell.self, forCellReuseIdentifier: self.reuseIdentifier) + self.title = self.config.title + self.tableView.register(OpenSourceTableViewCell.self, + forCellReuseIdentifier: self.reuseIdentifier) self.tableView.dataSource = self self.tableView.delegate = self self.tableView.separatorStyle = .singleLine - self.tableView.backgroundColor = AppearenceManager.whiteCustom + self.tableView.backgroundColor = config.uiConfig.backgroundColor + + // Navigation bar + self.navigationController?.navigationBar.barTintColor = self.config.uiConfig.barTintColor + let attribut = [NSForegroundColorAttributeName: self.config.uiConfig.titleColor] + self.navigationController?.navigationBar.titleTextAttributes = attribut // Close button (on the right corner of navigation bar) let closeButton = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(self.closePicker)) - closeButton.tintColor = AppearenceManager.black + closeButton.tintColor = config.uiConfig.closeButtonColor self.navigationItem.rightBarButtonItem = closeButton // Loading indicator @@ -121,19 +130,22 @@ class OpenSourceViewController: UITableViewController { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: self.reuseIdentifier, for: indexPath) as? OpenSourceTableViewCell + // Init cell if cell == nil { cell = OpenSourceTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: self.reuseIdentifier) } + // Configure the cell with licence if let licence = self.downloadedLicence?.get(at: indexPath.row) { - cell?.configure(licence: licence) + cell?.configure(licence: licence, config: self.config) } return cell! } override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { + // Automatic dimension return UITableViewAutomaticDimension } diff --git a/OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift b/OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift new file mode 100644 index 0000000..1caae08 --- /dev/null +++ b/OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift @@ -0,0 +1,38 @@ +// +// OpenSourceControllerConfig.swift +// Pods +// +// Created by Florian Gabach on 08/03/2017. +// +// + +/// Simple struct to hold settings +public struct OpenSourceControllerConfig { + + /// Sub-stuct holding configuration relevant to UI presentation ! + public struct UIConfig { + /// Will be applied to the navigation bar + public var barTintColor: UIColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1.0) + + /// Will be applied to the navigation bar + public var backgroundColor: UIColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1.0) + + /// Will be applied to licence text + public var licenceBackgroundColor: UIColor = UIColor.white + + /// Will be applied to the navigation bar + public var closeButtonColor: UIColor = UIColor.black + + /// Will be applied to licence text + public var licenceTextColor: UIColor = UIColor.black + + /// Will be applied to licence text + public var titleColor: UIColor = UIColor.black + } + + /// Will be applied to the navigation bar title + public var title: String = "Tiers library" + + /// UI-specific configuration. + public var uiConfig = UIConfig() +} diff --git a/OpenSourceController/Classes/View/OpenSourceTableViewCell.swift b/OpenSourceController/Classes/View/OpenSourceTableViewCell.swift index 7679769..9be19c2 100644 --- a/OpenSourceController/Classes/View/OpenSourceTableViewCell.swift +++ b/OpenSourceController/Classes/View/OpenSourceTableViewCell.swift @@ -16,6 +16,7 @@ class OpenSourceTableViewCell: UITableViewCell { super.init(style: .default, reuseIdentifier: reuseIdentifier) + // Prepare cell self.prepareCellComponent() } @@ -47,7 +48,7 @@ class OpenSourceTableViewCell: UITableViewCell { /// Initialize the cell content with Licence model /// /// - Parameter licence: the licence model - func configure(licence: LicenceFile) { + func configure(licence: LicenceFile, config: OpenSourceControllerConfig) { // Build attributed text let libraryTitleAttribut = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize + 2)] @@ -60,5 +61,9 @@ class OpenSourceTableViewCell: UITableViewCell { // Set text self.textLabel?.attributedText = libraryTitle + self.textLabel?.textColor = config.uiConfig.licenceTextColor + + // Background + self.backgroundColor = config.uiConfig.licenceBackgroundColor } } diff --git a/README.md b/README.md index 01a8e63..ea7246b 100644 --- a/README.md +++ b/README.md @@ -80,10 +80,36 @@ openSourceVC.presentOpenSourceController(from: self) OpenSourceController is currently write in english. If you need translation for the permission popup (or whatever thing), just add this line in your localized file : ``` -"Tiers library" = ""; "Unable to load this licence." = ""; ``` +## Customisation / configuration + +You can apply some customisation. To do it you can use the OpenSourceControllerConfig structure like this : + +```swift +// Navigation bar title +controller.config.title = "MyCustomTitle" + +// Navigation bar tint color +controller.config.uiConfig.barTintColor = UIColor.red + +// Close button color +controller.config.uiConfig.closeButtonColor = UIColor.white + +// BackgroundColor +controller.config.uiConfig.backgroundColor = UIColor.red.withAlphaComponent(0.6) + +// Licence text color +controller.config.uiConfig.licenceTextColor = UIColor.white + +// Navigation bar title color +controller.config.uiConfig.titleColor = UIColor.white + +// Licence cell background color +controller.config.uiConfig.licenceBackgroundColor = UIColor.red +``` + ## Applications Some applications already use this controller like : From 797b669aa18b4e18d72a674592e1d16c9a642f83 Mon Sep 17 00:00:00 2001 From: Florian Gabach Date: Wed, 8 Mar 2017 16:47:14 +0100 Subject: [PATCH 2/4] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ea7246b..79f7b9f 100644 --- a/README.md +++ b/README.md @@ -75,14 +75,6 @@ url: "https://raw.githubusercontent.com/jessesquires/JSQMessagesViewController/d openSourceVC.presentOpenSourceController(from: self) ``` -## Translation - -OpenSourceController is currently write in english. If you need translation for the permission popup (or whatever thing), just add this line in your localized file : - -``` -"Unable to load this licence." = ""; -``` - ## Customisation / configuration You can apply some customisation. To do it you can use the OpenSourceControllerConfig structure like this : @@ -110,6 +102,14 @@ controller.config.uiConfig.titleColor = UIColor.white controller.config.uiConfig.licenceBackgroundColor = UIColor.red ``` +## Translation + +OpenSourceController is currently write in english. If you need translation for the permission popup (or whatever thing), just add this line in your localized file : + +``` +"Unable to load this licence." = ""; +``` + ## Applications Some applications already use this controller like : From ddca5365fc8da0b59935b2f5726286da954ac6c5 Mon Sep 17 00:00:00 2001 From: Florian Gabach Date: Wed, 8 Mar 2017 16:52:24 +0100 Subject: [PATCH 3/4] Add verbose mode --- .../OpenSourceController/ViewController.swift | 21 +++++++++++-------- .../Controller/OpenSourceViewController.swift | 3 ++- .../Classes/Model/LicenceDownloader.swift | 3 ++- .../Classes/Model/LicenceFile.swift | 7 +++++-- .../Model/OpenSourceControllerConfig.swift | 3 +++ README.md | 17 ++++++++------- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Example/OpenSourceController/ViewController.swift b/Example/OpenSourceController/ViewController.swift index db6db7a..3486190 100644 --- a/Example/OpenSourceController/ViewController.swift +++ b/Example/OpenSourceController/ViewController.swift @@ -31,7 +31,7 @@ class ViewController: UIViewController { let openSourceVC = OpenSourceController() // Apply somre customisation if you want ! - self.controllerCustomUI(controller: openSourceVC) + //self.controllerCustomUI(openSourceVC: openSourceVC) // Init with LicenceFile object openSourceVC.licences = [LicenceFile(title: "FacebookImagePicker", @@ -47,26 +47,29 @@ class ViewController: UIViewController { // Customization - fileprivate func controllerCustomUI(controller: OpenSourceController) { + fileprivate func controllerCustomUI(openSourceVC: OpenSourceController) { // Navigation bar title - controller.config.title = "MyCustomTitle" + openSourceVC.config.title = "MyCustomTitle" // Navigation bar tint color - controller.config.uiConfig.barTintColor = UIColor.red + openSourceVC.config.uiConfig.barTintColor = UIColor.red // Close button color - controller.config.uiConfig.closeButtonColor = UIColor.white + openSourceVC.config.uiConfig.closeButtonColor = UIColor.white // BackgroundColor - controller.config.uiConfig.backgroundColor = UIColor.red.withAlphaComponent(0.6) + openSourceVC.config.uiConfig.backgroundColor = UIColor.red.withAlphaComponent(0.6) // Licence text color - controller.config.uiConfig.licenceTextColor = UIColor.white + openSourceVC.config.uiConfig.licenceTextColor = UIColor.white // Navigation bar title color - controller.config.uiConfig.titleColor = UIColor.white + openSourceVC.config.uiConfig.titleColor = UIColor.white // Licence cell background color - controller.config.uiConfig.licenceBackgroundColor = UIColor.red + openSourceVC.config.uiConfig.licenceBackgroundColor = UIColor.red + + // Verbose mode + openSourceVC.config.verbose = true } } diff --git a/OpenSourceController/Classes/Controller/OpenSourceViewController.swift b/OpenSourceController/Classes/Controller/OpenSourceViewController.swift index 0daebca..8a24038 100644 --- a/OpenSourceController/Classes/Controller/OpenSourceViewController.swift +++ b/OpenSourceController/Classes/Controller/OpenSourceViewController.swift @@ -57,7 +57,8 @@ class OpenSourceViewController: UITableViewController { fileprivate func prepareLicences() { if let licences = self.licences { self.startLoading() - LicenceDownloader.downloadLicences(licences: licences) { () in + LicenceDownloader.downloadLicences(licences: licences, + config: self.config) { () in // Update licences self.downloadedLicence = self.licences } diff --git a/OpenSourceController/Classes/Model/LicenceDownloader.swift b/OpenSourceController/Classes/Model/LicenceDownloader.swift index faf8264..a34ebb9 100644 --- a/OpenSourceController/Classes/Model/LicenceDownloader.swift +++ b/OpenSourceController/Classes/Model/LicenceDownloader.swift @@ -16,6 +16,7 @@ class LicenceDownloader: NSObject { /// - licences: array which contains licence model model /// - completion: end downloading completion class func downloadLicences(licences: [LicenceFile], + config: OpenSourceControllerConfig, completion: @escaping () -> Void) { // Number of licences let licenceCount = licences.count @@ -25,7 +26,7 @@ class LicenceDownloader: NSObject { // Download licence's detail for every lience for licence in licences { - licence.downloadLicenceDetail(completion: { () in + licence.downloadLicenceDetail(config: config, completion: { () in alreadyDownloaded += 1 // If this is the last licence, perfom completion diff --git a/OpenSourceController/Classes/Model/LicenceFile.swift b/OpenSourceController/Classes/Model/LicenceFile.swift index 0e21e00..0911b08 100644 --- a/OpenSourceController/Classes/Model/LicenceFile.swift +++ b/OpenSourceController/Classes/Model/LicenceFile.swift @@ -38,7 +38,8 @@ public class LicenceFile: NSObject { /// Download licence detail /// /// - Parameter completion: end of download handler - func downloadLicenceDetail(completion: @escaping (Void) -> Void) { + func downloadLicenceDetail(config: OpenSourceControllerConfig, + completion: @escaping (Void) -> Void) { if let url = URL(string: self.url) { let task = URLSession.shared.dataTask(with: url) { (data, _, _) in if let data = data, @@ -48,7 +49,9 @@ public class LicenceFile: NSObject { self.detail = html // Log - print("Licence for \(self.title ?? "") downloaded with success.") + if config.verbose { + print("Licence for \(self.title ?? "") downloaded with success.") + } } completion() } diff --git a/OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift b/OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift index 1caae08..394fbcb 100644 --- a/OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift +++ b/OpenSourceController/Classes/Model/OpenSourceControllerConfig.swift @@ -33,6 +33,9 @@ public struct OpenSourceControllerConfig { /// Will be applied to the navigation bar title public var title: String = "Tiers library" + // Verbose mode for print log when licence are downloaded + public var verbose: Bool = false + /// UI-specific configuration. public var uiConfig = UIConfig() } diff --git a/README.md b/README.md index 79f7b9f..e3e2f6e 100644 --- a/README.md +++ b/README.md @@ -81,25 +81,28 @@ You can apply some customisation. To do it you can use the OpenSourceControllerC ```swift // Navigation bar title -controller.config.title = "MyCustomTitle" +openSourceVC.config.title = "MyCustomTitle" // Navigation bar tint color -controller.config.uiConfig.barTintColor = UIColor.red +openSourceVC.config.uiConfig.barTintColor = UIColor.red // Close button color -controller.config.uiConfig.closeButtonColor = UIColor.white +openSourceVC.config.uiConfig.closeButtonColor = UIColor.white // BackgroundColor -controller.config.uiConfig.backgroundColor = UIColor.red.withAlphaComponent(0.6) +openSourceVC.config.uiConfig.backgroundColor = UIColor.red.withAlphaComponent(0.6) // Licence text color -controller.config.uiConfig.licenceTextColor = UIColor.white +openSourceVC.config.uiConfig.licenceTextColor = UIColor.white // Navigation bar title color -controller.config.uiConfig.titleColor = UIColor.white +openSourceVC.config.uiConfig.titleColor = UIColor.white // Licence cell background color -controller.config.uiConfig.licenceBackgroundColor = UIColor.red +openSourceVC.config.uiConfig.licenceBackgroundColor = UIColor.red + +// Verbose mode +openSourceVC.config.verbose = true ``` ## Translation From c39454be01fa772c55b8a94a1492ea02973f52f9 Mon Sep 17 00:00:00 2001 From: Florian Gabach Date: Wed, 8 Mar 2017 16:54:29 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e3e2f6e..d677719 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@

Cocoapods version - Cocoapods licence Cocoapods plateform Prs welcome

@@ -14,6 +13,7 @@ • Installation • Usage • Translation +• Customisation • License

@@ -75,7 +75,7 @@ url: "https://raw.githubusercontent.com/jessesquires/JSQMessagesViewController/d openSourceVC.presentOpenSourceController(from: self) ``` -## Customisation / configuration +## Customisation You can apply some customisation. To do it you can use the OpenSourceControllerConfig structure like this :