Add public customisation

This commit is contained in:
Florian Gabach
2017-03-08 16:46:23 +01:00
parent 8746135531
commit ec4d296b9f
7 changed files with 134 additions and 33 deletions
@@ -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
}
}
@@ -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
}
@@ -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)
@@ -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
}
@@ -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()
}
@@ -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
}
}
+27 -1
View File
@@ -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" = "<your_translation>";
"Unable to load this licence." = "<your_translation>";
```
## 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 :