Skip to content

Commit

Permalink
Created a CalculatedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyclaysmith committed Mar 26, 2015
1 parent bcb3669 commit bb9b85f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions AwfulBinding.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
5062BFC21AA5075900B485B1 /* WeakDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5062BFC11AA5075900B485B1 /* WeakDictionaryTests.swift */; };
508A44841AC46A580040C427 /* CalculatedArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508A44831AC46A580040C427 /* CalculatedArray.swift */; };
50965EE71A97B5C600258080 /* BindableArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50C49F111A7831890010C199 /* BindableArray.swift */; };
50965EE81A97B5C600258080 /* BindableValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50C49F101A7831890010C199 /* BindableValue.swift */; };
50965EE91A97B5C600258080 /* CalculatedValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50AA32BF1A8EB8410073EA6A /* CalculatedValue.swift */; };
Expand Down Expand Up @@ -36,6 +37,7 @@
/* Begin PBXFileReference section */
5033BA211A815ED900516627 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
5062BFC11AA5075900B485B1 /* WeakDictionaryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeakDictionaryTests.swift; sourceTree = "<group>"; };
508A44831AC46A580040C427 /* CalculatedArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CalculatedArray.swift; sourceTree = "<group>"; };
50AA32BD1A8EB5EF0073EA6A /* PUpdateable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PUpdateable.swift; sourceTree = "<group>"; };
50AA32BF1A8EB8410073EA6A /* CalculatedValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CalculatedValue.swift; sourceTree = "<group>"; };
50C49EEB1A7831710010C199 /* AwfulBinding.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AwfulBinding.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -97,6 +99,7 @@
50C49F121A7831890010C199 /* PBindableCollection.swift */,
50AA32BD1A8EB5EF0073EA6A /* PUpdateable.swift */,
50C49EEE1A7831710010C199 /* Supporting Files */,
508A44831AC46A580040C427 /* CalculatedArray.swift */,
);
path = AwfulBinding;
sourceTree = "<group>";
Expand Down Expand Up @@ -237,6 +240,7 @@
50AA32C01A8EB8410073EA6A /* CalculatedValue.swift in Sources */,
50C49F141A7831890010C199 /* BindableArray.swift in Sources */,
50C49F131A7831890010C199 /* BindableValue.swift in Sources */,
508A44841AC46A580040C427 /* CalculatedArray.swift in Sources */,
50C49F151A7831890010C199 /* PBindableCollection.swift in Sources */,
50AA32BE1A8EB5EF0073EA6A /* PUpdateable.swift in Sources */,
);
Expand Down
49 changes: 49 additions & 0 deletions AwfulBinding/CalculatedArray.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// CalculatedArray.swift
// AwfulBinding
//
// Created by Zachary Smith on 3/26/15.
// Copyright (c) 2015 Scal.io. All rights reserved.
//

import Foundation

public extension BindableArray{
public func transform<ValueType>(calculator:([PUpdateable]) -> [ValueType]) -> CalculatedArray<ValueType>{
return CalculatedArray(boundValues: [self], calculator: calculator)
}
}

public class CalculatedArray<ValueType>:BindableArray<ValueType>{
private let _listenerOwner:NSObject = NSObject()

private func calculateValue(){
self.internalArray = calculator(_boundValues)
}

public var calculator:([PUpdateable]) -> [ValueType]

private var _boundValues:[PUpdateable]

//TODO: fine-tune updates for PBindableCollections
public init(boundValues:[PUpdateable], calculator:([PUpdateable]) -> [ValueType]){
_boundValues = boundValues
self.calculator = calculator

super.init(internalArray:calculator(boundValues))

for boundValue in boundValues{
boundValue.addAnyUpdateListener(_listenerOwner, listener: boundValueUpdated, alertNow: false)
}
}

deinit{
for boundValue in _boundValues{
boundValue.removeAnyUpdateListener(_listenerOwner)
}
}

private func boundValueUpdated(){
calculateValue()
}
}

0 comments on commit bb9b85f

Please sign in to comment.