An extension that provides essential state modifiers to SwiftUI views.
ViewState
is a SwiftUI extension that introduces state modifiers to manage common view states. ViewState
simplifies the process of displaying loaders, representing empty states and showcasing errors.
You can add ViewState
as a dependency to your project using Swift Package Manager by adding it to the dependencies value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/kevinhermawan/ViewState.git", .upToNextMajor(from: "1.0.0"))
]
Alternatively, in Xcode:
- Open your project in Xcode.
- Click on
File
->Swift Packages
->Add Package Dependency...
- Enter the repository URL:
https://github.com/kevinhermawan/ViewState.git
- Choose the version you want to add. You probably want to add the latest version.
- Click
Add Package
.
import SwiftUI
import ViewState
struct Example: View {
@State private var viewState: ViewState? = .loading
var body: some View {
VStack {
Text("This is the main content.")
}
.when(viewState, is: .loading) {
ProgressView()
.progressViewStyle(.circular)
}
.when(viewState, is: .empty) {
ContentUnavailableView {
Text("There is no data to display.")
}
}
.whenError(viewState) { message in
ContentUnavailableView {
Text(message)
}
}
}
}