Skip to content

Commit

Permalink
Swift 4 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdrone committed Oct 8, 2017
1 parent 2c58ee4 commit 3ae9157
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 82 deletions.
28 changes: 15 additions & 13 deletions Buffer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
160E25631CF44C3700A5F828 /* Buffer.h in Headers */ = {isa = PBXBuildFile; fileRef = 160E25621CF44C3700A5F828 /* Buffer.h */; settings = {ATTRIBUTES = (Public, ); }; };
160E25771CF459B500A5F828 /* Buffer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 160E25721CF459B500A5F828 /* Buffer.swift */; };
160E25781CF459B500A5F828 /* CollectionViewDiffAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 160E25731CF459B500A5F828 /* CollectionViewDiffAdapter.swift */; };
160E257A1CF459B500A5F828 /* Diff.swift in Sources */ = {isa = PBXBuildFile; fileRef = 160E25751CF459B500A5F828 /* Diff.swift */; };
160E257A1CF459B500A5F828 /* Diffing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 160E25751CF459B500A5F828 /* Diffing.swift */; };
160E257B1CF459B500A5F828 /* TableViewDiffAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 160E25761CF459B500A5F828 /* TableViewDiffAdapter.swift */; };
160E25A41CF4752700A5F828 /* AdapterType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 160E25A31CF4752700A5F828 /* AdapterType.swift */; };
1611A8C81CF6365600DF2493 /* TableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1611A8C71CF6365600DF2493 /* TableView.swift */; };
Expand All @@ -24,7 +24,7 @@
160E25641CF44C3700A5F828 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
160E25721CF459B500A5F828 /* Buffer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Buffer.swift; sourceTree = "<group>"; };
160E25731CF459B500A5F828 /* CollectionViewDiffAdapter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewDiffAdapter.swift; sourceTree = "<group>"; };
160E25751CF459B500A5F828 /* Diff.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Diff.swift; sourceTree = "<group>"; };
160E25751CF459B500A5F828 /* Diffing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Diffing.swift; sourceTree = "<group>"; };
160E25761CF459B500A5F828 /* TableViewDiffAdapter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewDiffAdapter.swift; sourceTree = "<group>"; };
160E25A31CF4752700A5F828 /* AdapterType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdapterType.swift; sourceTree = "<group>"; };
1611A8C71CF6365600DF2493 /* TableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -62,35 +62,35 @@
160E25611CF44C3700A5F828 /* src */ = {
isa = PBXGroup;
children = (
1611A8C61CF6361C00DF2493 /* Core */,
1611A8C51CF6361500DF2493 /* UI */,
1611A8C61CF6361C00DF2493 /* core */,
1611A8C51CF6361500DF2493 /* ui */,
);
name = src;
sourceTree = "<group>";
};
1611A8C51CF6361500DF2493 /* UI */ = {
1611A8C51CF6361500DF2493 /* ui */ = {
isa = PBXGroup;
children = (
165D96FE1CF5994C0040A48F /* PrototypeCell.swift */,
1611A8C71CF6365600DF2493 /* TableView.swift */,
);
name = UI;
name = ui;
path = src;
sourceTree = "<group>";
};
1611A8C61CF6361C00DF2493 /* Core */ = {
1611A8C61CF6361C00DF2493 /* core */ = {
isa = PBXGroup;
children = (
160E25721CF459B500A5F828 /* Buffer.swift */,
160E25751CF459B500A5F828 /* Diff.swift */,
160E25751CF459B500A5F828 /* Diffing.swift */,
1611A8C91CF636EF00DF2493 /* AnyListItem.swift */,
160E25A31CF4752700A5F828 /* AdapterType.swift */,
160E25731CF459B500A5F828 /* CollectionViewDiffAdapter.swift */,
160E25761CF459B500A5F828 /* TableViewDiffAdapter.swift */,
160E25621CF44C3700A5F828 /* Buffer.h */,
160E25641CF44C3700A5F828 /* Info.plist */,
);
name = Core;
name = core;
path = src;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -137,7 +137,7 @@
TargetAttributes = {
160E255E1CF44C3700A5F828 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
Expand Down Expand Up @@ -177,7 +177,7 @@
160E257B1CF459B500A5F828 /* TableViewDiffAdapter.swift in Sources */,
165D96FF1CF5994C0040A48F /* PrototypeCell.swift in Sources */,
160E25781CF459B500A5F828 /* CollectionViewDiffAdapter.swift in Sources */,
160E257A1CF459B500A5F828 /* Diff.swift in Sources */,
160E257A1CF459B500A5F828 /* Diffing.swift in Sources */,
1611A8CA1CF636EF00DF2493 /* AnyListItem.swift in Sources */,
1611A8C81CF6365600DF2493 /* TableView.swift in Sources */,
160E25A41CF4752700A5F828 /* AdapterType.swift in Sources */,
Expand Down Expand Up @@ -311,7 +311,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -330,7 +331,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.s.Buffer;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Buffer [![Swift](https://img.shields.io/badge/swift-3.1-orange.svg?style=flat)](#) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://opensource.org/licenses/MIT)
# Buffer [![Swift](https://img.shields.io/badge/swift-4-orange.svg?style=flat)](#) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://opensource.org/licenses/MIT)

<img src="https://raw.githubusercontent.com/alexdrone/Buffer/master/docs/logo_small.png" width=150 alt="Buffer" align=right />

Expand Down
5 changes: 0 additions & 5 deletions src/AdapterType.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import Foundation

public protocol AdapterType {

associatedtype `Type`
associatedtype ViewType

/// Returns the element currently on the front buffer at the given index path.
func displayedElement(at index: Int) -> Type

/// The total number of elements currently displayed.
func countDisplayedElements() -> Int

/// Replace the elements buffer and compute the diffs.
/// - parameter newValues: The new values.
/// - parameter synchronous: Wether the filter, sorting and diff should be executed
/// synchronously or not.
/// - parameter completion: Code that will be executed once the buffer is updated.
func update(with values: [Type]?, synchronous: Bool, completion: (() -> Void)?)

/// The section index associated with this adapter.
var sectionIndex: Int { get set }

/// The target view.
var view: ViewType? { get }

Expand Down
16 changes: 2 additions & 14 deletions src/AnyListItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,26 @@ public protocol ListViewCell: class { }
#endif

public protocol ListItemType: Diffable {

/// The reuse identifier for the cell passed as argument.
var reuseIdentifier: String { get set }

/// The TableView, or the CollectionView that will own this element.
var referenceView: ListContainerView? { get }

var modelRef: Any? { get }

/// Configure the cell with the current item.
func configure(cell: ListViewCell)
}

public class ListItem<Type: Diffable>: ListItemType, CustomDebugStringConvertible {

public var reuseIdentifier: String
public var diffIdentifier: String {
return "\(reuseIdentifier)_\(model.diffIdentifier)"
}
public let referenceView: ListContainerView?

/// The actual item data.
public var model: Type
public var modelRef: Any? {
return self.model
}

public var debugDescription: String {
return self.diffIdentifier
}

public var modelRef: Any? { return self.model }
public var debugDescription: String { return self.diffIdentifier }
public var cellConfiguration: ((ListViewCell, Type) -> Void)?

#if os(iOS)
Expand Down Expand Up @@ -102,7 +91,6 @@ public class ListItem<Type: Diffable>: ListItemType, CustomDebugStringConvertibl
}

public func configure(cell: ListViewCell) {

self.cellConfiguration?(cell, self.model)
}
}
44 changes: 16 additions & 28 deletions src/Buffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,40 @@ import Foundation
public protocol BufferType { }

public protocol BufferDelegate: class {

/// Notifies the receiver that the content is about to change.
func buffer(willChangeContent buffer: BufferType)

/// Notifies the receiver that rows were deleted.
func buffer(didDeleteElementAtIndices buffer: BufferType, indices: [UInt])

/// Notifies the receiver that rows were inserted.
func buffer(didInsertElementsAtIndices buffer: BufferType, indices: [UInt])

/// Notifies the receiver that an element has been moved to a different position.
func buffer(didMoveElement buffer: BufferType, from: UInt, to: UInt)

/// Notifies the receiver that the content updates has ended.
func buffer(didChangeContent buffer: BufferType)

/// Notifies the receiver that the content updates has ended.
/// This callback method is called when the number of changes are too many to be
/// handled for the UI thread - it's recommendable to just reload the whole data in this case.
/// - Note: The 'diffThreshold' property in 'Buffer' defines what is the maximum number
/// of changes
/// that you want the receiver to be notified for.
func buffer(didChangeAllContent buffer: BufferType)

/// Called when one of the observed properties for this object changed.
func buffer(didChangeElementAtIndex buffer: BufferType, index: UInt)
}

public class Buffer<ElementType: Diffable>: NSObject, BufferType {

/// The object that will get notified every time chavbcnges occures to the array.
public weak var delegate: BufferDelegate?

/// The elements in the array observer's buffer.
public var currentElements: [ElementType] {
return self.frontBuffer
}

/// Defines what is the maximum number of changes that you want the receiver to be notified for.
public var diffThreshold = 300

public var diffThreshold = 256
// If set to 'true' the LCS algorithm is run synchronously on the main thread.
fileprivate var synchronous: Bool = false

private var synchronous: Bool = false
// The two buffers.
fileprivate var frontBuffer = [ElementType]() {
private var frontBuffer = [ElementType]() {
willSet {
assert(Thread.isMainThread)
self.observe(shouldObserveTrackedKeyPaths: false)
Expand All @@ -58,26 +46,24 @@ public class Buffer<ElementType: Diffable>: NSObject, BufferType {
self.observe(shouldObserveTrackedKeyPaths: true)
}
}
fileprivate var backBuffer = [ElementType]()

private var backBuffer = [ElementType]()
// Sort closure.
fileprivate var sort: ((ElementType, ElementType) -> Bool)?

private var sort: ((ElementType, ElementType) -> Bool)?
// Filter closure.
fileprivate var filter: ((ElementType) -> Bool)?
private var filter: ((ElementType) -> Bool)?

// The serial operation queue for this controller.
fileprivate let serialOperationQueue: OperationQueue = {
private let serialOperationQueue: OperationQueue = {
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
return operationQueue
}()

fileprivate var flags = (isRefreshing: false,
private var flags = (isRefreshing: false,
shouldRefresh: false)

// Used if 'Element' is KVO-compliant.
fileprivate var trackedKeyPaths = [String]()
private var trackedKeyPaths = [String]()

public init(initialArray: [ElementType],
sort: ((ElementType, ElementType) -> Bool)? = nil,
Expand Down Expand Up @@ -199,7 +185,7 @@ extension Buffer {

/// Adds or remove observations.
/// - Note: This code is executed only when 'Element: AnyObject'.
fileprivate func observe(shouldObserveTrackedKeyPaths: Bool = true) {
private func observe(shouldObserveTrackedKeyPaths: Bool = true) {
if self.trackedKeyPaths.count == 0 {
return
}
Expand All @@ -221,7 +207,7 @@ extension Buffer {
}

// - Note: This code is executed only when 'Element: AnyObject'.
fileprivate func objectDidChangeValue(for keyPath: String?, in object: AnyObject?) {
private func objectDidChangeValue(for keyPath: String?, in object: AnyObject?) {
guard let object = object else {
return
}
Expand All @@ -245,7 +231,7 @@ private var __observationContext: UInt8 = 0

//MARK: Dispatch Helpers

fileprivate extension Buffer {
private extension Buffer {

func dispatchOnMainThread(synchronous: Bool = false,
block: @escaping () -> Void) {
Expand All @@ -254,7 +240,9 @@ fileprivate extension Buffer {
block()
} else {
if Thread.isMainThread { block()
} else { DispatchQueue.main.async(execute: block) }
} else {
DispatchQueue.main.async(execute: block)
}
}
}

Expand All @@ -263,7 +251,7 @@ fileprivate extension Buffer {
if synchronous {
block()
} else {
self.serialOperationQueue.addOperation(){
self.serialOperationQueue.addOperation {
DispatchQueue.global().async(execute: block)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/CollectionViewDiffAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import UIKit
open class CollectionViewDiffAdapter<ElementType: Diffable>: NSObject,
AdapterType,
UICollectionViewDataSource {

public typealias `Type` = ElementType
public typealias ViewType = UICollectionView
open fileprivate(set) var buffer: Buffer<ElementType>
Expand Down
4 changes: 0 additions & 4 deletions src/Diff.swift → src/Diffing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ public typealias EqualityChecker<T: Diffable> = (T, T) -> Bool

/// An object that can be diffed.
public protocol Diffable {

/// The identifier used from the diffing algorithm.
var diffIdentifier: String { get }
}

public protocol Diffing {

/// Return the diff result from 'oldArray' to 'newArray'
static func diffing<T: Diffable>(oldArray: [T],
newArray: [T],
Expand Down Expand Up @@ -85,7 +83,6 @@ public struct DiffResult<H: Hashable> : DiffResultType,
}

extension DiffResultType {

public var hasChanges: Bool {
guard inserts.isEmpty else { return true }
guard deletes.isEmpty else { return true }
Expand Down Expand Up @@ -150,7 +147,6 @@ public enum Diff: Diffing {
}

private struct OptimizedIdentity<E: Hashable> : Hashable {

let hashValue: Int
let identity: UnsafePointer<E>

Expand Down
Loading

0 comments on commit 3ae9157

Please sign in to comment.