Skip to content

Add the .task modifier to view its lifecycle #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ViewLifecycle/LifecycleMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct LifecycleMonitor: View {
var label: String
@State private var stateTimestamp: Date = .now
@State private var onAppearTimestamp: Date? = nil
@State private var taskStartTimestamp: Date? = nil
@State private var onDisappearTimestamp: Date? = nil
@State private var color: Color = .random()

Expand Down Expand Up @@ -35,6 +36,14 @@ struct LifecycleMonitor: View {
.monospacedDigit()
}
.help("When onDisappear was last called for this view")

GridRow(alignment: .firstTextBaseline) {
Text("task")
Text(timestampLabel(for: taskStartTimestamp))
.monospacedDigit()
}
.help("When task was last called for this view")

}
.font(.callout)
}
Expand All @@ -60,6 +69,14 @@ struct LifecycleMonitor: View {
onDisappearTimestamp = timestamp
}
}
.task {
let timestamp = Date.now
print("\(timestamp) \(label): task started")
let animation: Animation? = taskStartTimestamp == nil ? nil : .easeOut(duration: 1)
withAnimation(animation) {
taskStartTimestamp = timestamp
}
}
}

private func timestampLabel(for timestamp: Date?) -> LocalizedStringKey {
Expand Down