diff options
author | Nicholas Tay <nick@windblume.net> | 2022-03-24 15:46:13 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-03-24 15:46:13 +1100 |
commit | abb8f691fe6f276205d1cea655ee9938194d1e67 (patch) | |
tree | 752610821eb4862aad72b823efda7934793e0b1a /foray | |
parent | dac7f928d2a1ed555aeedd97ec9433e266f47cf1 (diff) | |
download | forayios-abb8f691fe6f276205d1cea655ee9938194d1e67.tar.gz forayios-abb8f691fe6f276205d1cea655ee9938194d1e67.tar.bz2 forayios-abb8f691fe6f276205d1cea655ee9938194d1e67.zip |
Split details view into separate View and VC
Also fixed the scrollview margins while I was at it - it looked weird
being so inside
Diffstat (limited to '')
-rw-r--r-- | foray.xcodeproj/project.pbxproj | 8 | ||||
-rw-r--r-- | foray/ForayDetailView.swift | 96 | ||||
-rw-r--r-- | foray/ForayDetailViewController.swift | 78 | ||||
-rw-r--r-- | foray/UIViewController+Extensions.swift | 23 |
4 files changed, 133 insertions, 72 deletions
diff --git a/foray.xcodeproj/project.pbxproj b/foray.xcodeproj/project.pbxproj index 79eb5cb..f73a315 100644 --- a/foray.xcodeproj/project.pbxproj +++ b/foray.xcodeproj/project.pbxproj @@ -18,6 +18,8 @@ C04EDE4227E428AB00D83005 /* SnapKit in Frameworks */ = {isa = PBXBuildFile; productRef = C04EDE4127E428AB00D83005 /* SnapKit */; }; C04EDE4427E4298D00D83005 /* ForayNewTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04EDE4327E4298D00D83005 /* ForayNewTableViewCell.swift */; }; C09676BA27E86B6E00353D46 /* ForayLoadingOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = C09676B927E86B6E00353D46 /* ForayLoadingOverlay.swift */; }; + C09676BC27EC27E700353D46 /* UIViewController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C09676BB27EC27E700353D46 /* UIViewController+Extensions.swift */; }; + C09676BE27EC28B100353D46 /* ForayDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C09676BD27EC28B100353D46 /* ForayDetailView.swift */; }; C0FEAF5F27E14C52000A7648 /* ForayDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */; }; /* End PBXBuildFile section */ @@ -34,6 +36,8 @@ C04B45B727DEF2ED001451A3 /* ForayTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayTableViewController.swift; sourceTree = "<group>"; }; C04EDE4327E4298D00D83005 /* ForayNewTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayNewTableViewCell.swift; sourceTree = "<group>"; }; C09676B927E86B6E00353D46 /* ForayLoadingOverlay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayLoadingOverlay.swift; sourceTree = "<group>"; }; + C09676BB27EC27E700353D46 /* UIViewController+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Extensions.swift"; sourceTree = "<group>"; }; + C09676BD27EC28B100353D46 /* ForayDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailView.swift; sourceTree = "<group>"; }; C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailViewController.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ @@ -80,6 +84,8 @@ C049BBFD27E82B9E003820A9 /* Coordinator.swift */, C049BBFF27E82C90003820A9 /* ForayCoordinator.swift */, C09676B927E86B6E00353D46 /* ForayLoadingOverlay.swift */, + C09676BB27EC27E700353D46 /* UIViewController+Extensions.swift */, + C09676BD27EC28B100353D46 /* ForayDetailView.swift */, ); path = foray; sourceTree = "<group>"; @@ -162,9 +168,11 @@ C049BBFE27E82B9E003820A9 /* Coordinator.swift in Sources */, C011E4F127E6211400C248D6 /* ForayNetworkManager.swift in Sources */, C049BC0027E82C90003820A9 /* ForayCoordinator.swift in Sources */, + C09676BE27EC28B100353D46 /* ForayDetailView.swift in Sources */, C04B45B827DEF2ED001451A3 /* ForayTableViewController.swift in Sources */, C0FEAF5F27E14C52000A7648 /* ForayDetailViewController.swift in Sources */, C04EDE4427E4298D00D83005 /* ForayNewTableViewCell.swift in Sources */, + C09676BC27EC27E700353D46 /* UIViewController+Extensions.swift in Sources */, C04B45A427DEF117001451A3 /* AppDelegate.swift in Sources */, C09676BA27E86B6E00353D46 /* ForayLoadingOverlay.swift in Sources */, C011E4F327E6216C00C248D6 /* ForayItems.swift in Sources */, diff --git a/foray/ForayDetailView.swift b/foray/ForayDetailView.swift new file mode 100644 index 0000000..5997016 --- /dev/null +++ b/foray/ForayDetailView.swift @@ -0,0 +1,96 @@ +// +// ForayDetailView.swift +// foray +// +// Created by Nicholas Tay on 24/3/2022. +// + +import UIKit + +class ForayDetailView: UIView { + + let scrollView: UIScrollView = { + let sv = UIScrollView() + sv.alwaysBounceVertical = true // just for fun + return sv + }() + let container = UIView() + + let nameLabel: UILabel = { + let l = UILabel() + l.font = UIFont.preferredFont(forTextStyle: .largeTitle) + l.adjustsFontForContentSizeCategory = true + l.numberOfLines = 3 + l.textAlignment = .center + return l + }() + + let itemImageView: UIImageView = { + let iv = UIImageView() + iv.contentMode = .scaleAspectFit + return iv + }() + + let descLabel: UILabel = { + let l = UILabel() + l.font = UIFont.preferredFont(forTextStyle: .body) + l.adjustsFontForContentSizeCategory = true + l.numberOfLines = 10 + return l + }() + + override init(frame: CGRect) { + super.init(frame: frame) + initialiseViews() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func initialiseViews() { + backgroundColor = .systemBackground + + addSubview(scrollView) + scrollView.addSubview(container) + + container.addSubview(nameLabel) + container.addSubview(itemImageView) + container.addSubview(descLabel) + + setupConstraints() + } + + private func setupConstraints() { + scrollView.snp.makeConstraints { (make) in + make.edges.equalToSuperview() + } + container.snp.makeConstraints { (make) in + make.top.bottom.equalToSuperview() + make.leading.equalTo(snp.leadingMargin) + make.trailing.equalTo(snp.trailingMargin) + } + + nameLabel.snp.makeConstraints { (make) in + make.top.equalToSuperview().inset(16) + make.leading.trailing.equalToSuperview().inset(8) + } + itemImageView.snp.makeConstraints { (make) in + make.top.equalTo(nameLabel.snp.bottom).offset(32) + make.leading.trailing.equalToSuperview() + make.height.equalTo(150) + } + descLabel.snp.makeConstraints { (make) in + make.top.equalTo(itemImageView.snp.bottom).offset(32) + make.leading.trailing.equalToSuperview() + make.bottom.equalTo(container.snp.bottom).inset(16) + } + } + + public func setDetails(name: String, description: String, image: UIImage) { + nameLabel.text = name + descLabel.text = description + itemImageView.image = image + } + +} diff --git a/foray/ForayDetailViewController.swift b/foray/ForayDetailViewController.swift index f87e4b0..c676df6 100644 --- a/foray/ForayDetailViewController.swift +++ b/foray/ForayDetailViewController.swift @@ -7,89 +7,23 @@ import UIKit -class ForayDetailViewController: UIViewController, ForayCoordinated { +class ForayDetailViewController: UIViewController, HasCustomView, ForayCoordinated { - var coordinator: ForayCoordinator? - - let scrollView: UIScrollView = { - let sv = UIScrollView() - sv.alwaysBounceVertical = true // just for fun - return sv - }() - let container = UIView() - - let nameLabel: UILabel = { - let l = UILabel() - l.font = UIFont.preferredFont(forTextStyle: .largeTitle) - l.adjustsFontForContentSizeCategory = true - l.numberOfLines = 3 - l.textAlignment = .center - return l - }() - - let itemImageView: UIImageView = { - let iv = UIImageView() - iv.contentMode = .scaleAspectFit - return iv - }() + typealias CustomView = ForayDetailView - let descLabel: UILabel = { - let l = UILabel() - l.font = UIFont.preferredFont(forTextStyle: .body) - l.adjustsFontForContentSizeCategory = true - l.numberOfLines = 10 - return l - }() + var coordinator: ForayCoordinator? override func viewDidLoad() { super.viewDidLoad() - self.title = "Details" - self.view.backgroundColor = .systemBackground - - initialiseViews() - } - - private func initialiseViews() { - self.view.addSubview(scrollView) - scrollView.addSubview(container) - - container.addSubview(nameLabel) - container.addSubview(itemImageView) - container.addSubview(descLabel) - - setupConstraints() } - private func setupConstraints() { - scrollView.snp.makeConstraints { (make) in - make.edges.equalTo(self.view.snp.margins) - } - container.snp.makeConstraints { (make) in - make.edges.equalToSuperview() - make.width.equalTo(scrollView.snp.width) - } - - nameLabel.snp.makeConstraints { (make) in - make.top.equalToSuperview().inset(16) - make.leading.trailing.equalToSuperview().inset(8) - } - itemImageView.snp.makeConstraints { (make) in - make.top.equalTo(nameLabel.snp.bottom).offset(32) - make.leading.trailing.equalToSuperview() - make.height.equalTo(150) - } - descLabel.snp.makeConstraints { (make) in - make.top.equalTo(itemImageView.snp.bottom).offset(32) - make.leading.trailing.equalToSuperview() - make.bottom.equalTo(container.snp.bottom).inset(16) - } + override func loadView() { + view = ForayDetailView() } public func setDetails(name: String, description: String, image: UIImage) { - nameLabel.text = name - descLabel.text = description - itemImageView.image = image + self.customView.setDetails(name: name, description: description, image: image) } } diff --git a/foray/UIViewController+Extensions.swift b/foray/UIViewController+Extensions.swift new file mode 100644 index 0000000..ee2c436 --- /dev/null +++ b/foray/UIViewController+Extensions.swift @@ -0,0 +1,23 @@ +// +// UIViewController+Extensions.swift +// foray +// +// Created by Nicholas Tay on 24/3/2022. +// + +import Foundation +import UIKit + +protocol HasCustomView { + associatedtype CustomView: UIView +} + +extension HasCustomView where Self: UIViewController { + internal var customView: CustomView { + guard let view = self.view as? CustomView else { + fatalError("Could not cast custom view") + } + + return view + } +} |