Last active
July 15, 2022 10:35
-
-
Save amfathi/98f2e3426b5f25764751bbc73f350503 to your computer and use it in GitHub Desktop.
**Base** Views and Controllers (SnapKit + RxSwift)
This file contains hidden or 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
| import UIKit | |
| class BaseCollectionViewCell: UICollectionViewCell, Identifiable { | |
| override init(frame: CGRect) { | |
| super.init(frame: .zero) | |
| setupViews() | |
| setupConstraints() | |
| } | |
| required init?(coder: NSCoder) { | |
| super.init(coder: coder) | |
| } | |
| // MARK: - Layout | |
| /// Override to setup your views and subviews. | |
| func setupViews() {} | |
| /// Override to setup your views constraints. | |
| func setupConstraints() {} | |
| } |
This file contains hidden or 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
| import UIKit | |
| class BaseTableViewCell: UITableViewCell, Identifiable { | |
| override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
| super.init(style: style, reuseIdentifier: reuseIdentifier) | |
| setupViews() | |
| setupConstraints() | |
| } | |
| required init?(coder: NSCoder) { | |
| super.init(coder: coder) | |
| } | |
| // MARK: - Layout | |
| /// Override to setup your views and subviews. | |
| func setupViews() {} | |
| /// Override to setup your views constraints. | |
| func setupConstraints() {} | |
| } |
This file contains hidden or 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
| import UIKit | |
| class BaseView: UIView { | |
| // MARK: - Initializers | |
| override init(frame: CGRect) { | |
| super.init(frame: .zero) | |
| // Setting up subviews | |
| setupViews() | |
| setupConstraints() | |
| setupContentData() | |
| // Binding | |
| bindViews() | |
| bindActions() | |
| } | |
| required init?(coder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| // MARK: - Layout | |
| /// Override to setup your views and subviews. | |
| func setupViews() {} | |
| /// Override to setup your views constraints. | |
| func setupConstraints() {} | |
| /// Override to setup your static data to your views. | |
| func setupContentData() {} | |
| // MARK: - Binding | |
| /// Override to bind your views to your view model. | |
| func bindViews() {} | |
| /// Override to bind your actions to your view model. | |
| func bindActions() {} | |
| } |
This file contains hidden or 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
| import UIKit | |
| class BaseViewController: UIViewController { | |
| // MARK: - Lifecycle | |
| // No need to implement `viewDidLoad`. | |
| // Instead implement `setupViews()` and `setupConstraints()`, still no harm of implementing it. | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Base Customization | |
| setupBaseCustomization() | |
| // Setting up views & content (Overrideable functions) | |
| setupViews() | |
| setupConstraints() | |
| setupContentData() | |
| // Binding | |
| bindViews() | |
| bindActions() | |
| } | |
| override func viewWillDisappear(_ animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| // force-close the keybaord when the viewController is disappearing | |
| view.endEditing(true) | |
| } | |
| private func setupBaseCustomization() { | |
| view.backgroundColor = .mainBackground | |
| navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) | |
| } | |
| // MARK: - Layout | |
| /// Override to setup your views and subviews. | |
| func setupViews() {} | |
| /// Override to setup your views constraints. | |
| func setupConstraints() {} | |
| /// Override to setup your static data to your views. | |
| func setupContentData() {} | |
| // MARK: - Binding | |
| /// Override to bind your views to your view model. | |
| func bindViews() {} | |
| /// Override to bind your actions to your view model. | |
| func bindActions() {} | |
| } |
This file contains hidden or 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
| protocol Identifiable { | |
| static var identifier: String { get } | |
| } | |
| extension Identifiable where Self: UITableViewCell { | |
| static var identifier: String { | |
| return self.classForCoder().description() | |
| } | |
| } | |
| extension Identifiable where Self: UICollectionViewCell { | |
| static var identifier: String { | |
| return self.classForCoder().description() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment