Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from realm-demos/mt/markdown
Browse files Browse the repository at this point in the history
Mt/markdown
  • Loading branch information
icanzilb authored Aug 7, 2017
2 parents eeb4921 + 58b4c1b commit 0b8c8f7
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 55 additions & 1 deletion Example/RealmContent/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,35 @@
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="Advanced Formats" id="ck6-nb-PQe">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="wjV-ZG-VhT">
<rect key="frame" x="0.0" y="619.5" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="wjV-ZG-VhT" id="G3R-TY-dqe">
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Markdown &amp; HTML" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="GHR-BR-kbI">
<rect key="frame" x="16" y="11" width="344" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstAttribute="trailingMargin" secondItem="GHR-BR-kbI" secondAttribute="trailing" constant="7" id="IQR-vd-dgL"/>
<constraint firstAttribute="bottomMargin" secondItem="GHR-BR-kbI" secondAttribute="bottom" constant="3.5" id="aDa-Z4-Jnp"/>
<constraint firstItem="GHR-BR-kbI" firstAttribute="leading" secondItem="G3R-TY-dqe" secondAttribute="leadingMargin" constant="8" id="gGe-Bn-57R"/>
<constraint firstItem="GHR-BR-kbI" firstAttribute="top" secondItem="G3R-TY-dqe" secondAttribute="topMargin" constant="3" id="u8t-dv-SQC"/>
</constraints>
</tableViewCellContentView>
<connections>
<segue destination="sJa-C9-kzu" kind="show" identifier="MarkdownAndHTML" id="NG2-1U-Ggd"/>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
<connections>
<outlet property="dataSource" destination="nFA-EA-AG8" id="HT5-l6-f2H"/>
Expand Down Expand Up @@ -431,8 +460,15 @@
<constraint firstItem="ER8-Bs-lB7" firstAttribute="height" secondItem="hok-AL-22p" secondAttribute="height" multiplier="0.5" id="xwn-Ww-B8C"/>
</constraints>
</view>
<navigationItem key="navigationItem" title="Store App" id="9Le-xP-9Mp"/>
<navigationItem key="navigationItem" title="Store App" id="9Le-xP-9Mp">
<barButtonItem key="rightBarButtonItem" title="Cart" id="Rw8-5w-N6f">
<connections>
<action selector="showCart:" destination="uEg-e8-a6d" id="bbw-Fc-Li3"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="cartItem" destination="Rw8-5w-N6f" id="RNY-uz-CvP"/>
<outlet property="collectionView" destination="VrQ-x2-bfn" id="1ld-k1-1jj"/>
<outlet property="tableView" destination="ER8-Bs-lB7" id="rhr-3E-UBt"/>
</connections>
Expand Down Expand Up @@ -759,6 +795,24 @@
</objects>
<point key="canvasLocation" x="769" y="170"/>
</scene>
<!--Markdown View Controller-->
<scene sceneID="mqy-Iu-FAG">
<objects>
<viewController storyboardIdentifier="MarkdownViewController" id="sJa-C9-kzu" customClass="MarkdownViewController" customModule="RealmContent" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="NC6-VJ-CUO"/>
<viewControllerLayoutGuide type="bottom" id="8Qe-Vh-S3g"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="dsn-xe-JgJ">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="xjC-NC-TPo" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1598" y="173"/>
</scene>
</scenes>
<inferredMetricsTieBreakers>
<segue reference="mDQ-Ng-DMv"/>
Expand Down
20 changes: 20 additions & 0 deletions Example/RealmContent/DemoData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>H1 Heading</h1><h3>H3 Heading</h3>More <i>HT</i><b>ML</b> follows here. <table border=1 padding=10><tr><td>Price:</td><td>218.30</td></tr><tr><td>Product:</td><td>Product name</td></tr></table><a href='app://123/myproduct' style='display:block; padding:20px; background: #569e6b; border:1px solid #40664a; width:110px; height:18px;color:white; font-weight:bold;text-decoration:none;font-size:18px;margin-top:20px;-webkit-border-radius:10px;'>Add to Cart</a>"])
]
format1.elements.append(objectsIn: elements1)

realm.add(format1)
}
}

static func createDemoDataSet3(in realm: Realm) {
try! realm.write {
realm.deleteAll()
Expand Down
82 changes: 49 additions & 33 deletions Example/RealmContent/DemosTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
10 changes: 0 additions & 10 deletions Example/RealmContent/PageCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions Example/RealmContent/ProductCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,6 +35,5 @@ class ProductCellView: UICollectionViewCell {
} else {
price.text = nil
}

}
}
21 changes: 19 additions & 2 deletions Example/RealmContent/StoreViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -30,7 +33,6 @@ class StoreViewController: UIViewController {
products.loadContent(from: realm, filter: NSPredicate(format: "tag = %@", "product"))
products.updating(view: collectionView)
}

}

extension StoreViewController: UITableViewDataSource, UITableViewDelegate {
Expand Down Expand Up @@ -88,11 +90,26 @@ 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)
}))
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)
}
}
3 changes: 3 additions & 0 deletions Example/RealmContent/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions RealmContent.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Empty file removed RealmContent/Classes/.gitkeep
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 0b8c8f7

Please sign in to comment.