• Stars
    star
    136
  • Rank 259,740 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 2 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

A modest attempt to port UICollectionView to SwiftUI.

LazyCollectionView

SPM Compatible Platform License

A modest attempt to port UICollectionView to SwiftUI.

Sample

Table of Contents

Description

The goal of this component is to bring the flexibility of UIKit's UICollectionView to SwiftUI, especially for custom layouts. It is easily embeddable in a ScrollView and supports lazy loading its content like LazyVStack.

It is used in the Unsplash for iOS app to display a list of photos in a column layout. It also works on macOS but has not been thoroughly tested on the platform.

It is best suited to display a large number of items that would benefit from lazy loading in a ScrollView. Otherwise, the Layout API introduced in iOS 16 and macOS 13 is a better option.

Improvements and more sample layouts are welcome!

โš ๏ธ The list and grid layouts in the sample project are just examples and should not be used outside of it. You should instead use LazyVStack and LazyVGrid, respectively.

Requirements

  • iOS 14+
  • macOS 12+
  • Xcode 13.4+
  • Swift 5.6+

Installation

LazyCollectionView is available through Swift Package Manager. To install it, follow these steps:

  1. In Xcode, click File, then Swift Package Manager, then Add Package Dependency
  2. Choose your project
  3. Enter this URL in the search bar https://github.com/unsplash/swiftui-lazycollectionview.git

Usage

The first step is to create a layout object, which is a class that adopts the LazyCollectionLayout and ObservableObject protocols. This object is in charge of generating the frame for all the views in LazyCollectionView. The layouts in the sample project can be used as a reference.

Here is the typical way to use LazyCollectionView.

GeometryReader { geometryProxy in
    let parentFrame = geometryProxy.frame(in: .local)
	
    ScrollView {
        LazyCollectionView(data: myArray,
                           layout: myCustomLayout,
                           parentFrame: parentFrame) { item in
            ItemView(item)
        }
    }
}

For lazy-loading to work, it is important to set a value for the parentFrame property.

Note that unlike UICollectionView, LazyCollectionView does not add its subviews inside a scroll view. This makes it easy to mix it with other views (headers, footers and more) inside a parent scroll view.

Components

1. LazyCollectionView

A view that presents an ordered collection of data items using a customizable layout.

Parameters

  • data: The identified data that the LazyCollectionView instance uses to create views dynamically.
  • layout: The layout object that calculates the positions of the views.
  • parentFrame (Optional): The frame of the instance's parent view.
  • content: The view builder that creates views dynamically.

2. LazyCollectionLayout

A type that calculates how the items in a lazy collection view are laid out.

3. LazyCollectionLayoutAttributes

A layout object that holds the layout-related attributes for a given item in a LazyCollectionView. It is created by an instance of LazyCollectionViewLayout and used by LazyCollectionView to lay out its content.

Improvements

Here's a short list of potential improvements.

  • Add support to update the layout granularly (when adding/inserting a new element or deleting an element from the data). Recalculating the whole layout when adding new elements is not good.
  • If possible, find a way to get rid of the parentFrame parameter because it adds complexity to the usage.

License

MIT License

Copyright (c) 2022 Unsplash Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

react-trend

๐Ÿ“ˆ Simple, elegant spark lines
JavaScript
2,462
star
2

datasets

๐ŸŽ 4,800,000+ Unsplash images made available for research and machine learning
Jupyter Notebook
2,224
star
3

unsplash-js

๐Ÿค– Official JavaScript wrapper for the Unsplash API
TypeScript
2,027
star
4

unsplash-php

๐Ÿ‘ป Official PHP wrapper for the Unsplash API
PHP
405
star
5

unsplash-photopicker-ios

๐Ÿ“ฑAn iOS photo picker to search and download photos from Unsplash.
Swift
390
star
6

unsplash-photopicker-android

๐Ÿ“ฑAn Android photo picker to search and download photos from Unsplash.
Kotlin
327
star
7

unsplash_rb

๐Ÿ’Ž Ruby wrapper for the Unsplash API.
Ruby
224
star
8

unsplash-source-js

๐Ÿฎ A javascript wrapper for the Unsplash Source API
JavaScript
176
star
9

comment-on-pr

A GitHub Action to comment on the relevant open PR when a commit is pushed.
Ruby
146
star
10

react-progressive-enhancement

A handy collection of HOCs for universally renderedย apps
TypeScript
63
star
11

intlc

Compile ICU messages into code. Supports TypeScript and JSX. No runtime.
Haskell
51
star
12

uploader-prototype

An open-source prototype of the Unsplash uploader.
TypeScript
45
star
13

sum-types

Safe, ergonomic, non-generic sum types in TypeScript.
TypeScript
41
star
14

url-transformers

Small helper library for manipulating URL strings in Node and in the browser.
TypeScript
35
star
15

ts-imgix

Strongly-typed imgix URL builder function, `buildImgixUrl`.
TypeScript
32
star
16

ts-namespace-import-plugin

Auto import common namespaces in your modules
TypeScript
30
star
17

pipe-ts

TypeScript
30
star
18

request-frp

`request-frp` is a package that provides pure wrappers around `fetch` and `XMLHttpRequest`.
TypeScript
14
star
19

ts-redux-finite-state-machine-example

TypeScript
13
star
20

tinplate

โญ๏ธ TinEye API wrapper
Ruby
13
star
21

responsive-image-test

TypeScript
12
star
22

unsplash-imageview-ios

A UIImageView subclass that displays a random photo from Unsplash.
Swift
9
star
23

imagga-auto-tag

๐ŸŽฉ Ruby client for fetching tags from the Imagga Auto Tagging API
Ruby
8
star
24

sum-types-fast-check

fast-check bindings for @unsplash/sum-types.
TypeScript
3
star
25

service-dash

๐Ÿ”ฅ Is anything on fire?
Ruby
2
star
26

swift-storyboard-identifiers

A script that generates identifiers strings from .storyboard files.
Ruby
2
star
27

mercury

The guide of souls to the underworld.
Rust
2
star
28

imgix-trackable

๐Ÿ›ฐ Tracklable Imgix URLs via the ixid param
TypeScript
2
star
29

sum-types-io-ts

io-ts bindings for @unsplash/sum-types.
TypeScript
2
star
30

ts-type-tests-example

TypeScript
2
star
31

import-sort-style-unsplash

TypeScript
2
star
32

sum-types-fp-ts

fp-ts bindings for @unsplash/sum-types.
TypeScript
2
star