-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor StationViewController to remove storyboard dependency
- Loading branch information
Showing
11 changed files
with
439 additions
and
286 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
95 changes: 71 additions & 24 deletions
95
SwiftRadio/Cells/StationTableViewCell.swift
100755 → 100644
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,93 @@ | ||
// | ||
// StationTableViewCell.swift | ||
// Swift Radio | ||
// SwiftRadio | ||
// | ||
// Created by Matthew Fecher on 4/4/15. | ||
// Copyright (c) 2015 MatthewFecher.com. All rights reserved. | ||
// Created by Fethi El Hassasna on 2023-06-24. | ||
// Copyright © 2023 matthewfecher.com. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import NVActivityIndicatorView | ||
|
||
class StationTableViewCell: UITableViewCell { | ||
|
||
@IBOutlet weak var stationNameLabel: UILabel! | ||
@IBOutlet weak var stationDescLabel: UILabel! | ||
@IBOutlet weak var stationImageView: UIImageView! | ||
|
||
let stationImageView: UIImageView = { | ||
let imageView = UIImageView() | ||
imageView.contentMode = .scaleAspectFill | ||
imageView.clipsToBounds = true | ||
NSLayoutConstraint.activate([ | ||
imageView.heightAnchor.constraint(equalToConstant: 75), | ||
imageView.widthAnchor.constraint(equalToConstant: 110) | ||
]) | ||
return imageView | ||
}() | ||
|
||
let titleLabel: UILabel = { | ||
let label = UILabel() | ||
label.font = .preferredFont(forTextStyle: .title3) | ||
label.numberOfLines = 2 | ||
label.translatesAutoresizingMaskIntoConstraints = false | ||
return label | ||
}() | ||
|
||
let subtitleLabel: UILabel = { | ||
let label = UILabel() | ||
label.font = .preferredFont(forTextStyle: .footnote) | ||
label.translatesAutoresizingMaskIntoConstraints = false | ||
return label | ||
}() | ||
|
||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | ||
super.init(style: style, reuseIdentifier: reuseIdentifier) | ||
setupViews() | ||
} | ||
|
||
override func prepareForReuse() { | ||
super.prepareForReuse() | ||
titleLabel.text = nil | ||
subtitleLabel.text = nil | ||
stationImageView.image = nil | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private func setupViews() { | ||
|
||
override func awakeFromNib() { | ||
super.awakeFromNib() | ||
selectionStyle = .default | ||
|
||
// set ImageView shadow | ||
stationImageView.applyShadow() | ||
let vStackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel]) | ||
vStackView.spacing = 8 | ||
vStackView.axis = .vertical | ||
vStackView.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
// set table selection color | ||
let selectedView = UIView(frame: CGRect.zero) | ||
selectedView.backgroundColor = UIColor(red: 78/255, green: 82/255, blue: 93/255, alpha: 0.6) | ||
selectedBackgroundView = selectedView | ||
let hStackView = UIStackView(arrangedSubviews: [stationImageView, vStackView]) | ||
hStackView.spacing = 8 | ||
hStackView.axis = .horizontal | ||
hStackView.alignment = .center | ||
hStackView.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
contentView.addSubview(hStackView) | ||
|
||
NSLayoutConstraint.activate([ | ||
hStackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), | ||
hStackView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), | ||
hStackView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor), | ||
hStackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor) | ||
]) | ||
} | ||
} | ||
|
||
extension StationTableViewCell { | ||
func configureStationCell(station: RadioStation) { | ||
|
||
// Configure the cell... | ||
stationNameLabel.text = station.name | ||
stationDescLabel.text = station.desc | ||
titleLabel.text = station.name | ||
subtitleLabel.text = station.desc | ||
|
||
station.getImage { [weak self] image in | ||
self?.stationImageView.image = image | ||
} | ||
} | ||
|
||
override func prepareForReuse() { | ||
super.prepareForReuse() | ||
stationNameLabel.text = nil | ||
stationDescLabel.text = nil | ||
stationImageView.image = nil | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,6 @@ | |
|
||
import UIKit | ||
|
||
import UIKit | ||
|
||
struct Config { | ||
|
||
static let debugLog = true | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// UITableViewCell+reuseIdentifier.swift | ||
// SwiftRadio | ||
// | ||
// Created by Fethi El Hassasna on 2023-06-25. | ||
// Copyright © 2023 matthewfecher.com. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UITableViewCell { | ||
static var reuseIdentifier: String { | ||
return String(describing: self) | ||
} | ||
} | ||
|
||
extension UITableView { | ||
func register<T: UITableViewCell>(_: T.Type) { | ||
register(T.self, forCellReuseIdentifier: T.reuseIdentifier) | ||
} | ||
|
||
func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T { | ||
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else { | ||
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)") | ||
} | ||
return cell | ||
} | ||
} |
Oops, something went wrong.