Skip to content

Frugghi/AsyncView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AsyncView

A SwiftUI view that asynchronously loads and displays its content, similar to AsyncImage.

Usage

AsyncView asynchronously loads and displays its content and while the load operation is in progress it displays a placeholder that fills the available space.

For example:

AsyncView { () -> String in
    try await Task.sleep(nanoseconds: NSEC_PER_SEC)

    return "Success!"
} content: { value in
    Text(value)
} placeholder: {
    Text("Loading...")
}

To gain more control over the loading process, you can use the initializer which takes a content closure that receives an AsyncViewPhase to indicate the state of the loading operation:

AsyncView { () -> String in
    try await Task.sleep(nanoseconds: NSEC_PER_SEC)

    return "Success!"
} content: { phase in
    if let value = phase.value {
        Text(value) // Displays the loaded value.
    } else if phase.error != nil {
        Text("Failed") // Indicates an error.
    } else {
        Text("Loading...") // Acts as a placeholder.
    }
}

About

A SwiftUI view that asynchronously loads and displays its content.

Topics

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages