0% found this document useful (0 votes)
12 views4 pages

Live Activity 2

The document contains Swift code for a Pomodoro timer application using ActivityKit and SwiftUI. It defines a user interface with buttons to start and stop a timer, along with a widget that displays the timer's status. The TimerAttributes structure is used to manage the timer's state and attributes.

Uploaded by

Isaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Live Activity 2

The document contains Swift code for a Pomodoro timer application using ActivityKit and SwiftUI. It defines a user interface with buttons to start and stop a timer, along with a widget that displays the timer's status. The TimerAttributes structure is used to manage the timer's state and attributes.

Uploaded by

Isaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//

// NewView.swift
// Rituals
//
// Created by Isaiah Akhidenor on 17/02/2024.
//

import ActivityKit
import SwiftUI

struct NewView: View {


@State private var activity: Activity<TimerAttributes>? = nil

var body: some View {


VStack(spacing: 16) {
Button("Start Activity") {
startActivity()
}

Button("Stop Activity") {
stopActivity()
}
.buttonStyle(.bordered)
.controlSize(.large)
}
.padding()
}

func startActivity() {
let attributes = TimerAttributes(timerName: "Dummy Timer")
let state = TimerAttributes.TimerStatus(endTime: Date().addingTimeInterval(60 * 5))

activity = try? Activity<TimerAttributes>.request(attributes: attributes, contentState: state,


pushType: nil)
}

func stopActivity() {
let state = TimerAttributes.TimerStatus(endTime: Date())

Task {
await activity?.end(using: state, dismissalPolicy: .immediate)
}
}
func updateActivity() {
let state = TimerAttributes.TimerStatus(endTime: Date().addingTimeInterval(60 * 10))

Task {
await activity?.update(using: state)
}
}
}

#Preview {
NewView()
}

//
// Pomodoro_Widget.swift
// Pomodoro Widget
//
// Created by Isaiah Akhidenor on 17/02/2024.
//

import ActivityKit
import WidgetKit
import SwiftUI

struct TimerActivityView: View {


let context: ActivityViewContext<TimerAttributes>
var body: some View {
VStack {
Text(context.attributes.timerName)
.font(.headline)
Text(context.state.endTime, style: .timer)

}.padding(.horizontal)
}
}

struct Pomodoro_Widget: Widget {


let kind: String = "Tutorial_Widget"

var body: some WidgetConfiguration {


ActivityConfiguration(for: TimerAttributes.self, content: { context in
TimerActivityView(context: context)
}, dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
Text("MAIN")
}
} compactLeading: {
Text("CL")
} compactTrailing: {
Text("CT")
} minimal: {
Text("M")
}
}
)
}
}

import ActivityKit
import WidgetKit
import SwiftUI

struct Pomodoro_WidgetBundle: WidgetBundle {


var body: some Widget {
Pomodoro_Widget()
}
}

import WidgetKit
import ActivityKit
import SwiftUI

struct TimerAttributes: ActivityAttributes {


public typealias TimerStatus = ContentState
public struct ContentState: Codable, Hashable {
var endTime: Date
}

var timerName: String


}

You might also like