How linkedinattic/layoutkit works
Project type and purpose
• Open-source UI layout library (LayoutKit) for iOS, macOS, tvOS.
• Offers a declarative, immutable, high-performance alternative to Auto Layout.
• Exposes both Swift and Objective-C APIs, with adapters for UITableView/UICollectionView.
File structure
Top-level folders and their roles:
• Sources/ – core library code, organized into submodules (Layouts, LayoutArrangement, LayoutMeasurement, Views, ObjCSupport, etc.).
• ExampleLayouts/ – standalone Swift layouts demonstrating API patterns.
• LayoutKitSampleApp/ & LayoutKitObjCSampleApp/ – sample iOS apps (Swift Obj-C) that consume the library.
• LayoutKit.playground/ – interactive playground showing usage.
• LayoutKitTests/ – unit tests for core modules.
• Tests/cocoapods & Tests/swift-package-manager/ – validation of CocoaPods, Carthage, SwiftPM integration.
• docs/ – user documentation, benchmarks, guides.
• Package.swift, *.podspec – package managers configuration.
README for insights
• Core features: asynchronous layout computation, immutable “Layout” data structures, view adapters.
• Supported platforms: iOS, macOS, tvOS.
• Integration: CocoaPods, Carthage, Swift Package Manager, Playground.
Components
a. Consumer layer:
– Sample Apps (Swift/Obj-C)
– ExampleLayouts
– Playground
b. Package manager layer:
– CocoaPods / Carthage / SwiftPM (Package.swift / podspec)
c. Library core modules (Sources/):
1. Layout Definitions (Sources/Layouts/…): ButtonLayout, StackLayout, InsetLayout, etc.
2. Layout Model & API (Sources/Layout.swift, Alignment.swift, Flexibility.swift)
3. Measurement & Arrangement (Sources/LayoutMeasurement.swift, LayoutArrangement.swift)
4. Views & Adapters (Sources/Views/…): ReloadableView, LayoutAdapterTableView/CollectionView
5. Platform Support: UIKitSupport.swift, AppKitSupport.swift
6. ObjC Bridging (Sources/ObjCSupport/…): Builders, LOKLayout classes, header interfaces
7. Internal Utilities (Sources/Internal/…, Math/)
d. Testing & Documentation:
– Unit tests (LayoutKitTests/)
– Docs site (mkdocs.yml & docs/)
Relationships and data flow
• At build time, consumer apps import LayoutKit via CocoaPods/Carthage/SwiftPM.
• At runtime:
1. Client code constructs a tree of immutable Layout objects (e.g., StackLayout, LabelLayout).
2. Calls layout.arrangement() → produces a LayoutArrangement (position+size for each sublayout).
3. Calls arrangement.makeViews(in: rootView) → instantiates UIViews or NSViews via UIKitSupport/AppKitSupport.
4. If using UITableView/UICollectionView, the Views layer’s LayoutAdapter manages batch updates and cell reuse.
• ObjC consumers call into ObjCSupport layer, which wraps Swift types in Objective-C friendly classes/builders.
Architectural patterns & design principles
• Layered modular architecture: clear separation between definition (Layouts), algorithm (Measurement & Arrangement), and rendering (Views).
• Declarative immutable data structures → facilitates background computation and caching.
• Builder pattern in ObjCSupport for Objective-C API ergonomics.
• Adapter pattern for integrating with UITableView/UICollectionView.