StatefulView for Swift

I have recently spent some time developing a small library designed to reduce the repetitive task of handling specific states when developing an iOS application.

Background

Developing an open source project, no matter how small, has been a goal of mine for sometime. As a developer, I want all of my contributions to be available to provide insight or guidance to other developers that may be facing similar challenges. However, due to the need for quick development and business practices outside of my control, it is often difficult to work on documenting a project and ensuring nothing within the project could adversely affect the company that employs me. Therefore, I thought a simple, business-agnostic, UI-based library would be a perfect place to begin.

Motivation

When making API calls within a native or web-based application, I had found myself designing around a similar pattern for handling responses and errors. Recently, this pattern kept resurfacing when I had been working on iOS applications. I did what any other apt developer would do, and I looked for an existing solution to make my life easier.

I found a couple projects that pretty much outlined the exact redundancy that I wanted to reduce. One being StatefulViewController for Swift and the other being DynamicBox for Android.

This is where my solution becomes a little niche. Most of my iOS projects usually utilize UICollectionViews, and I heavily rely on a Instagram library called IGListKit to manage data and cell visibility. In this instance, my goal is to change a UIView that overlays on the UICollectionView within the IGListKit protocol based on the state of the request. While StatefulViewController allows me to manage the insets of the view to achieve this vie management, it felt kind of forced when mapping the insets of a view to the view overlaying the UICollectionView.

I wanted to avoid relying heavily on UIViewControllers when managing state, because, hypothetically, I think there can exist multiple state-dependent views within a ViewController. This is where StatefulView was born. A library built to change the state of a UIView based on a API request or other await-able concept.

Usability

The main concept is there exists 4 states for a UIView that can be shown at anytime.

  1. Loading
  2. Empty
  3. Error
  4. Custom

The designated views can be loaded all at once or simple utilize only a few. Additionally, the UIView can be added to the StatefulView by instantiating or simply referencing an XIB file. This allows for as much to as little interaction for each view as you would like. Anytime you would like to change the appearing view state (loading in this example), you simply call:

self.statefulView.setState(state: .loading)

Conclusion

This was my first attempt to create a small, usable library. I borrowed a lot of concepts from previous projects like StatefulViewController and DynamicBox, so I cannot say this is anything too revolutionary. But I hope it can become useful to some developers that are looking to solve a similar issue. Feel free to share your thoughts and feedback.

Find StatefulView here.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.