:droplet: A generic view model for both basic and complex scenarios

Overview

Brick

CI Status Version Carthage Compatible License Platform Documentation Swift

Description

Brick Icon

Brick is a generic view model for both basic and complex scenarios. Mapping a basic table view cells is as easy as pie, if you have more properties, you can use the meta dictionary to add all additional properties that you might need. It also supports relations so that you can nest view models inside of view models.

public struct Item: Mappable {
  public var index = 0
  public var title = ""
  public var subtitle = ""
  public var text = ""
  public var image = ""
  public var kind = ""
  public var action: String?
  public var size = CGSize(width: 0, height: 0)
  public var meta = [String : AnyObject]()
}
  • .index Calculated value to determine the index it has inside of the component.
  • .title The headline for your data, in a UITableViewCell it is normally used for textLabel.text but you are free to use it as you like.
  • .subtitle Same as for the title, in a UITableViewCell it is normally used for detailTextLabel.text.
  • .text This is an optional property that can be used for larger amount of text needed to describe your Item
  • .image Can be either a URL string or a local string, you can easily determine if it should use a local or remote asset in your view.
  • .kind Is used for the reuseIdentifier of your UITableViewCell or UICollectionViewCell.
  • .action Action identifier for you to parse and process when a user taps on a list item. We recommend Compass as centralized navigation system.
  • .size Can either inherit from the UITableViewCell/UICollectionViewCell, or be manually set by the height calculations inside of your view.
  • .meta This is used for extra data that you might need access to inside of your view, it can be a hex color, a unique identifer or additional images for your view.

Usage

let item = Item(
  title: "John Hyperseed",
  subtitle: "Build machine",
  meta: [
    "operatingSystem" : "OS X",
    "xcodeVersion" : 7.3
])

print(item.meta("operatingSystem", "")) // prints "OS X"
print(item.meta("xcodeVersion", 0.0)) // prints 7.3

Installation

Brick is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Brick'

Brick is also available through Carthage. To install just write into your Cartfile:

github "hyperoslo/Brick"

Author

Hyper Interaktiv AS, [email protected]

Contributing

We would love you to contribute to Brick, check the CONTRIBUTING file for more info.

License

Brick is available under the MIT license. See the LICENSE file for more info.

Comments
  • Add types to Item

    Add types to Item

    This is a super minor PR. It just adds the types to all properties on Item to help the complier along. Even if this is super tiny and shouldn’t really affect anything, I thought it would be a good idea to just do it, one could consider it “best practice” to always declare properties with a type.

    I am one of those who think that is a good idea :)

    opened by zenangst 5
  • Feature: mappable meta

    Feature: mappable meta

    @zenangst Got a new idea regarding our discussions about meta. So instead of using enums or magic generators we can use Mappable structs 😄

    struct ProductMeta {
      var id = 0
      var name: String?
    }
    
    extension ProductMeta: Mappable {
      init(_ map: JSONDictionary) {
        id <- map.property("id")
        name <- map.property("name")
      }
    }
    
    let productMeta = ProductMeta(id: 11, name: "Name")
    let viewModel = ViewModel(title: "Title", meta: productMeta)
    // ...
    let meta: ProductMeta = viewModel.metaInstance()
    print(meta.id) // => 11
    

    My only concern is than it might be too much code, because you have to implement mapping, don't see any way to avoid this, even though property key always reflects the property name. But with this approach you write string only once which is nice.

    opened by vadymmarkov 4
  • Remove Fakery

    Remove Fakery

    This PR removes Fakery from the tests and as a dependency. I couldn't get it working for the tests on tvOS so I decide on removing it. We can always introduce it later on but right now it is needed for migration of other projects.

    opened by zenangst 2
  • Meta properties generator

    Meta properties generator

    Would be nice to implement a script that goes through your models and generates a code for the each property on it:

    import Brick
    
    extension ViewModel {
    
      var metaId: Int? {
        set(value) {
          meta["id"] = value
        }
        get {
          return meta("id", type: Int.self)
        }
      }
    
      func metaId(value: Int) -> Int {
        return meta("id", value)
      }
    
      ///...
    }
    

    Then instead of "magic strings" you would use:

    viewModel.id = 11
    viewModel.id(0) // => value to 0 by default
    viewModel.id // optional value
    
    opened by vadymmarkov 2
  • Improve/handling children

    Improve/handling children

    This PR improves working with the children property on Item.

    It is now included when comparing Item's using ===.

    The property is now also available in the initializer.

    When working with custom structs, you can implement the new DictionaryConvertable to make it easier to add them as children to an Item.

    opened by zenangst 1
  • Check if there are any children when before creating the key

    Check if there are any children when before creating the key

    This PR cleans up the dictionary representation of Item. The thing that it fixes is that it checks if there are any children before adding it to the dictionary. This will help clean up your JSON file as it won’t add empty keys for children.

    opened by zenangst 1
  • Feature text property

    Feature text property

    We have had quiet a few cases where we have to fallback to use meta when showing longer pieces of text when displaying an Item.

    By adding text has a property, you get one more field that you can rely on and it fits quiet a lot of cases.

    It won’t solve everything that in my opinion I think it’s an okay thing to have on the Item.

    opened by zenangst 1
  • Rename all references from ViewModel to Item

    Rename all references from ViewModel to Item

    This is a breaking change, it renames the ViewModel struct to Item and ViewConfigurable to ItemConfigurable.

    It should be very easy to migrate to the new version as you only need to search and replace those changes and should be done in a jiffy.

    This PR came to be after a discussion that me and @vadymmarkov had about naming. ViewModel kind of implies that it holds content for the entire screen as it would potentially deal with ViewController.

    And the second reason is that it is mainly used in Spots where we always refer to the ViewModel as an Item.

    Item is abstract to work on multiple instances, where as ViewModel implies a specific scenario where it will be used. You can always argue that it is in fact styling a specific view as ViewConfigurable is a property on the underlaying component, a cell for example.

    Personally I think this is the best name for what it does. I hope you do as well.

    opened by zenangst 1
  • Exclude children in equitable operators

    Exclude children in equitable operators

    This PR removes the children property from the comparison as it doesn’t work like intended because it is an array of AnyObject. If you want to include it you can make an extension in your implementation.

    opened by zenangst 1
  • Fix/mapping size

    Fix/mapping size

    We had a minor bug in the init method that maps size to Item. This was caused by type-casting that worked in earlier versions of Swift but it no longer supported.

    What we do to fix this issue is to resolve the value as a Double and the create a CGSize using both width and height from the JSON payload.

    Improved the tests to take size into account when testing the Item.

    opened by zenangst 0
  • Feature/identifier

    Feature/identifier

    This PR adds a new property to the ViewModel. The new property is called Identifier and is of type Int. This can be used to achieve uniqueness in your view models. It can represent an ID that you get from your backend or it can be a computed String that you assign yourself.

    When assigning String, you can do so by passing the hash value for that string.

    let item = ViewModel(identifier: "user-34".hashValue)
    
    opened by zenangst 0
Releases(2.0.5)
Owner
HyperRedink
Connected creativity
HyperRedink
Examples of commonly used data structures and algorithms in Swift.

Swift Structures This project provides a framework for commonly used data structures and algorithms written in a new iOS development language called S

Wayne Bishop 2.1k Dec 28, 2022
Swift μ-framework for efficient array diffs and datasource adapters.

Buffer Swift μ-framework for efficient array diffs, collection observation and data source implementation. C++11 port here Installation cd {PROJECT_RO

Alex Usbergo 348 Aug 2, 2022
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

raywenderlich 27.3k Jan 8, 2023
Swift library to generate differences and patches between collections.

Differ Differ generates the differences between Collection instances (this includes Strings!). It uses a fast algorithm (O((N+M)*D)) to do this. Featu

Tony Arnold 628 Dec 29, 2022
A Swift probability and statistics library

Probably Probably is a set of Swift structures for computing the probability and cumulative distributions of different probablistic functions. Right n

Harlan Haskins 270 Dec 2, 2022
KeyPathKit is a library that provides the standard functions to manipulate data along with a call-syntax that relies on typed keypaths to make the call sites as short and clean as possible.

KeyPathKit Context Swift 4 has introduced a new type called KeyPath, with allows to access the properties of an object with a very nice syntax. For in

Vincent Pradeilles 406 Dec 25, 2022
Differific - a fast and convenient diffing framework.

Differific Description Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm. Creating a chan

Christoffer Winterkvist 127 Jun 3, 2022
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

A fast and flexible O(n) difference algorithm framework for Swift collection. The algorithm is optimized based on the Paul Heckel's algorithm. Made wi

Ryo Aoyama 3.3k Jan 4, 2023
Extension of Diffable API which allow not duplicate code and use less models. Included example for SideBar.

SPDiffable Apple's diffable API requerid models for each object type. If you want use it in many place, you pass many time to implemenet and get over

Ivan Vorobei 114 Jan 3, 2023
Swivl - A set of BLAS-accelerated linerar algebra structures and functions

Swivl - Swift Vector Library A set of BLAS-accelerated linerar algebra structure

null 0 Jan 19, 2022
A fast, convenient and nonintrusive conversion framework between JSON and model. Your model class doesn't need to extend any base class. You don't need to modify any model file.

MJExtension A fast, convenient and nonintrusive conversion framework between JSON and model. 转换速度快、使用简单方便的字典转模型框架 ?? ✍??Release Notes: more details Co

M了个J 8.5k Jan 3, 2023
Droplet is a very small SwiftUI application that runs in the menubar and allows a file to be dropped onto the popover.

A Mac OS menubar application that allows drag-and-drop file uploading to an S3 bucket with a presigned URL copied to the clipboard. Work in progress.

Josh McArthur 11 Sep 8, 2022
A basic iOS app that takes input from the user, displays it, allows changing both text color and background color.

Hello-iOSApp App Description A basic iOS app that takes input from the user, displays it, allows changing both text color and background color. App Wa

null 0 Jan 8, 2022
Generic model framework

Pistachio Pistachio is a generic model framework. By leveraging lenses and value transformers, it allows you to create type safe adapters for any recu

Felix Visée 164 Jun 29, 2022
Full configurable spreadsheet view user interfaces for iOS applications. With this framework, you can easily create complex layouts like schedule, gantt chart or timetable as if you are using Excel.

kishikawakatsumi/SpreadsheetView has moved! It is being actively maintained at bannzai/SpreadsheetView. This fork was created when the project was mov

Kishikawa Katsumi 34 Sep 26, 2022
Declaretive UICollectionViewCompositionalLayout interface to implement complex collection view layout.

CompositionalLayoutViewController Example To run the example project, clone the repo, and run pod install from the Example directory first. Requiremen

ONEinc 19 Dec 2, 2022
Full configurable spreadsheet view user interfaces for iOS applications. With this framework, you can easily create complex layouts like schedule, gantt chart or timetable as if you are using Excel.

kishikawakatsumi/SpreadsheetView has moved! It is being actively maintained at bannzai/SpreadsheetView. This fork was created when the project was mov

Kishikawa Katsumi 34 Sep 26, 2022
VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift)VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift)

VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift) VidyoPlatform reference application highlighting how to integrate video chat into a na

Taras Melko 0 Nov 19, 2021
CoreMLSample - CoreML Example for in app model and download model

CoreMLSample Sample for CoreML This project is CoreML Example for in app model a

Kim Seonghun 2 Aug 31, 2022
Compose is a library that helps you compose complex and dynamic views.

Compose is a data driven library that will help compose your complex and dynamic Views. It helps you create complex and dynamic Views using easy and s

OLX Brasil 123 Jun 9, 2021