Combine version of RxSwift/ActivityIndicator & RxSwift/ErrorIndicator that help us to track the loading state & errors of all publisher, particularly in network request publishers.
Let's declare an instance of ActivityIndicator wherever you want to handle the requests (ex: ViewModel):
let activityIndicator = ActivityIndicator()
// Recommend to expose the loading state only
lazy var loadingPublisher = activityIndicator.loading.eraseToAnyPublisher()
Then use the trackActivity
operator to track the state of request publishers:
refreshTokenPublisher.trackActivity(activityIndicator)
getUserInfoPublisher.trackActivity(activityIndicator)
Now you can handle the loading state on the View component:
viewModel.loadingPublisher
.sink { isLoading in
self.showHUD(isLoading)
}
Let's declare an instance of ErrorIndicator wherever you want to handle the error of publisher requests (ex: ViewModel):
let errorIndicator = ErrorIndicator()
// Recommend to expose the errors only
lazy var errorPublisher = errorIndicator.errors.eraseToAnyPublisher()
Then use the trackError
operator to track the state of request publishers:
refreshTokenPublisher.trackError(errorIndicator)
// Can track loading together
getUserInfoPublisher.trackActivity(activityIndicator)
.trackError(errorIndicator)
Now you can handle the errors in one place such as in the View component:
viewModel.errorPublisher
.sink { error in
self.showErrorPopup(error)
}
SwiftUI regularly re-render the View
, do not expose your loadingPublisher
& errorPublisher
as computed variable, using lazy variable instead. Please follow this issue for more detail & best practices.
dependencies: [
.package(url: "https://github.com/duyquang91/ActivityIndicator.git", from: "1.0")
]
The source code is very simple, feel free to copy into your project 🤗
Pull requests are welcome 🤗
Copyright by @duyquang91
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.