• Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ—‚ Expandable, hierarchical, flexible, declarative UICollectionView with diffable data sources & SwiftUI-like tree items builder [Swift 5.1, iOS & iPadOS 13].

expandable-collection-view-kit Awesome

Build Platform Platform Language SPM License

Last Update: 18/July/2021.

If you like the project, please give it a star โญ It will show the creator your appreciation and help others to discover the repo.

โœ๏ธ About

๐Ÿ—‚ Expandable, hierarchical, flexible, declarative UICollectionView with diffable data sources & SwiftUI-like tree items builder [Swift 5.1, iOS & iPadOS 13].

๐Ÿ“บ Demo

Please wait while the .gif files are loading...

๐Ÿฑ Features

  • Ease of use
    • Instantiate ExpandableCollectionViewManager class, provide its parent view controller and specify the Folders and Items.
  • Flexible API
    • There are many customization points that can be used to adjust: Folders, Items, Navigation, appeareances and also custom animations.
    • Swift's Function Builders make it very easy to create a tree of interconnected items
  • Autolayout
    • You don't need to do anything related to autolayout - the component properly handles all the changes. The only thing you need to do is to instantiate the ExpandableCollectionViewManager and fill it in with items.
  • Performant
    • The under-the-hood implementation uses Swift's Diffable Data Sources and Collection View Compositional Layout which provides high performance and tested backing APi.
  • Extendable
    • The component can be further extended with new types of items, Function Builder types (such as For Each that applies a group of modifiers to a set of items, rather than copy-pasting the same modifiers or managing boilerplate configuration), decoratable content views for items and the list goes on.

๐Ÿ“š Usage

Instantiation

Basic

Create a property that will hold the ExpandableCollectionViewManager reference and the instantiate it via one of the designated initializers:

let expVCManager = ExpandableCollectionViewManager(parentViewController: self)

Here self is the reference to the UIViewController where the property is used.

Pre-filling

To pre-fill the collection view, you may use the trailing closure syntax for one of the designated initializers:

let expVCManager = ExpandableCollectionViewManager(parentViewController: self) {
    Folder(title: "ToDo") {
        Item(title: "Compose Email")
            .setImage(systemName: "paperplane")
        Item(title: "Watch Netflix")
    }

    Item(title: "Buy new iPhone")
    Item(title: "Cleanup")
}

Configuration

Adding More Items

You can add more items by using appendItems method:

expVCManager.appendItems {
    Folder(title: "Marvel") {
        Item(title: "Spider-Man")
        Item(title: "Iron Man")
        Item(title: "Black Widow")
    }
    Folder(title: "DC") {
        Item(title: "Batman")
        Item(title: "Batgirl")
        Item(title: "Nightwing")
    }
}

Item Modifiers

There are a number of item modifiers that can be applied to them to change their behaviour or appearance:

expVCManager.appendItems {
    Folder(title: "Marvel") {
        Item(title: "Spider-Man")
            .setImage(systemName: "tray.full.fill") // 1
            
        Item(title: "Iron Man")
        Item(title: "Black Widow")
    }
    .setTintColor(.systemOrange) // 2
    .isExpanded(true) // 3
    .isItemsCountVisible(true) // 4

    Folder(title: "DC") {
        Item(title: "Batman")
        Item(title: "Batgirl")
        Item(title: "Nightwing")
    }
    .isChevronVisible(false) // 5
}

Let's break down the API's line by line:

  1. Changes the default image icon for either Item and/or Folder
  2. Changes tint color of a Folder's icon
  3. Determines whether a Folder is automatically expanded
  4. Displays a Folder's item counter label that
  5. Changes the visibility of chevron icon

Actions

Actions allow to specify a closure that will be executed on item tap action:

Item(title: "Spider-Man")
    .setImage(systemName: "spider.fill")
    .setAction { [weak self] indexPath, title in
        self?.showWarning("Item \(title) that is at \(indexPath) index path has been tapped.")
    }

Actions have two input parameters an item's index path and title. That should make the API more or less flexible for action handling use cases.

Animator

You can specify cells' unfold animation. It's expressed as enum type, where the default is .simple, which is the default diffing animation (hide/show). However, there is a an another option that you to use one of the pre-built animations or implement and inject your own.

expVCManager.unfoldAnimation = .custom(AnimationFactory.moveDown(duration: 0.415)) // 1
expVCManager.unfoldAnimation = .custom(AnimationFactory.verticalUnfold(duration: 0.435)) // 2

Let's break the sample code down. The AnimationFactory is a built-in type that has a number of pre-implemented animations.

  1. The moveDown animation simply moves each unfolded item to the direction of unfolding with the specified duration.
  2. The verticalUnfold animation is a bit more complicated: it applies a series of affine transformations to each unfolded cell.

To define an inject your own animations, you need to use the following typealias that defines the required closure's signature for the .custom unfold animation type:

let delayFactor = 0.015

let slideOutAnimation: ExpandableItem.Animation = { cell, indexPath, collectionView, completion in
    UIView.animate(
        withDuration: duration,
        delay: delayFactor * Double(indexPath.row),
        options: [.curveEaseInOut],
        animations: {
         cell.transform = CGAffineTransform(translationX: collectionView.bounds.width, y: 0)
         
    }, completion: completion)
}

expVCManager.unfoldAnimation = .custom(slideOutAnimation)

Here we used the ExpandalbeItem.Animation typealias to get the right closureโ€™s signature and then we implemented our custom cell animation. The final step is that we injected it via the .custom(slideOutAnimation) type.

Transition Handling

The component also supports custom handler for view controller transitioning. It can be specified via the following property:

expVCManager.onCellTapHandler = { _, destinationViewController in
    // 1. Prepare presentation part of the destination view controller
    let navController = UINavigationController(rootViewController: destinationViewController)
    // 2. Present the view controller
    self.present(navController, animated: true)
}

That makes different kinds of view controller navigation logic to work on a per-item basis. Which means for one view controller bound item we can use UINavigationController to present its view controller, wheres for some other item we can use modal view controller presentation style.

View Controller Configuration

There is yet an another modifier for Item type that allows to specify a UIViewController type:

Item(title: "Spider-Man")
    .setImage(systemName: "spider.fill")
    .setViewControllerType(IssuesViewController.self) { [weak self] viewController in
    viewController.comics = self?.endpoint(\.latest.spiderman.comics)
    }

Here we specified the target view controller type that needs to be transitioned when the Spider-Man cell is tapped and its configuration code, where we, for example can inject data, perform any other operation or simply omit.

๐Ÿ— Installation

Swift Package Manager

Xcode 11+

  1. Open MenuBar โ†’ File โ†’ Swift Packages โ†’ Add Package Dependency...
  2. Paste the package repository url https://github.com/jVirus/expandable-collection-view-kit and hit Next.
  3. Select the installment rules.

After specifying which version do you want to install, the package will be downloaded and attached to your project.

Package.swift

If you already have a Package.swift or you are building your own package simply add a new dependency:

dependencies: [
    .package(url: "https://github.com/jVirus/expandable-collection-view-kit", from: "1.0.0")
]

Manual

You can always use copy-paste the sources method ๐Ÿ˜„. Or you can compile the framework and include it with your project.

๐Ÿ™‹โ€โ™€๏ธ๐Ÿ™‹โ€โ™‚๏ธ Contributing

Your contributions are always appreciated. There are many ways how you help with the project:

  • You can suggest something
  • You can write additional documentation or sample codes
  • Implement a new feature
  • Fix a bug
  • Help to maintain by answering to the questions (if any) that other folks have
  • etc.

Overall guidelies are:

  • Please, discuss a feature or a major source change/addition before spending time and creating a pool requested via issues.
  • Create a separate branch and make sure that your code compiles and does not produce errors and warnings.
  • Please, don't be upset if it takes a while to review your code or receive an answer.

๐Ÿ‘จโ€๐Ÿ’ป Author

Astemir Eleev

๐Ÿ”– Licence

The project is available under MIT Licence

More Repositories

1

ios-learning-materials

๐Ÿ“š Curated list of articles, tutorials and repos that may help you dig a little bit deeper into iOS [and Apple Platforms].
Swift
1,976
star
2

uicollectionview-layouts-kit

๐Ÿ“Custom layouts for UICollectionView with examples [iOS 12+].
Swift
677
star
3

compositional-layouts-kit

๐Ÿ“ Advanced compositional layouts for UICollectionView [iOS 13+].
Swift
389
star
4

flappy-fly-bird

๐Ÿฆ Flappy Bird game reincarnation [GameplayKit, SpriteKit, iOS 12+].
Swift
331
star
5

swiftui-2048

๐ŸŽฒ Classic 2048 game [SwiftUI 3.0, iOS 15.0+, macOS 12.0+].
Swift
216
star
6

drawer-view

๐Ÿ“ค Custom UI component for iOS, replication of Apple's Apple Music player and Shortcutsโ€™ components view [Swift 5.0, iOS 12].
Swift
151
star
7

swift-design-patterns

๐Ÿš€ The ultimate collection of Software Design Patterns & Principles with examples [36/79 Patterns].
Swift
123
star
8

spritekit-water-node

๐ŸŒŠ Custom SpriteKit node that allows to simulate 2D water with respect to physics. The app demonstrates Flocking behaviour using GameplayKit, key-frame animation and custom fragment shader chaining (GLSL) ๐Ÿคฏ
Swift
93
star
9

ios-spritekit-shader-sandbox

๐Ÿ‘พ Collection of custom effects for SpriteKit implemented using GLSL/Metal shaders.
Swift
74
star
10

extensions-kit

๐Ÿ“ฆ Collection of Swift+Apple Frameworks extensions for speeding up software development [iOS & iPadOS].
Swift
71
star
11

tic-tac-toe

๐Ÿ•น iOS game - classic Tic Tac Toe with AI and state machines [Swift + SpriteKit + GameplayKit].
Swift
62
star
12

ios-scenekit-shadertoy

๐ŸŽฎ Easily add, changed and combine Vertex and Fragment shaders for SceneKit objects. Perfect tool for prototyping shaders in the context of SceneKit framework.
Swift
52
star
13

swift-algorithms-data-structs

๐Ÿ“’ Algorithms and Data Structures in Swift. The used approach attempts to fully utilize the Swift Standard Library and Protocol-Oriented paradigm.
Swift
49
star
14

soft-skills

๐ŸฆList of Soft Skills for software engineers/developers.
Markdown
39
star
15

grid-compositional-layout

๐Ÿงฒ Grid Compositional Layout is a SwiftUI 2.0 view that lays out the supplied views in a grid. Easily configurable and lazily retained [Swift 5.3, iOS 14.0+, iPadOS 14.0+, macOS 11.0(10.16)+]
Swift
39
star
16

skcomponents-kit

โš—๏ธ Collection of SpriteKit components for intermediate/advanced use cases.
Swift
36
star
17

sprite-liquids-ios-app

๐Ÿ’งDemonstrates a practical approach for liquid visualization using SpriteKit and CoreImage frameworks.
Objective-C
22
star
18

constraints-kit

๐Ÿ— Declarative, Chainable & Lightweight Auto Layout constraints framework for iOS.
Swift
21
star
19

hungry-wormy

๐Ÿ› iOS & macOS game - classic snake game [Swift + SpriteKit].
Swift
19
star
20

physics-vehicle

๐Ÿš— Physics-based 2D car behaviour and suspension demo [Swift, SpriteKit, iOS, macOS].
Swift
19
star
21

concurrency-kit

๐Ÿš„ Concurrency abstractions framework for Apple Platforms [Task, Atomic, Lock, Operation, etc.].
Swift
19
star
22

15-puzzle

๐Ÿงฉ iOS game - classic 15 Puzzle game [Swift + SpriteKit].
Swift
17
star
23

column-text-view-ui

๐Ÿ“„ Column Text View is an adaptive UI component that renders text in columns, horizontally [iOS 12, UIKit, TextKit, SwiftUI].
Swift
11
star
24

voice-recognition-ios-demo

๐ŸŽค Small app for voice recognition feature introduced in iOS 10.0 SDK.
Swift
9
star
25

metal-path-ios-app

๐Ÿค– Sandbox that holds various Metal API related scenes, from very fundamental to advanced developments
Swift
9
star
26

swift-functional

๐Ÿง  Articles and notes about functional programming in Swift.
Swift
7
star
27

message-view-ui

โœ‰๏ธ Easy to use HUD component for iOS [activity report, success or warning messages, etc.]
Swift
6
star
28

safari-tabs-extracter

๐Ÿ“— A script that extracts all tabs (as URLs) from Safari and saves them in a Markdown file [AppleScript]
AppleScript
6
star
29

jToolkit

โ˜•๏ธ Collection of various utilities, classes and shaders for Java + OpenGL + GLSL development.
Java
5
star
30

MTLTextureView

๐Ÿ–ผ๏ธ Render images as Metal textures.
Swift
4
star
31

device-kit

๐Ÿ“ฑGet extended information about an iOS/iPadOS device.
Swift
4
star
32

eleev.github.io

๐Ÿ’ผ Personal web page.
HTML
3
star
33

metal-particle-system-app

Particle system written on Metal shading language and Metal framework. ๐ŸŽŠ๐ŸŽ‰
Swift
2
star
34

jVirus

1
star