From the course: Swift 5: Protocol-Oriented Programming

Weather app design

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

Weather app design

- [Instructor] We aim to implement a simple, yet functional weather app using protocol-oriented programming principles. The app will display weather information for a given location by getting the data using web services. The weather app follows the Model-View-ViewModel architecture, in short MVVM. The MVVM architecture separates the user interface from business logic by introducing three components. The View, which consists of visual elements, text fields, labels, switches, and so on. The View is concerned with tasks related to presenting the data such as layout, font type, color, and animation. The ViewModel represents the state of the UI. We'll talk about it later. The Model is the application state and defines the logic to manage that state. The View depends on the ViewModel. And the ViewModel has a reference to the Model. However, this dependency is unidirectional. The ViewModel doesn't hold the reference to the View. And the Model doesn't know about the ViewModel. The View is completely decoupled from the Model. This design provides a clean separation of concerns and promotes testability and reusability. The ViewModel propagates Model changes to the View. It uses connections called data bindings. With data binding, the View reacts to ViewModel changes automatically with no additional coding. We're going to rely on SwiftUI and the combined framework to implement the MVVM architecture. Here's a high-level overview of the weather apps design. The View displays the current weather information and allows the user to enter a location. The ViewModel uses a data provider. The Model to fetch weather information. We'll make it conform to the ObservableObject protocol to enable data binding. The Model component provides weather information. We're going to fetch weather data from the internet using web services. We encapsulate the required initialization steps, network cause and data conversions into dedicated controller types. We'll rely on built-in protocols to decode the JSON payloads. So this is our high-level design. Now let's switch to Xcode.

Contents