Small code refactoring
This commit is contained in:
@@ -11,8 +11,6 @@ import UIKit
|
||||
final class LicenceListViewController: UITableViewController {
|
||||
|
||||
// MARK: - Var
|
||||
|
||||
fileprivate let reuseIdentifier = "openSourceCell"
|
||||
|
||||
fileprivate var config: OpenSourceControllerConfig
|
||||
|
||||
@@ -40,7 +38,7 @@ final class LicenceListViewController: UITableViewController {
|
||||
|
||||
fileprivate func prepareTableView() {
|
||||
self.tableView.tableFooterView = UIView()
|
||||
self.tableView.register(OpenSourceTableViewCell.self, forCellReuseIdentifier: self.reuseIdentifier)
|
||||
self.tableView.register(cellType: OpenSourceTableViewCell.self)
|
||||
self.tableView.dataSource = self
|
||||
self.tableView.delegate = self
|
||||
self.tableView.separatorStyle = .singleLine
|
||||
@@ -61,19 +59,10 @@ extension LicenceListViewController {
|
||||
}
|
||||
|
||||
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: UITableViewCell.CellStyle.default, reuseIdentifier: self.reuseIdentifier)
|
||||
}
|
||||
|
||||
// Configure the cell with licence
|
||||
if let licence = self.downloadedLicence.get(at: indexPath.row) {
|
||||
cell?.configure(licence: licence, config: self.config)
|
||||
}
|
||||
|
||||
return cell!
|
||||
let cell: OpenSourceTableViewCell = tableView.dequeueReusableCell(for: indexPath)
|
||||
guard let licence = self.downloadedLicence.get(at: indexPath.row) else { return cell }
|
||||
cell.configure(licence: licence, config: self.config)
|
||||
return cell
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
final class OpenSourceTableViewCell: UITableViewCell {
|
||||
final class OpenSourceTableViewCell: UITableViewCell, Reusable {
|
||||
|
||||
// MARK: - Lifecycle
|
||||
|
||||
@@ -29,15 +29,10 @@ final class OpenSourceTableViewCell: UITableViewCell {
|
||||
|
||||
// MARK: - Configure
|
||||
|
||||
/// Initialize the cell content with Licence model
|
||||
///
|
||||
/// - Parameter licence: the licence model
|
||||
func configure(licence: LicenceFile, config: OpenSourceControllerConfig) {
|
||||
// Set text
|
||||
self.textLabel?.attributedText = licence.attributedContent
|
||||
self.textLabel?.textColor = config.uiConfig.licenceTextColor
|
||||
|
||||
// Background
|
||||
if let backgroundColor = config.uiConfig.licenceBackgroundColor {
|
||||
self.backgroundColor = backgroundColor
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ final class OpenSourceViewController: UIViewController {
|
||||
|
||||
fileprivate var licences: [LicenceFile]
|
||||
|
||||
fileprivate var licenceLoader: LicenceLoader
|
||||
|
||||
fileprivate var config: OpenSourceControllerConfig
|
||||
|
||||
fileprivate lazy var loadingController = {
|
||||
@@ -28,7 +30,8 @@ final class OpenSourceViewController: UIViewController {
|
||||
|
||||
// MARK: - Lifecyle
|
||||
|
||||
init(licences: [LicenceFile], showCloseButton: Bool, configuration: OpenSourceControllerConfig) {
|
||||
init(licences: [LicenceFile], showCloseButton: Bool, configuration: OpenSourceControllerConfig, licenceLoader: LicenceLoader) {
|
||||
self.licenceLoader = licenceLoader
|
||||
self.licences = licences
|
||||
self.showCloseButton = showCloseButton
|
||||
self.config = configuration
|
||||
@@ -56,12 +59,10 @@ final class OpenSourceViewController: UIViewController {
|
||||
// MARK: - Prepare
|
||||
|
||||
fileprivate func prepareStyle() {
|
||||
// Apply navigation bar tint color if needed
|
||||
if let tintColor = self.config.uiConfig.barTintColor {
|
||||
self.navigationController?.navigationBar.barTintColor = tintColor
|
||||
}
|
||||
|
||||
// Apply navigation bar text color if needed
|
||||
if let textColor = self.config.uiConfig.titleColor {
|
||||
let attribut = [NSAttributedString.Key.foregroundColor: textColor]
|
||||
self.navigationController?.navigationBar.titleTextAttributes = attribut
|
||||
@@ -69,7 +70,7 @@ final class OpenSourceViewController: UIViewController {
|
||||
}
|
||||
|
||||
fileprivate func prepareLicences() {
|
||||
LicenceLoader.downloadLicences(licences: licences, config: config) { [weak self] in
|
||||
self.licenceLoader.downloadLicences(licences: licences, config: config) { [weak self] in
|
||||
guard let strongSelf = self else { return }
|
||||
let listController = LicenceListViewController(downloadedLicence: strongSelf.licences, config: strongSelf.config)
|
||||
strongSelf.loadingController.remove()
|
||||
|
||||
@@ -13,7 +13,7 @@ final class LicenceLoader {
|
||||
/// - Parameters:
|
||||
/// - licences: array which contains licence model model
|
||||
/// - completion: end downloading completion
|
||||
class func downloadLicences(licences: [LicenceFile],
|
||||
func downloadLicences(licences: [LicenceFile],
|
||||
config: OpenSourceControllerConfig,
|
||||
completion: @escaping () -> Void) {
|
||||
let licenceGroupe = DispatchGroup()
|
||||
|
||||
@@ -18,7 +18,11 @@ open class OpenSourceController: NSObject {
|
||||
///
|
||||
/// - Parameter from: the source controller
|
||||
open func presentOpenSourceController(from: UIViewController) {
|
||||
let licenceController = OpenSourceViewController(licences: licences, showCloseButton: true, configuration: config)
|
||||
let loader: LicenceLoader = LicenceLoader()
|
||||
let licenceController = OpenSourceViewController(licences: licences,
|
||||
showCloseButton: true,
|
||||
configuration: config,
|
||||
licenceLoader: loader)
|
||||
let navController = UINavigationController(rootViewController: licenceController)
|
||||
from.present(navController, animated: true)
|
||||
}
|
||||
@@ -27,7 +31,11 @@ open class OpenSourceController: NSObject {
|
||||
///
|
||||
/// - Parameter from: the source controller
|
||||
open func pushOpenSourceController(from: UIViewController) {
|
||||
let licenceController = OpenSourceViewController(licences: licences, showCloseButton: false, configuration: config)
|
||||
let loader: LicenceLoader = LicenceLoader()
|
||||
let licenceController = OpenSourceViewController(licences: licences,
|
||||
showCloseButton: false,
|
||||
configuration: config,
|
||||
licenceLoader: loader)
|
||||
guard let navigationController = from.navigationController else {
|
||||
assertionFailure("Source controller isn't embeded in navigation controller. Can't push.")
|
||||
return
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Reusable.swift
|
||||
// OpenSourceControllerDemo
|
||||
//
|
||||
// Created by Florian Gabach on 10/12/2018.
|
||||
// Copyright © 2018 OpenSourceController. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol Reusable: class {
|
||||
static var reuseIdentifier: String { get }
|
||||
}
|
||||
|
||||
public extension Reusable {
|
||||
static var reuseIdentifier: String {
|
||||
return String(describing: self)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// UITableView+Extensions.swift
|
||||
// OpenSourceControllerDemo
|
||||
//
|
||||
// Created by Florian Gabach on 10/12/2018.
|
||||
// Copyright © 2018 OpenSourceController. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UITableView {
|
||||
final func register<T: UITableViewCell>(cellType: T.Type)
|
||||
where T: Reusable {
|
||||
self.register(cellType.self, forCellReuseIdentifier: cellType.reuseIdentifier)
|
||||
}
|
||||
|
||||
final func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath, cellType: T.Type = T.self) -> T
|
||||
where T: Reusable {
|
||||
guard let cell = self.dequeueReusableCell(withIdentifier: cellType.reuseIdentifier, for: indexPath) as? T else {
|
||||
fatalError(
|
||||
"Failed to dequeue a cell with identifier \(cellType.reuseIdentifier) matching type \(cellType.self). "
|
||||
+ "Check that the reuseIdentifier is set properly in your XIB/Storyboard "
|
||||
+ "and that you registered the cell beforehand"
|
||||
)
|
||||
}
|
||||
return cell
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user