-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added collection view for categories and corresponding products view
- Loading branch information
1 parent
f29e751
commit 9b2e92b
Showing
9 changed files
with
396 additions
and
96 deletions.
There are no files selected for viewing
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
147 changes: 147 additions & 0 deletions
147
Examples/moltin tvOS Example/CategoriesViewController.swift
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,147 @@ | ||
// | ||
// ProductsViewController.swift | ||
// moltin tvOS Example | ||
// | ||
// Created by Craig Tweedy on 21/03/2018. | ||
// | ||
|
||
import UIKit | ||
import moltin | ||
|
||
extension UIImageView { | ||
|
||
func load(urlString string: String?) { | ||
guard let imageUrl = string, | ||
let url = URL(string: imageUrl) else { return } | ||
|
||
URLSession.shared.dataTask(with: url) { data, _, _ in | ||
if let data = data { | ||
DispatchQueue.main.async { | ||
self.image = UIImage(data: data) | ||
} | ||
} | ||
}.resume() | ||
} | ||
} | ||
|
||
extension UIColor { | ||
convenience init(hexString: String, alpha: CGFloat = 1.0) { | ||
let hexString: String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) | ||
let scanner = Scanner(string: hexString) | ||
if hexString.hasPrefix("#") { | ||
scanner.scanLocation = 1 | ||
} | ||
var color: UInt32 = 0 | ||
scanner.scanHexInt32(&color) | ||
let mask = 0x000000FF | ||
let r = Int(color >> 16) & mask | ||
let g = Int(color >> 8) & mask | ||
let b = Int(color) & mask | ||
let red = CGFloat(r) / 255.0 | ||
let green = CGFloat(g) / 255.0 | ||
let blue = CGFloat(b) / 255.0 | ||
self.init(red: red, green: green, blue: blue, alpha: alpha) | ||
} | ||
func toHexString() -> String { | ||
var r: CGFloat = 0 | ||
var g: CGFloat = 0 | ||
var b: CGFloat = 0 | ||
var a: CGFloat = 0 | ||
getRed(&r, green: &g, blue: &b, alpha: &a) | ||
let rgb: Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0 | ||
return String(format: "#%06x", rgb) | ||
} | ||
} | ||
|
||
class ProductCategory: moltin.Category { | ||
var backgroundColor: UIColor? | ||
var backgroundImage: String? | ||
|
||
enum ProductCategoryCodingKeys: String, CodingKey { | ||
case backgroundColor = "background_colour" | ||
case backgroundImage = "background_image" | ||
} | ||
|
||
required init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: ProductCategoryCodingKeys.self) | ||
if let color: String = try container.decodeIfPresent(String.self, forKey: .backgroundColor) { | ||
self.backgroundColor = UIColor(hexString: color) | ||
} | ||
self.backgroundImage = try container.decodeIfPresent(String.self, forKey: .backgroundImage) | ||
try super.init(from: decoder) | ||
} | ||
} | ||
|
||
class CategoriesViewController: UICollectionViewController { | ||
|
||
var data: [ProductCategory] = [] | ||
let moltin: Moltin = Moltin(withClientID: "j6hSilXRQfxKohTndUuVrErLcSJWP15P347L6Im0M4", withLocale: Locale(identifier: "en_US")) | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
// Do any additional setup after loading the view, typically from a nib. | ||
|
||
self.collectionView?.register(UINib(nibName: "ProductCategoryCollectionViewCell", bundle: Bundle.main), forCellWithReuseIdentifier: "Cell") | ||
|
||
let flowLayout = UICollectionViewFlowLayout() | ||
flowLayout.itemSize = CGSize(width: self.view.frame.width, height: self.view.frame.height * 0.8) | ||
flowLayout.minimumLineSpacing = 0 | ||
flowLayout.minimumInteritemSpacing = 0 | ||
self.collectionView?.setCollectionViewLayout(flowLayout, animated: false) | ||
|
||
self.moltin.category.all { (response: Result<PaginatedResponse<[ProductCategory]>>) in | ||
switch response { | ||
case .success(let categories): | ||
DispatchQueue.main.async { | ||
self.view.backgroundColor = categories.data?.first?.backgroundColor | ||
self.data = categories.data ?? [] | ||
self.collectionView?.reloadData() | ||
} | ||
case .failure(let error): | ||
print(error) | ||
} | ||
} | ||
} | ||
|
||
override func didReceiveMemoryWarning() { | ||
super.didReceiveMemoryWarning() | ||
// Dispose of any resources that can be recreated. | ||
} | ||
|
||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | ||
if segue.identifier == "CategoryToProducts", | ||
let viewController = segue.destination as? ProductsCollectionViewController, | ||
let category = sender as? ProductCategory { | ||
viewController.category = category | ||
} | ||
} | ||
|
||
} | ||
|
||
// MARK: - Data Source | ||
extension CategoriesViewController { | ||
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | ||
return self.data.count | ||
} | ||
} | ||
|
||
// MARK: - Delegate | ||
extension CategoriesViewController { | ||
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | ||
guard let cell: ProductCategoryCollectionViewCell = | ||
collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? ProductCategoryCollectionViewCell else { | ||
return UICollectionViewCell() | ||
} | ||
let product = self.data[indexPath.row] | ||
cell.backgroundColor = product.backgroundColor | ||
cell.image.load(urlString: product.backgroundImage) | ||
cell.label.text = product.name | ||
|
||
return cell | ||
} | ||
|
||
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | ||
let category = self.data[indexPath.row] | ||
self.performSegue(withIdentifier: "CategoryToProducts", sender: category) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Examples/moltin tvOS Example/ProductCategoryCollectionViewCell.swift
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,14 @@ | ||
// | ||
// ProductCollectionViewCell.swift | ||
// moltin tvOS Example | ||
// | ||
// Created by Craig Tweedy on 21/03/2018. | ||
// | ||
|
||
import UIKit | ||
|
||
class ProductCategoryCollectionViewCell: UICollectionViewCell { | ||
|
||
@IBOutlet var label: UILabel! | ||
@IBOutlet var image: UIImageView! | ||
} |
53 changes: 53 additions & 0 deletions
53
Examples/moltin tvOS Example/ProductCategoryCollectionViewCell.xib
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,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder.AppleTV.XIB" version="3.0" toolsVersion="14109" targetRuntime="AppleTV" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES"> | ||
<device id="appleTV" orientation="landscape"> | ||
<adaptation id="light"/> | ||
</device> | ||
<dependencies> | ||
<deployment identifier="tvOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/> | ||
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/> | ||
<capability name="Safe area layout guides" minToolsVersion="9.0"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<objects> | ||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> | ||
<collectionViewCell opaque="NO" multipleTouchEnabled="YES" contentMode="center" id="Rz2-K0-ksD" customClass="ProductCategoryCollectionViewCell" customModule="moltin_tvOS_Example" customModuleProvider="target"> | ||
<rect key="frame" x="0.0" y="0.0" width="1184" height="793"/> | ||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center"> | ||
<rect key="frame" x="0.0" y="0.0" width="1184" height="793"/> | ||
<autoresizingMask key="autoresizingMask"/> | ||
<subviews> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ak9-D0-qvS"> | ||
<rect key="frame" x="545" y="160" width="94" height="47"/> | ||
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/> | ||
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> | ||
<nil key="highlightedColor"/> | ||
<color key="shadowColor" red="0.5273903998957411" green="0.5339013924870466" blue="0.5339013924870466" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> | ||
<size key="shadowOffset" width="1" height="1"/> | ||
</label> | ||
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="jVl-cX-aCn"> | ||
<rect key="frame" x="213" y="136" width="432" height="524"/> | ||
</imageView> | ||
</subviews> | ||
</view> | ||
<constraints> | ||
<constraint firstItem="ak9-D0-qvS" firstAttribute="leading" secondItem="jVl-cX-aCn" secondAttribute="trailing" constant="-100" id="8Vm-cT-KWT"/> | ||
<constraint firstItem="7PP-Ag-gPX" firstAttribute="bottom" secondItem="jVl-cX-aCn" secondAttribute="bottom" constant="73" id="Nh9-4x-7rp"/> | ||
<constraint firstItem="jVl-cX-aCn" firstAttribute="leading" secondItem="7PP-Ag-gPX" secondAttribute="leading" multiplier="2" constant="33" id="UfQ-fU-yui"/> | ||
<constraint firstItem="ak9-D0-qvS" firstAttribute="centerX" secondItem="7PP-Ag-gPX" secondAttribute="centerX" id="a3O-tO-2iN"/> | ||
<constraint firstItem="jVl-cX-aCn" firstAttribute="top" secondItem="7PP-Ag-gPX" secondAttribute="top" constant="76" id="kxw-Hi-2Ge"/> | ||
<constraint firstItem="ak9-D0-qvS" firstAttribute="top" secondItem="7PP-Ag-gPX" secondAttribute="top" constant="100" id="woD-xf-OJc"/> | ||
</constraints> | ||
<viewLayoutGuide key="safeArea" id="7PP-Ag-gPX"/> | ||
<size key="customSize" width="1184" height="793"/> | ||
<connections> | ||
<outlet property="image" destination="jVl-cX-aCn" id="C9I-5b-flZ"/> | ||
<outlet property="label" destination="ak9-D0-qvS" id="0t8-dk-XSY"/> | ||
</connections> | ||
<point key="canvasLocation" x="8" y="82.5"/> | ||
</collectionViewCell> | ||
</objects> | ||
</document> |
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
Oops, something went wrong.