Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Managing "Network States" with protocol extensions

Eeb061c8b2816b771920da1b3e7904a3?s=47 Swift India
February 09, 2019

Managing "Network States" with protocol extensions

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

February 09, 2019
Tweet

Transcript

  1. Managing “Network States” with protocol extensions Lubaba Hasnain Swift Bangalore

    Chapter 12, Saturday Feb 9th, 2019
  2. How many times in a networked application have you met

    with states like these..?
  3. Content Data is loaded successfully from the server. It is

    available and presented to the user.
  4. Empty Network call is successful. But there is no data

    to load the view.
  5. Loading Network call is still in progress. So, the user

    has to wait on that screen till the data is completely loaded.
  6. Error Error encountered while loading data over network. No Internet

    connection/internal server error/user unauthorised, etc
  7. In my last projects, I used them a lot. But

    you know, state management was not proper in my code. Problems with the current state management in the applications we build.
  8. Massive View Controllers A single view controller is tasked with

    managing the appearance of many other views, controls and other user interface elements based on state.
  9. 1. A view controller is handling many responsibilities 2. What

    if more states are added?
  10. - Class will become bloated - Number of lines of

    code will increase…. eventually leading to massive view controller issue - Some people prefer to have all their state management code in BaseViewController (A controller which all the other view controller implements)
  11. - Multiple inheritance not allowed in swift - Code is

    not re-usable. Leads to code duplication & bad design. - Are you following DRY pattern? - Will code refactoring be easier following this kind of architecture?
  12. At WWDC 2015, Apple introduced protocol-oriented programming. With it came

    the most powerful feature: Protocol Extensions. Solution
  13. 1. Achieve multiple inheritance as Swift allows multiple inheritance of

    protocols 2. Re-usable code 3. Protocols can be implemented by a class, struct or enum. Protocols can have default implementations for requirements specified in a protocol extension allowing `mixin` or `trait` like patterns.
  14. Architect a solution to manage network states using “Protocol Extensions”.

  15. 1. StateType Enum At first we will create an enum

    called StateType where we will define network states like error, empty, content, loading and a default state called none.
  16. 2. State Manager class StateManager is a class that will

    manage the addition/removal of views. It has access to the current view state being displayed on the screen. 1. viewstore variable -> to store the views on the basis of the state type 2. getView method -> Returns the view for a given state 3. addView method -> Associates/adds a view for the given state 4. removeView method -> Removes a view for the given state 5. removeAllViews method -> Remove all views and empty the viewstore property
  17. None
  18. 3. ViewStateProtocol = Magic ViewStateProtocol is the protocol that should

    be implemented by any View Controller class to add/remove view for a given state. It has some abstract methods and properties. The extension of ViewStateProtocol has the default implementation of those abstract methods and properties.
  19. None
  20. View Controller Implementation of “ViewStateProtocol”

  21. Implementing Protocol

  22. Multiple Inheritance of protocols Multiple Protocols can be implemented. Implementing

    multiple Protocols
  23. Main Advantages of this kind of architecture Code - reusability

    Any view controller class can implement this protocol to add/remove views without writing any extra line of code. Multiple Inheritance Protocol extension allows us to retain one of the best features of subclassing(inheritance). Using protocols allows us to inherit from multiple parents simultaneously Not Limited to classes Protocols can be adopted by classes, enums or structs whereas base classes and inheritance are available for classes only. Conditional Extensions By using where keyword, we only extend protocol for types that inherit from UIViewController, we are able to use UIViewController specific methods.
  24. References 1. https://github.com/aschuch/StatefulViewController 2. https://medium.com/@lubabahasnain93/managing-network-states-with-pr otocol-extensions-cab07ed9048d 3. http://ioscoachfrank.com/2018/01/07/stateful-content-controller.html 4. http://www.vadimbulavin.com/multiple-inheritance-swift/

    5. https://medium.com/@abhimuralidharan/all-about-protocols-in-swift-11a7 2d6ea354 6. https://www.swiftbysundell.com/posts/custom-container-view-controllers -in-swift
  25. Conclusion - Managing network states seems trivial but there’s lot

    of state management involved - The biggest challenge comes in re-using the code but most tutorials tend to ignore them - Proper architecture to manage states in any networked application. - Protocol extensions is a powerful feature that needs to be explored more.
  26. Quick Demo