Binding to a datasource using RxDatasources - swift

I have a list of models that I fetch from a server, and basically I get array of these:
struct ContactModel: Codable, Equatable {
static func == (lhs: ContactModel, rhs: ContactModel) -> Bool {
return lhs.name == rhs.name &&
lhs.avatar == rhs.avatar &&
lhs.job == rhs.job &&
lhs.follow == rhs.follow
}
let id: Int
let name: String
let avatar:String?
let job: JobModel?
let follow:Bool
enum CodingKeys: String, CodingKey {
case id, name, avatar, job, follow
}
}
So I want to show a list of contacts in my tableview.
Now I have this struct as well which is wrapper around this model:
struct ContactCellModel : Equatable, IdentifiableType {
static func == (lhs: ContactCellModel, rhs: ContactCellModel) -> Bool {
return lhs.model == rhs.model
}
var identity: Int {
return model.id
}
var model: ContactModel
var cellIdentifier = ContactTableViewCell.identifier
}
What I am trying to do, is to create datasource using RxDatasources, and bind to it, like this(ContactsViewController.swift):
let dataSource = RxTableViewSectionedAnimatedDataSource<ContactsSectionModel>(
configureCell: { dataSource, tableView, indexPath, item in
if let cell = tableView.dequeueReusableCell(withIdentifier: item.cellIdentifier, for: indexPath) as? BaseTableViewCell{
cell.setup(data: item.model)
return cell
}
return UITableViewCell()
})
but I am not sure what I should do right after. I tried something like this:
Observable.combineLatest(contactsViewModel.output.contacts, self.contactViewModel.changedStatusForContact)
.map{ (allContacts, changedContact) -> ContactsSectionModel in
//what should I return here?
}.bind(to: dataSource)
I use combineLatest, cause I have one more observable (self.contactViewModel.changedStatusForContact) that notifies when certain contact has been changed (this happens when you tap a certain button on a contact cell).
So what should I return from .map above in order to successfully bind to previously created dataSource?

You have to replace the contacts that have changed; all the contacts that were changed, but you can't do that because you aren't tracking all of them, only the most recent one. So you can't do it in a map. You need to use scan instead.
I made a lot of assumptions about code you didn't post, so the below is a compilable example. If your types are different, then you will have to make some changes:
func example(contacts: Observable<[ContactModel]>, changedStatusForContact: Observable<ContactModel>, tableView: UITableView, disposeBag: DisposeBag) {
let dataSource = RxTableViewSectionedAnimatedDataSource<ContactsSectionModel>(
configureCell: { dataSource, tableView, indexPath, item in
if let cell = tableView.dequeueReusableCell(withIdentifier: item.cellIdentifier, for: indexPath) as? BaseTableViewCell {
cell.setup(data: item.model)
return cell
}
return UITableViewCell() // this is quite dangerious. Better would be to crash IMO.
})
let contactsSectionModels = Observable.combineLatest(contacts, changedStatusForContact) {
(original: $0, changed: $1)
}
.scan([ContactsSectionModel]()) { state, updates in
// `state` is the last array handed to the table view.
// `updates` contains the values from the combineLatest above.
var contactModels = state.flatMap { $0.items.map { $0.model } }
// get all the contactModels out of the state.
if contactModels.isEmpty {
contactModels = updates.original
}
// if there aren't any, then update with the values coming from `contacts`
else {
guard let index = contactModels
.firstIndex(where: { $0.id == updates.changed.id })
else { return state }
contactModels[index] = updates.changed
}
// otherwise find the index of the contact that changed.
// and update the array with the changed contact.
return [ContactsSectionModel(
model: "",
items: updates.original
.map { ContactCellModel(model: $0) }
)]
// rebuild the section model and return it.
}
contactsSectionModels
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
typealias ContactsSectionModel = AnimatableSectionModel<String, ContactCellModel>
struct ContactCellModel: Equatable, IdentifiableType {
var identity: Int { model.id }
let model: ContactModel
let cellIdentifier = ContactTableViewCell.identifier
}
struct ContactModel: Equatable {
let id: Int
let name: String
let avatar: String?
let job: JobModel?
let follow: Bool
}
struct JobModel: Equatable { }
class BaseTableViewCell: UITableViewCell {
func setup(data: ContactModel) { }
}
class ContactTableViewCell: BaseTableViewCell {
static let identifier = "Cell"
}

Related

How to binding data in swift?

I'm going to show the view the data I got from the server. An error occurred while developing using Rxswift and MVVM. The id value must be received from the product model, and an error occurs in this part.
extension SelectViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("Tap")
let detailVC = DetailViewController()
detailVC.product = products[indexPath.row]
detailVC.productId = products[indexPath.row].id
self.navigationController?.pushViewController(detailPassVC, animated: true)
}
}
This is the code for the part that passes the id value. detailVC.product = products[indexPath.row]An error occurs in this area. error message is "Thread 1: Fatal error: Index out of range"
var products:[ProductModel] = []
var product = ProductModel()
var productId = Int()
func initViewModel() {
let input = DetailViewModel.Input(loadDetailData: getData.asSignal(onErrorJustReturn: ()), id: productId)
let output = viewModel.transform(input: input)
output.loadDetail.asObservable().bind(onNext: { data in
self.infoLabel.text = data?.detailDescription
self.passView.setData(data!)
self.secondView.setData(data!)
self.fareView.setData(data!)
self.totalLabel.text = totalPrice(data!)
}).disposed(by: disposeBag)
}
This is the code for the part where the view model is bound.
And This is my viewmodel code
class DetailViewModel: BaseViewModel {
private let repo = ProductRepository()
private let disposeBag = DisposeBag()
struct Input {
let loadDetailData: Signal<Void>
let id: Int
}
struct Output {
let loadDetail: Driver<DetailModel?>
let error: Driver<String?>
}
func transform(input: Input) -> Output {
let loadDetail = PublishSubject<DetailModel?>()
let msg= PublishSubject<String?>()
input.loadDetailData.asObservable()
.flatMap { self.productRepo.detailProduct(input.id)}
.subscribe(onNext: { data in
switch data {
case let .success(detail):
loadDetail.onNext(detail)
default:
errorMessage.onNext("load fail")
}
}).disposed(by: disposeBag)
return Output(loadDetail: loadDetail.asDriver(onErrorJustReturn: nil),error: msg.asDriver(onErrorJustReturn: nil))
}
}
How can I send the ID value?

Next object instance IGListKit

Edit: I am new to the field, I did not get a response. Can anyone tell me if I am missing some information? or how I could improve it?
I want an instance from the next object to set a Label text in my current cell
cell.Label.text = talents(nextIndex).name //<- Something of this sort
Tried: passing array input to SectionController to use as
talents[index+1]
Error: File out of range
My Section Controller
class SectionController: ListSectionController {
var talents: Talent!
weak var delegate: SectionControllerDelegate?
}
extension SectionController {
override func sizeForItem(at index: Int) -> CGSize {
guard
let context = collectionContext
else {
return .zero
}
let width = context.containerSize.width
let height = context.containerSize.height
return CGSize(width: width, height: height)
}
override func cellForItem(at index: Int) -> UICollectionViewCell {
guard let cell = collectionContext?.dequeueReusableCellFromStoryboard(withIdentifier: "HomeCell",
for: self,
at: index) as? HomeCell else {
fatalError()
}
cell.nameLabel.text = talents.name
cell.descView.text = talents.largeDesc
cell.shortDesc.text = talents.smallDesc
// let nextTalent = talents[index+1]
// cell.nextIntroFunc(nextlabels: nextTalent)
return cell
}
override func didUpdate(to object: Any) {
talents = object as? Talent
}
}
My ListAdapterDataSource
extension Home: ListAdapterDataSource {
func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
print(talents)
return talents
}
func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any)
-> ListSectionController {
return SectionController()
}
func emptyView(for listAdapter: ListAdapter) -> UIView? {
print("emptyView")
return nil
}
}
talents in the section controller isn't an array. It's just a single Talent, so calling [index + 1] on it is going to throw an error. (You might want to rename the property of the model in the section controller to talent so it's less confusing.)
Best practice for ListSectionController is to keep a 1:1 relationship between your object model and the section controller. If you want to have reference to the next object, you could create a cell/view model layer based on talent that includes reference to the next talent as well.
For example:
class TalentSectionModel: ListDiffable {
let talent: Talent
let nextTalent: Talent?
init(_ talent: Talent, nextTalent: Talent?) {
self.talent = talent
self.nextTalent = nextTalent
}
// whatever properties you want to expose
var name: String {
return talent.name
}
// example of exposing info about next talent
var nextTalentName: String? {
return nextTalent?.name
}
// ListDiffable protocol
// make sure to implement these two so diffing works
func diffIdentifier() -> NSObjectProtocol {}
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {}
}
You could also just only pass in the necessary properties from the next talent without passing in the entire nextTalent if you don't want the section model to store the entire object.
Now with the TalentSectionModel you would map over your talents array in Home and return an array of view models in objects() under ListAdapterDataSource. Then, in your section controller you'll have the next talent's information available to you through the view model.
Resources:
ListKit Modeling and Binding (talks about the ListBindingSectionController, but also helpful to read for general understanding of model and section controller relationship),
ListKit best practices

UISearchBar in UITableView by Rxswift

I use RxSwift to show list of Persons in my tableview, and my tableview has two sections, the first one is old searches and the second one is all Persons. now I don't know how should I filter Persons when users type a name on UISearchBar's textfield.
This is my Person model:
struct PersonModel {
let name: String
let family:String
let isHistory:Bool
}
This is my ContactsViewModel
struct SectionOfPersons {
var header: String
var items: [Item]
}
extension SectionOfPersons: SectionModelType {
typealias Item = PersonModel
init(original: SectionOfPersons, items: [SectionOfPersons.Item]) {
self = original
self.items = items
}
}
class ContactsViewModel {
let items = PublishSubject<[SectionOfPersons]>()
func fetchData(){
var subItems : [SectionOfPersons] = []
subItems.append( SectionOfPersons(header: "History", items: [
SectionOfPersons.Item(name:"Michelle", family:"Obama", isHistory:true ),
SectionOfPersons.Item(name:"Joanna", family:"Gaines", isHistory:true )
]))
subItems.append( SectionOfPersons(header: "All", items: [
SectionOfPersons.Item(name:"Michelle", family:"Obama", isHistory:false ),
SectionOfPersons.Item(name:"James", family:"Patterson", isHistory:false ),
SectionOfPersons.Item(name:"Stephen", family:"King", isHistory:false ),
SectionOfPersons.Item(name:"Joanna", family:"Gaines", isHistory:false )
]))
self.items.onNext( subItems )
}
}
and this is my ContactsViewController:
class ContactsViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var searchBar: UISearchBar!
private lazy var dataSource = RxTableViewSectionedReloadDataSource<SectionOfPersons>(configureCell: configureCell, titleForHeaderInSection: titleForHeaderInSection)
private lazy var configureCell: RxTableViewSectionedReloadDataSource<SectionOfPersons>.ConfigureCell = { [weak self] (dataSource, tableView, indexPath, contact) in
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ContactTableViewCell", for: indexPath) as? ContactTableViewCell else { return UITableViewCell() }
cell.contact = contact
return cell
}
private lazy var titleForHeaderInSection: RxTableViewSectionedReloadDataSource<SectionOfPersons>.TitleForHeaderInSection = { [weak self] (dataSource, indexPath) in
return dataSource.sectionModels[indexPath].header
}
private let viewModel = ContactsViewModel()
private let disposeBag = DisposeBag()
var showContacts = PublishSubject<[SectionOfPersons]>()
var allContacts = PublishSubject<[SectionOfPersons]>()
override func viewDidLoad() {
super.viewDidLoad()
bindViewModel()
viewModel.fetchData()
}
func bindViewModel(){
tableView.backgroundColor = .clear
tableView.register(UINib(nibName: "ContactTableViewCell", bundle: nil), forCellReuseIdentifier: "ContactTableViewCell")
tableView.rx.setDelegate(self).disposed(by: disposeBag)
viewModel.items.bind(to: allContacts).disposed(by: disposeBag)
viewModel.items.bind(to: showContacts).disposed(by: disposeBag)
showContacts.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
searchBar
.rx.text
.orEmpty
.debounce(0.5, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.filter { !$0.isEmpty }
.subscribe(onNext: { [unowned self] query in
////// if my datasource was simple string I cand do this
self.showContacts = self.allContacts.filter { $0.first?.hasPrefix(query) } // if datasource was simple array string, but what about complex custome object?!
})
.addDisposableTo(disposeBag)
}
}
Thanks for your response.
You don't need the two PublishSubjects in your ContactsViewController. You can bind the Observables you obtain from the UISearchBar and your viewModel directly to your UITableView. To filter the contacts with your query you have to filter each section separately. I used a little helper function for that.
So here is what I did
Get rid of the showContacts and allContacts properties
Create an query Observable that emits the text that the user entered into the search bar (don't filter out the empty text, we need that to bring back all contacts when the user deletes the text in the search bar)
Combine the query Observable and the viewModel.items Observable into one Observable
Use this observable to filter all contacts with the query.
Bind that Observable directly to the table view rx.items
I used combineLatest so the table view gets updated whenever the query or viewModel.items changes (I don't know if that list of all contacts is static or if you add / remove contacts).
So now your bindViewModel() code looks like this (I moved the tableView.register(...) to viewDidLoad):
func bindViewModel(){
let query = searchBar.rx.text
.orEmpty
.distinctUntilChanged()
Observable.combineLatest(viewModel.items, query) { [unowned self] (allContacts, query) -> [SectionOfPersons] in
return self.filteredContacts(with: allContacts, query: query)
}
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
Here is the function that filters all contacts using the query:
func filteredContacts(with allContacts: [SectionOfPersons], query: String) -> [SectionOfPersons] {
guard !query.isEmpty else { return allContacts }
var filteredContacts: [SectionOfPersons] = []
for section in allContacts {
let filteredItems = section.items.filter { $0.name.hasPrefix(query) || $0.family.hasPrefix(query) }
if !filteredItems.isEmpty {
filteredContacts.append(SectionOfPersons(header: section.header, items: filteredItems))
}
}
return filteredContacts
}
I assumed that you wanted to check the Persons' name and family against the query.
One more thing: I removed the debounce because you filter a list that is already in memory, which is really fast. You would typically use debounce when typing into the search bar triggers a network request.

When to detach firebase listeners in tableView cell?

In chat app, in order to keep track of each chat's last message and unread messages for each user participating in chat, when the tableView cell is dequeued I am attaching a .childChanged listener on the cell. When the listener is fired, I update the chat label.text on each row for the corresponding chat.
When should I remove these listeners or what is the best practice to update the chat in the cell in my case?
What is the flow of the program?
1. Download current user
2. Download current user chatIDs
3. Download chat for each chatID
4. Populate tableView with chats
5. In each cell observe childChanged at
chats/chat.chatUID/currentUserUID/.observe(.childChanged)
6. If "unreadMessagesCount" was changed, update it on the cell
class ChatTableViewCell: UITableViewCell {
#IBOutlet weak var lastMessageLabel: UILabel!
var chat: Chat! {
didSet{
self.updateUI()
}
}
func updateUI() {
self.chat.observeChildChanged(chat: self.chat, currentUserUID:user.userUID) { (lastMessage, unreadMessagesCount) in
if !lastMessage.isEmpty{
self.lastMessageLabel.text = lastMessage
}
if unreadMessagesCount > 0 {
self.lastMessageLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
self.chatUnreadMessagesCount.text = "\(unreadMessagesCount)"
} else {
self.lastMessageLabel.font = UIFont.systemFont(ofSize: 15.0)
self.chatUnreadMessagesCount.text = ""
}
}
}
}
class MessagesViewController: UITableViewController {
override func viewDidLoad() {
//observe ~/users/uid
DDatabaseRReference.users(uid: uid).reference().observeSingleEvent(of: .value, with: { (snapshot) in
guard snapshot.exists() else {return}
if let userDict = snapshot.value as? [String : Any] {
self.currentUser = UserModel(dictionary: userDict)
self.userWasDownloaded = true //this will trigger the setter and start downloading chatId's of current user
}
})
}
var userWasDownloaded: Bool {
get {
return true
}
set {
self.fetchChatsIdsOf(currentUser: self.currentUser)
self.tableView.reloadData()
}
}
func fetchChatsIdsOf(currentUser: UserModel) {
//get chatIds of currentUser from ~/users/currentUser.userUID/chatIds
DDatabaseRReference.users(uid: currentUser.userUID).reference().child("chatIds").observe(.childAdded, with: { (snapshot) in
let chatUID = snapshot.key
if !self.chatIdsDownloaded.contains(chatUID) {
self.chatIdsDownloaded.append(chatUID)
}
})
}
//after chatIdsDownloaded is set,
//download the new chat for the last chat appended to chatIdsDownloaded array
var chatIdsDownloaded = [String]() {
didSet {
guard let chatID = chatIdsDownloaded.last else {return}
self.downloadNewChat(chatID: chatID)
}
}
func downloadNewChat(chatID: String) {
DDatabaseRReference.chats.reference().child(chatID).observeSingleEvent(of: .value, with: { (snapshot) in
......
self.currentUserChats.insert(chatChecked, at: 0)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChatTableViewCell", for: indexPath) as! ChatTableViewCell
cell.chat = currentUserChats[indexPath.row]
return cell
}
}
chats // <- all chats in the app for all users in the app
-LOMVtcjOEOu2p1apMKV
chatUID: "-LOMVtcjOEOu2p1apMKV"
isGroupChat: true
lastMessage: "Katherine Gregory has joined the group"
lastUpdate: 1539761870.2237191
+users
IN4pgCS5NqQZZLpdmoz1KeDiFqj2
fcmToken: ""
firstName: "Alex"
userUID: "IN4pgCS5NqQZZLpdmoz1KeDiFqj2"
unreadMessagesCount: 5
users // <- all users in the app
IN4pgCS5NqQZZLpdmoz1KeDiFqj2
+chatIds
-LOMVtcjOEOu2p1apMKV: true
- name: ""
- email: ""
...etc
You can check if you added 2 or more observers for cell.
Add breakpoint or print() here:
self.chat.observeChildChanged(chat: self.chat, currentUserUID: user.userUID) { (lastMessage, unreadMessagesCount) in {
//breakpoint or print("observeChildChanged")
...
}
Please, reuse your cell.
Send new message.
If you have 2 or more messages it means that you have not set only one observer.
Perhaps this approach is not perfect, but it can help you (remove old observers before add new):
var chat: Chat! {
didSet {
self.removeOldObserver()
self.updateUI()
}
}
func removeOldObserver() {
...
}
As Jay suggested, I am attaching .childChanged observer on each chat newly downloaded.
However, if I go to firebase console and update the value of a child at ref path, childChanged observer is not always triggered. Sometimes, it works, sometimes it does not What could be the problem?
I am using break points on all lines and none of them is hit when value is changed in database.
example: name:"Alex"
change with: name: "John"
UPDATE
This answer is correct, I had forgotten to delete my previous implementation in which was detaching the listeners in prepareForReuse()
var currentUserChats = [Chat]() {
didSet(newValue){
attachChildChangedObserverOn(chat: newValue)
}
}
var observersArray = [String: UInt]() // chatUID:handle
func attachChildChangedObserverOn(chat: Chat) {
var handle: UInt = 0
let ref = DDatabaseRReference.chats.reference().child(chat.chatUID).child("users").child(currentUser.userUID)
handle = ref.observe(.childChanged, with: {[weak self] (snapshot) in
self?.observersArray[chat.chatUID] = handle
print("snapshot.value is \(snapshot.value) and snapKey is \(snapshot.key)")
guard snapshot.exists() else {return}
let chatChanged = chat
var lastMessage = ""
var unreadMessagesCount = 0
var lastUpdate = 0.0
switch snapshot.key {
//case....
}
})
}

Swift find superview of given class with generics

I guess I'm struggling with generics. I want to create simple UIView extension to find recursively a superview of class passed in the function param. I want the function to return optional containing obviously either nil, or object visible as instance of provided class.
extension UIView {
func superviewOfClass<T>(ofClass: T.Type) -> T? {
var currentView: UIView? = self
while currentView != nil {
if currentView is T {
break
} else {
currentView = currentView?.superview
}
}
return currentView as? T
}
}
Any help much appreciated.
Swift 3/4
This is a more concise way:
extension UIView {
func superview<T>(of type: T.Type) -> T? {
return superview as? T ?? superview.compactMap { $0.superview(of: type) }
}
func subview<T>(of type: T.Type) -> T? {
return subviews.compactMap { $0 as? T ?? $0.subview(of: type) }.first
}
}
Usage:
let tableView = someView.superview(of: UITableView.self)
let tableView = someView.subview(of: UITableView.self)
No need to pass in the type of the class you want (at least in Swift 4.1)…
extension UIView {
func firstSubview<T: UIView>() -> T? {
return subviews.compactMap { $0 as? T ?? $0.firstSubview() as? T }.first
}
}
I'm using this.
// Lookup view ancestry for any `UIScrollView`.
if let scrollView = view.searchViewAnchestors(for: UIScrollView.self) {
print("Found scrollView: \(scrollView)")
}
Extension is really a single statement.
extension UIView {
func searchViewAnchestors<ViewType: UIView>(for viewType: ViewType.Type) -> ViewType? {
if let matchingView = self.superview as? ViewType {
return matchingView
} else {
return superview?.searchViewAnchestors(for: viewType)
}
}
}
With this alternative implementation below, you can actually let the call site determine what type to look for, but I found it somewhat unconventional.
extension UIView {
func searchInViewAnchestors<ViewType: UIView>() -> ViewType? {
if let matchingView = self.superview as? ViewType {
return matchingView
} else {
return superview?.searchInViewAnchestors()
}
}
}
You can call it like this.
if let scrollView: UIScrollView = view.searchInViewAnchestors() {
print("Found scrollView: \(scrollView)")
}