Skip to content

Commit 8a433a4

Browse files
authored
Merge pull request #186 from analogcode/dev
Refactor StationViewController to remove storyboard dependency
2 parents a4a76c0 + d34aaf7 commit 8a433a4

File tree

11 files changed

+439
-286
lines changed

11 files changed

+439
-286
lines changed

SwiftRadio.xcodeproj/project.pbxproj

Lines changed: 92 additions & 23 deletions
Large diffs are not rendered by default.

SwiftRadio.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SwiftRadio/Cells/StationTableViewCell.swift

100755100644
Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,93 @@
11
//
22
// StationTableViewCell.swift
3-
// Swift Radio
3+
// SwiftRadio
44
//
5-
// Created by Matthew Fecher on 4/4/15.
6-
// Copyright (c) 2015 MatthewFecher.com. All rights reserved.
5+
// Created by Fethi El Hassasna on 2023-06-24.
6+
// Copyright © 2023 matthewfecher.com. All rights reserved.
77
//
88

99
import UIKit
10+
import NVActivityIndicatorView
1011

1112
class StationTableViewCell: UITableViewCell {
12-
13-
@IBOutlet weak var stationNameLabel: UILabel!
14-
@IBOutlet weak var stationDescLabel: UILabel!
15-
@IBOutlet weak var stationImageView: UIImageView!
13+
14+
let stationImageView: UIImageView = {
15+
let imageView = UIImageView()
16+
imageView.contentMode = .scaleAspectFill
17+
imageView.clipsToBounds = true
18+
NSLayoutConstraint.activate([
19+
imageView.heightAnchor.constraint(equalToConstant: 75),
20+
imageView.widthAnchor.constraint(equalToConstant: 110)
21+
])
22+
return imageView
23+
}()
24+
25+
let titleLabel: UILabel = {
26+
let label = UILabel()
27+
label.font = .preferredFont(forTextStyle: .title3)
28+
label.numberOfLines = 2
29+
label.translatesAutoresizingMaskIntoConstraints = false
30+
return label
31+
}()
32+
33+
let subtitleLabel: UILabel = {
34+
let label = UILabel()
35+
label.font = .preferredFont(forTextStyle: .footnote)
36+
label.translatesAutoresizingMaskIntoConstraints = false
37+
return label
38+
}()
39+
40+
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
41+
super.init(style: style, reuseIdentifier: reuseIdentifier)
42+
setupViews()
43+
}
44+
45+
override func prepareForReuse() {
46+
super.prepareForReuse()
47+
titleLabel.text = nil
48+
subtitleLabel.text = nil
49+
stationImageView.image = nil
50+
}
51+
52+
required init?(coder: NSCoder) {
53+
fatalError("init(coder:) has not been implemented")
54+
}
55+
56+
private func setupViews() {
1657

17-
override func awakeFromNib() {
18-
super.awakeFromNib()
58+
selectionStyle = .default
1959

20-
// set ImageView shadow
21-
stationImageView.applyShadow()
60+
let vStackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
61+
vStackView.spacing = 8
62+
vStackView.axis = .vertical
63+
vStackView.translatesAutoresizingMaskIntoConstraints = false
2264

23-
// set table selection color
24-
let selectedView = UIView(frame: CGRect.zero)
25-
selectedView.backgroundColor = UIColor(red: 78/255, green: 82/255, blue: 93/255, alpha: 0.6)
26-
selectedBackgroundView = selectedView
65+
let hStackView = UIStackView(arrangedSubviews: [stationImageView, vStackView])
66+
hStackView.spacing = 8
67+
hStackView.axis = .horizontal
68+
hStackView.alignment = .center
69+
hStackView.translatesAutoresizingMaskIntoConstraints = false
70+
71+
contentView.addSubview(hStackView)
72+
73+
NSLayoutConstraint.activate([
74+
hStackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
75+
hStackView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
76+
hStackView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
77+
hStackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor)
78+
])
2779
}
80+
}
2881

82+
extension StationTableViewCell {
2983
func configureStationCell(station: RadioStation) {
3084

3185
// Configure the cell...
32-
stationNameLabel.text = station.name
33-
stationDescLabel.text = station.desc
86+
titleLabel.text = station.name
87+
subtitleLabel.text = station.desc
3488

3589
station.getImage { [weak self] image in
3690
self?.stationImageView.image = image
3791
}
3892
}
39-
40-
override func prepareForReuse() {
41-
super.prepareForReuse()
42-
stationNameLabel.text = nil
43-
stationDescLabel.text = nil
44-
stationImageView.image = nil
45-
}
4693
}

SwiftRadio/Config.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import UIKit
1010

11-
import UIKit
12-
1311
struct Config {
1412

1513
static let debugLog = true

SwiftRadio/Coordinators/MainCoordinator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MainCoordinator: NavigationCoordinator {
5555

5656
extension MainCoordinator: LoaderControllerDelegate {
5757
func didFinishLoading(_ controller: LoaderController, stations: [RadioStation]) {
58-
let stationsVC = Storyboard.viewController as StationsViewController
58+
let stationsVC = StationsViewController()
5959
stationsVC.delegate = self
6060
navigationController.setViewControllers([stationsVC], animated: false)
6161
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// UITableViewCell+reuseIdentifier.swift
3+
// SwiftRadio
4+
//
5+
// Created by Fethi El Hassasna on 2023-06-25.
6+
// Copyright © 2023 matthewfecher.com. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UITableViewCell {
12+
static var reuseIdentifier: String {
13+
return String(describing: self)
14+
}
15+
}
16+
17+
extension UITableView {
18+
func register<T: UITableViewCell>(_: T.Type) {
19+
register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
20+
}
21+
22+
func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T {
23+
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
24+
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
25+
}
26+
return cell
27+
}
28+
}

0 commit comments

Comments
 (0)