Back to rankings

nalexn/clean-architecture-swiftui

Swift

SwiftUI sample app using Clean Architecture. Examples of working with SwiftData persistence, networking, dependency injection, unit testing, and more.

swiftuiswiftarchitectureclean-architecturecleanarchitectureexample-projectswiftui-examplemvvminteractorsampledemoswift-data
Star Growth
Stars
6.6k
Forks
828
Weekly Growth
Issues
20
2k4k6k
Oct 2019Jan 2022Apr 2024Jul 2026
README

Clean Architecture for SwiftUI + Combine

A demo project showcasing the setup of the SwiftUI app with Clean Architecture.

The app uses the restcountries.com REST API to show the list of countries and details about them.

Check out mvvm branch for the MVVM revision of the same app.

For the example of handling the authentication state in the app, you can refer to my other tiny project that harnesses the locks and keys principle for solving this problem.

platforms codecov codebeat badge

Diagram

Key features

  • End of 2024 update: the project was fully revamped to use modern iOS stack technologies
  • Decoupled Presentation, Business Logic, and Data Access layers
  • Programmatic navigation. Push notifications with deep link
  • Redux-like centralized AppState as the single source of truth
  • Native SwiftUI dependency injection
  • Handling of the system events (such as didBecomeActive, willResignActive)
  • Full test coverage, including the UI (thanks to the ViewInspector)
  • Simple yet flexible networking layer built on async - await
  • UI - vanilla SwiftUI + Combine
  • Data persistence with SwiftData

Architecture overview

Diagram

Presentation Layer

SwiftUI views that contain no business logic and are a function of the state.

Side effects are triggered by the user's actions (such as a tap on a button) or view lifecycle event onAppear and are forwarded to the Interactors.

State and business logic layer (AppState + Interactors) are natively injected into the view hierarchy with @Environment.

Business Logic Layer

Business Logic Layer is represented by Interactors.

Interactors receive requests to perform work, such as obtaining data from an external source or making computations, but they never return data back directly.

Instead, they forward the result to the AppState or to a Binding. The latter is used when the result of work (the data) is used locally by one View and does not belong to the AppState.

Previously, this app did not use CoreData for persistence, and all loaded data were stored in the AppState.

With the persistence layer in place we have a choice - either to load the DB content onto the AppState, or serve the data from Interactors on an on-demand basis through Binding.

The first option suits best when you don't have a lot of data, for example, when you just store the last used login email in the UserDefaults. Then, the corresponding string value can just be loaded onto the AppState at launch and updated by the Interactor when the user changes the input.

The second option is better when you have massive amounts of data and introduce a fully-fledged database for storing it locally.

Data Access Layer

Data Access Layer is represented by Repositories.

Repositories provide asynchronous API (Publisher from Combine) for making CRUD operations on the backend or a local database. They don't contain business logic, neither do they mutate the AppState. Repositories are accessible and used only by the Interactors.


Twitter blog

Related repositories
dkhamsing/open-source-ios-apps

:iphone: Collaborative List of Open-Source iOS Apps

Creative Commons Zero v1.0 Universaliosswift
51.3k6k
jordanbaird/Ice

Powerful menu bar manager for macOS

SwiftGNU General Public License v3.0macosmenubar
icemenubar.app
29k821
NativeScript/NativeScript

⚡ Write Native with TypeScript ✨ Best of all worlds (TypeScript, Swift, Objective C, Kotlin, Java, Dart). Use what you love ❤️ Angular, React, Solid, Svelte, Vue with: iOS (UIKit, SwiftUI), Android (View, Jetpack Compose), Flutter and you name it compatible.

TypeScriptnpmMIT Licensenativescriptandroid
nativescript.org
25.6k1.7k
CodeEditApp/CodeEdit

📝 CodeEdit App for macOS – Elevate your code editing experience. Open source, free forever.

SwiftMIT Licensemacosapple
codeedit.app
23k1.2k
Whisky-App/Whisky

A modern Wine wrapper for macOS built with SwiftUI

SwiftGNU General Public License v3.0macosswiftui
getwhisky.app
15.2k602
pointfreeco/swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.

SwiftMIT Licensearchitecturecomposition
pointfree.co/collections/composable-architecture
14.8k1.7k
mrkai77/Loop

Window management made elegant.

SwiftGNU General Public License v3.0macosswift
11.2k254
gao-sun/eul

🖥️ macOS status monitoring app written in SwiftUI.

SwiftMIT Licensemacosmonitoring
9.9k373
stonerl/Thaw

Menu bar manager for macOS 26

SwiftGNU General Public License v3.0macosmacos-app
8.9k204
sindresorhus/Gifski

🌈 Convert videos to high-quality GIFs on your Mac

SwiftMIT Licensegifskigif
sindresorhus.com/gifski
8.5k320
XcodesOrg/XcodesApp

The easiest way to install and switch between multiple versions of Xcode - with a mouse click.

SwiftMIT Licensexcodeswift
xcodes.app
8.5k376
ronitsingh10/FineTune

FineTune, a macOS menu bar app for per-app volume control, multi-device output, audio routing, and 10-band EQ. Free and open-source alternative to SoundSource.

SwiftGNU General Public License v3.0audioaudio-utility
8.2k278