Declarative loading for Apple's declarative framework, SwiftUI
- File -> Swift Packages -> Add Package Dependency...
- Select your project
- Enter
https://github.com/kenmueller/LoadingState
for the package repository URL - Select Branch: master
- Press Finish
none
- Check with
loadingState.isNone
orloadingState == .none
- Check with
loading(startDate: Date)
- Check with
loadingState.isLoading
orloadingState == .loading
- Check with
success(endDate: Date)
- Check with
loadingState.didSucceed
orloadingState == .success
- Check with
failure(endDate: Date, error: Error)
- Check with
loadingState.didFail
orloadingState == .failure
- Check with
loadingState.reset()
- Sets to
.none
- Sets to
loadingState.startLoading()
- Sets to
.loading
with the startDate as the current date
- Sets to
loadingState.succeed()
- Sets to
.success
with the endDate as the current date
- Sets to
loadingState.fail(message: String)
- Sets to
.failure
with the endDate as the current date and the error as a BasicError with the message you passed in
- Sets to
loadingState.fail(error: Error)
- Sets to
.failure
with the end date as the current date and the error you passed in
- Sets to
loadingState.isNone: Bool
- Returns
true
if theloadingState
is none
- Returns
loadingState.isLoading: Bool
- Returns
true
if theloadingState
is loading
- Returns
loadingState.didSucceed: Bool
- Returns
true
if theloadingState
did succeed
- Returns
loadingState.didFail: Bool
- Returns
true
if theloadingState
did fail
- Returns
loadingState.error: Error?
- Returns the error if
loadingState
failed. Otherwise, returnsnil
- Returns the error if
loadingState.errorMessage: String?
- Returns the error message if
loadingState
failed. Otherwise, returnsnil
- Returns the error message if
loadingState.errorCode: Int?
- Returns the error code if
loadingState
failed. Otherwise, returnsnil
- Returns the error code if
loadingState.date: Date?
- Returns the date associated with the
loadingState
. - For example, if the
loadingState
is loading, this is thestartDate
. - If the
loadingState
is.none
, this returnsnil
.
- Returns the date associated with the
import SwiftUI
import LoadingState
struct ContentView: View {
@State var loadingState = LoadingState()
var body: some View {
VStack {
if loadingState.isNone {
Text("Click the button!")
} else if loadingState.isLoading {
Text("Loading...")
} else if loadingState.didSucceed {
Text("Success!")
} else {
Text("Oh no! An error occurred. \(loadingState.errorMessage!)")
}
Button(action: {
loadingState.startLoading()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
loadingState.succeed()
}
}) {
Text("Click me!")
}
}
}
}