Home > Enterprise >  SwiftUI-How to formate seconds into Minutes:Seconds?
SwiftUI-How to formate seconds into Minutes:Seconds?

Time:11-22

I am trying to make a timer in SwiftUI and everything works well so far but I can't figure out how to format the seconds into seconds and minutes. So for example if the countdown was 299 seconds, I want it to show 4(mins):59(secs) instead of just 300 seconds.

I saw a video on youtube making a timer too and they managed to format it via making minutes and seconds constants then format it via a function but it didn't really work for me.

import SwiftUI

struct TimerView: View {
    var timerEnd = 0
    @State var countdownTimer = 300
    @State var timerRunning = false
    @State var isPaused = false
    @State var isActive = false
    @State var showingAlert = false
    let timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common).autoconnect()
    
    func format(result: Int) -> String {
        let value = String(format:"%d:\(300/60)", countdownTimer)
        return value
    }
    
    func reset() {
        countdownTimer = 300
        timerRunning = true
    }
    
    
    
    var body: some View {
        VStack {
            Text("Time: \(format(result: countdownTimer))")
                .padding()
                .onReceive(timer) { _ in
                    if countdownTimer > 0 && timerRunning {
                        countdownTimer -= 1
                    } else {
                        timerRunning = false
                    }
                    
                }
                .font(.system(size: 30))
            
            HStack(spacing:30) {
                Button(timerRunning ? "Reset" : "Start") {
                    reset()
                }
                .padding()
                .background((Color(red: 184/255, green: 243/255, blue: 255/255)))
                .foregroundColor(.black)
                .cornerRadius(10)
                .font(Font.system(size: UIFontMetrics.default.scaledValue(for: 16)))
                
                Button(timerRunning ? "Pause" : "Resume") {
                    if timerRunning == true {
                        timerRunning = false
                    } else {
                        timerRunning = true
                    }
                    
                }
                .padding()
                .foregroundColor(.black)
                .background(.red)
                .cornerRadius(10)
                .font(Font.system(size: UIFontMetrics.default.scaledValue(for: 16)))
            }
        }
    }
}

struct TimerView_Previews: PreviewProvider {
    static var previews: some View {
        TimerView()
    }
}

CodePudding user response:

You can do with an extension class like this. After that code you will see a method in your int value.

You can use countdownTimer.convertDurationToString()

extension Int {
    
    public func hmsFrom() -> (Int, Int, Int) {
        return (self / 3600, (self % 3600) / 60, (self % 3600) % 60)
    }
    
    public func convertDurationToString() -> String {
        var duration = ""
        let (hour, minute, second) = self.hmsFrom()
        if (hour > 0) {
            duration = self.getHour(hour: hour)
        }
        return "\(duration)\(self.getMinute(minute: minute))\(self.getSecond(second: second))"
    }
    
    private func getHour(hour: Int) -> String {
        var duration = "\(hour):"
        if (hour < 10) {
            duration = "0\(hour):"
        }
        return duration
    }
    
    private func getMinute(minute: Int) -> String {
        if (minute == 0) {
            return "00:"
        }

        if (minute < 10) {
            return "0\(minute):"
        }

        return "\(minute):"
    }
    
    private func getSecond(second: Int) -> String {
        if (second == 0){
            return "00"
        }

        if (second < 10) {
            return "0\(second)"
        }
        return "\(second)"
    }
}
  • Related