{"id":2872,"url":"https://github.com/GenericDataSource/GenericDataSource","last_synced_at":"2025-08-03T12:31:38.276Z","repository":{"id":56912649,"uuid":"55894214","full_name":"GenericDataSource/GenericDataSource","owner":"GenericDataSource","description":"A generic small reusable components for data source implementation for UITableView/UICollectionView in Swift.","archived":false,"fork":false,"pushed_at":"2023-10-21T14:33:48.000Z","size":697,"stargazers_count":133,"open_issues_count":4,"forks_count":17,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-11T12:20:44.517Z","etag":null,"topics":["clean-code","composable-components","datasource","generic","light-view-controller","swift","uicollectionview","uitableview","xcode"],"latest_commit_sha":null,"homepage":"","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/GenericDataSource.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2016-04-10T11:03:40.000Z","updated_at":"2024-02-17T11:40:19.000Z","dependencies_parsed_at":"2024-01-28T08:18:59.737Z","dependency_job_id":null,"html_url":"https://github.com/GenericDataSource/GenericDataSource","commit_stats":{"total_commits":153,"total_committers":5,"mean_commits":30.6,"dds":0.3921568627450981,"last_synced_commit":"5a4783387ea7eae44b4bb5035b47ba9469d8981e"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenericDataSource%2FGenericDataSource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenericDataSource%2FGenericDataSource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenericDataSource%2FGenericDataSource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenericDataSource%2FGenericDataSource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GenericDataSource","download_url":"https://codeload.github.com/GenericDataSource/GenericDataSource/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228543179,"owners_count":17934439,"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":["clean-code","composable-components","datasource","generic","light-view-controller","swift","uicollectionview","uitableview","xcode"],"created_at":"2024-01-05T20:16:25.078Z","updated_at":"2024-12-07T00:31:04.691Z","avatar_url":"https://github.com/GenericDataSource.png","language":"Swift","readme":"# GenericDataSource\n\n[![Swift Version](https://img.shields.io/badge/Swift-4.0+-orange.svg)](https://swift.org)\n\n[![Version](https://img.shields.io/cocoapods/v/GenericDataSources.svg?style=flat)](http://cocoapods.org/pods/GenericDataSources)\n\n\n[![Build Status](https://travis-ci.org/GenericDataSource/GenericDataSource.svg?branch=master)](https://travis-ci.org/GenericDataSource/GenericDataSource)\n[![Coverage Status](https://codecov.io/gh/GenericDataSource/GenericDataSource/branch/master/graphs/badge.svg)](https://codecov.io/gh/GenericDataSource/GenericDataSource/branch/master)\n[![Documentation](https://img.shields.io/cocoapods/metrics/doc-percent/GenericDataSources.svg)](http://cocoadocs.org/docsets/GenericDataSources)\n\nA generic small reusable components for data source implementation for `UITableView`/`UICollectionView` written in Swift.\n\n## Features\n\n- [x] `BasicDataSource` easily bind model to cells with automatic dequeuing.\n- [x] `SegmentedDataSource` easily build segmented controls or for empty state of your `UICollectionView`/`UITableView` data source.\n- [x] `CompositeDataSource` builds complex cells/models structure with easy to use components (`BasicDataSource` `SegmentedDataSource` or other `CompositeDataSource`).\n- [x] `UICollectionView` supplementary, `UITableView` header, and footer views support.\n- [x] Ability to override any data source method from `UIKit` classes.\n- [x] Comprehensive Unit Test Coverage.\n- [x] [Complete Documentation](http://cocoadocs.org/docsets/GenericDataSources)\n\n## Requirements\n\n- iOS 8.0+\n- Xcode 10\n- Swift 4.0+\n\n## Installation\n\n#### CocoaPods\n\nTo integrate `GenericDataSource` into your Xcode project using CocoaPods, specify it in your `Podfile`:\n\n```ruby\npod 'GenericDataSources'\n```\n\n**IMPORTANT:** The pod name is GenericDataSource**s** with \"s\" at the end.\n\n#### Carthage\n\nTo integrate GenericDataSource into your Xcode project using Carthage, specify it in your Cartfile:\n\n```bash\ngithub \"GenericDataSource/GenericDataSource\"\n```\n\n#### Manually\n\nAdd `GenericDataSource.xcodeproj` to your project file by drag and drop.\n\nYou can then consult to [Adding an Existing Framework to a Project](https://developer.apple.com/library/ios/recipes/xcode_help-structure_navigator/articles/Adding_a_Framework.html).\n\n---\n## Example\n\n### Basic Data Source Example\n\n#### UITableView\nCreate a basic data source and bind it to to a table view.\n\n```swift\nlet dataSource = BasicBlockDataSource\u003cExample, BasicTableViewCell\u003e() { (item: Example, cell: BasicTableViewCell, indexPath) -\u003e Void in\n    cell.titleLabel?.text = item.title\n}\n\n// Need to keep a strong reference to our data source.\nself.dataSource = dataSource\n\n// register the cell\ntableView.ds_register(cellClass: BasicTableViewCell.self)\n// bind the data source to the table view\ntableView.ds_useDataSource(dataSource)\n\ndataSource.items =  \u003c\u003cretrieve items\u003e\u003e // Can be set and altered at anytime\n```\n\nThat's it! Your first data source is implemented. No dequeuing! no casting! simple and smart.\n\n#### UICollectionView\nLet's now take it to the next level. Suppose after we implemented it, the requirements changed and we need to implement it using `UICollectionView`.\n\n```swift\nlet dataSource = BasicBlockDataSource\u003cExample, BasicCollectionViewCell\u003e() { (item: Example, cell: BasicCollectionViewCell, indexPath) -\u003e Void in\n    cell.titleLabel?.text = item.title\n}\n\n// Need to keep a strong reference to our data source.\nself.dataSource = dataSource\n\n// register the cell\ncollectionView.ds_register(cellClass: BasicCollectionViewCell.self)\n// bind the data source to the collection view\ncollectionView.ds_useDataSource(dataSource)\n\ndataSource.items =  \u003c\u003cretrieve items\u003e\u003e // Can be set and altered at anytime\n```\n\n__All you need to do is change the cell class and of course the table view to collection view.__\n\nActually this opens the door for so much possibilities. You can inherit from `BasicDataSource` and implement your custom generic data source that is based on a protocol implemented by the cell and you don't need to repeat the configuration part. You would create data source like that.\n```\nlet dataSource1 = CustomDataSource\u003cBasicTableViewCell\u003e() // for table view\nlet dataSource2 = CustomDataSource\u003cBasicCollectionViewCell\u003e() // for collection view\n```\n\n### App store Featured Example\n\nSuppose we want to implement the following screen, the App Store featured tab.\n\n![App Store Example Screenshot](https://cloud.githubusercontent.com/assets/5665498/24696881/6a4e7778-19eb-11e7-9e65-d96eac0dce76.gif)\n\n__If you want to have a look at the complete source code, it is under Example project -\u003e `AppStoreViewController.swift`.__\n\n1. We will create cells as we do normally.\n2. Now we need to think about DataSources.\n3. It's simple, one data source for each cell type (`BasicDataSource`).\n4. `CompositeDataSource(sectionType: .single)` for the table view rows. Since these rows are of different cell types.\n5. `SegmentedDataSource` for switching between loading and data views.\n6. Bind the `SegmentedDataSource` data source to the table view and that's it.\n7. See how we think structurally about our UI and data sources instead of one big cell.\n\nOne thing we didn't talk about is the `UICollectionView` of the featured section cells. It's very simple, just `BasicDataSource`.\n\nSee how we can implement the screen in the following code:\n\n1. Create the cells.\n```Swift\nclass AppStoreFeaturedSectionTableViewCell: UITableViewCell { ... }\nclass AppStoreQuickLinkLabelTableViewCell: UITableViewCell { ... }\nclass AppStoreQuickLinkTableViewCell: UITableViewCell { ... }\nclass AppStoreFooterTableViewCell: UITableViewCell { ... }\nclass AppStoreLoadingTableViewCell: UITableViewCell { ... }\n```\n2. Create `BasicDataSource`s.\n```Swift\nclass AppStoreLoadingDataSource: BasicDataSource\u003cVoid, AppStoreLoadingTableViewCell\u003e {\n    // loading should take full screen size.\n    override func ds_collectionView(_ collectionView: GeneralCollectionView, sizeForItemAt indexPath: IndexPath) -\u003e CGSize {\n        return collectionView.size\n    }\n}\nclass AppStoreFooterDataSource: BasicDataSource\u003cVoid, AppStoreFooterTableViewCell\u003e { ... }\nclass AppStoreQuickLinkDataSource: BasicDataSource\u003cFeaturedQuickLink, AppStoreQuickLinkTableViewCell\u003e { ...  }\nclass AppStoreFeaturedAppsDataSource: BasicDataSource\u003cFeaturedApp, AppStoreFeaturedAppCollectionViewCell\u003e { ... }\nclass AppStoreFeaturedAppsSectionDataSource: BasicDataSource\u003cFeaturedSection, AppStoreFeaturedSectionTableViewCell\u003e { ... }\nclass AppStoreQuickLinkLabelDataSource: BasicDataSource\u003cString, AppStoreQuickLinkLabelTableViewCell\u003e { ... }\n```\n3. Create `CompositeDataSource` that holds the featured page.\n```Swift\nclass AppStoreFeaturedPageDataSource: CompositeDataSource {\n    init() { super.init(sectionType: .single)] }\n\n    var page: FeaturedPage? {\n        didSet {\n            // remove all existing data sources\n            removeAllDataSources()\n\n            guard let page = page else {\n                return\n            }\n\n            // add featured apps\n            let featuredApps = AppStoreFeaturedAppsSectionDataSource()\n            featuredApps.setSelectionHandler(UnselectableSelectionHandler())\n            featuredApps.items = page.sections\n            add(featuredApps)\n\n            // add quick link label\n            let quickLinkLabel = AppStoreQuickLinkLabelDataSource()\n            quickLinkLabel.setSelectionHandler(UnselectableSelectionHandler())\n            quickLinkLabel.items = [page.quickLinkLabel]\n            add(quickLinkLabel)\n\n            // add quick links\n            let quickLinks = AppStoreQuickLinkDataSource()\n            quickLinks.items = page.quickLinks\n            add(quickLinks)\n\n            // add footer\n            let footer = AppStoreFooterDataSource()\n            footer.setSelectionHandler(UnselectableSelectionHandler())\n            footer.items = [Void()] // we add 1 element to show the footer, 2 elements will show it twice. 0 will not show it.\n            add(footer)\n        }\n    }\n}\n```\n\n4. Create the outer most data source.\n```Swift\nclass AppStoreDataSource: SegmentedDataSource {\n\n    let loading = AppStoreLoadingDataSource()\n    let page = AppStoreFeaturedPageDataSource()\n\n    // reload data on index change\n    override var selectedDataSourceIndex: Int {\n        didSet {\n            ds_reusableViewDelegate?.ds_reloadData()\n        }\n    }\n\n    override init() {\n        super.init()\n        loading.items = [Void()] // we add 1 element to show the loading, 2 elements will show it twice. 0 will not show it.\n\n        add(loading)\n        add(page)\n    }\n}\n```\n\n5. Register cells.\n```Swift\ntableView.ds_register(cellNib: AppStoreFeaturedSectionTableViewCell.self)\ntableView.ds_register(cellNib: AppStoreQuickLinkLabelTableViewCell.self)\ntableView.ds_register(cellNib: AppStoreQuickLinkTableViewCell.self)\ntableView.ds_register(cellNib: AppStoreFooterTableViewCell.self)\ntableView.ds_register(cellNib: AppStoreLoadingTableViewCell.self)\n```\n\n6. Set data sources to the collection view.\n```Swift\ntableView.ds_useDataSource(dataSource)\n```\n\n7. Finally set the data when it is available.\n```Swift\n  // show loading indicator\n  dataSource.selectedDataSourceIndex = 0\n\n  // get the data from the service\n  service.getFeaturedPage { [weak self] page in\n\n    // update the data source model\n    self?.dataSource.page.page = page\n\n    // show the page\n    self?.dataSource.selectedDataSourceIndex = 1\n}\n```\n\nThere are many benefits of doing that:\n\n1. Lightweight view controllers.\n2. You don't need to think about indexes anymore, all is handled for us. Only think about how you can structure your cells into smaller data sources.\n3. We can switch between `UITableView` and `UICollectionView` without touching data sources or models. Only change the cells to inherit from `UITableViewCell` or `UICollectionViewCell` and everything else works.\n4. We can add/delete/update cells easily. For example we decided to add more blue links. We can do it by just adding new item to the array passed to the data source.\n5. We can re-arrange cells as we want. Just move around the `add` of data sources calls.\n6. Most importantly no `if`/`else` in our code.\n\n\n\nCheck the Examples application for complete implementations.\n\n## Attribution\n\nThe main idea comes from [WWDC 2014 Advanced User Interfaces with Collection Views] (https://developer.apple.com/videos/play/wwdc2014/232/)\nwritten in swift with generics.\n\n## Author\n\nMohamed Afifi, mohamede1945@gmail.com\n\n## License\n\nGenericDataSource is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":["UI"],"sub_categories":["Table View / Collection View","Other free courses"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGenericDataSource%2FGenericDataSource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGenericDataSource%2FGenericDataSource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGenericDataSource%2FGenericDataSource/lists"}