Skip to content

Commit

Permalink
Observable collections refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
srdanrasic committed Sep 30, 2018
1 parent 8f9eb63 commit b5963b7
Show file tree
Hide file tree
Showing 61 changed files with 3,164 additions and 6,100 deletions.
94 changes: 93 additions & 1 deletion Bond-App/AppDelegate.swift
Expand Up @@ -7,6 +7,8 @@
//

import UIKit
import Bond
import ReactiveKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -15,8 +17,98 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

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

class CustomBinder: TableViewBinderDataSource<TreeChangeset<Array2D<String, Int>>> {

@objc func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return changeset?.dataSource[[section]].value.section
}

// override func applyChageset(_ changeset: TreeChangeset<Array2D<String, Int>>) {
// tableView?.reloadData()
// }
}

class ViewController: UIViewController {


let tableView = UITableView()
let mutableArray = MutableObservableArray2D<String, Int>()

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.estimatedRowHeight = 50
randomOperation()

mutableArray.bind(to: self) { s, d in
print(d.diffs, d.collection)
}

mutableArray.bind(to: tableView, cellType: UITableViewCell.self, using: CustomBinder()) { (cell, data) in
cell.textLabel?.text = "\(data)"
}
}


func randomOperation() {
mutableArray.apply(.randomOperation(collection: mutableArray.value.collection))
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.randomOperation()
}
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
}

extension TreeChangeset.Operation where Collection == Array2D<String, Int> {

static func randomOperation(collection: Collection) -> TreeChangeset<Collection>.Operation {
let data = [0, 0].randomElement() == 0 ? Array2DElement(item: Int.random(in: 11..<100)) : Array2DElement(section: "\(Int.random(in: 11..<100))")
let element = TreeNode(data)
let indices = collection.indices
guard indices.count > 3 else {
return .insert(TreeNode(Array2DElement(section: "Sec \(Int.random(in: 11..<100))"), [TreeNode(Array2DElement(item: 0))]), at: [0])
}
switch [0, 0, 1, 3, 4].randomElement() {
case 0:
var at = indices.randomElement()!
if Bool.random() && at.count == 1 {
at = at.appending(0)
}
if at.count == 2 {
return .insert(TreeNode(Array2DElement(item: Int.random(in: 11..<100))), at: at)
} else {
return .insert(TreeNode(Array2DElement(section: "\(Int.random(in: 11..<100))")), at: at)
}
case 1:
let at = indices.randomElement()!
return .delete(at: at)
case 2:
let at = indices.randomElement()!
return .update(at: at, newElement: element)
case 3:
guard let from = indices.filter({ $0.count == 2 }).randomElement() else { return randomOperation(collection: collection) }
var collection = collection
collection.remove(at: from)
let to = collection.indices.filter { $0.count == 2}.randomElement() ?? from // to endindex
return .move(from: from, to: to)
case 4:
guard let from = indices.filter({ $0.count == 1 }).randomElement() else { return randomOperation(collection: collection) }
var collection = collection
collection.remove(at: from)
let to = collection.indices.filter { $0.count == 1 }.randomElement() ?? from // to endindex
return .move(from: from, to: to)
default:
fatalError()
}
}
}
396 changes: 132 additions & 264 deletions Bond.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Expand Up @@ -17,16 +17,3 @@ var t = TreeArray<String>([
]),
TreeNode("Child 01")
])

let tree = MutableObservableCollection(t)

tree.observeNext { (event) in
print(event.collection, "diff", event.diff, "patch", event.patch)
}

tree.batchUpdate { (tree) in
// tree[[0]].value = "Ohoho" //TreeNode<String>("Child X")
tree.append(TreeNode<String>("Child Y"))
tree.move(from: [0, 2], to: [2, 0])
// tree.move(from: [0, 1], to: [1])
}

0 comments on commit b5963b7

Please sign in to comment.