import UIKit import GoogleMobileAds class IconChange: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var bannerView: GADBannerView! @IBOutlet weak var bannerViewHeight: NSLayoutConstraint! @IBOutlet weak var adLoadingError: UILabel! @IBOutlet weak var IconSelector: UITableView! var icons = ["Normal", "Summer", "Taco", "Pizza", "HotDog", "Hamburger", "Doughnut"] var names = ["Normal", "Summer", "Taco", "Pizza", "HotDog", "Hamburger", "Doughnut"] var sections = ["Unlocked", "Locked"] static var setIconPurchased = false var normalIconUnlocked = true static var summerIconUnlocked = UserDefaults.standard.bool(forKey: "summerIconUnlocked") static var tacoIconUnlocked = UserDefaults.standard.bool(forKey: "tacoIconUnlocked") static var pizzaIconUnlocked = UserDefaults.standard.bool(forKey: "pizzaIconUnlocked") static var hotdogIconUnlocked = UserDefaults.standard.bool(forKey: "hotdogIconUnlocked") static var hamburgerIconUnlocked = UserDefaults.standard.bool(forKey: "hamburgerIconUnlocked") static var doughnutIconUnlocked = UserDefaults.standard.bool(forKey: "doughnutIconUnlocked") var iconSelected = UserDefaults.standard.string(forKey: "iconSelected") static var numUnlocked = UserDefaults.standard.integer(forKey: "numUnlocked") static var pushBack = false var unlocked = Array<String>() var locked = Array<String>() override func viewDidLoad() { super.viewDidLoad() IconChange.pushBack = false unlocked = ["Normal"] if IconChange.numUnlocked == 0 { IconChange.numUnlocked = 1 } if iconSelected == "" || iconSelected == nil { iconSelected = "Normal" } if IconChange.summerIconUnlocked == Bool() { IconChange.summerIconUnlocked = false UserDefaults.standard.setValue(false, forKey: "summerIconUnlocked") } if IconChange.tacoIconUnlocked == Bool() { IconChange.tacoIconUnlocked = false UserDefaults.standard.setValue(false, forKey: "tacoIconUnlocked") } if IconChange.pizzaIconUnlocked == Bool() { IconChange.pizzaIconUnlocked = false UserDefaults.standard.setValue(false, forKey: "pizzaIconUnlocked") } if IconChange.hotdogIconUnlocked == Bool() { IconChange.hotdogIconUnlocked = false UserDefaults.standard.setValue(false, forKey: "hotdogIconUnlocked") } if IconChange.hamburgerIconUnlocked == Bool() { IconChange.hamburgerIconUnlocked = false UserDefaults.standard.setValue(false, forKey: "hamburgerIconUnlocked") } if IconChange.doughnutIconUnlocked == Bool() { IconChange.doughnutIconUnlocked = false UserDefaults.standard.setValue(false, forKey: "doughnutIconUnlocked") } if NetworkMonitor.shared.isConnected { Utilities.checkads(bannerView: bannerView, bannerViewHeight: bannerViewHeight) bannerView.rootViewController = self }else { adLoadingError.text = "Error Loading Ad" Utilities.checkToHideAds(bannerViewHeight: bannerViewHeight) } IconSelector.delegate = self IconSelector.dataSource = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) IconChange.pushBack = false if NetworkMonitor.shared.isConnected { adLoadingError.text = "Loading Ad" Utilities.checkads(bannerView: bannerView, bannerViewHeight: bannerViewHeight) bannerView.rootViewController = self }else { adLoadingError.text = "Error Loading Ad" } if iconSelected == "" { iconSelected = "Normal" } Utilities.checkToHideAds(bannerViewHeight: bannerViewHeight) Categories.open = false } func checkForUnlocked(name: String) { func checkFailedPurchase() { if Utilities.purchaseFailed == true { print("purchase failed") // Create new Alert let dialogMessage = UIAlertController(title: "Purchase Failed", message: "We failed to complete your purchase, please try again later.", preferredStyle: .alert) // Create OK button with action handler let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in print("\"Purchase Failed\" alert was dismised") }) //Add OK button to a dialog message dialogMessage.addAction(ok) // Present Alert self.present(dialogMessage, animated: true, completion: nil) }else { print("purchase succeeded") } } if name == "Normal" { IconChange.setIconPurchased = true }else if name == "Summer" { print("summerIconUnlocked = \(IconChange.summerIconUnlocked)") if IconChange.summerIconUnlocked == true { IconChange.setIconPurchased = true }else { Utilities.purchaseFailed = Bool() IAPManager.shared.purchase(product: .summerIcon) } }else if name == "Taco" { print("tacoIconUnlocked = \(IconChange.tacoIconUnlocked)") if IconChange.tacoIconUnlocked == true { IconChange.setIconPurchased = true }else { Utilities.purchaseFailed = Bool() IAPManager.shared.purchase(product: .tacoIcon) } }else if name == "Pizza" { print("pizzaIconUnlocked = \(IconChange.pizzaIconUnlocked)") if IconChange.pizzaIconUnlocked == true { IconChange.setIconPurchased = true }else { Utilities.purchaseFailed = Bool() IAPManager.shared.purchase(product: .pizzaIcon) } }else if name == "HotDog" { print("hotdogIconUnlocked = \(IconChange.hotdogIconUnlocked)") if IconChange.hotdogIconUnlocked == true { IconChange.setIconPurchased = true }else { Utilities.purchaseFailed = Bool() IAPManager.shared.purchase(product: .hotdogIcon) } }else if name == "Hamburger" { print("hamburgerIconUnlocked = \(IconChange.hamburgerIconUnlocked)") if IconChange.hamburgerIconUnlocked == true { IconChange.setIconPurchased = true }else { Utilities.purchaseFailed = Bool() IAPManager.shared.purchase(product: .hamburgerIcon) } }else if name == "Doughnut" { print("doughnutIconUnlocked = \(IconChange.doughnutIconUnlocked)") if IconChange.doughnutIconUnlocked == true { IconChange.setIconPurchased = true }else { Utilities.purchaseFailed = Bool() IAPManager.shared.purchase(product: .doughnutIcon) } } } func setIcon(name: String) { IconChange.setIconPurchased = false print(name) let app = UIApplication.shared if #available(iOS 10.3, *) { if app.supportsAlternateIcons { checkForUnlocked(name: name) if IconChange.setIconPurchased == true { app.setAlternateIconName(name, completionHandler: { (error) in if error != nil { print("error => \(String(describing: error?.localizedDescription))") }else { print("Changed Icon Sucessfully.") } }) } } } } func setTableSections() { if normalIconUnlocked == true { if unlocked.contains("Normal") { }else { unlocked.append("Normal") } }else { if locked.contains("Normal") { }else { locked.append("Normal") } } if IconChange.summerIconUnlocked == true { if unlocked.contains("Summer") { }else { unlocked.append("Summer") } }else { if locked.contains("Summer") { }else { locked.append("Summer") } } if IconChange.tacoIconUnlocked == true { if unlocked.contains("Taco") { }else { unlocked.append("Taco") } }else { if locked.contains("Taco") { }else { locked.append("Taco") } } if IconChange.pizzaIconUnlocked == true { if unlocked.contains("Pizza") { }else { unlocked.append("Pizza") } }else { if locked.contains("Pizza") { }else { locked.append("Pizza") } } if IconChange.hotdogIconUnlocked == true { if unlocked.contains("HotDog") { }else { unlocked.append("HotDog") } }else { if locked.contains("HotDog") { }else { locked.append("HotDog") } } if IconChange.hamburgerIconUnlocked == true { if unlocked.contains("Hamburger") { }else { unlocked.append("Hamburger") } }else { if locked.contains("Hamburger") { }else { locked.append("Hamburger") } } if IconChange.doughnutIconUnlocked == true { if unlocked.contains("Doughnut") { }else { unlocked.append("Doughnut") } }else { if locked.contains("Doughnut") { }else { locked.append("Doughnut") } } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print(indexPath.row) tableView.deselectRow(at: indexPath, animated: true) if indexPath.section == 0 { let icon = self.unlocked[indexPath.row] setIcon(name: icon) if IconChange.setIconPurchased == true { iconSelected = names[indexPath.row] UserDefaults.standard.setValue(names[indexPath.row], forKey: "iconSelected") IconSelector.reloadData() tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark } }else if indexPath.section == 1 { let icon = self.locked[indexPath.row] setIcon(name: icon) if IconChange.setIconPurchased == true { iconSelected = names[indexPath.row] UserDefaults.standard.setValue(names[indexPath.row], forKey: "iconSelected") IconSelector.reloadData() tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark IconChange.pushBack = true self.navigationController?.popViewController(animated: true) } } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { setTableSections() print("num unlocked: \(IconChange.numUnlocked)") if section == 0 { return IconChange.numUnlocked } else if section == 1 { return 7-IconChange.numUnlocked } return 0 } func numberOfSections(in tableView: UITableView) -> Int { return sections.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sections[section] } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) setTableSections() if indexPath.section == 0 { print("unlocked: \(unlocked)") cell.textLabel?.text = unlocked[indexPath.row] let unlockedImage : UIImage = UIImage(named: unlocked[indexPath.row])! print("The loaded image: \(unlockedImage)") cell.imageView!.image = unlockedImage print("icon selected: \(iconSelected!)") for _ in 1...unlocked.count { if unlocked[indexPath.row] == iconSelected { cell.accessoryType = .checkmark }else { cell.accessoryType = .none } } } if indexPath.section == 1{ print(locked) cell.textLabel?.text = locked[indexPath.row] let lockedImage : UIImage = UIImage(named: locked[indexPath.row])! print("The loaded image: \(lockedImage)") cell.imageView!.image = lockedImage } return cell } }