Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift formatted code for v5 #673

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

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

6 changes: 2 additions & 4 deletions Bond-App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
// Copyright © 2016 Swift Bond. All rights reserved.
//

import UIKit
import Bond
import ReactiveKit
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()
return true
}
}

49 changes: 23 additions & 26 deletions Extensions/Bond+Realm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,42 @@ import RealmSwift

/*

// Get the default Realm
let realm = try! Realm()
// Get the default Realm
let realm = try! Realm()

// Convert realm results into a changeset signal
let puppies = realm.objects(Dog.self).toChangesetSignal()
// Convert realm results into a changeset signal
let puppies = realm.objects(Dog.self).toChangesetSignal()

// Changeset signals can then be bound to table or collection views
puppies.suppressError(logging: true).bind(to: tableView, cellType: UITableViewCell.self) { (cell, dog) in
cell.textLabel?.text = dog.name
}
// Changeset signals can then be bound to table or collection views
puppies.suppressError(logging: true).bind(to: tableView, cellType: UITableViewCell.self) { (cell, dog) in
cell.textLabel?.text = dog.name
}

// Adding something to the results will cause the table view to insert the respective row
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
try! realm.write {
realm.add(myDog)
}
}
// Adding something to the results will cause the table view to insert the respective row
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
try! realm.write {
realm.add(myDog)
}
}

*/
*/

public extension RealmCollectionChange where CollectionType: Swift.Collection, CollectionType.Index == Int {

public func toOrderedCollectionChangeset() throws -> OrderedCollectionChangeset<CollectionType> {
func toOrderedCollectionChangeset() throws -> OrderedCollectionChangeset<CollectionType> {
switch self {
case .initial(let collection):
case let .initial(collection):
return OrderedCollectionChangeset(collection: collection, diff: OrderedCollectionDiff())
case .update(let collection, let deletions, let insertions, let modifications):
case let .update(collection, deletions, insertions, modifications):
let diff = OrderedCollectionDiff(inserts: insertions, deletes: deletions, updates: modifications, moves: [])
return OrderedCollectionChangeset(collection: collection, diff: diff)
case .error(let error):
case let .error(error):
throw error
}
}
}

public extension Results {

public func toChangesetSignal() -> Signal<OrderedCollectionChangeset<Results<Element>>, NSError> {
func toChangesetSignal() -> Signal<OrderedCollectionChangeset<Results<Element>>, NSError> {
return Signal { observer in
let token = self.observe { change in
do {
Expand All @@ -82,16 +80,15 @@ public extension Results {
}

public extension Results: QueryableSectionedDataSourceProtocol {

public var numberOfSections: Int {
var numberOfSections: Int {
return 1
}

public func numberOfItems(inSection section: Int) -> Int {
func numberOfItems(inSection _: Int) -> Int {
return count
}

public func item(at indexPath: IndexPath) -> Element {
func item(at indexPath: IndexPath) -> Element {
return self[indexPath.row]
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//: [Previous](@previous)

///: Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
///: targets with any iOS Simulator as a destination.
/// : Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
/// : targets with any iOS Simulator as a destination.

import UIKit
import Bond
import ReactiveKit
import PlaygroundSupport
import ReactiveKit
import UIKit

class Contact: NSObject {
@objc dynamic var name: String? = "n/a"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//: Playground - noun: a place where people can play

///: Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
///: targets with any iOS Simulator as a destination.
/// : Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
/// : targets with any iOS Simulator as a destination.

import Bond
import ReactiveKit
import PlaygroundSupport
import ReactiveKit

// Array

let a = MutableObservableArray([1, 2, 3])

a.observeNext { (cs) in
a.observeNext { cs in
print(cs.diff, cs.patch)
}

a.batchUpdate { (a) in
a.batchUpdate { a in
a.append(1)
a.append(2)
}
Expand All @@ -26,7 +26,7 @@ let array2D = MutableObservableArray2D(Array2D<String, String>(sectionsWithItems
("Cities", ["Paris", "Berlin"])
]))

array2D.observeNext { (cs) in
array2D.observeNext { cs in
print(cs.collection, cs.diff, cs.patch)
}

Expand All @@ -39,7 +39,7 @@ array2D.appendItem("France", toSectionAt: 1)

let s = MutableObservableSet(Set([1, 4, 3]))

s.sortedCollection().mapCollection { $0 * 2 }.filterCollection { $0 > 2 }.observeNext { (changeset) in
s.sortedCollection().mapCollection { $0 * 2 }.filterCollection { $0 > 2 }.observeNext { changeset in
print(changeset.collection, changeset.diff, changeset.patch)
}

Expand All @@ -49,7 +49,7 @@ s.insert(5)

let dictionary = MutableObservableDictionary(["A": 1])

dictionary.sortedCollection(by: { $0.key < $1.key }).mapCollection({ "\($0.key): \($0.value)"}).observeNext { (changeset) in
dictionary.sortedCollection(by: { $0.key < $1.key }).mapCollection { "\($0.key): \($0.value)" }.observeNext { changeset in
print(changeset.collection, changeset.diff, changeset.patch)
}

Expand All @@ -59,7 +59,7 @@ dictionary["B"] = 2

let data = MutableObservableCollection(Data([0x0A, 0x0B]))

data.observeNext { (changeset) in
data.observeNext { changeset in
print(changeset.collection, changeset.diff, changeset.patch)
}

Expand All @@ -75,20 +75,20 @@ var t = TreeArray<String>([
TreeNode("Child 002", [
TreeNode("Child 0020"),
TreeNode("Child 0021")
])
]),
])
]),
TreeNode("Child 01")
])
])

let ot = MutableObservableTree(t)

ot.observeNext { (cs) in
ot.observeNext { cs in
print(cs.collection, cs.diff, cs.patch)
}

ot.insert(TreeNode("New"), at: [0, 0])

ot.batchUpdate { (ot) in
ot.batchUpdate { ot in
ot.remove(at: [0, 0])
ot.remove(at: [0, 2, 1])
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//: [Previous](@previous)

///: Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
///: targets with any iOS Simulator as a destination.
/// : Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
/// : targets with any iOS Simulator as a destination.

import Bond
import Foundation
import UIKit
import Bond

// Tree node is a tree with a single root

Expand Down Expand Up @@ -48,7 +48,6 @@ print(t.depthFirst.randomElement()!)
// Custom trees

extension UIView: TreeProtocol {

public var children: [UIView] {
return subviews
}
Expand All @@ -61,5 +60,4 @@ print(v.breadthFirst.map { type(of: $0) })

print(v[childAt: [0, 0]])


//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
//: [Previous](@previous)

///: Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
///: targets with any iOS Simulator as a destination.
/// : Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
/// : targets with any iOS Simulator as a destination.

import Bond
import Foundation
import PlaygroundSupport
import UIKit
import Bond
import ReactiveKit
import UIKit

class Cell: UICollectionViewCell {

let titleLabel = UILabel()

override init(frame: CGRect) {
Expand All @@ -20,7 +19,7 @@ class Cell: UICollectionViewCell {
contentView.backgroundColor = .white
}

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

Expand All @@ -31,13 +30,12 @@ class Cell: UICollectionViewCell {
}

class SectionHeader: UICollectionReusableView {

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .red
}

required init?(coder aDecoder: NSCoder) {
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Expand All @@ -59,7 +57,6 @@ PlaygroundPage.current.needsIndefiniteExecution = true

// Using custom binder to provide table view header titles
class CustomBinder<Changeset: SectionedDataSourceChangeset>: CollectionViewBinderDataSource<Changeset>, UICollectionViewDelegateFlowLayout where Changeset.Collection == Array2D<String, Int> {

override var collectionView: UICollectionView? {
didSet {
collectionView?.delegate = self
Expand All @@ -68,7 +65,7 @@ class CustomBinder<Changeset: SectionedDataSourceChangeset>: CollectionViewBinde

// Due to a bug in Swift related to generic subclases, we have to specify ObjC delegate method name
// if it's different than Swift name (https://bugs.swift.org/browse/SR-2817).
@objc (collectionView:viewForSupplementaryElementOfKind:atIndexPath:)
@objc(collectionView:viewForSupplementaryElementOfKind:atIndexPath:)
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
Expand All @@ -79,7 +76,7 @@ class CustomBinder<Changeset: SectionedDataSourceChangeset>: CollectionViewBinde
}
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
func collectionView(_: UICollectionView, layout _: UICollectionViewLayout, referenceSizeForHeaderInSection _: Int) -> CGSize {
return CGSize(width: 200, height: 20)
}
}
Expand All @@ -91,12 +88,11 @@ class CustomBinder<Changeset: SectionedDataSourceChangeset>: CollectionViewBinde
let initialData = Array2D<String, Int>(sectionsWithItems: [
("A", [1, 2]),
("B", [10, 20])
])
])

let data = MutableObservableArray2D(initialData)


data.bind(to: collectionView, cellType: Cell.self, using: CustomBinder()) { (cell, item) in
data.bind(to: collectionView, cellType: Cell.self, using: CustomBinder()) { cell, item in
cell.titleLabel.text = "\(item)"
}

Expand All @@ -105,7 +101,7 @@ DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
}

DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
data.batchUpdate { (data) in
data.batchUpdate { data in
data.appendItem(4, toSectionAt: 0)
data.insert(section: "Aa", at: 1)
data.appendItem(100, toSectionAt: 1)
Expand All @@ -120,4 +116,5 @@ DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
data.replaceItems(ofSectionAt: 1, with: [1, 100, 20], performDiff: true)
}

//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//: [Previous](@previous)

///: Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
///: targets with any iOS Simulator as a destination.
/// : Before running the playground, make sure to build "Bond-iOS" and "PlaygroundSupport"
/// : targets with any iOS Simulator as a destination.

import Bond
import Foundation
import PlaygroundSupport
import UIKit
import Bond
import ReactiveKit
import UIKit

let pickerView = UIPickerView()
pickerView.frame.size = CGSize(width: 300, height: 300)
Expand All @@ -31,7 +31,7 @@ data.bind(to: pickerView)
// Handle cell selection
let selectedRow = pickerView.reactive.selectedRow

selectedRow.observeNext { (row, component) in
selectedRow.observeNext { row, component in
print("selected", row, component)
}

Expand All @@ -41,7 +41,7 @@ let selectedPair = selectedRow.scan([0, 0]) { (pair, rowAndComponent) -> [Int] i
return pair
}

selectedPair.observeNext { (pair) in
selectedPair.observeNext { pair in
print("selected indices", pair)
let items = pair.enumerated().map {
data[itemAt: [$0.offset, $0.element]]
Expand Down
Loading