{"id":13778159,"url":"https://github.com/onmyway133/Micro","last_synced_at":"2025-05-11T11:35:08.897Z","repository":{"id":69038812,"uuid":"239261640","full_name":"onmyway133/Micro","owner":"onmyway133","description":"🏎Fast diffing and type safe SwiftUI style data source for UICollectionView","archived":false,"fork":false,"pushed_at":"2020-06-11T06:25:20.000Z","size":8847,"stargazers_count":78,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-09T04:51:40.827Z","etag":null,"topics":["datasource","diff","reload","safe","swift","swiftui","uicollectionview"],"latest_commit_sha":null,"homepage":"https://onmyway133.com/apps","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/onmyway133.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2020-02-09T07:03:30.000Z","updated_at":"2024-11-06T15:41:57.000Z","dependencies_parsed_at":"2023-03-24T22:44:27.050Z","dependency_job_id":null,"html_url":"https://github.com/onmyway133/Micro","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onmyway133%2FMicro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onmyway133%2FMicro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onmyway133%2FMicro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onmyway133%2FMicro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onmyway133","download_url":"https://codeload.github.com/onmyway133/Micro/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225043287,"owners_count":17411956,"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":["datasource","diff","reload","safe","swift","swiftui","uicollectionview"],"created_at":"2024-08-03T18:00:51.678Z","updated_at":"2024-11-17T13:31:26.529Z","avatar_url":"https://github.com/onmyway133.png","language":"Swift","readme":"## Micro\n\n❤️ Support my apps ❤️ \n\n- [Push Hero - pure Swift native macOS application to test push notifications](https://onmyway133.com/pushhero)\n- [PastePal - Pasteboard, note and shortcut manager](https://onmyway133.com/pastepal)\n- [Quick Check - smart todo manager](https://onmyway133.com/quickcheck)\n- [Alias - App and file shortcut manager](https://onmyway133.com/alias)\n- [My other apps](https://onmyway133.com/apps/)\n\n❤️❤️😇😍🤘❤️❤️\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"Screenshots/demo.gif\" height=400\u003e\n\u003c/div\u003e\n\nFor demo, check [DemoMicro](https://github.com/onmyway133/Micro/tree/master/Example/DemoMicro)\n\nRead more\n\n- [How to build SwiftUI style UICollectionView data source in Swift](https://onmyway133.github.io/blog/How-to-build-SwiftUI-style-UICollectionView-data-source-in-Swift/)\n- [A better way to update UICollectionView data in Swift with diff framework](https://medium.com/flawless-app-stories/a-better-way-to-update-uicollectionview-data-in-swift-with-diff-framework-924db158db86)\n\n## Description\n\nMost of the time, we want to apply model data to cell with smart diffing.\nMicro provides type safe SwiftUI style data source for UICollectionView, with super fast diffing powered by [DeepDiff](https://github.com/onmyway133/DeepDiff).\nJust declare a `State` with SwiftUI style `forEach` and Micro will reload with animated diffing\n\n```swift\nstruct Blog: DiffAware {}\nclass BlogCell: UICollectionViewCell {}\n\nlet dataSource = DataSource(collectionView: collectionView)\ndataSource.state = State {\n    ForEach(blogs) { blog in\n        Cell\u003cBlogCell\u003e() { context, cell in\n            cell.nameLabel.text = blog.name\n        }\n        .onSelect { context in \n            print(\"cell at index \\(context.indexPath.item) is selected\")\n        }\n        .onSize { context in \n            CGSize(\n                width: context.collectionView.frame.size.width, \n                height: 40\n            )\n        }\n    }\n}\n```\n\nThe above uses Swift 5.1 function builder syntax, which uses `forEach` method under the hood. You can also do like below with `forEach` method. \n\n```swift\ndataSource.state = forEach(blogs) { blog in\n    Cell\u003cBlogCell\u003e() { context, cell in\n        cell.nameLabel.text = blog.name\n    }\n    .onSelect { context in \n        print(\"cell at index \\(context.indexPath.item) is selected\")\n    }\n    .onSize { context in \n        CGSize(\n            width: context.collectionView.frame.size.width, \n            height: 40\n        )\n    }\n}\n```\n\nFeatures\n\n- Supports iOS 8+\n- Declare in type safe manner with `forEach`\n- `context` provides additional information, like `UICollectionView` and `IndexPath`\n- Automatic reload with smart diffing whenever `state` is set\n- By default, diffing is animated, you can use `dataSource.reload(newState:isAnimated:completion)` to specify animated and completion\n\n## Advanced\n\n### Animate reloading\n\nBy default, when you set `state` on `DataSource`, animated diffing is performed. If you want to set animated property and to listen to completion event, you can use `reload` method\n\n```swift\ndataSource.reload(\n    newState: newState,\n    isAnimated: false,\n    completion: { finished in\n        print(\"reloade is finished\")\n    }\n)\n```\n\n### Complex model with multiple cell types\n\nYou can declare different `Cell` in `forEach` with different kinds of cell.\n\n```swift\nstruct Movie: DiffAware {\n    enum Kind: Equatable {\n        case show(String)\n        case loading\n        case ad\n    }\n\n    let diffId = UUID()\n    let kind: Kind\n\n    static func compareContent(_ a: Movie, _ b: Movie) -\u003e Bool {\n        return a.kind == b.kind\n    }\n}\n\nclass MovieCell: UICollectionViewCell {\n    let nameLabel: UILabel = .init()\n}\n\nclass LoadingCell: UICollectionViewCell {}\n\nclass AdCell: UICollectionViewCell {}\n\nlet layout = UICollectionViewFlowLayout()\nlet collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)\nlet dataSource = DataSource(collectionView: collectionView)\ncollectionView.dataSource = dataSource\ncollectionView.delegate = dataSource\n\nlet movies: [Movie] = [\n    Movie(kind: .show(\"Titanic\")),\n    Movie(kind: .show(\"Batman\")),\n    Movie(kind: .loading),\n    Movie(kind: .ad)\n]\n\ndataSource.state = forEach(movies) { movie in\n    switch movie.kind {\n    case .show(let name):\n        return Cell\u003cMovieCell\u003e() { context, cell in\n            cell.nameLabel.text = name\n        }\n        .onSelect { _ in\n\n        }\n        .onDeselect { _ in\n\n        }\n        .onWillDisplay { _, _ in\n\n        }\n        .onDidEndDisplay { _, _ in\n\n        }\n        .onSize { context in\n            CGSize(width: context.collectionView.frame.size.width, height: 40)\n        }\n    case .loading:\n        return Cell\u003cLoadingCell\u003e()\n    case .ad:\n        return Cell\u003cAdCell\u003e()\n    }\n}\n```\n\n### Customize with subclass\n\n`DataSource` is completely overridable, if you want to customize any methods, just subclass `DataSource`, override methods and access its `state.models` \n\n```swift\nclass CustomDataSource: DataSource {\n    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {\n        let blog = state.models[indexPath.item] as? Blog\n        print(blog)\n    }\n}\n```\n\n## Installation\n\n**Micro** is also available through [Swift Package Manager](https://swift.org/package-manager/)\n\n```swift\n.package(url: \"https://github.com/onmyway133/Micro\", from: \"1.2.0\")\n```\n\n## Author\n\nKhoa Pham, onmyway133@gmail.com\n\n## License\n\n**Micro** is available under the MIT license. See the [LICENSE](https://github.com/onmyway133/Micro/blob/master/LICENSE.md) file for more info.\n","funding_links":[],"categories":["Uncategorized","Grid"],"sub_categories":["Uncategorized","Content"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonmyway133%2FMicro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonmyway133%2FMicro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonmyway133%2FMicro/lists"}