Skip to content

Instantly share code, notes, and snippets.

@alfian0
Created July 31, 2018 07:22
Show Gist options
  • Select an option

  • Save alfian0/de0506cdd4f67a0da9fe60b770d05f06 to your computer and use it in GitHub Desktop.

Select an option

Save alfian0/de0506cdd4f67a0da9fe60b770d05f06 to your computer and use it in GitHub Desktop.
import UIKit
import Common
import RxSwift
typealias AboutCellConfigured = CellConfigurator<AboutCell, AboutCell.Input>
class AboutCell: UITableViewCell {
@IBOutlet weak var textView: UITextView!
private var disposeBag: DisposeBag!
override func prepareForReuse() {
super.prepareForReuse()
disposeBag = nil
}
}
extension AboutCell: IReusableCell {
struct Input {
let viewModel: BuatGroupDraftViewModel
}
func configureCell(item: Input) {
let bag = DisposeBag()
textView.rx.didChange
.bind(to: item.viewModel.input.updateTrigger)
.disposed(by: bag)
disposeBag = bag
}
}
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
class BuatGroupDraftController: UITableViewController {
private var tableHeaderView: GroupImage = {
let view = GroupImage()
return view
}()
var viewModel: BuatGroupDraftViewModel!
private var dataSource: RxTableViewSectionedReloadDataSource<SectionOfGroupDraftData>!
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let backBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_back"), style: .plain, target: nil, action: nil)
let doneBarButton = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_done"), style: .plain, target: nil, action: nil)
navigationItem.title = "Buat grup baru"
navigationItem.leftBarButtonItem = backBarButtonItem
navigationItem.rightBarButtonItem = doneBarButton
navigationItem.configure(with: .subtitled(text: "Profil grup"))
backBarButtonItem.rx.tap
.bind(to: viewModel.input.backTrigger)
.disposed(by: disposeBag)
tableView.dataSource = nil
tableView.delegate = nil
tableView.registerReusableCell(ObrolanCell.self)
tableView.registerReusableCell(AboutCell.self)
tableView.registerReusableCell(SettingHeaderCell.self)
tableView.tableFooterView = UIView()
tableView.tableHeaderView = tableHeaderView
dataSource = RxTableViewSectionedReloadDataSource<SectionOfGroupDraftData>(
configureCell: { (dataSource, tableView, indexPath, item) in
guard let cell = tableView.dequeueReusableCell(withIdentifier: item.reuseIdentifier) else {
return UITableViewCell()
}
item.configure(cell: cell)
return cell
})
tableView.rx
.setDelegate(self)
.disposed(by: disposeBag)
viewModel.output.items
.drive(tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
viewModel.output.updateAction
.drive(onNext: { [weak self] (_) in
self?.tableView.beginUpdates()
self?.tableView.endUpdates()
})
.disposed(by: disposeBag)
}
}
extension BuatGroupDraftController {
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 48.0
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableCell() as SettingHeaderCell
view.configureCell(item: SettingHeaderCell.Input(title: dataSource.sectionModels[section].header))
return view
}
}
import Foundation
import RxSwift
import RxCocoa
import Common
final class BuatGroupDraftViewModel: ViewModelType {
let input: Input
var output: Output!
struct Input {
let backTrigger: AnyObserver<Void>
let updateTrigger: AnyObserver<Void>
}
struct Output {
let backAction: Driver<Void>
let items: Driver<[SectionOfGroupDraftData]>
let updateAction: Driver<Void>
}
private var navigator: BuatGroupDraftNavigator
private let backSubject = PublishSubject<Void>()
private let updateSubject = PublishSubject<Void>()
init(navigator: BuatGroupDraftNavigator) {
self.navigator = navigator
input = Input(backTrigger: backSubject.asObserver(),
updateTrigger: updateSubject.asObserver())
output = Output(
backAction: backSubject.asDriverOnErrorJustComplete(),
items: Driver.just([
SectionOfGroupDraftData(
header: GroupDraftDataDummyData.about.title,
items: [
AboutCellConfigured(item: AboutCell.Input(viewModel: self))
]),
SectionOfGroupDraftData(
header: GroupDraftDataDummyData.contact.title + " (2)",
items: [
ObrolanCellConfigured(
item: ObrolanCell.Input(
data: ObrolanModel(
photo: "",
title: "Alan Hartawan",
time: "19.29",
message: "Doel, kalo pulang opletnya dicuci yak! haha",
info: "10",
isOnline: false)
)
),
ObrolanCellConfigured(
item: ObrolanCell.Input(
data: ObrolanModel(
photo: "",
title: "Alan Hartawan",
time: "19.29",
message: "Doel, kalo pulang opletnya dicuci yak! haha",
info: "10",
isOnline: false)
)
)
])
]),
updateAction: updateSubject.asDriverOnErrorJustComplete())
}
}
import UIKit
import Common
class GroupImage: UIView {
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var name: UITextField!
override init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 240))
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
let view = loadNib()
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment