Skip to content
Permalink
17dd1df3c3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
82 lines (69 sloc) 2.67 KB
//
// WellnessInterfaceController.swift
// SynchronyFinancial WatchKit Extension
//
// Created by Alan Maynard on 1/14/19.
// Copyright © 2019 Alan Maynard. All rights reserved.
//
import WatchKit
import Foundation
class WellnessInterfaceController: WKInterfaceController {
var accounts: [Account] = []
var wellnessScore: Int = 0
var utilizationScore: Int = 0
var previousUtil: Int = 0
var lastpayment = 0.0
var lastPmtMetric = 0
var avgUtilization = 0.0
var healthyCount = 0
@IBOutlet weak var image: WKInterfaceImage!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
if let context = context as? [String: [Account]], let accts = context["accts"] {
accounts = accts
}
calculateUtilization()
calculateWellness()
image.setImageNamed("scores")
image.startAnimatingWithImages(in: NSRange(location: 1, length: wellnessScore), duration: 1.5, repeatCount: 1)
}
private func calculateUtilization() {
var totalBalance: Double = 0
var totalCredit: Double = 0
var previous: Double = 0
var average = 0.0
accounts.forEach {
totalCredit += $0.creditLimit
totalBalance += $0.curBalance
let util = $0.curBalance / $0.creditLimit
if util <= 0.3 {
healthyCount += 1
}
average += util
previous += Double(arc4random_uniform(UInt32($0.creditLimit)))
}
avgUtilization = average / Double(accounts.count)
utilizationScore = Int((totalBalance / totalCredit) * 100)
previousUtil = Int((previous / totalCredit) * 100)
if accounts.count == 1 {
lastpayment = accounts[0].lastPayment
let rate = Float(accounts[0].curBalance / lastpayment).rounded(.up)
lastPmtMetric = Int(rate)
}
}
private func calculateWellness() {
let m1 = Double(utilizationScore) * 0.50
wellnessScore = Int(round(m1))
}
@IBAction func moreButtonTapped() {
if accounts.count > 1 {
let pages = ["UtilizationScore", "UtilizationTrend", "AvgUtilization", "HealthyAccts"]
let contexts: [Any] = [utilizationScore, [utilizationScore, previousUtil], avgUtilization, healthyCount]
presentController(withNames: pages, contexts: contexts)
} else {
let pages = ["UtilizationScore", "UtilizationTrend", "LastPayment"]
let contexts: [Any] = [utilizationScore, [utilizationScore, previousUtil], [lastpayment, lastPmtMetric]]
presentController(withNames: pages, contexts: contexts)
}
}
}