joelarcos
11/5/2018 - 9:39 PM

DeviceFeeder.swift

import Foundation
import AppKit

protocol DeviceFeederDelegate {
    func didFinishArray()
}


class DeviceFeeder {
    var delegate: DeviceFeeder?
    var dictionaries: Array<Dictionary<String,Any>>?
    var origMap: Dictionary<String,Any>?
    var arraysByIndex: Dictionary<Int,Any> = [:]
    var indexSelected: Int?
    var timer: TimeManager!
    
    func createArrays(deviceIds: [String]?) {
        
        guard let arrays = deviceIds?.chunks(25) else {return}
        for (index, array) in arrays.enumerated() {
            let tempMap: Dictionary<String,Any> = ["array":array, "status":"incomplete"]
            arraysByIndex[index] = tempMap
        }
    }
    
    
    func findNextArrayForClient() -> [String]? {
        var nextArray: [String]?
        
        for (index,dictionary) in arraysByIndex {
            for (key, value) in (dictionary as? [String:Any])! {
                if key == "status" && value as? String == "incomplete" {
                    if let dict = dictionary as? [String:Any] {
                        if let array = dict["array"] as? [String] {
                            nextArray = array
                            indexSelected = index
                        }
                    }
                }
            }
        }
        return nextArray
    }
    
    func markArrayComplete(index: Int) {
        guard var dictionary = arraysByIndex[index] as? Dictionary<String,Any> else {return}
        dictionary["status"] = "complete"
        arraysByIndex[index] = dictionary
    }
}