Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Usbergo committed Mar 7, 2017
1 parent f43b938 commit 788a84d
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 611 deletions.
42 changes: 42 additions & 0 deletions Buffer.podspec
@@ -0,0 +1,42 @@
#
# Be sure to run `pod lib lint Render.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
s.name = "Buffer"
s.version = "2.1"
s.summary = "Swift and UIKit a la React."

# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!

s.description = <<-DESC
React-inspired swift library for writing UIKit UIs.
DESC

s.homepage = "https://github.com/alexdrone/Render"
s.screenshots = "https://github.com/alexdrone/Render/raw/master/Doc/logo.png"
s.license = 'MIT'
s.author = { "Alex Usbergo" => "alexakadrone@gmail.com" }
s.source = { :git => "https://github.com/alexdrone/Render.git", :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/alexdrone'

s.ios.deployment_target = '8.0'

s.source_files = 'Render/**/*'

# s.resource_bundles = {
# 'Render' => ['Render/Assets/*.png']
# }

# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
45 changes: 10 additions & 35 deletions Buffer/AdapterType.swift
@@ -1,53 +1,28 @@
//
// AdapterType.swift
// Buffer
//
// Copyright (c) 2016 Alex Usbergo.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//


import Foundation

public protocol AdapterType {

associatedtype `Type`
associatedtype ViewType

///Returns the element currently on the front buffer at the given index path.
/** 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.
/** 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.
/** 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) -> Void)?)

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

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

init(buffer: BufferType, view: ViewType)
Expand Down
33 changes: 4 additions & 29 deletions Buffer/AnyListItem.swift
@@ -1,28 +1,3 @@
//
// AnyListItemType.swift
// BufferDiff
//
// Copyright (c) 2016 Alex Usbergo.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public protocol ListContainerView: class { }
Expand All @@ -42,13 +17,13 @@ public func ==<Type>(lhs: AnyListItem<Type>, rhs: AnyListItem<Type>) -> Bool {

public struct AnyListItem<Type: Equatable>: Equatable {

///The reuse identifier for the cell passed as argument.
/** The reuse identifier for the cell passed as argument. */
public var reuseIdentifier: String

///The actual item data.
/** The actual item data. */
public var state: Type

///The TableView, or the CollectionView that will own this element.
/** The TableView, or the CollectionView that will own this element. */
public let referenceView: ListContainerView?

public var cellConfiguration: ((ListViewCell, Type) -> Void)?
Expand All @@ -61,7 +36,7 @@ public struct AnyListItem<Type: Equatable>: Equatable {
state: Type,
configurationClosure: ((ListViewCell, Type) -> Void)? = nil) {

//registers the prototype cell if necessary.
// registers the prototype cell if necessary.
if !Prototypes.isPrototypeCellRegistered(reuseIdentifer) {
let cell = V(reuseIdentifier: reuseIdentifer)
Prototypes.registerPrototypeCell(reuseIdentifer, cell: cell)
Expand Down
87 changes: 31 additions & 56 deletions Buffer/Buffer.swift
@@ -1,78 +1,51 @@

//
// Buffer.swift
// Buffer
//
// Created by Alex Usbergo on 02/05/16.
//
// Copyright (c) 2016 Alex Usbergo.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public protocol BufferType { }

public protocol BufferDelegate: class {

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

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

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

/// Notifies the receiver that the content updates has ended.
/** 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.
/** 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
/** Called when one of the observed properties for this object changed. */
func buffer(didChangeElementAtIndex buffer: BufferType, index: UInt)
}

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

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

/// The elements in the array observer's buffer.
/** 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.
/** Defines what is the maximum number of changes that you want the receiver to be notified for. */
public var diffThreshold = 50

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

/// The two buffers.
// The two buffers.
fileprivate var frontBuffer = [ElementType]() {
willSet {
assert(Thread.isMainThread)
Expand All @@ -85,13 +58,13 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
}
fileprivate var backBuffer = [ElementType]()

/// Sort closure.
// Sort closure.
fileprivate var sort: ((ElementType, ElementType) -> Bool)?

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

/// The serial operation queue for this controller.
// The serial operation queue for this controller.
fileprivate let serialOperationQueue: OperationQueue = {
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
Expand All @@ -101,7 +74,7 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
fileprivate var flags = (isRefreshing: false,
shouldRefresh: false)

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

public init(initialArray: [ElementType],
Expand All @@ -117,7 +90,7 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
self.observe(shouldObserveTrackedKeyPaths: false)
}

/// Compute the diffs between the current array and the new one passed as argument.
/** Compute the diffs between the current array and the new one passed as argument. */
public func update(
with values: [ElementType]? = nil,
synchronous: Bool = false,
Expand Down Expand Up @@ -182,8 +155,9 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
}
}

/// This message is sent to the receiver when the value at the specified key path relative
/// to the given object has changed.
/** This message is sent to the receiver when the value at the specified key path relative
* to the given object has changed.
*/
public override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
Expand All @@ -200,11 +174,11 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
}
}

//MARK: KVO Extension
// MARK: KVO Extension

extension Buffer where ElementType: AnyObject {

///Observe the keypaths passed as argument.
/** Observe the keypaths passed as argument. */
public func trackKeyPaths(_ keypaths: [String]) {
self.observe(shouldObserveTrackedKeyPaths: false)
self.trackedKeyPaths = keypaths
Expand All @@ -214,8 +188,9 @@ extension Buffer where ElementType: AnyObject {

extension Buffer {

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

/// - Note: This code is executed only when 'Element: AnyObject'.
// - Note: This code is executed only when 'Element: AnyObject'.
fileprivate func objectDidChangeValue(for keyPath: String?, in object: AnyObject?) {
dispatchOnMainThread {
self.update() {
Expand Down

0 comments on commit 788a84d

Please sign in to comment.