<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Siddharth Kothari on Medium]]></title>
        <description><![CDATA[Stories by Siddharth Kothari on Medium]]></description>
        <link>https://medium.com/@siddharthkothari007?source=rss-f9c8f2046cea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XlTY0p3NSJb4kHwutkXYkg.png</url>
            <title>Stories by Siddharth Kothari on Medium</title>
            <link>https://medium.com/@siddharthkothari007?source=rss-f9c8f2046cea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:53:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@siddharthkothari007/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding VIPER architecture in Swift]]></title>
            <link>https://medium.com/@siddharthkothari007/understanding-viper-architecture-in-swift-26d1fd9771f4?source=rss-f9c8f2046cea------2</link>
            <guid isPermaLink="false">https://medium.com/p/26d1fd9771f4</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[architecture-design]]></category>
            <category><![CDATA[architecture-pattern]]></category>
            <dc:creator><![CDATA[Siddharth Kothari]]></dc:creator>
            <pubDate>Sat, 14 Dec 2024 17:09:09 GMT</pubDate>
            <atom:updated>2024-12-14T17:09:09.556Z</atom:updated>
            <content:encoded><![CDATA[<p>VIPER is a software architecture pattern designed to build scalable, maintainable, and testable iOS applications. VIPER aims to separate concerns, improve code organization, and facilitate collaboration among developers.</p><h4><strong>What is VIPER?</strong></h4><p>VIPER consists of five interconnected components:</p><blockquote><strong>View (V)</strong>: Responsible for rendering the UI.<br><strong>Interactor (I)</strong>: Handles business logic, interacts with InteractorOutput.<br><strong>Presenter (P)</strong>: Manages the View’s state, receives data from InteractorOutput.<br><strong>Entity (E)</strong>: Represents data models, encapsulates business logic.<br><strong>Router (R)</strong>: Handles navigation, manages app flow.</blockquote><h4>Benefits of VIPER</h4><p>VIPER offers numerous benefits:</p><p>1. <strong>Separation of Concerns (SoC)</strong>: Each component has a clear responsibility.<br>2.<strong> Easier Testing and Debugging</strong>: Components are isolated, making testing and debugging simpler.<br>3. <strong>Improved Code Organization</strong>: Code is structured and easy to navigate.<br>4. <strong>Scalability and Maintainability</strong>: VIPER enables easy addition of new features and components.<br>5. <strong>Clear Responsibilities</strong>: Each component has a well-defined role.</p><h3><strong>Example Use Case</strong></h3><p>Suppose we are building a to-do list app. The VIPER components would work as follows:</p><p>1. User taps “Add Task” button.<br>2. View notifies Presenter.<br>3. Presenter requests data from Interactor.<br>4. Interactor retrieves task data from Entity.<br>5. Interactor notifies Presenter of data retrieval.<br>6. Presenter updates View with new task.<br>7. Router handles navigation to task details screen.</p><h4><a href="https://github.com/siddharth-kothari-spk/SwiftBasics/tree/main/TodoItem_VIPER">Github: TodoItem_VIPER</a></h4><h4>Entity (TodoItem.swift)</h4><pre>import Foundation<br><br>protocol Entity {<br>    func saveItem(_ item: TodoItem, completion: @escaping (TodoItem) -&gt; Void)<br>}<br><br>class TodoItem: Entity {<br>    var id: Int<br>    var title: String<br>    var itemChecked: Bool<br><br>    init(id: Int, title: String, itemChecked: Bool = false) {<br>        self.id = id<br>        self.title = title<br>        self.itemChecked = itemChecked<br>    }<br><br>    func fetchData(completion: @escaping ([TodoItem]) -&gt; Void) {<br>    // Fetch data from storage or API<br>    let items = [<br>            TodoItem(id: 1, title: &quot;Buy vegetables&quot;),<br>            TodoItem(id: 2, title: &quot;Water the plants&quot;),<br>            TodoItem(id: 3, title: &quot;Laundry&quot;)<br>        ]<br>     completion(items)<br>    }<br>    <br>    func saveItem(_ item: TodoItem, completion: @escaping (TodoItem) -&gt; Void) {<br>        <br>    }<br>}</pre><h4>Interactor (TodoInteractor.swift)</h4><pre>import Foundation<br><br>protocol Interactor {}<br><br>protocol TodoInteractorOutput {<br>    func didFetchTodoItems(_ items: [TodoItem])<br>    func didAddTodoItem(_ item: TodoItem)<br>    func didToggleTodoItem(_ item: TodoItem)<br>}<br><br>import Foundation<br>class TodoInteractor: Interactor {<br>    var output: TodoInteractorOutput!<br>    var entity: TodoItem!<br><br>    func fetchTodoItems() {<br>        entity.fetchData { [weak self] items in<br>            self?.output.didFetchTodoItems(items)<br>        }<br>    }<br><br>    func addTodoItem(title: String) {<br>        let item = TodoItem(id: UUID().hashValue, title: title)<br>        entity.saveItem(item) { item in<br>            self.output.didAddTodoItem(item)<br>        }<br>    }<br><br>    func toggleTodoItem(item: TodoItem) {<br>        item.itemChecked = !item.itemChecked<br>        entity.saveItem(item) { item in<br>            self.output.didToggleTodoItem(item)<br>        }<br>    }<br>}</pre><h4>Presenter (TodoPresenter.swift)</h4><pre>import Foundation<br><br>protocol Presenter {}<br><br>class TodoPresenter: Presenter {<br>    weak var view: TodoViewController!<br>    var interactor: TodoInteractor!<br><br>    func updateView(with items: [TodoItem]) {<br>        view.todoItems = items<br>    }<br><br>    func addTodoItem(title: String) {<br>        interactor.addTodoItem(title: title)<br>    }<br><br>    func toggleTodoItem(at index: Int) {<br>        interactor.toggleTodoItem(item: view.todoItems[index])<br>    }<br>}</pre><h4>View (TodoViewController.swift)</h4><pre>import Foundation<br>import UIKit<br><br>class TodoViewController: UIViewController {<br>    @IBOutlet weak var tableView: UITableView!<br>    var presenter: TodoPresenter!<br><br>    var todoItems: [TodoItem] = []<br><br>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        presenter.view = self<br>        presenter.interactor = TodoInteractor()<br>    }<br><br>    @IBAction func addButtonTapped(_ sender: UIButton) {<br>        presenter.addTodoItem(title: &quot;New Todo Item&quot;)<br>    }<br>    <br>    func updateView(_ items: [TodoItem]) {<br>        // table view updated<br>        presenter.updateView(with: items)<br>    }<br>    <br>    func toggleTodoItemAt(_ index: Int) {<br>        // table view toggled<br>        presenter.toggleTodoItem(at: index)<br>    }<br>}</pre><h4>Router (TodoRouter.swift)</h4><pre>import Foundation<br><br>protocol Router {}<br><br>enum Route {}<br><br>import UIKit<br>    class TodoRouter: Router {<br>        func navigate(to route: Route) {<br>        // Handle navigation<br>        }<br>}</pre><h3>Testing VIPER Components</h3><h4>TodoInteractorTests.swift</h4><pre><br>import XCTest<br>@testable import TodoItem_VIPER<br><br>class TodoInteractorTests: XCTestCase {<br>    var interactor: TodoInteractor!<br>    var output: TodoInteractorOutputMock!<br>    <br>    override func setUp() {<br>        super.setUp()<br>        interactor = TodoInteractor()<br>        output = TodoInteractorOutputMock()<br>        interactor.output = output<br>    }<br>    <br>    func testFetchTodoItems() {<br>        interactor.fetchTodoItems()<br>        XCTAssertEqual(output.fetchTodoItemsCalled, true)<br>    }<br>    <br>    func testAddTodoItem() {<br>        interactor.addTodoItem(title: &quot;New Todo Item&quot;)<br>        XCTAssertEqual(output.addTodoItemCalled, true)<br>    }<br>    <br>    func testToggleTodoItem() {<br>        let item = TodoItem(id: 1, title: &quot;Buy vegetables&quot;)<br>        interactor.toggleTodoItem(item: item)<br>        XCTAssertEqual(output.toggleTodoItemCalled, true)<br>    }<br>}</pre><h4>TodoPresenterTests.swift</h4><pre><br>import Foundation<br>import XCTest<br>@testable import TodoItem_VIPER<br><br>class TodoPresenterTests: XCTestCase {<br>    var presenter: TodoPresenter!<br>    var view: TodoViewControllerMock!<br>    <br>    override func setUp() {<br>        super.setUp()<br>        presenter = TodoPresenter()<br>        view = TodoViewControllerMock()<br>        presenter.view = view<br>    }<br>    <br>    func testUpdateView() {<br>        let items = [TodoItem(id: 1, title: &quot;Buy milk&quot;)]<br>        presenter.updateView(with: items)<br>        XCTAssertEqual(view.updateViewCalled, true)<br>    }<br>    <br>    func testAddTodoItem() {<br>        presenter.addTodoItem(title: &quot;New Todo Item&quot;)<br>        XCTAssertEqual(view.addButtonTappedCalled, true)<br>    }<br>    <br>    func testToggleTodoItem() {<br>        let item = TodoItem(id: 1, title: &quot;Buy milk&quot;)<br>        presenter.toggleTodoItem(at: 0)<br>        XCTAssertEqual(view.toggleTodoItemCalled, true)<br>    }<br>}</pre><h4>Mock Classes</h4><h4>TodoInteractorOutputMock.swift</h4><pre>import Foundation<br>@testable import TodoItem_VIPER<br><br>class TodoInteractorOutputMock: TodoInteractorOutput {<br>    var fetchTodoItemsCalled = false<br>    var addTodoItemCalled = false<br>    var toggleTodoItemCalled = false<br>    <br>    func didFetchTodoItems(_ items: [TodoItem]) {<br>        fetchTodoItemsCalled = true<br>    }<br>    <br>    func didAddTodoItem(_ item: TodoItem) {<br>        addTodoItemCalled = true<br>    }<br>    <br>    func didToggleTodoItem(_ item: TodoItem) {<br>        toggleTodoItemCalled = true<br>    }<br>}</pre><h4>TodoViewControllerMock.swift</h4><pre>import Foundation<br>import UIKit<br>@testable import TodoItem_VIPER<br><br>class TodoViewControllerMock: TodoViewController {<br>    var addButtonTappedCalled = false<br>    var updateViewCalled = false<br>    var toggleTodoItemCalled = false<br>    <br>    override func addButtonTapped(_ sender: UIButton) {<br>        addButtonTappedCalled = true<br>    }<br>    <br>    override func updateView(_ items: [TodoItem]) {<br>        updateViewCalled = true<br>    }<br>    <br>    override func toggleTodoItemAt(_ index: Int) {<br>        toggleTodoItemCalled = true<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26d1fd9771f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding The Composable Architecture (TCA) in Swift]]></title>
            <link>https://medium.com/@siddharthkothari007/understanding-the-composable-architecture-tca-in-swift-cf683c9c3602?source=rss-f9c8f2046cea------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf683c9c3602</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[composable-architecture]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Siddharth Kothari]]></dc:creator>
            <pubDate>Sun, 24 Nov 2024 02:25:46 GMT</pubDate>
            <atom:updated>2024-11-26T02:35:05.326Z</atom:updated>
            <content:encoded><![CDATA[<p>TCA introduced by <a href="https://www.pointfree.co/">Point-Free</a> , is inspired by functional programming principles and aims to provide a structured and predictable way to build complex applications. It encourages the separation of concerns, making code more modular, easier to test, and maintainable.</p><h3><strong>Core Concepts:</strong></h3><ol><li><strong><em>State</em>:</strong></li></ol><ul><li>Represents the current state of your application.</li><li>It’s an immutable data structure that encapsulates all the information needed to render the UI and handle user interactions.</li></ul><p><strong><em>2</em>. <em>Action</em>:</strong></p><ul><li>Represents a user interaction or a system event that can change the state.</li><li>It’s a simple data structure that describes what happened.</li></ul><p><strong><em>3</em>. <em>Reducer</em>:</strong></p><ul><li>A pure function that takes the current state and an action as input and returns a new state.</li><li>It’s responsible for updating the state in response to actions.</li></ul><p><strong><em>4. Effect:</em></strong></p><ul><li>A side effect, like network requests, file I/O, or timers.</li><li>It’s often asynchronous and can trigger actions to update the state.</li></ul><p><strong><em>5. Environment</em>:</strong></p><ul><li>A dependency container that provides external services like network clients, databases, or other dependencies to the reducer and effects.</li></ul><pre>struct Feature {<br> let store: Store&lt;State, Action, Environment&gt;<br>}<br>struct State: Equatable {<br> // …<br>}<br>enum Action: Equatable {<br> // …<br>}<br>struct Environment {<br> // …<br>}<br>let reducer = Reducer&lt;State, Action, Environment&gt; { state, action, environment in<br> switch action {<br> case .someAction:<br> // Update state<br> return .none<br> case .someEffect:<br> // Trigger an effect<br> return .effect(environment.someEffect())<br> }<br>}</pre><h3><strong>How TCA Works:</strong></h3><ol><li><strong><em>Initialization</em>:</strong></li></ol><ul><li>Create a Store instance, providing an initial state, a reducer, and an environment.</li></ul><p><strong><em>2. State Updates</em>:</strong></p><ul><li>When a user interacts with the UI, an action is dispatched to the store.</li><li>The reducer processes the action and returns a new state.</li><li>The store updates its internal state and notifies the UI to re-render.</li></ul><p><strong><em>3. Effects</em>:</strong></p><ul><li>Effects are triggered by the reducer and can perform asynchronous operations.</li><li>Once completed, they can dispatch new actions to update the state.</li></ul><h3><strong>Benefits of TCA:</strong></h3><ul><li><strong>Testability:</strong> Pure functions and immutable data structures make testing easier.</li><li><strong>Readability:</strong> Declarative style and clear separation of concerns.</li><li><strong>Maintainability:</strong> Modular and reusable components.</li><li><strong>Predictability:</strong> State changes are deterministic and easier to reason about.</li></ul><h3><strong>Implementing Effects in TCA</strong></h3><p>Effects are a crucial part of TCA, allowing you to handle asynchronous operations like network requests, database interactions, or timers. They provide a clean way to separate side effects from the core state logic.</p><p><strong><em>Understanding Effects</em>:</strong></p><ol><li><strong>Triggering Effects:</strong></li></ol><ul><li>A reducer can trigger an effect by returning an Effect value.</li><li>This effect is a function that takes an EffectRunner and returns a Cancellable.</li><li>The EffectRunner is used to execute the effect and the Cancellable can be used to cancel it if needed.</li></ul><p><strong>2. Handling Effects:</strong></p><ul><li>The store handles the execution of effects.</li><li>When an effect is triggered, the store passes the EffectRunner to the effect function.</li><li>The effect function can then use the EffectRunner to perform its asynchronous operation.</li></ul><pre>enum Action: Equatable {<br>    case fetchData<br>    case fetchDataSuccess(Data)<br>    case fetchDataFailure(Error)<br>}<br><br>struct Environment {<br>    let networkClient: NetworkClient<br>}<br><br>let reducer = Reducer&lt;State, Action, Environment&gt; { state, action, environment in<br>    switch action {<br>    case .fetchData:<br>        return .effect(environment.networkClient.fetchData()<br>            .catchToEffect { error in .fetchDataFailure(error) }<br>            .map(Action.fetchDataSuccess))<br>    case .fetchDataSuccess(let data):<br>        // Update state with fetched data<br>        return .none<br>    case .fetchDataFailure(let error):<br>        // Handle error, e.g., show an error message<br>        return .none<br>    }<br>}</pre><p>In this example:</p><ol><li>When the fetchData action is dispatched, the reducer triggers a network request using the networkClient from the environment.</li><li>The network request is wrapped in an effect and passed to the store.</li><li>The store executes the effect and, upon success or failure, dispatches the appropriate action to update the state.</li></ol><p><strong><em>Key Points to Remember:</em></strong></p><ul><li><strong>Keep Effects Pure:</strong> Effects should only perform side effects and return actions to update the state.</li><li><strong>Handle Errors Gracefully:</strong> Use catchToEffect to handle errors and dispatch appropriate actions.</li><li><strong>Cancel Effects:</strong> Use Cancellable to cancel ongoing effects if necessary.</li><li><strong>Test Effects:</strong> Write unit tests for effects to ensure their correctness.</li></ul><h3><strong>Testing TCA</strong></h3><p>Testing is a crucial aspect of software development, and TCA’s functional and declarative nature makes it particularly well-suited for testing.</p><p><strong><em>Key Testing Strategies for TCA</em>:</strong></p><ol><li><strong>Unit Testing Reducers:</strong></li></ol><ul><li>Test reducers in isolation to ensure they produce the correct state given a specific action and initial state.</li></ul><pre>func testReducer() {<br>    let initialState = ...<br>    let action = ...<br>    let expectedState = ...<br><br>    let newState = reducer(initialState, action, Environment())<br><br>    XCTAssertEqual(newState, expectedState)<br>}</pre><p><strong>2. Testing Effects:</strong></p><ul><li>Test effects independently to verify their behavior.</li><li>Simulate the EffectRunner and assert that the effect triggers the correct actions.</li></ul><pre>func testEffect() {<br>    let expectation = XCTestExpectation()<br>    let effect = ...<br><br>    effect(EffectRunner { action in<br>        XCTAssertEqual(action, expectedAction)<br>        expectation.fulfill()<br>    })<br><br>    wait(for: [expectation], timeout: 1)<br>}</pre><p><strong>3. Integration Testing:</strong></p><ul><li>Test the entire feature, including reducers, effects, and UI components.</li><li>Use a testing framework like XCTest to write integration tests that simulate user interactions and verify the resulting state changes.</li></ul><h3><strong>Integrating TCA with SwiftUI</strong></h3><p>TCA can be seamlessly integrated with SwiftUI to build user interfaces. Here’s a basic approach:</p><ol><li><strong>Create a SwiftUI View:</strong></li></ol><ul><li>Define a SwiftUI view that observes the store’s state.</li><li>Use the state to render the UI.</li></ul><p><strong>2. Dispatch Actions:</strong></p><ul><li>Bind UI elements to actions and dispatch them when the user interacts with the UI.</li></ul><pre>struct MyView: View {<br>    let store: Store&lt;AppState, AppAction, AppEnvironment&gt;<br><br>    var body: some View {<br>        VStack {<br>            Text(store.state.count)<br>            Button(&quot;Increment&quot;) {<br>                store.send(.increment)<br>            }<br>        }<br>    }<br>}</pre><p>References:<br>1. <a href="https://github.com/pointfreeco/swift-composable-architecture">Pointfree github</a><br>2. <a href="https://pointfreeco.github.io/swift-composable-architecture/main/tutorials/meetcomposablearchitecture/">Composable Architecture VideoGuide</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf683c9c3602" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>