Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YogeshPateliOS/b2b13bfe5f7eef5cd7fa4a894cd35d5a to your computer and use it in GitHub Desktop.
Save YogeshPateliOS/b2b13bfe5f7eef5cd7fa4a894cd35d5a to your computer and use it in GitHub Desktop.
import UIKit
@available(iOS 13.0, *)
typealias UserDataSource = UITableViewDiffableDataSource<TblSection, YoutubeVideoModel>
@available(iOS 13.0, *)
typealias UserSnapshot = NSDiffableDataSourceSnapshot<TblSection, YoutubeVideoModel>
enum TblSection{
case first
}
class SearchViewController: UIViewController {
@IBOutlet weak var lblRecord: UILabel!
@IBOutlet weak var searchTblView: UITableView!
var arrVideo = [YoutubeVideoModel]()
@available(iOS 13.0, *)
lazy var datasource = UserDataSource()
override func viewDidLoad() {
super.viewDidLoad()
lblRecord.text = "Let's start searching videos"
if #available(iOS 13.0, *) {
configureDatasource()
} else {
// Fallback on earlier versions
}
searchTblView.register(VideoTableViewCell.self, forCellReuseIdentifier: "VideoTableViewCell")
setupSearchController()
APICalling()
}
@available(iOS 13.0, *)
func getDatasource() -> UserDataSource{
var datasource: UserDataSource!
return datasource
}
@available(iOS 13.0, *)
func configureDatasource(){
datasource = UITableViewDiffableDataSource<TblSection, YoutubeVideoModel>(tableView: searchTblView, cellProvider: { (tableView, indexPath, modelVideo) -> VideoTableViewCell? in
self.configurationCell(indexPath: indexPath)
})
}
@available(iOS 13.0, *)
func createSnapshot(users: [YoutubeVideoModel]){
var snapshot = UserSnapshot()
snapshot.appendSections([.first])
snapshot.appendItems(users)
datasource.apply(snapshot, animatingDifferences: true)
}
func configurationCell(indexPath: IndexPath) -> VideoTableViewCell{
let cellVideo = VideoTableViewCell.cellVideo
var cell = searchTblView.dequeueReusableCell(withIdentifier: cellVideo.identifier, for: indexPath) as? VideoTableViewCell
if cell != nil{
if let arr = Bundle.main.loadNibNamed("VideoTableViewCell", owner: self, options: [:]){
cell = arr[cellVideo.index] as? VideoTableViewCell
}
}
let modelVideo = arrVideo[indexPath.row]
cell?.modelVideo = modelVideo
cell?.tapYoutube = {
modelVideo.videoId.playInYoutube()
}
cell?.tapWatchLater = {
DatabaseHelper.shareInstance.saveYoutubeVideo(modelYoutube: modelVideo)
}
return cell!
}
}
extension SearchViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
arrVideo.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
configurationCell(indexPath: indexPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment