Silver Challenge solution (dark mode)

override func viewDidAppear(_ animated: Bool) {
        let date = Date()
        let hour = Calendar.current.component(.hour, from: date)
        
        if hour > 17 || hour < 6 {
            self.view.backgroundColor=UIColor.darkGray
        }
        
    }

I did not feel the need to add code changing it back to a lighter background as that is already done by the storyboard if the condition isn’t met.

3 Likes

In order to generate random colors, I extended CGFloat at the file scope level to include a random number generator that I found on the internet:

public extension CGFloat {
    /// SwiftRandom extension
    public static func random(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
        return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower
    }
}

Then I played around with different colors. I don’t particularly love the colors I chose but I do love the random color generator that is activated at dusk:

override func viewWillAppear(_ animated: Bool) {
        
    // get the hour component of the current time
    let calendar = Calendar(identifier: Calendar.Identifier.iso8601)
    let currentHour = calendar.component(.hour, from: Date())

    // Utilize the CGFloat extension above to generate random floats
        var randomFloat: CGFloat {
            get {
                return CGFloat.random()
            }
        }
        
        // Define my colors, including the random color generator at dusk
        let darkColor = UIColor(red: 13/255.0, green: 61/255.0, blue: 91/255.0, alpha: 1.0)
        let morningColor = UIColor(red: 250/255.0, green: 150/255.0, blue: 12/255.0, alpha: 0.6)
        let noonColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 150/255.0, alpha: 1.0)
        let duskColor = UIColor(red: randomFloat, green: randomFloat, blue: randomFloat, alpha: 0.6)
        
        // Switch colors based on the hour of the day
        switch currentHour {
        case 7...10:
            view.backgroundColor = morningColor
        case 11...14:
            view.backgroundColor = noonColor
        case 15...19:
            view.backgroundColor = duskColor
        default:
            view.backgroundColor = darkColor
        }

    }

This is my first time working with UIColor so would appreciate any thoughts / advice.

1 Like

I didn’t know about accessing the calendar like that. Cool. I went about it with a formatter. I also didn’t think to leave the color state alone if the condition wasn’t met. I was assuming the state would carry over as long as the view had already been loaded in memory once. Interesting. I’ll have to play with that. Anyway, the formatter approach. It’s not as clean:

override func viewWillAppear(_ animated: Bool) {
    // Get the current hour and see if it's past 6
    let now = Date()
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone.current
    formatter.dateFormat = "HH"
    if let hour = Int(formatter.string(from: now)) {
        if hour > 18 || hour < 6 {
            // originalColor = "F5F4F1"
            self.view.backgroundColor = UIColor.gray
        } else {
            self.view.backgroundColor = UIColor.lightGray
        }
    }
}

Stupid question: viewDidLoad() requires a call out to super.viewDidLoad(). However . . . viewWillAppear() doesn’t??? That was throwing me for a bit.

yup, viewWillAppear() too.
// super.viewWillAppear(animated)

Good, nice simple solution.
I did the same. plus:

} else {
self.view.backgroundColor = UIColor.lightGray
}