overCurrentContent( contentOpacity: 0.5, alignment. IndicatorView: SpinnerProgressView( color. init( name: "Placeholder ", age: 99, emoji: " ")) We will be using this view and controller in our examples: Let’s take a look at how we can use it in our views: Decide if the error state will have a retry action or not. (This could be an EmptyView() or the loadedView with a. Create the placeholder view (The one that will be there before the initial loading).Decide your strategy for initialLoading, loadingAfterInfo, errorView, and loadingAfterError states.The LoadingModifierType provides some different loading options with configurable parameters. LoadingAfterErrorType: LoadingModifierType =. LoadingAfterInfoType: LoadingModifierType =. InitialLoadingType: LoadingModifierType =. IndicatorView: IndicatorView = ProgressView(), / - loadingAfterErrorType: The type of loading indicator to show when the view is loading after it has displayed /// an error. / - errorView: The view to show when the view has an error. / - loadingAfterInfoType: The type of loading indicator to show when the view is loading after it has already /// displayed valid information. / - loadedView: The view to show when the view is not loading and has valid information. / - initialLoadingType: The type of loading indicator to show when the view is initially loading. / - indicatorView: The view to show when the view is loading. / - Parameters: /// - controller: The `ViewStateController` that controls the state of the view. / Adds a view state modifier that can display different views depending on the state of a `ViewStateController`. The code of the modifier is pretty straight forward: The ViewStateModifier is a ViewModifier that uses the given ViewStateController and configurable options to automatically update the state of a view. setState(_ state: ViewState): Sets the new state into the states array.latestNonLoading: Returns the latest informational state (info, or error) if exists.latestError: Info associated to the last time loaded error was set, disregarding if there has been an error afterwards.Nil if info has been loaded after the latest error. latestValidError: Info associated to the last time error state was set.latestInfo: Info associated to the last time loaded state was set, disregarding if there has been an error afterwards.Nil if there has been an error after the latest info. latestValidInfo: Info associated to the last time loaded state was set.isLoading: Returns true if state is loading.isInitialLoading: Returns true only if loading state was set once and there hasn’t been errors or info yet.The ViewStateController struct is the one that contains the array of historical ViewStates and has computed properties that will be used by the ViewStateModifier to determine what to do. There is an Example app available here, where most of the configuration options can be tweaked. With ViewStateController, you can easily implement complex views that depend on asynchronous data loading, and create a better user experience by showing loading spinners or error messages. It allows you to handle different states based on a historical array of states, and provides properties and methods to help you access and modify the state. gray, radius: 1, x: 0, y: 1)įorEach(0.<, id: \.ViewStateController is a framework for Swift and SwiftUI developers that provides a simple and flexible way to manage the state of views that load information from a backend. The code below provides a minimum viable example: struct MainView: View var viewModel: ViewModel Those contain around 10 images each with a size of 256 x 256 pixels. I have a SwiftUI view that consists of a TabView that has multiple pages containing a VStack with multiple Views of type SubView.
0 Comments
Leave a Reply. |