How I Made Gradulator

Wow. Its submitted. All the work is done. Now for a quick code review of some key stuff I learnt during Gradulator.


Nested Functions

I’m almost certain we all use this in our projects, but Gradulator took it to the next level. I set out with the goal of writing as little code as possible, considering Gradulator V1 and V2 used crazy amounts of code to draw graphs and the like, I needed a smarter solution. Here are some of the functions I created to help me make it easier.

Full disclosure, some of these are from Stack Overflow but I honestly can’t remember which, hat tip to anyone who I used the code of!

This one is really useful for all projects.

// MARK: -
/// Obtain a uiColor from a Hex value.
/// You might need to add a '0x' to the beginning of your Hex value.
/// Pass in a value of type UInt32
func uicolorFromHex(rgbValue:UInt32)->UIColor{
    let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
    let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
    let blue = CGFloat(rgbValue & 0xFF)/256.0
    return UIColor(red:red, green:green, blue:blue, alpha:1.0)
}

Basically a must have for any project using custom textfields.

// MARK: -
/// Add padding to the beginning of a textfield.
/// paddingAmount must be an integer.
func addPaddingToTextfield (paddingAmount: Int, textfield: UITextField) {
    let paddingHeight = UIView(frame: CGRect(x: 0, y: 0, width: paddingAmount, height: Int(textfield.frame.height)))
    textfield.leftView = paddingHeight
    textfield.leftViewMode = UITextFieldViewMode.always
}

I used this function -alot- of times.

// MARK: -
/// Calculates how much to score on next test.
/// Can return a value greater than 100 or a negative value.
/// For purposes of informing the user if obtaining goal is not possible, greater than 100 will be handled in viewcontroller.
func returnScoreNextTest (underSubject: String, percentage: Int, weightage: Double) -> Int {
    // Obtain an individual ResultData object for the subject in question.
    let subjectData = resultsList.first(where: { $0.subject == underSubject })
    // If there is data in subjectData
    if !(subjectData == nil) {
        var resultsArray = subjectData?.results
        resultsArray?.append(percentage)
        var weightagesArray = subjectData?.weightage
        weightagesArray?.append(weightage)
        var WeightagesTotalArray: [Double] = []
        for (perc, weight) in zip(resultsArray!, weightagesArray!) {           WeightagesTotalArray.append(calculateWeightage(percentage: perc, weightage: weight))
        }
        if !(subjectData?.goal == nil) {
            let markToScoreNextTest = ( ( ( Double((subjectData?.goal)!) - (WeightagesTotalArray.reduce(0, +)) ) / returnRemainingWeightage(ofSubject: underSubject) ) * 100 )
            return Int(markToScoreNextTest)
        }
        // No goal set, defaulting to a default goal of 75% or A1
        else { return 75 }
    }
    // The expected subjectData doesn't exist, likely first time creation, return 75.
    else { return 75 }
}

$echo SST Gradulator Redux

Wow. Barely done with SpaceRace and already a new project on my plate? Talk about overwork. In all honesty, I do enjoy making new stuff so it isn’t that bad, but it does mean no more Netflix binging.

So I could just present Gradulator in its current state, but I hate showing off work I’m not that proud of.
So in that light, Gradulator is getting a makeover, inside and out, hopefully rewriting it to be less of a resource hog, see if I can get the GPU to handle some of that graph rendering instead of making the CPU do all the dirty work. Actually, the graphs I’m rendering are so small its negligible but I like the idea of perfecting resource management at the small scale so when I have to work on a bigger app, I’d have done it before. Sorry, I’m rambling.

Hope to update soon on the state of Gradulator, I’ve had some luck implementing core graphics today, hope nothing breaks.

var me = (sleep – ( code + caffeine * red bull ))

A lot of the time, I step back and feel overwhelmed by the number of projects I have to accomplish. Each project breaks down into a vast expanse of minute sub-projects.

But then I take another step back and see the beauty of it all. Hundreds of lines of code, to accomplish multiple tasks. Its a collection of art.

But that doesn’t dispel the fact that I have to take out time from my sleep to finish up the projects. Along with the red bull and Pokka canned coffee.

Sleep is for the weak.

I am weak.