-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcb3669
commit bb9b85f
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |