Skip to content

Commit

Permalink
Add playground
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Jul 31, 2018
1 parent 5ced063 commit 9afe45c
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
41 changes: 41 additions & 0 deletions DifferenceKit.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*:
## Welcome to `DifferenceKit` Playground
----
> 1. Open DifferenceKit.xcworkspace.
> 2. Build the DifferenceKit.
> 3. Open DifferenceKit playground in project navigator.
> 4. Show the live view in assistant editor.
*/
import DifferenceKit
import PlaygroundSupport
import UIKit

let viewController = TableViewController()
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)

PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = navigationController.view

let source = [
Section(model: "Section 1", elements: ["A", "B", "C"]),
Section(model: "Section 2", elements: ["D", "E", "F"]),
Section(model: "Section 3", elements: ["G", "H", "I"]),
Section(model: "Section 4", elements: ["J", "K", "L"])
]

let target = [
Section(model: "Section 5", elements: ["M", "N", "O"]),
Section(model: "Section 1", elements: ["A", "C"]),
Section(model: "Section 4", elements: ["J", "I", "K", "L"]),
Section(model: "Section 3", elements: ["G", "H", "Z"]),
Section(model: "Section 6", elements: ["P", "Q", "R"]),
]

viewController.dataInput = source

var isSourceShown = true
viewController.refreshAction = {
viewController.dataInput = isSourceShown ? target : source
isSourceShown = !isSourceShown
}
55 changes: 55 additions & 0 deletions DifferenceKit.playground/Sources/TableViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import UIKit
import DifferenceKit
import DifferenceKitUI

extension String: Differentiable {}

public final class TableViewController: UITableViewController {
public var refreshAction: (() -> Void)?

public var dataInput: [Section<String, String>] {
get { return data }
set {
let changeset = StagedChangeset(source: data, target: newValue)
tableView.reload(using: changeset, with: .fade) { data in
self.data = data
}
}
}

private var data = [Section<String, String>]()

public init() {
super.init(style: .grouped)
tableView.sectionHeaderHeight = 30
tableView.sectionFooterHeight = 0
tableView.register(UITableViewCell.self, forCellReuseIdentifier: String(describing: UITableViewCell.self))
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refresh))
}

public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc private func refresh() {
refreshAction?()
}

public override func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}

public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].elements.count
}

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UITableViewCell.self), for: indexPath)
cell.textLabel?.text = data[indexPath.section].elements[indexPath.row]
return cell
}

public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return data[section].model
}
}
4 changes: 4 additions & 0 deletions DifferenceKit.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' display-mode='rendered'>
<timeline fileName='timeline.xctimeline'/>
</playground>
6 changes: 6 additions & 0 deletions DifferenceKit.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
2 changes: 2 additions & 0 deletions DifferenceKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 6B2DF88A210E39A8004D2D40 /* DifferenceKit.xcconfig */;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Manual;
DEFINES_MODULE = NO;
Expand Down Expand Up @@ -619,6 +620,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 6B2DF88A210E39A8004D2D40 /* DifferenceKit.xcconfig */;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Manual;
DEFINES_MODULE = NO;
Expand Down
10 changes: 10 additions & 0 deletions DifferenceKit.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

0 comments on commit 9afe45c

Please sign in to comment.