{"id":13535232,"url":"https://github.com/octree/LegoKit","last_synced_at":"2025-04-02T00:32:55.444Z","repository":{"id":41168243,"uuid":"191682044","full_name":"octree/LegoKit","owner":"octree","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-17T14:39:03.000Z","size":1817,"stargazers_count":14,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T10:11:42.897Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/octree.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-06-13T03:08:52.000Z","updated_at":"2025-01-17T14:38:45.000Z","dependencies_parsed_at":"2024-01-14T02:35:37.992Z","dependency_job_id":"7cc5950d-a323-41d2-b50f-eec2ed10d7d1","html_url":"https://github.com/octree/LegoKit","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":"0.045454545454545414","last_synced_commit":"7cc26687db483d66d13b1e2eb82f077ddbfc95c6"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FLegoKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FLegoKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FLegoKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FLegoKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/octree","download_url":"https://codeload.github.com/octree/LegoKit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246735356,"owners_count":20825221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T08:00:51.642Z","updated_at":"2025-04-02T00:32:55.100Z","avatar_url":"https://github.com/octree.png","language":"Swift","readme":"# LegoKit\n\n使用声明式语法组织 UICollectionView。支持类似 SwiftUI 的 `@State`、`@Published` 和 `@StateObject`\n\n\n## Installation\n\n### Swift Package Manager\n\nFile \u003e Swift Packages \u003e Add Package Dependency\nAdd https://github.com/octree/LegoKit.git\nSelect \"Up to Next Major\" with \"1.0.0\"\n\n\n## How to Use\n\n### Cell 的实现\n\n```swift\npublic struct ColorItem: TypedItemType {\n    public typealias CellType = ColorCell\n    public var id: UUID\n    public var color: UIColor\n    public var height: CGFloat\n}\n\npublic class ColorCell: UICollectionViewCell, TypedCellType {\n    public typealias Item = ColorItem\n\n    public func update(with item: ColorItem) {\n        backgroundColor = item.color\n    }\n}\n```\n\n* id: 可以是任意 `Hashable`类型，为了给以后做动画，进行 diff 准备的预留属性。\n* 其他的属性，是用来配置 Cell 的信息\n* Cell 需要实现 `TypedCellType` 的方法\n  * **update**：根据对应的 `Item` 更新 UI\n\n### 渲染 UICollectionView\n\n#### 配置 Lego\n\n```swift\nclass ViewController: UIViewController {\n    var lego: Lego {\n        Lego {\n            Section(id: ..., WaterfallLayout()) {\n                ColorItem(...)\n                ColorItem(...)\n                ColorItem(...)\n            }\n\n            Section(WaterfallLayout()) {\n                for elt in array {\n                    ColorItem(elt)\n                }\n            }\n        }\n    }\n}\n```\n\n* Lego 用于描述 **CollectionView** 中的 Section 和 Cell 的信息\n* 使用类似 SwiftUI 的声明式语法构建\n* 支持 `if`、`#if @available `、`for in`、`switch case`等结构\n\n#### 渲染\n\n```swift\nclass ViewController: UIViewController {\n    lazy var legoRenderer: LegoRenderer = .init(lego: lego)\n    override func viewDidLoad() {\n        legoRenderer.render(in: view) {\n            $0.backgroundColor = UIColor(white: 0.95, alpha: 1)\n        }\n    }\n\n    private func reload() {\n        legoRenderer.apply(lego)\n    }\n}\n```\n\n\n\n### 使用 @State、@StateObject 等，自动更新 CollectionView\n\n```swift\nclass ViewModel: LegoObservableObject {\n    @LegoPublished var items: [ColorItem] = []\n    func addRandomColor() {\n        items.append(.random)\n    }\n}\n\nclass ViewController: UIViewController, LegoContainer {\n    @StateObject var viewModel = ViewModel()\n    @State var flag: Bool = false\n    lazy var legoRenderer: LegoRenderer = .init(lego: lego)\n    var lego: Lego {\n        Lego {\n            Section(id: 0, layout: WaterfallLayout()) {\n                if flag {\n                    // ... some items.\n                }\n                viewModel.items\n            }\n        }\n    }\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        legoRenderer.render(in: view) {\n            $0.backgroundColor = UIColor(white: 0.95, alpha: 1)\n        }\n    }\n}\n```\n\n* 当用于配置 Lego 的数据，使用了 `@State` 或者 `@StateObject ` 时就不需要手动调用 `apply` 函数。当数据发生变动时，会自动更新 `UICollectionView`。\n\n## License\n\n**LegoKit** is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":["UIKit"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctree%2FLegoKit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foctree%2FLegoKit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctree%2FLegoKit/lists"}