From 6d754e1e96ce2a12f6fb2fbdbe6c7d4224b4d3ee Mon Sep 17 00:00:00 2001 From: Marin Todorov Date: Tue, 18 Jul 2017 13:48:09 +0200 Subject: [PATCH 1/2] re-adds the markdown classes, fixes podspec so lint both sub-specs, adds markdown/html demos --- Example/Podfile | 2 +- .../RealmContent.xcodeproj/project.pbxproj | 8 ++ .../RealmContent/Base.lproj/Main.storyboard | 56 ++++++++++++- Example/RealmContent/DemoData.swift | 20 +++++ .../DemosTableViewController.swift | 82 +++++++++++-------- Example/RealmContent/PageCellView.swift | 10 --- Example/RealmContent/ProductCellView.swift | 2 - .../RealmContent/StoreViewController.swift | 21 ++++- Example/RealmContent/ViewController.swift | 3 + RealmContent.podspec | 4 +- RealmContent/Classes/.gitkeep | 0 RealmContent/{Assets => Core}/.gitkeep | 0 .../Classes/ContentListDataSource.swift | 0 .../View/ContentViewController.swift | 0 .../View/ImageContentCell.swift | 0 .../View/TextContentCell.swift | 0 .../Entities/ContentElement.swift | 0 .../{Classes => }/Entities/ContentPage.swift | 0 .../Classes/MarkdownContentConverter.swift | 0 .../View}/MarkdownView.swift | 0 .../View}/MarkdownViewController.swift | 26 ++++-- 21 files changed, 174 insertions(+), 60 deletions(-) delete mode 100644 RealmContent/Classes/.gitkeep rename RealmContent/{Assets => Core}/.gitkeep (100%) rename RealmContent/{Classes => Core}/Classes/ContentListDataSource.swift (100%) rename RealmContent/{Classes => Core}/View/ContentViewController.swift (100%) rename RealmContent/{Classes => Core}/View/ImageContentCell.swift (100%) rename RealmContent/{Classes => Core}/View/TextContentCell.swift (100%) rename RealmContent/{Classes => }/Entities/ContentElement.swift (100%) rename RealmContent/{Classes => }/Entities/ContentPage.swift (100%) rename RealmContent/{Classes => Markdown}/Classes/MarkdownContentConverter.swift (100%) rename RealmContent/{Classes/View/Markdown => Markdown/View}/MarkdownView.swift (100%) rename RealmContent/{Classes/View/Markdown => Markdown/View}/MarkdownViewController.swift (88%) diff --git a/Example/Podfile b/Example/Podfile index f4be570..eff3684 100644 --- a/Example/Podfile +++ b/Example/Podfile @@ -4,7 +4,7 @@ target 'RealmContent_Example' do pod 'RealmSwift' pod 'RealmContent', :path => '../' - #pod 'RealmContent/Markdown', :path => '../' + pod 'RealmContent/Markdown', :path => '../' target 'RealmContent_Tests' do inherit! :search_paths diff --git a/Example/RealmContent.xcodeproj/project.pbxproj b/Example/RealmContent.xcodeproj/project.pbxproj index d1a029a..ddf8be1 100644 --- a/Example/RealmContent.xcodeproj/project.pbxproj +++ b/Example/RealmContent.xcodeproj/project.pbxproj @@ -122,6 +122,7 @@ children = ( 9C027FB71F14CFD600D1E781 /* App Demo */, 9CB782211F13948500FE1828 /* List Style Demos */, + 9C21AD601F1E10CA005C30C2 /* Markdown */, 9CB7EF7F1F0C1D5400E70F21 /* Assets */, 9CB7EF791F0C0F1700E70F21 /* Entities */, 607FACD51AFB9204008FA782 /* AppDelegate.swift */, @@ -189,6 +190,13 @@ name = "App Demo"; sourceTree = ""; }; + 9C21AD601F1E10CA005C30C2 /* Markdown */ = { + isa = PBXGroup; + children = ( + ); + name = Markdown; + sourceTree = ""; + }; 9CB782211F13948500FE1828 /* List Style Demos */ = { isa = PBXGroup; children = ( diff --git a/Example/RealmContent/Base.lproj/Main.storyboard b/Example/RealmContent/Base.lproj/Main.storyboard index 5856c05..9aebcea 100644 --- a/Example/RealmContent/Base.lproj/Main.storyboard +++ b/Example/RealmContent/Base.lproj/Main.storyboard @@ -256,6 +256,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + @@ -431,8 +460,15 @@ - + + + + + + + + @@ -759,6 +795,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/Example/RealmContent/DemoData.swift b/Example/RealmContent/DemoData.swift index 733db82..1c27a82 100644 --- a/Example/RealmContent/DemoData.swift +++ b/Example/RealmContent/DemoData.swift @@ -12,6 +12,26 @@ import RealmContent struct DemoData { + static func createDemoDataSet4(in realm: Realm) { + try! realm.write { + realm.deleteAll() + + // store offers + let format1 = ContentPage(value: ["title": "Formatting showcase", "id": "formatting", "priority": 10, "mainColor": "#D34CA3"]) + let elements1: [ContentElement] = [ + ContentElement(value: ["type": "h1", "content": "Plain text"]), + ContentElement(value: ["type": "p", "content": "Some plain text here\n\n-------------------"]), + ContentElement(value: ["type": "h1", "content": "Markdown"]), + ContentElement(value: ["type": "p", "content": "Heading\n====\nSub-Heading\n----\nHere is some _markdown_ *formatted* text.\n\n>This is a *markdown* _formatted_ blockquote.\n[Link back to app](app://123/MyProduct)\n\n-------------------"]), + ContentElement(value: ["type": "h1", "content": "HTML"]), + ContentElement(value: ["type": "p", "content": "

H1 Heading

H3 Heading

More HTML follows here.
Price:218.30
Product:Product name
Add to Cart"]) + ] + format1.elements.append(objectsIn: elements1) + + realm.add(format1) + } + } + static func createDemoDataSet3(in realm: Realm) { try! realm.write { realm.deleteAll() diff --git a/Example/RealmContent/DemosTableViewController.swift b/Example/RealmContent/DemosTableViewController.swift index 9fc452d..dbdd3bb 100644 --- a/Example/RealmContent/DemosTableViewController.swift +++ b/Example/RealmContent/DemosTableViewController.swift @@ -22,46 +22,62 @@ class DemosTableViewController: UITableViewController { /// load demo content depending on the row tapped override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - guard let identifier = segue.identifier, let vc = segue.destination as? ContentViewController else { return } + guard let identifier = segue.identifier else { return } TextContentCell.defaultTextColor = defaultTextColor - - // fetch page let realm = try! Realm() - // inject it in view controller - switch identifier { - case "DefaultPage": - // vanilla formatting - let page = getPage(realm: realm, prep: DemoData.createDemoDataSet1, id: "showcase") - try! realm.write { - page.mainColor = nil - } + /// show content via MarkdownViewController + if let vc = segue.destination as? MarkdownViewController { + switch identifier { + case "MarkdownAndHTML": + // show markdown and html content + let page = getPage(realm: realm, prep: DemoData.createDemoDataSet4, id: "formatting") + vc.page = page + vc.openCustomURL = handleCustomUrl - vc.page = page + default: break + } + } - case "CustomColor": - // custom color - let page = getPage(realm: realm, prep: DemoData.createDemoDataSet1, id: "showcase") - try! realm.write { - page.mainColor = "#cc3355" + /// show content via ContentViewController + if let vc = segue.destination as? ContentViewController { + + // inject it in view controller + switch identifier { + case "DefaultPage": + // vanilla formatting + let page = getPage(realm: realm, prep: DemoData.createDemoDataSet1, id: "showcase") + try! realm.write { + page.mainColor = nil + } + + vc.page = page + + case "CustomColor": + // custom color + let page = getPage(realm: realm, prep: DemoData.createDemoDataSet1, id: "showcase") + try! realm.write { + page.mainColor = "#cc3355" + } + TextContentCell.defaultTextColor = .brown + + vc.page = page + + case "CustomElements": + // customized elements + let page = getPage(realm: realm, prep: DemoData.createDemoDataSet1, id: "showcase") + vc.page = page + vc.customizeCell = customizeElementBlock + + case "Interactions": + // shows different interactions + let page = getPage(realm: realm, prep: DemoData.createDemoDataSet2, id: "interactions") + vc.page = page + vc.openCustomURL = handleCustomUrl + + default: break } - TextContentCell.defaultTextColor = .brown - - vc.page = page - - case "CustomElements": - // customized elements - let page = getPage(realm: realm, prep: DemoData.createDemoDataSet1, id: "showcase") - vc.page = page - vc.customizeCell = customizeElementBlock - - case "Interactions": - // shows different interactions - let page = getPage(realm: realm, prep: DemoData.createDemoDataSet2, id: "interactions") - vc.page = page - vc.openCustomURL = handleCustomUrl - default: break } } diff --git a/Example/RealmContent/PageCellView.swift b/Example/RealmContent/PageCellView.swift index ceb175f..3d3484f 100644 --- a/Example/RealmContent/PageCellView.swift +++ b/Example/RealmContent/PageCellView.swift @@ -24,16 +24,6 @@ class PageCellView: UITableViewCell { @IBOutlet var label: UILabel! - override func awakeFromNib() { - super.awakeFromNib() - - } - - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) - - } - func populate(with page: ContentPage, atRow row: Int) { label.text = page.title label.textColor = OrbColor.at(index: row) diff --git a/Example/RealmContent/ProductCellView.swift b/Example/RealmContent/ProductCellView.swift index 636087c..b83948e 100644 --- a/Example/RealmContent/ProductCellView.swift +++ b/Example/RealmContent/ProductCellView.swift @@ -26,7 +26,6 @@ class ProductCellView: UICollectionViewCell { imageElement.type == ContentElement.Kind.img.rawValue { image.kf.setImage(with: URL(string: imageElement.content)) - } // checks if second element is a price (custom type) @@ -36,6 +35,5 @@ class ProductCellView: UICollectionViewCell { } else { price.text = nil } - } } diff --git a/Example/RealmContent/StoreViewController.swift b/Example/RealmContent/StoreViewController.swift index 47fa4af..2078bbc 100644 --- a/Example/RealmContent/StoreViewController.swift +++ b/Example/RealmContent/StoreViewController.swift @@ -14,6 +14,9 @@ class StoreViewController: UIViewController { @IBOutlet var tableView: UITableView! @IBOutlet var collectionView: UICollectionView! + @IBOutlet var cartItem: UIBarButtonItem! + + var cart = [String: Int]() let offers = ContentListDataSource(style: .plain) let products = ContentListDataSource(style: .plain) @@ -30,7 +33,6 @@ class StoreViewController: UIViewController { products.loadContent(from: realm, filter: NSPredicate(format: "tag = %@", "product")) products.updating(view: collectionView) } - } extension StoreViewController: UITableViewDataSource, UITableViewDelegate { @@ -88,7 +90,11 @@ extension StoreViewController: UICollectionViewDataSource, UICollectionViewDeleg } func addToCart(url: URL) { - let alert = UIAlertController(title: "Added to cart", message: "\(url.lastPathComponent) add to your cart", preferredStyle: .alert) + let product = url.lastPathComponent + cart[product] = (cart[product] ?? 0) + 1 + cartItem.title = "Cart (\(cart.values.reduce(0, +)))" + + let alert = UIAlertController(title: "Added to cart", message: "\(product) added to your cart", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak self] _ in self?.dismiss(animated: true, completion: nil) self?.navigationController!.popViewController(animated: true) @@ -96,3 +102,14 @@ extension StoreViewController: UICollectionViewDataSource, UICollectionViewDeleg present(alert, animated: true, completion: nil) } } + +extension StoreViewController /* Cart */ { + @IBAction func showCart(_ sender: Any) { + let contents = cart.map { key, value in "\(value) x \(key)" }.joined(separator: "\n") + let alert = UIAlertController(title: "Your Cart", message: contents, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Close", style: .default, handler: { [weak self] _ in + self?.dismiss(animated: true, completion: nil) + })) + present(alert, animated: true, completion: nil) + } +} diff --git a/Example/RealmContent/ViewController.swift b/Example/RealmContent/ViewController.swift index 8260bcd..3b53822 100644 --- a/Example/RealmContent/ViewController.swift +++ b/Example/RealmContent/ViewController.swift @@ -30,14 +30,17 @@ class ViewController: UIViewController { // one section, lists all pages by priority items = ContentListDataSource(style: .plain) items.loadContent(from: realm) + case "List with Sections": // mulitple section, lists pages by priority within section items = ContentListDataSource(style: .sectionsByTag) items.loadContent(from: realm) + case "Custom List Subset": // filter the content with a custom predicate items = ContentListDataSource(style: .plain) items.loadContent(from: realm, filter: NSPredicate(format: "priority > %d", 5)) + default: break } diff --git a/RealmContent.podspec b/RealmContent.podspec index fb4012f..7956407 100644 --- a/RealmContent.podspec +++ b/RealmContent.podspec @@ -28,12 +28,12 @@ Realm powered content management system providing built-in view controllers and s.default_subspec = 'Core' s.subspec 'Core' do |cs| - cs.source_files = 'RealmContent/Classes/Classes/*', 'RealmContent/Classes/Entities/*', 'RealmContent/Classes/View/*' + cs.source_files = 'RealmContent/Entities/*', 'RealmContent/Core/Classes/*', 'RealmContent/Core/View/*' end s.subspec 'Markdown' do |cs| cs.dependency 'MMMarkdown' - cs.source_files = 'RealmContent/Classes/View/Markdown/*' + cs.source_files = 'RealmContent/Entities/*', 'RealmContent/Markdown/Classes/*', 'RealmContent/Markdown/View/*' end end diff --git a/RealmContent/Classes/.gitkeep b/RealmContent/Classes/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/RealmContent/Assets/.gitkeep b/RealmContent/Core/.gitkeep similarity index 100% rename from RealmContent/Assets/.gitkeep rename to RealmContent/Core/.gitkeep diff --git a/RealmContent/Classes/Classes/ContentListDataSource.swift b/RealmContent/Core/Classes/ContentListDataSource.swift similarity index 100% rename from RealmContent/Classes/Classes/ContentListDataSource.swift rename to RealmContent/Core/Classes/ContentListDataSource.swift diff --git a/RealmContent/Classes/View/ContentViewController.swift b/RealmContent/Core/View/ContentViewController.swift similarity index 100% rename from RealmContent/Classes/View/ContentViewController.swift rename to RealmContent/Core/View/ContentViewController.swift diff --git a/RealmContent/Classes/View/ImageContentCell.swift b/RealmContent/Core/View/ImageContentCell.swift similarity index 100% rename from RealmContent/Classes/View/ImageContentCell.swift rename to RealmContent/Core/View/ImageContentCell.swift diff --git a/RealmContent/Classes/View/TextContentCell.swift b/RealmContent/Core/View/TextContentCell.swift similarity index 100% rename from RealmContent/Classes/View/TextContentCell.swift rename to RealmContent/Core/View/TextContentCell.swift diff --git a/RealmContent/Classes/Entities/ContentElement.swift b/RealmContent/Entities/ContentElement.swift similarity index 100% rename from RealmContent/Classes/Entities/ContentElement.swift rename to RealmContent/Entities/ContentElement.swift diff --git a/RealmContent/Classes/Entities/ContentPage.swift b/RealmContent/Entities/ContentPage.swift similarity index 100% rename from RealmContent/Classes/Entities/ContentPage.swift rename to RealmContent/Entities/ContentPage.swift diff --git a/RealmContent/Classes/Classes/MarkdownContentConverter.swift b/RealmContent/Markdown/Classes/MarkdownContentConverter.swift similarity index 100% rename from RealmContent/Classes/Classes/MarkdownContentConverter.swift rename to RealmContent/Markdown/Classes/MarkdownContentConverter.swift diff --git a/RealmContent/Classes/View/Markdown/MarkdownView.swift b/RealmContent/Markdown/View/MarkdownView.swift similarity index 100% rename from RealmContent/Classes/View/Markdown/MarkdownView.swift rename to RealmContent/Markdown/View/MarkdownView.swift diff --git a/RealmContent/Classes/View/Markdown/MarkdownViewController.swift b/RealmContent/Markdown/View/MarkdownViewController.swift similarity index 88% rename from RealmContent/Classes/View/Markdown/MarkdownViewController.swift rename to RealmContent/Markdown/View/MarkdownViewController.swift index 2963735..202231f 100644 --- a/RealmContent/Classes/View/Markdown/MarkdownViewController.swift +++ b/RealmContent/Markdown/View/MarkdownViewController.swift @@ -22,6 +22,13 @@ public class MarkdownViewController: UIViewController { } private var content: Content! + public var page: ContentPage! { + didSet { + content = .page(self.page) + } + } + + public var openCustomURL: ((URL)->Void)? // the controller's view private let webView = MarkdownView() @@ -58,17 +65,13 @@ public class MarkdownViewController: UIViewController { public var usesSafariController = true public var customCssStyle: String? = nil - public init(content: Content) { - self.content = content - + public init(page: ContentPage) { + self.content = .page(page) super.init(nibName: nil, bundle: nil) - self.view = webView - //webView.navigationDelegate = self - webView.delegate = self } - + required public init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") + super.init(coder: aDecoder) } private func populateFrom(markdown md: String) { @@ -108,6 +111,9 @@ public class MarkdownViewController: UIViewController { override public func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) + view = webView + webView.delegate = self + switch content! { case .page(let page): observe(page: page) @@ -122,7 +128,9 @@ extension MarkdownViewController: UIWebViewDelegate { public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == .linkClicked, let url = request.url { // website - if usesSafariController, let scheme = url.scheme, scheme.hasPrefix("http") { + if let scheme = url.scheme, scheme == "app" { + openCustomURL?(url) + } else if usesSafariController, let scheme = url.scheme, scheme.hasPrefix("http") { let safari = SFSafariViewController(url: url) present(safari, animated: true, completion: nil) } else { From ca26e6dcf38332ddf9e064311c103c12e770e86d Mon Sep 17 00:00:00 2001 From: Marin Todorov Date: Mon, 7 Aug 2017 10:39:21 +0200 Subject: [PATCH 2/2] bumps podspec --- Example/RealmContent.xcodeproj/project.pbxproj | 8 -------- RealmContent.podspec | 2 +- RealmContent/Core/View/ContentViewController.swift | 2 +- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/Example/RealmContent.xcodeproj/project.pbxproj b/Example/RealmContent.xcodeproj/project.pbxproj index ddf8be1..d1a029a 100644 --- a/Example/RealmContent.xcodeproj/project.pbxproj +++ b/Example/RealmContent.xcodeproj/project.pbxproj @@ -122,7 +122,6 @@ children = ( 9C027FB71F14CFD600D1E781 /* App Demo */, 9CB782211F13948500FE1828 /* List Style Demos */, - 9C21AD601F1E10CA005C30C2 /* Markdown */, 9CB7EF7F1F0C1D5400E70F21 /* Assets */, 9CB7EF791F0C0F1700E70F21 /* Entities */, 607FACD51AFB9204008FA782 /* AppDelegate.swift */, @@ -190,13 +189,6 @@ name = "App Demo"; sourceTree = ""; }; - 9C21AD601F1E10CA005C30C2 /* Markdown */ = { - isa = PBXGroup; - children = ( - ); - name = Markdown; - sourceTree = ""; - }; 9CB782211F13948500FE1828 /* List Style Demos */ = { isa = PBXGroup; children = ( diff --git a/RealmContent.podspec b/RealmContent.podspec index 7956407..e2cdf9e 100644 --- a/RealmContent.podspec +++ b/RealmContent.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'RealmContent' - s.version = '0.1.0' + s.version = '0.2.0' s.summary = 'Realm powered content management system' s.description = <<-DESC diff --git a/RealmContent/Core/View/ContentViewController.swift b/RealmContent/Core/View/ContentViewController.swift index e0f0faa..4d4e940 100644 --- a/RealmContent/Core/View/ContentViewController.swift +++ b/RealmContent/Core/View/ContentViewController.swift @@ -162,7 +162,7 @@ public class ContentViewController: UIViewController, UITableViewDataSource, UIT tableView.insertRows(at: insertions.map(fromRow(section)), with: .automatic) tableView.reloadRows(at: modifications.map(fromRow(section)), with: .none) - moves.forEach { print("move from \($0.from.value) to \($0.to.value)") } + //moves.forEach { print("move from \($0.from.value) to \($0.to.value)") } moves.forEach { tableView.moveRow(