Skip to content

Commit

Permalink
Merge pull request #186 from analogcode/dev
Browse files Browse the repository at this point in the history
Refactor StationViewController to remove storyboard dependency
  • Loading branch information
fethica committed Jun 26, 2023
2 parents a4a76c0 + d34aaf7 commit 8a433a4
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 286 deletions.
115 changes: 92 additions & 23 deletions SwiftRadio.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"revision" : "c3f472ed8cb59b442312362bca363863962c88b3"
}
},
{
"identity" : "nvactivityindicatorview",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ninjaprox/NVActivityIndicatorView.git",
"state" : {
"revision" : "bcb52371f2259254bac6690f92bb474a61768c47",
"version" : "5.1.1"
}
},
{
"identity" : "spring",
"kind" : "remoteSourceControl",
Expand Down
95 changes: 71 additions & 24 deletions SwiftRadio/Cells/StationTableViewCell.swift
100755 → 100644
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
}
}
2 changes: 0 additions & 2 deletions SwiftRadio/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import UIKit

import UIKit

struct Config {

static let debugLog = true
Expand Down
2 changes: 1 addition & 1 deletion SwiftRadio/Coordinators/MainCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MainCoordinator: NavigationCoordinator {

extension MainCoordinator: LoaderControllerDelegate {
func didFinishLoading(_ controller: LoaderController, stations: [RadioStation]) {
let stationsVC = Storyboard.viewController as StationsViewController
let stationsVC = StationsViewController()
stationsVC.delegate = self
navigationController.setViewControllers([stationsVC], animated: false)
}
Expand Down
28 changes: 28 additions & 0 deletions SwiftRadio/Helpers/UITableViewCell+reuseIdentifier.swift
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
}
}
Loading

0 comments on commit 8a433a4

Please sign in to comment.