Skip to content

Instantly share code, notes, and snippets.

@pgherveou
Created April 22, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgherveou/b286818f2ecfe62672c4 to your computer and use it in GitHub Desktop.
Save pgherveou/b286818f2ecfe62672c4 to your computer and use it in GitHub Desktop.
import Foundation
// global singleton instance
let tweaks = FBTweakStore.sharedInstance()
/**
* ## Usage
*
* class MyClass {
* struct statics {
* var damping: Double {
* return tweaks.getValue(category: "animations", collection: "menu", name: "spring", defaultValue: NSNumber(int: 0)).doubleValue
* }
* }
* }
*/
extension FBTweakStore {
func get<T:AnyObject>(category categoryName: String, collection collectionName: String, name: String, defaultValue: T) -> FBTweak {
// get category
let category: FBTweakCategory
if let existingCategory = tweakCategoryWithName(categoryName) {
category = existingCategory
} else {
category = FBTweakCategory(name: categoryName)
addTweakCategory(category)
}
// get collection
let collection: FBTweakCollection
if let existingCollection = category.tweakCollectionWithName(collectionName) {
collection = existingCollection
} else {
collection = FBTweakCollection(name: collectionName)
category.addTweakCollection(collection)
}
// get tweak
let identifier = "\(categoryName).\(collectionName).\(name)".lowercaseString
let tweak: FBTweak
if let existingTweak = collection.tweakWithIdentifier(identifier) {
tweak = existingTweak
} else {
tweak = FBTweak(identifier: identifier)
tweak.name = name
tweak.defaultValue = defaultValue
collection.addTweak(tweak)
}
return tweak
}
func getValue<T:AnyObject>(#category: String, collection: String, name: String, defaultValue: T) -> T {
let tweak = get(category: category, collection: collection, name: name, defaultValue: defaultValue)
return tweak.value as! T
}
}
extension FBTweak {
var value: FBTweakValue {
get {
return currentValue ?? defaultValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment