diff --git a/Example/RealmContent/Base.lproj/Main.storyboard b/Example/RealmContent/Base.lproj/Main.storyboard index 996bbcb..5856c05 100644 --- a/Example/RealmContent/Base.lproj/Main.storyboard +++ b/Example/RealmContent/Base.lproj/Main.storyboard @@ -322,26 +322,26 @@ - + - + - + - + - + - + + @@ -390,16 +397,33 @@ + + + + + + + + + @@ -408,6 +432,10 @@ + + + + diff --git a/Example/RealmContent/DemoData.swift b/Example/RealmContent/DemoData.swift index 5fd2a78..733db82 100644 --- a/Example/RealmContent/DemoData.swift +++ b/Example/RealmContent/DemoData.swift @@ -17,36 +17,75 @@ struct DemoData { realm.deleteAll() // store offers - let offer1 = ContentPage(value: ["title": "This weekend everything is 20% off", "tag": "offer", "mainColor": "f25192"]) + let offer1 = ContentPage(value: ["title": "This weekend everything is 20% off", "tag": "offer", "priority": 10, "mainColor": "#D34CA3"]) let elements1: [ContentElement] = [ ContentElement(value: ["type": "h1", "content": "Big weekend sale!"]), ContentElement(value: ["type": "h2", "content": "This weekend everything is 20% off"]), - ContentElement(value: ["type": "p", "content": "Use promo code 'bigsummersale20' to get your discount at checkout"]), - ContentElement(value: ["type": "img", "content": "http://realm.io/assets/img/news/2016-05-17-realm-rxswift/rx.png", "url": "https://news.realm.io/news/marin-todorov-realm-rxswift/"]) + ContentElement(value: ["type": "img", "content": "https://raw.githubusercontent.com/realm-demos/RealmContent/master/assets/pexels-photo-248797.jpg", "url": "https://news.realm.io/news/marin-todorov-realm-rxswift/"]), + ContentElement(value: ["type": "p", "content": "Use promo code 'bigsummersale20' to get your discount at checkout"]) ] offer1.elements.append(objectsIn: elements1) - let offer2 = ContentPage(value: ["title": "Buy 4 garden gnomes, pay 5!", "tag": "offer", "mainColor": "f25192"]) + let offer2 = ContentPage(value: ["title": "Buy 4 garden gnomes, pay 5!", "tag": "offer", "priority": 8, "mainColor": "#9A50A5"]) let elements2: [ContentElement] = [ ContentElement(value: ["type": "h1", "content": "Big spring sale!"]), ContentElement(value: ["type": "h2", "content": "Buy 4 garden gnomes, pay 5!"]), - ContentElement(value: ["type": "p", "content": "Use promo code '4for5supergnome' to get your discount at checkout"]), - ContentElement(value: ["type": "img", "content": "http://realm.io/assets/img/news/2016-05-17-realm-rxswift/rx.png", "url": "https://news.realm.io/news/marin-todorov-realm-rxswift/"]) + ContentElement(value: ["type": "img", "content": "https://raw.githubusercontent.com/realm-demos/RealmContent/master/assets/pexels-photo-296230.jpg", "url": "https://news.realm.io/news/marin-todorov-realm-rxswift/"]), + ContentElement(value: ["type": "p", "content": "Use promo code '4for5supergnome' to get your discount at checkout"]) ] offer2.elements.append(objectsIn: elements2) - let offer3 = ContentPage(value: ["title": "Get our fidelity card for a chance to win!", "tag": "offer", "mainColor": "f25192"]) + let offer3 = ContentPage(value: ["title": "Get our fidelity card for a chance to win!", "tag": "offer", "priority": 6, "mainColor": "#59569E"]) let elements3: [ContentElement] = [ ContentElement(value: ["type": "h1", "content": "Get the card, win the prizes! Do it now!"]), - ContentElement(value: ["type": "p", "content": "Not only you will get permanent discount of 5% on all products, but you can also win big time!"]), - ContentElement(value: ["type": "img", "content": "http://realm.io/assets/img/news/2016-05-17-realm-rxswift/rx.png", "url": "https://news.realm.io/news/marin-todorov-realm-rxswift/"]) + ContentElement(value: ["type": "h3", "content": "Not only you will get permanent discount of 5% on all products, but you can also win big time!"]), + ContentElement(value: ["type": "img", "content": "https://raw.githubusercontent.com/realm-demos/RealmContent/master/assets/pexels-photo.jpg", "url": "https://news.realm.io/news/marin-todorov-realm-rxswift/"]) ] offer3.elements.append(objectsIn: elements3) - realm.add([offer1, offer2, offer3]) + let product1 = ContentPage(value: ["title": "Fujifilm Camera", "tag": "product"]) + let productEls1: [ContentElement] = [ + ContentElement(value: ["type": "img", "content": "https://raw.githubusercontent.com/realm-demos/RealmContent/master/assets/pexels-photo-90946.jpeg"]), + ContentElement(value: ["type": "price", "content": "USD 559.95"]), + ContentElement(value: ["type": "h2", "content": "Fujifilm Camera"]), + ContentElement(value: ["type": "p", "content": "Use promo code '1checkout' for your first purchase"]), + ContentElement(value: ["type": "h3", "content": "Add to Cart", "url": "app://addtocart/1435/"+"Fujifilm Camera".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!]) + ] + product1.elements.append(objectsIn: productEls1) + + let product2 = ContentPage(value: ["title": "Nikon Lens", "tag": "product"]) + let productEls2: [ContentElement] = [ + ContentElement(value: ["type": "img", "content": "https://raw.githubusercontent.com/realm-demos/RealmContent/master/assets/pexels-photo-279906.jpeg"]), + ContentElement(value: ["type": "price", "content": "USD 205.45"]), + ContentElement(value: ["type": "h2", "content": "Nikon Lens"]), + ContentElement(value: ["type": "p", "content": "Use promo code '1checkout' for your first purchase"]), + ContentElement(value: ["type": "h3", "content": "Add to Cart", "url": "app://addtocart/"+"Nikon Lens".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!]) + ] + product2.elements.append(objectsIn: productEls2) + + let product3 = ContentPage(value: ["title": "Nikon Zoom Lens", "tag": "product"]) + let productEls3: [ContentElement] = [ + ContentElement(value: ["type": "img", "content": "https://raw.githubusercontent.com/realm-demos/RealmContent/master/assets/light-night-lens-shadow.jpg"]), + ContentElement(value: ["type": "price", "content": "USD 428.95"]), + ContentElement(value: ["type": "h2", "content": "Nikon Zoom Lens"]), + ContentElement(value: ["type": "p", "content": "Use promo code '1checkout' for your first purchase"]), + ContentElement(value: ["type": "h3", "content": "Add to Cart", "url": "app://addtocart/"+"Nikon Zoom Lens".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!]) + ] + product3.elements.append(objectsIn: productEls3) + + let product4 = ContentPage(value: ["title": "Retro Smena Camera", "tag": "product"]) + let productEls4: [ContentElement] = [ + ContentElement(value: ["type": "img", "content": "https://raw.githubusercontent.com/realm-demos/RealmContent/master/assets/pexels-photo-50924.jpeg"]), + ContentElement(value: ["type": "price", "content": "USD 95.59"]), + ContentElement(value: ["type": "h2", "content": "Retro Smena Camera"]), + ContentElement(value: ["type": "p", "content": "Use promo code '1checkout' for your first purchase"]), + ContentElement(value: ["type": "h3", "content": "Add to Cart", "url": "app://addtocart/"+"Retro Smena Camera".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!]) + ] + product4.elements.append(objectsIn: productEls4) + realm.add([product2, product3, product4, product1]) } } diff --git a/Example/RealmContent/ProductCellView.swift b/Example/RealmContent/ProductCellView.swift index c9ebb73..636087c 100644 --- a/Example/RealmContent/ProductCellView.swift +++ b/Example/RealmContent/ProductCellView.swift @@ -14,19 +14,28 @@ class ProductCellView: UICollectionViewCell { @IBOutlet var image: UIImageView! @IBOutlet var label: UILabel! + @IBOutlet var price: UILabel! func populate(with page: ContentPage) { label.text = page.title // expects the top element in the page to be an image, // grabs it, and shows it in the cell - + if let imageElement = page.elements.first, - imageElement.type == ContentElement.Kind.img.rawValue, - let urlString = imageElement.url, - let url = URL(string: urlString) { + imageElement.type == ContentElement.Kind.img.rawValue { + + image.kf.setImage(with: URL(string: imageElement.content)) + + } - image.kf.setImage(with: url) + // checks if second element is a price (custom type) + if page.elements.count > 1, + page.elements[1].type == "price" { + price.text = page.elements[1].content + } else { + price.text = nil } + } } diff --git a/Example/RealmContent/StoreViewController.swift b/Example/RealmContent/StoreViewController.swift index 732b486..f9cd421 100644 --- a/Example/RealmContent/StoreViewController.swift +++ b/Example/RealmContent/StoreViewController.swift @@ -12,6 +12,9 @@ import RealmContent class StoreViewController: UIViewController { + @IBOutlet var tableView: UITableView! + @IBOutlet var collectionView: UICollectionView! + let offers = ContentListDataSource(style: .plain) let products = ContentListDataSource(style: .plain) @@ -22,9 +25,10 @@ class StoreViewController: UIViewController { DemoData.createDemoDataSet3(in: realm) offers.loadContent(from: realm, filter: NSPredicate(format: "tag = %@", "offer")) - products.loadContent(from: realm, filter: NSPredicate(format: "tag = %@", "product")) - + offers.updating(view: tableView) + products.loadContent(from: realm, filter: NSPredicate(format: "tag = %@", "product")) + products.updating(view: collectionView) } } @@ -67,6 +71,27 @@ extension StoreViewController: UICollectionViewDataSource, UICollectionViewDeleg func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { collectionView.deselectItem(at: indexPath, animated: true) + let product = products.itemAt(indexPath: indexPath) + let productDetailsVC = ContentViewController(page: product) + productDetailsVC.openCustomURL = addToCart + productDetailsVC.customizeCell = styleElement + + navigationController!.pushViewController(productDetailsVC, animated: true) + } + + func styleElement(cell: UITableViewCell, indexPath: IndexPath, element: ContentElement) { + if element.content == "Add to Cart" { + cell.contentView.backgroundColor = NSString(string: "#FCC397").representedColor() + } else { + cell.contentView.backgroundColor = UIColor(white: 1.0, alpha: 1.0) + } } + func addToCart(url: URL) { + let alert = UIAlertController(title: "Added to cart", message: "\(url.lastPathComponent) add to your cart", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak self] _ in + self?.dismiss(animated: true, completion: nil) + })) + present(alert, animated: true, completion: nil) + } } diff --git a/RealmContent/Classes/Entities/ContentElement.swift b/RealmContent/Classes/Entities/ContentElement.swift index ad6e14b..a80d2a3 100644 --- a/RealmContent/Classes/Entities/ContentElement.swift +++ b/RealmContent/Classes/Entities/ContentElement.swift @@ -13,8 +13,14 @@ public class ContentElement: Object { /// the supported element types public enum Kind: String { case p, img, h1, h2, h3, h4 + + static public func allRawValues() -> [String] { + return [p.rawValue, img.rawValue, h1.rawValue, + h2.rawValue, h2.rawValue, h4.rawValue] + } } + public dynamic var type = "p" public dynamic var content = "" public dynamic var url: String? diff --git a/RealmContent/Classes/View/ContentViewController.swift b/RealmContent/Classes/View/ContentViewController.swift index cd57622..e0f0faa 100644 --- a/RealmContent/Classes/View/ContentViewController.swift +++ b/RealmContent/Classes/View/ContentViewController.swift @@ -110,7 +110,9 @@ public class ContentViewController: UIViewController, UITableViewDataSource, UIT } } - pageElementsUpdatesToken = page.elements.addNotificationBlock(applyChanges) + pageElementsUpdatesToken = page.elements + .filter(NSPredicate(format: "type in %@", ContentElement.Kind.allRawValues())) + .addNotificationBlock(applyChanges) } private func populateFrom(page: ContentPage) { @@ -124,7 +126,7 @@ public class ContentViewController: UIViewController, UITableViewDataSource, UIT } } - private func applyChanges(_ changes: RealmCollectionChange>) { + private func applyChanges(_ changes: RealmCollectionChange>) { let section = 0 switch changes {