Skip to content

Instantly share code, notes, and snippets.

@ctrevarthen
Last active November 24, 2015 15:55
Show Gist options
  • Save ctrevarthen/3829a9200bd74c386f82 to your computer and use it in GitHub Desktop.
Save ctrevarthen/3829a9200bd74c386f82 to your computer and use it in GitHub Desktop.
ShopQuick - PrioritiesManager
func applicationWillEnterForeground(application: UIApplication) {
PrioritiesManager.sharedInstance.checkForResetTrigger()
}
let resetKey = "com.detroitlabs.shopfast.ResetPriorities"
func checkForResetTrigger() {
let userDefaults = NSUserDefaults.standardUserDefaults()
let shouldReset = userDefaults.boolForKey(resetKey)
if shouldReset {
self.products = []
userDefaults.removeObjectForKey(self.productsKey)
userDefaults.setBool(false, forKey: resetKey)
}
}
<dict>
<key>Title</key>
<string>ClearDataGroup</string>
<key>Type</key>
<string>PSGroupSpecifier</string>
</dict>
<dict>
<key>DefaultValue</key>
<false/>
<key>Key</key>
<string>com.detroitlabs.shopfast.ClearPriorities</string>
<key>Title</key>
<string>Clear saved sort order</string>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
</dict>
class PrioritiesManager : ProductManager {
static let sharedInstance = PrioritiesManager()
var lastProductPurchased : Product?
override init() {
super.init()
self.productsKey = "com.detroitlabs.shopfast.priorities"
self.loadProductsFromDefaults()
}
func addProduct(product: Product) {
let newProduct = Product(name: product.name, qty: 0)
if let _ = lastProductPurchased,
let index : Int = self.findIndexForProductName(lastProductPurchased!.name) {
self.insertProduct(newProduct, atIndex: index + 1) // Rule #1
}
lastProductPurchased = product
self.saveProductsToDefaults()
}
func addProduct(product: Product) {
let newProduct = Product(name: product.name, qty: 0)
if let _ = lastProductPurchased,
let index : Int = self.findIndexForProductName(lastProductPurchased!.name) {
// if the new product already exists
if let _ = self.findProductByName(newProduct.name) {
// do nothing -- Rule #2
}
else {
self.insertProduct(newProduct, atIndex: index + 1)
}
}
lastProductPurchased = product
self.saveProductsToDefaults()
}
func addProduct(product: Product) {
let newProduct = Product(name: product.name, qty: 0)
// if the product exists
if self.doesProductExist(newProduct.name) {
// if there is a last item purchased
if let lastProduct = self.lastProductPurchased {
// remove the last product purchased so it can be moved
let currentLastProductPriority = self.findIndexForProductName(lastProduct.name)
self.removeProductWithName(lastProduct.name)
// insert the last product purchased directly above the current product
self.insertProduct(lastProduct, atIndex: currentLastProductPriority!) // Rule #3
} // else, do nothing -- Rule #2
}
else {
// if there is a last item purchased
if let lastProduct = self.lastProductPurchased {
if let lastProductIndex = self.findIndexForProductName(lastProduct.name) {
// add the new product right after the last product purchased
self.insertProduct(newProduct, atIndex: lastProductIndex + 1) // Rule #1
}
}
else {
// just add the product to the end of the list
self.products.append(newProduct) // Rule #0
}
}
self.lastProductPurchased = newProduct
self.saveProductsToDefaults()
}
class PrioritiesManager : ProductManager {
static let sharedInstance = PrioritiesManager()
var productsInThisTrip : [Product]?
func addProduct(product: Product) {
let newProduct = Product(name: product.name, qty: 0)
// if the product exists
if self.doesProductExist(newProduct.name) {
// Rule #3
productsInThisTrip?.forEach({ (p: Product) -> () in
// remove the last product purchased so it can be moved
let lastProductPriority = self.findIndexForProductName(p.name)
self.removeProductWithName(p.name)
// insert the last product purchased directly above the current product
if let existingIndex = self.findIndexForProductName(newProduct.name) {
// only move products UP the priority list
self.insertProduct(p, atIndex: min(existingIndex, lastProductPriority!))
}
})
// Rule #2 - do nothing
}
else {
// if there is a last item purchased
if let lastProduct = productsInThisTrip?.last {
if let lastProductIndex = self.findIndexForProductName(lastProduct.name) {
// add the new product right after the last product purchased
self.insertProduct(newProduct, atIndex: lastProductIndex + 1) // Rule #1
}
}
else {
// just add the product to the end of the list
products.append(newProduct) // Rule #0
}
}
productsInThisTrip?.append(newProduct)
self.saveProductsToDefaults()
}
}
func addProduct(product: Product) {
let newProduct = Product(name: product.name, qty: 0)
// if the product exists
if self.doesProductExist(newProduct.name) {
// if there is a last item purchased
if let lastProduct = self.lastProductPurchased {
// remove the last product purchased so it can be moved
let currentLastProductPriority = self.findIndexForProductName(lastProduct.name)
self.removeProductWithName(lastProduct.name)
// insert the last product purchased directly above the current product
if let existingIndex = self.findIndexForProductName(newProduct.name) {
self.insertProduct(lastProduct, atIndex: min(existingIndex, currentLastProductPriority!)) // Rule #3
}
} // else, do nothing -- Rule #2
}
else {
// if there is a last item purchased
if let lastProduct = self.lastProductPurchased {
if let lastProductIndex = self.findIndexForProductName(lastProduct.name) {
// add the new product right after the last product purchased
self.insertProduct(newProduct, atIndex: lastProductIndex + 1) // Rule #1
}
}
else {
// just add the product to the end of the list
self.products.append(newProduct) // Rule #0
}
}
self.lastProductPurchased = newProduct
self.saveProductsToDefaults()
}
func addProduct(product: Product) {
let newProduct = Product(name: product.name, qty: 0)
if let _ = lastProductPurchased,
let index : Int = self.findIndexForProductName(lastProductPurchased!.name) {
// if the new product already exists
if let _ = self.findProductByName(newProduct.name) {
// do nothing -- Rule #2
}
else {
self.insertProduct(newProduct, atIndex: index + 1) // Rule #1
}
}
else {
self.products.append(newProduct) // Rule #0
}
lastProductPurchased = product
self.saveProductsToDefaults()
}
class ShoppingListManager : ProductManager {
static let sharedInstance = ShoppingListManager()
let prioritiesManager = PrioritiesManager.sharedInstance
func markPurchasedAtIndex(index: Int) {
let product = self.products[index]
product.purchased = true
self.prioritiesManager.addProduct(product)
if !self.hasUnpurchasedItems() {
self.prioritiesManager.lastProductPurchased = nil
}
self.saveProductsToDefaults()
}
func hasUnpurchasedItems() -> Bool {
let unpurchased = self.products.filter { (p : Product) -> Bool in
return !p.purchased
}
return unpurchased.count > 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment