Commit 31e37bd4 authored by Andreas Traczyk's avatar Andreas Traczyk Committed by Kateryna Kostiuk

contact requests: move cell logic into ContactRequestCell

- moves cell initialization logic from ContactRequestsViewController
  to ContactRequestCell

Change-Id: Ia3e6c61630a5427b32821688ce4b0cf61df6d0ae
Reviewed-by: Kateryna Kostiuk's avatarKateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
parent 0ccfccaa
......@@ -1241,8 +1241,8 @@
1A5DC0331F3567080075E8EF /* ContactRequests */ = {
isa = PBXGroup;
children = (
1A5DC0341F3567360075E8EF /* Cells */,
1A5DC0411F3567DF0075E8EF /* ContactRequestsCoordinator.swift */,
1A5DC0341F3567360075E8EF /* Cells */,
1A5DC03A1F35678D0075E8EF /* ContactRequestsViewController.storyboard */,
1A5DC03B1F35678D0075E8EF /* ContactRequestsViewController.swift */,
1A5DC03C1F35678D0075E8EF /* ContactRequestsViewModel.swift */,
......
......@@ -49,4 +49,82 @@ class ContactRequestCell: UITableViewCell, NibReusable {
override func prepareForReuse() {
self.disposeBag = DisposeBag()
}
func configureFromItem(_ item: ContactRequestItem) {
item.userName
.asObservable()
.observeOn(MainScheduler.instance)
.bind(to: self.nameLabel.rx.text)
.disposed(by: self.disposeBag)
// Avatar placeholder initial
self.fallbackAvatar.text = nil
self.fallbackAvatarImage.isHidden = true
let name = item.userName.value
let scanner = Scanner(string: name.toMD5HexString().prefixString())
var index: UInt64 = 0
if scanner.scanHexInt64(&index) {
self.fallbackAvatar.isHidden = false
self.fallbackAvatar.backgroundColor = avatarColors[Int(index)]
if item.contactRequest.ringId != name {
self.fallbackAvatar.text = name.prefixString().capitalized
} else {
self.fallbackAvatarImage.isHidden = false
}
}
item.userName.asObservable()
.observeOn(MainScheduler.instance)
.filter({ [weak item] userName in
return userName != item?.contactRequest.ringId
})
.map { value in value.prefixString().capitalized }
.bind(to: self.fallbackAvatar.rx.text)
.disposed(by: self.disposeBag)
item.userName.asObservable()
.observeOn(MainScheduler.instance)
.map { [weak item] userName in userName != item?.contactRequest.ringId }
.bind(to: self.fallbackAvatarImage.rx.isHidden)
.disposed(by: self.disposeBag)
// UIColor that observes "best Id" prefix
item.userName.asObservable()
.observeOn(MainScheduler.instance)
.map { name in
let scanner = Scanner(string: name.toMD5HexString().prefixString())
var index: UInt64 = 0
if scanner.scanHexInt64(&index) {
return avatarColors[Int(index)]
}
return defaultAvatarColor
}
.subscribe(onNext: { backgroundColor in
self.fallbackAvatar.backgroundColor = backgroundColor
})
.disposed(by: self.disposeBag)
// Set image if any
if let imageData = item.profileImageData.value {
if let image = UIImage(data: imageData) {
self.profileImageView.image = image
self.fallbackAvatar.isHidden = true
}
} else {
self.fallbackAvatar.isHidden = false
self.profileImageView.image = nil
}
item.profileImageData.asObservable()
.observeOn(MainScheduler.instance)
.subscribe(onNext: { data in
if let imageData = data, let image = UIImage(data: imageData) {
self.fallbackAvatar.isHidden = true
self.profileImageView.image = image
} else {
self.fallbackAvatar.isHidden = false
self.profileImageView.image = nil
}
}).disposed(by: self.disposeBag)
}
}
......@@ -35,8 +35,6 @@ class ContactRequestsViewController: UIViewController, StoryboardBased, ViewMode
fileprivate let cellIdentifier = "ContactRequestCell"
fileprivate let log = SwiftyBeaver.self
fileprivate var backgroundColorObservable: Observable<UIColor>!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rx.modelSelected(ContactRequestItem.self)
......@@ -67,79 +65,7 @@ class ContactRequestsViewController: UIViewController, StoryboardBased, ViewMode
.contactRequestItems
.observeOn(MainScheduler.instance)
.bind(to: tableView.rx.items(cellIdentifier: cellIdentifier, cellType: ContactRequestCell.self)) { [unowned self] _, item, cell in
item.userName
.asObservable()
.observeOn(MainScheduler.instance)
.bind(to: cell.nameLabel.rx.text)
.disposed(by: cell.disposeBag)
// Avatar placeholder initial
cell.fallbackAvatar.text = nil
cell.fallbackAvatarImage.isHidden = true
let name = item.userName.value
let scanner = Scanner(string: name.toMD5HexString().prefixString())
var index: UInt64 = 0
if scanner.scanHexInt64(&index) {
cell.fallbackAvatar.isHidden = false
cell.fallbackAvatar.backgroundColor = avatarColors[Int(index)]
if item.contactRequest.ringId != name {
cell.fallbackAvatar.text = name.prefixString().capitalized
} else {
cell.fallbackAvatarImage.isHidden = false
}
}
item.userName.asObservable()
.observeOn(MainScheduler.instance)
.filter({ [weak item] userName in
return userName != item?.contactRequest.ringId
})
.map { value in value.prefixString().capitalized }
.bind(to: cell.fallbackAvatar.rx.text)
.disposed(by: cell.disposeBag)
item.userName.asObservable()
.observeOn(MainScheduler.instance)
.map { [weak item] userName in userName != item?.contactRequest.ringId }
.bind(to: cell.fallbackAvatarImage.rx.isHidden)
.disposed(by: cell.disposeBag)
// UIColor that observes "best Id" prefix
self.backgroundColorObservable = item.userName.asObservable()
.observeOn(MainScheduler.instance)
.map { name in
let scanner = Scanner(string: name.toMD5HexString().prefixString())
var index: UInt64 = 0
if scanner.scanHexInt64(&index) {
return avatarColors[Int(index)]
}
return defaultAvatarColor
}
// Set placeholder avatar to backgroundColorObservable
self.backgroundColorObservable
.subscribe(onNext: { backgroundColor in
cell.fallbackAvatar.backgroundColor = backgroundColor
})
.disposed(by: cell.disposeBag)
// Set image if any
cell.fallbackAvatar.isHidden = false
cell.profileImageView.image = nil
item.profileImageData.asObservable()
.observeOn(MainScheduler.instance)
.subscribe(onNext: { data in
if let imageData = data {
if let image = UIImage(data: imageData) {
cell.profileImageView.image = image
cell.fallbackAvatar.isHidden = true
}
} else {
cell.fallbackAvatar.isHidden = false
cell.profileImageView.image = nil
}
}).disposed(by: cell.disposeBag)
cell.configureFromItem(item)
//Accept button
cell.acceptButton.backgroundColor = UIColor.clear
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment