Skip to content

Instantly share code, notes, and snippets.

@lazyvar
Created April 7, 2020 16:44
Show Gist options
  • Save lazyvar/2c79ae770920b92dc2dfc5d5af350613 to your computer and use it in GitHub Desktop.
Save lazyvar/2c79ae770920b92dc2dfc5d5af350613 to your computer and use it in GitHub Desktop.
//
// RxDataSources+SkeletonView.swift
//
// Created by mack on 4/7/20.
//
import Foundation
import UIKit
import RxSwift
import RxDataSources
import SkeletonView
enum Row {
case skeleton
case username(String)
}
typealias Section = SectionModel<Void, Row>
class UsersViewController: UITableViewController {
private let disposeBag = DisposeBag()
private let dataSource = RxTableViewSectionedReloadDataSource<Section>(configureCell: { _, tableView, indexPath, row -> UITableViewCell in
switch row {
case .skeleton:
return UsernameTableViewCell.skeleton(tableView: tableView, indexPath: indexPath)
case .username(let username):
return UsernameTableViewCell.configure(tableView: tableView, indexPath: indexPath, username: username)
}
})
override func viewDidLoad() {
tableView.register(UsernameTableViewCell.self, forCellReuseIdentifier: "UsernameTableViewCell")
tableView.register(UsernameTableViewCell.self, forCellReuseIdentifier: "UsernameTableViewCell-Skeleton")
let skeletons = Observable<[Row]>
.just(Array(repeating: .skeleton, count: 10))
let actual = Observable<[Row]>
.just([.username("scorpion"), .username("vanilla"), .username("couch"), .username("meat")])
.delay(.seconds(1), scheduler: MainScheduler.instance)
Observable.merge(skeletons, actual)
.map { [SectionModel(model: (), items: $0)] }
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
}
class UsernameTableViewCell: UITableViewCell {
@IBOutlet weak var usernameLabel: UILabel!
static func skeleton(tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UsernameTableViewCell-Skeleton", for: indexPath) as! UsernameTableViewCell
cell.usernameLabel.isSkeletonable = true
cell.usernameLabel.showAnimatedSkeleton()
return cell
}
static func configure(tableView: UITableView, indexPath: IndexPath, username: String) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UsernameTableViewCell", for: indexPath) as! UsernameTableViewCell
cell.usernameLabel.text = username
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment