Model View Presenter Framework written in Swift.

Last update: Jul 15, 2022

Karumi logoBothamUI Build Status CocoaPods Compatible

BothamUI is MVP (Model-View-Presenter) framework written in Swift.

This project will help you setup all your presentation logic. BothamUI provides classes to represent the main components of this pattern like BothamViewController and BothamPresenter.

In addition we will use a wireframe navigation model and a service locator example[[5] [di]].

Screenshots

Screenshot1

Application UI/UX designs by Luis Herrero.

Data provided by Marvel. © 2015 MARVEL

Usage

This framework contains all the classes needed to implement your presentation logic following the MVP pattern. To use the view package, make your ViewController extend from Botham ViewController and specify in the storyboard wich class and Storyboard ID is linked to:

import BothamUI

class SampleViewController: BothamViewController {
    /*...*/
}

storyboardReference

Storyboard

BothamStoryboard provide a series of methods to help you instantiate view controllers by there storyboard ID. By default instantiateViewController() will search for view controller with the storyboard ID with the same name as the class.

import BothamUI

let mainStoryboard = BothamStoryboard(name: "Main")
let viewController: SampleViewController = mainStoryboard.instantiateViewController("SampleViewController")
let viewController: SampleViewController = mainStoryboard.instantiateViewController()

Presenter

To follow the MVP pattern, BothamUI also provides a BothamPresenter protocol that will be responsible for all your presentation logic. BothamUI will take care of linking your view (a BothamViewController) with your presenter and subscribing it to its lifecycle. In order to do that, create a class that implement BothamPresenter and link it to your view:

import BothamUI

class SamplePresenter: BothamPresenter {
    private weak var ui: SampleUI?

    init(ui: CharacterDetailUI) {
        self.ui = ui
    }
    
    func viewDidLoad() {
        /* ... */
    }
}
protocol SampleUI: BothamUI {
	/* ... */
}
class SampleViewController: BothamViewController, SampleUI {
    /*...*/
}

Dependency injection

BothamUI is built around the concept of dependency injection, all the dependencies are provided by constructor or properties, base on what UIKit allows us.

ViewController Instantiation

In the example a Service Locator is used in order to instantiate view controllers, but you can also use Swinject or others DI frameworks.

class ServiceLocator {

    static let sharedInstance = ServiceLocator()

    func provideSampleViewController() -> SampleViewController {
        let viewController: SampleViewController = provideMainStoryboard().viewController()
        viewController.presenter = SamplePresenter(ui: viewController)
        return viewController
    }
}

Lifecycle

Once both, view and presenter, are linked you can react to your view lifecycle directly from the presenter. You will be also able to call your view easily from the presenter:

class SamplePresenter: BothamPresenter {
    private weak var ui: SampleUI?

    init(ui: CharacterDetailUI) {
        self.ui = ui
    }
    
    func viewDidLoad() {
        self.ui?.showMessage("Welcome to Botham")
    }
}

To understand when the lifecycle methods are called take a look at the following table:

BothamPresenter UIViewController
viewDidLoad viewDidLoad
viewWillAppear viewWillAppear
viewDidAppear viewDidAppear
viewWillDisappear viewWillDisappear
viewDidDisappear viewDidDisappear

Caveats

  • ViewControllers instantiated view UIStoryboard, can't reference Generic Type.
  • Presenter and ViewController have a circular reference (like a ViewController and Datasource).

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 0.39.0+ is required to build BothamUI.

To integrate BothamUI into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'BothamUI', '~> 1.0'

Then, run the following command:

$ pod install

Do you want to contribute?

Feel free to report us or add any useful feature to the library, we will be glad to improve it with your help.

Keep in mind that your PRs must be validated by Travis-CI.

License

Copyright 2015 Karumi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

GitHub

https://github.com/Karumi/BothamUI
Comments
  • 1. Minor fixes

    Just adding syntax sugar to selectors and removing some redundant imports.

    Congrats for the project guys, it is a really helpful repo as a clean reference to explain and to understand some concepts.

    Reviewed by dcordero at 2016-05-12 17:08
  • 2. Added default value to BothamStoryboard instantiateViewController(viewControllerIdentifier:)

    The function instantiateViewController<T>() -> T can be removed and combined into instantiateViewController<T>(viewControllerIdentifier: String) by adding a default value.

    Reviewed by cgoldsby at 2016-07-27 22:21
  • 3. Rename sources directory and prepare code for the first release.

    This PR contains:

    • Sources directory renaming from BothamUI to Sources. Fix for #23.
    • Some fixes for typos I've found and variable renaming.
    • Example application logos for iPhone.
    • README.md updates to add a new screenshot. We should record the gif again with a real iphone.
    • BothamUI.podspec updates needed to publish the first version.

    This is the last PR before the Release 1.0.0!!!!! happy

    Reviewed by pedrovgs at 2015-12-23 14:53
  • 4. Add empty case view.

    This PR contains:

    • EmptyCaseView class and .xib file with the default implementation.
    • BothamEmptyCaseUI protocol with methods needed to show/hide the empty case view.
    • BothamEmptyCaseViewController protocol and extension.
    • ExampleViewController update to implement the new empty case functionality.
    • Acceptance tests needed to check if the feature is working or not using a MockComicsPresenter configured show the empty case because there are no comics to show.
    Reviewed by pedrovgs at 2015-12-13 15:08
  • 5. Presenter as objc object

    Because having presenter as generics inside ViewController is not an option. I propose an alternative solution, that instead of having property to force to be initialized. We dynamically check if the current ViewController has a property called Presenter that implement BothamPresenter. And call the corresponding methods.

    caveats:

    • We have explicitly define the presenter in each ViewController.
    • Presenter should implement NSObject(or at least be visible in @objc)
    Reviewed by davideme at 2016-04-06 10:31
  • 6. Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods 🤓 https://github.com/CocoaPods/shared_resources/tree/master/media

    Reviewed by ReadmeCritic at 2016-02-11 16:39
  • 7. Add support to indicate the bundle in which the Storyboards should be located

    In case of using BothamUI to create a framework, BothamStoryboard won't find the Storyboard because it is looking for it in the Main Bundle.

    Keeping NSBundle.mainBundle() as the default value to avoid a breaking release.

    Reviewed by dcordero at 2016-01-24 22:14
  • 8. ServiceLocator vs DI

    Hi guys,

    Can I ask you why you recommend using a Service Locator pattern instead of a DI framework like Typhoon?

    Many thanks!

    pd. Flag this as a question, I can't do it.

    Reviewed by PaNaVTEC at 2015-12-17 09:22
  • 9. Updated Nimble to v5.0.0 which supports Swift 3

    Nimble has tagged an official Swift 3 release and the swift-3.0 branch has been deleted (causing pods to fail installation).

    This PR updates the Podfile to use the latest and greatest Nimble. Pods now install & update without error.

    Reviewed by cgoldsby at 2016-10-01 11:09
  • 10. Force cocoapods 0.39 install

    Cocoapods 1.0.0 still has open issues like https://github.com/kif-framework/KIF/issues/848 so we need to force an installation with an outdated version

    Reviewed by Serchinastico at 2016-05-13 10:12
  • 11. Marvel UI example series detail

    This PR is the last one needed to fix #10. Here you are going to find:

    • A new UIViewController implementation named SeriesDetailViewController with a UICollectionView inside configured to show the UI following the design requirements.
    • New fake data associated to the new SeriesDetailViewController.

    Here you have a screenshot of the new feature I've added:

    screen shot 2015-12-22 at 20 15 15
    Reviewed by pedrovgs at 2015-12-22 19:26
  • 12. Migrate to Swift 4.

    Xcode did almost all the changes, just some dependencies had to be updated by hand in order to use their last commits in order to be able to build this.

    This should fix the issue #59

    Reviewed by untalfranfernandez at 2017-09-13 09:10
  • 13. Swift 4: Argument #selector is not exposed to Objective-C

    If you try to use BothamUI with Swift 4, you'll get that error in BothamPullToRefreshHandler.swift file.

    The following text explains what's going on:

    The whole reason for this warning in the first place is the result of SE-0160. Prior to Swift 4, internal or higher Objective-C compatible members of NSObject inheriting classes were inferred to be @objc and therefore exposed to Objective-C, therefore allowing them to be called using selectors (as the Obj-C runtime is required in order to lookup the method implementation for a given selector). However in Swift 4, this is no longer the case. Only very specific declarations are now inferred to be @objc, for example overrides of @objc methods, implementations of @objc protocol requirements and declarations with attributes that imply @objc, such as @IBOutlet. The motivation behind this, as detailed in the above linked proposal, is firstly to prevent method overloads in NSObject inheriting classes from colliding with each other due to having identical selectors. Secondly, it helps reduce the binary size by not having to generate thunks for members that don't need to be exposed to Obj-C, and thirdly improves the speed of dynamic linking.

    Reviewed by untalfranfernandez at 2017-09-13 07:59
  • 14. Multiple inheritance demand

    When I need to introduce other third-party libraries need to inherit to the parent class but BothamUI requirements ExampleViewController, how should solve?

    Reviewed by JqyModi at 2017-08-21 08:05
  • 15. Add optional table view data source methods

    Problem If you customize a BothamTableViewDataSource theres no optional methods from UITableViewDataSource like canEditRow:indexPath. So subclassing it don't give you access to them:

    var dataSource: BothamTableViewDataSource<ModelType, CellType>!
    dataSource = CustomBothamTableViewDataSource()
    class CustomBothamTableViewDataSource: BothamTableViewDataSource<ModelType, CellType> {
        //You can override this
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> { }
    
       //But not this
       func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { }
    }
    

    Cause BothamTableViewDatasource just implements the required methods in the protocol. So i added the default behaviour.

    Reviewed by ivanruizscm at 2016-10-27 13:11
  • 16. Improve the public API of instantiateViewController

    • public func instantiateViewController(viewControllerIdentifier: String = String(T.self)) -> T {

    See https://github.com/Karumi/BothamUI/pull/47#issuecomment-235829568 in PR https://github.com/Karumi/BothamUI/pull/47

    Reviewed by davideme at 2016-07-28 08:35
Related tags
Mvvm - Collection View Notes With Swift
Mvvm - Collection View Notes With Swift

CollectionViewNotes Haciendo apuntes para cuando pierda la memoria Comenzando ??

Jan 13, 2022
Movies app written in Swift 5 using the Custom API created on the Mocky website
Movies app written in Swift 5 using the Custom API created on the Mocky website

Movie App shows you collections of TV streaming and other movies. Movie app writ

Jun 26, 2022
Mini-application iOS native avec Xcode et Swift exploitant l'architecture MVVM et le framework Combine d'Apple pour la mise en place de la programmation réactive fonctionnelle, le tout avec UIKit.
Mini-application iOS native avec Xcode et Swift exploitant l'architecture MVVM et le framework Combine d'Apple pour la mise en place de la programmation réactive fonctionnelle, le tout avec UIKit.

iOS (Swift 5): Test MVVM avec Combine et UIKit L'architecture MVVM et la programmation réactive fonctionnelle sont très utlisées dans le développement

Nov 28, 2021
iOS native app demo with Xcode and Swift using MVVM architecture and Apple's Combine framework for functional reactive programming, all with UIKit
iOS native app demo with Xcode and Swift using MVVM architecture and Apple's Combine framework for functional reactive programming, all with UIKit

iOS (Swift 5): MVVM test with Combine and UIKit MVVM and functional reactive programming (FRP) are very used in iOS development inside companies. Here

Dec 31, 2021
SignalKit is a reactive Swift framework with focus on clean and readable API.
SignalKit is a reactive Swift framework with focus on clean and readable API.

Abstract SignalKit is a lightweight event and binding framework. The core of SignalKit is the Observable protocol. Each implementation of the Observab

Jul 14, 2022
A small app that uses the private FlightUtilities.framework to show information about any flight.
A small app that uses the private FlightUtilities.framework to show information about any flight.

FlightUtilities A small app that uses the private FlightUtilities.framework to show information about any flight given the airline code, flight code a

Mar 26, 2022
Lightweight Framework for using Core Data with Value Types
Lightweight Framework for using Core Data with Value Types

Features Uses Swift Reflection to convert value types to NSManagedObjects iOS and Mac OS X support Use with structs Works fine with let and var based

Aug 1, 2022
A framework to create proxies for XCBBuildService, which allows for custom Xcode build integrations.

XCBBuildServiceProxyKit XCBBuildServiceProxyKit is a framework that enables you to write a proxy for Xcode's XCBBuildService, which enables you to ext

Jul 14, 2022
SwiftLint - A tool to enforce Swift style and conventions, loosely based on Swift Style Guide.
SwiftLint - A tool to enforce Swift style and conventions, loosely based on Swift Style Guide.

SwiftLint - A tool to enforce Swift style and conventions, loosely based on Swift Style Guide.

Aug 6, 2022
Sweet-swift - Make Swift Sweet by Gracefully Introducing Syntactic Sugar, Helper Functions and Common Utilities

Sweet Swift Make Swift Sweet by Gracefully Introducing Syntactic Sugar, Helper F

Feb 6, 2022
Airbnb's Swift Style Guide.

Airbnb Swift Style Guide Goals Following this style guide should: Make it easier to read and begin understanding unfamiliar code. Make code easier to

Aug 11, 2022
LinkedIn's Official Swift Style Guide

Swift Style Guide Make sure to read Apple's API Design Guidelines. Specifics from these guidelines + additional remarks are mentioned below. This guid

Aug 5, 2022
The Official raywenderlich.com Swift Style Guide.
 The Official raywenderlich.com Swift Style Guide.

The Official raywenderlich.com Swift Style Guide. Updated for Swift 5 This style guide is different from others you may see, because the focus is cent

Aug 3, 2022
A self-taught project to learn Swift.
A self-taught project to learn Swift.

30 Days of Swift Hi Community I am Allen Wang, a product designer and currently learning Swift. This project was totally inspired by Sam Lu's 100 Days

Aug 1, 2022
Explanations and samples about the Swift programming language
Explanations and samples about the Swift programming language

About Swift Contents Explanations and samples about: Swift Programming Language Swift Standard Library Target audience Developers familiar with object

May 10, 2022
A collection useful tips for the Swift language
A collection useful tips for the Swift language

SwiftTips The following is a collection of tips I find to be useful when working with the Swift language. More content is available on my Twitter acco

Jul 14, 2022
Swift Featured Projects in brain Mapping
Swift Featured Projects in brain Mapping

Swift 开源精选   自 2014年 WWDC 发布 Swift 语言以来,本项目 一直致力于将主流 Swift 中文学习、开发资源汇集于此,并且尽力紧密地跟踪、甄选优秀 Swift 开源项目,以方便开发者快速获得并使用。考虑 Swift 已经正式发布超过四年半(更无力管理维护海量的 Swift

Aug 5, 2022
A collection of Swift tips & tricks that I've shared on Twitter

⚠️ This list is no longer being updated. For my latest Swift tips, checkout the "Tips" section on Swift by Sundell. Swift tips & tricks ⚡️ One of the

Aug 4, 2022
An Xcode playground showcasing the new features in Swift 4.0.
An Xcode playground showcasing the new features in Swift 4.0.

What’s new in Swift 4 An Xcode playground showcasing the new features in Swift 4.0. As seen in the What’s New in Swift session at WWDC 2017. Written b

Aug 7, 2022