RxSwift - How to reflect the number of item's count to TableView - swift

I'm new to RxSwift. This is quite tricky.
I'm creating like ToDoList that views which are tableView and add-item view are separated by TabBarController.
I have successfully displayed the list array and added a new item into tableView.
I also wanted to display the number of array's count and favourite count in the view that has tableView so that I have displayed it by throwing a value with .just.
But displaying a value based on the result of the array displayed by SearchBar, the value is not reflected as I expected.
In MainViewModel, I made sure if I could get the number of array's count properly by print, but apparently the value was fine.
It is just not reflected in the View.
// Model
struct Item: Codable {
var name = String()
var detail = String()
var tag = String()
var memo = String()
var fav = Bool()
var cellNo = Int()
init(name: String, detail: String, tag: String, memo: String, fav: Bool, celllNo: Int) {
self.name = name
self.detail = detail
self.tag = tag
self.memo = memo
self.fav = fav
self.cellNo = celllNo
}
init() {
self.init(
name: "Apple",
detail: "ringo",
tag: "noun",
memo: "",
fav: false,
celllNo: 0
)
}
}
struct SectionModel: Codable {
var list: [Item]
}
extension SectionModel: SectionModelType {
var items: [Item] {
return list
}
init(original: SectionModel, items: [Item]) {
self = original
self.list = items
}
}
Singleton share class
final class Sharing {
static let shared = Sharing()
var items: [Item] = [Item()]
var list: [SectionModel] = [SectionModel(list: [Item()])] {
didSet {
UserDefault.shared.saveList(list: list)
}
}
let listItems = BehaviorRelay<[SectionModel]>(value: [])
}
extension Sharing {
func calcFavCount(array: [Item]) -> Int {
var count = 0
if array.count > 0 {
for i in 0...array.count - 1 {
if array[i].fav {
count += 1
}
}
}
return count
}
}
// MainTabViewController
class MainTabViewController: UIViewController {
#IBOutlet weak var listTextField: UITextField!
#IBOutlet weak var tagTextField: UITextField!
#IBOutlet weak var itemCountLabel: UILabel!
#IBOutlet weak var favCountLabel: UILabel!
#IBOutlet weak var favIcon: UIImageView!
#IBOutlet weak var infoButton: UIButton!
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var tableView: UITableView!
private lazy var viewModel = MainTabViewModel(
searchTextObservable: searchTextObservable
)
private let disposeBag = DisposeBag()
private var dataSource: RxTableViewSectionedReloadDataSource<SectionModel>!
override func viewDidLoad() {
super.viewDidLoad()
setupTableViewDataSource()
tableViewSetup()
listDetailSetup()
}
// create Observable searchBar.text to pass to ViewModel
var searchTextObservable: Observable<String> {
let debounceValue = 200
// observable to get the incremental search text
let incrementalTextObservable = rx
.methodInvoked(#selector(UISearchBarDelegate.searchBar(_:shouldChangeTextIn:replacementText:)))
.debounce(.milliseconds(debounceValue), scheduler: MainScheduler.instance)
.flatMap { [unowned self] _ in Observable.just(self.searchBar.text ?? "") }
// observable to get the text when the clear button or enter are tapped
let textObservable = searchBar.rx.text.orEmpty.asObservable()
// merge these two above
let searchTextObservable = Observable.merge(incrementalTextObservable, textObservable)
.skip(1)
.debounce(.milliseconds(debounceValue), scheduler: MainScheduler.instance)
.distinctUntilChanged()
return searchTextObservable
}
func setupTableViewDataSource() {
dataSource = RxTableViewSectionedReloadDataSource<SectionModel>(configureCell: {(_, tableView, indexPath, item) in
let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell") as! ListCell
cell.selectionStyle = .none
cell.backgroundColor = .clear
cell.configure(item: item)
return cell
})
}
func tableViewSetup() {
tableView.rx.itemDeleted
.subscribe {
print("delete")
}
.disposed(by: disposeBag)
viewModel.dispItems.asObservable()
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
func listDetailSetup() {
viewModel.itemCountObservable
.bind(to: itemCountLabel.rx.text)
.disposed(by: disposeBag)
viewModel.favCountObservable
.bind(to: favCountLabel.rx.text)
.disposed(by: disposeBag)
}
}
MainTabViewModel
final class MainTabViewModel {
private let disposeBag = DisposeBag()
private let userDefault: UserDefaultManager
var dispItems = BehaviorRelay<[SectionModel]>(value: [])
private let shared = Sharing.shared
// lazy var itemCount = shared.list[0].list.count
// lazy var favCount = shared.calcFavCount
var itemCountObservable: Observable<String>
var favCountObservable: Observable<String>
init(searchTextObservable: Observable<String>,
userDefault: UserDefaultManager = UserDefault()) {
self.userDefault = userDefault
let initialValue = shared.list
shared.listItems.accept(initialValue)
dispItems = shared.listItems
// this part is to display the initil number -> success
var itemCount = shared.list[0].list.count
itemCountObservable = .just(itemCount.description + " items")
var favCount = shared.calcFavCount(array: shared.list[0].list)
favCountObservable = .just(favCount.description)
// this part is based on the searching result -> failure
searchTextObservable.subscribe(onNext: { text in
if text.isEmpty {
let initialValue = self.shared.list
self.shared.listItems.accept(initialValue)
self.dispItems = self.shared.listItems
}else{
let filteredItems: [Item] = self.shared.list[0].list.filter {
$0.name.contains(text)
}
let filteredList = [SectionModel(list: filteredItems)]
self.shared.listItems.accept(filteredList)
self.dispItems = self.shared.listItems
itemCount = filteredItems.count
self.itemCountObservable = .just(itemCount.description + " items")
favCount = self.shared.calcFavCount(array: filteredItems)
self.favCountObservable = .just(favCount.description)
print("\(itemCount) items") // the ideal number is in but not shown in the view
}
})
.disposed(by: disposeBag)
}
}
I removed unnecessary code but I mostly pasted a whole code for your understanding.
Hope you could help me.
Thank you.

I solved this issue anyway; the value was reflected.
the issue was that itemCountObservable was declared as observable and .just was used.
How .just works is to throw onNext once and it is completed, which means the change I did in searchTextObservable.subscribe(onNext~ is unacceptable.
So I shifted the itemCountObservable: Observable<String> to BehaviorRelay<String>that only onNext is thrown and not completed, then it works.
My understanding of this issue is that itemCountObservable: Observable<String> stopped throwing a value due to .just as I've written above.
Am I correct??
If you're familiar with the difference between Observable and BehaviorRelay, it would be appreciated if you could tell me.
Thanks.

Related

Update data using MVVM architecture in swift

I am using MVVM Architecture for learning to develop a small weather application for learning purposes.
View
class ViewController: UIViewController {
private var cityDataViewModel = CityDataViewModel()
private var data = [ConsolidatedWeather]()
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
loadCityData()
}
func loadCityData(){
print("loadCityData")
cityDataViewModel.getCityData {
}
}
}
ViewModel
class CityDataViewModel{
private var networkManager = CityNetworkManager()
private var weatherNetworkManager = WeatherNetworkManager()
var weatherModel = [ConsolidatedWeather]()
var myStruct :[WeatherModel] = []
var weatherState: String?
var minTemp: Double?
var maxTemp: Double?
var currentTemperature: Double?
var summary: String?
var dateString: String = ""
//MARK: - Get cityInformation
func getCityData(completion: #escaping () -> ()) {
networkManager.getCityDataNetworkCall { [weak self](result) in
switch result{
case .success(let information):
information.forEach { (data) in
print("\(data.title) || \(data.locationType) || \(data.woeid) || \(data.lattLong)")
print("loadCityData 3")
self?.getCityWeatherInformation(with: data.woeid)
print("loadCityData 4")
}
completion()
case .failure(let error):
print(error)
}
}
}
//MARK: - Get Weather data
func getCityWeatherInformation(with woeid: Int){
//[weak self]
weatherNetworkManager.getWeatherDataNetworkCall(cityId: woeid) {[weak self] (result) in
print("loadCityData 5")
switch result{
case .success(let listOfData):
self?.weatherModel = listOfData.consolidatedWeather
}
case .failure(let error):
print(error)
}
}
}
var ttile: String{
return weatherState ?? ""
}
}
From the view, I am sending a call to ViewModel to get cityId by using func getCityDat()
After get the cityId I called func getCityWeatherInformation(with woeid: Int) for get details weather data. I am successfully getting those data from server.
How can I send that information to view for updating my viewController?
Setting up a protocol/closure system as mentioned in the comments is certainly a popular option.
As of iOS 13, you also have the option of using Combine to publish the changes on your ViewModel, which can trigger the ViewController to update.
A simplified example:
import Combine
import UIKit
class MyVC : UIViewController {
private var label = UILabel()
private var label2 = UILabel()
private var viewModel = ViewModel()
private var cancellables = Set<AnyCancellable>()
override func viewDidLoad() {
addLabels()
linkPublishers()
viewModel.getData()
}
func linkPublishers() {
//OPTION 1
viewModel.objectWillChange.sink { (_) in
DispatchQueue.main.async {
self.label.text = self.viewModel.text1
self.label2.text = self.viewModel.text2
}
}
.store(in: &cancellables)
// **** OR ****
//OPTION 2
viewModel
.$text1
.receive(on: RunLoop.main)
.sink { (newLabelText) in
self.label.text = newLabelText
}.store(in: &cancellables)
viewModel
.$text2
.receive(on: RunLoop.main)
.sink { (newLabelText) in
self.label2.text = newLabelText
}.store(in: &cancellables)
}
func addLabels() {
label.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
self.view.addSubview(label)
label2.frame = CGRect(x: 0, y: 40, width: 200, height: 40)
self.view.addSubview(label2)
}
}
class ViewModel : ObservableObject {
#Published var text1 = ""
#Published var text2 = ""
func getData() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.text1 = "Hello, world"
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.text2 = "Hello, world 2"
}
}
}
The ViewModel here does a fake task mocking an async network call. Then, it sets one of its #Published properties to the result of that data.
Back in the ViewController, linkPublishers has two different ways of hooking up those published properties:
Observing objectWillChange, which gets triggered before any of the published properties update
Observing each #Published property independently.

How to tell another view controller what song to play that is listed on a table view?

In my PlayerViewController, I am trying to play a song that is selected from my Table view. Also this first line of code is in another view controller. What would be a better way to write the player.playStream line?
EDIT: PlayerViewController
#IBOutlet weak var playPauseButton: UIButton!
var player: Player!
var song: [Song] = []
let indexPath = IndexPath()
var MusicPlayer: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
player = Player()
setSession()
UIApplication.shared.beginReceivingRemoteControlEvents()
becomeFirstResponder()
NotificationCenter.default.addObserver(self, selector: Selector(("handleInterruption")), name: AVAudioSession.interruptionNotification, object: nil)
player.playStream(fileUrl: "http://127.0.0.1/musicfiles" + song[indexPath.row].getName())
changePlayButton()
}
Song.swift:
class Song {
var id = Int()
var name = String()
var year = Int()
var numLikes = Int()
var numPlays = Int()
init?(id:String, name:String, year:String, numlikes:String, numplays:String) {
self.id = Int(id)!
self.name = name
self.year = Int(year)!
self.numLikes = Int(numLikes)
self.numPlays = Int(numplays)!
}
func getId() -> Int {
return id
}
func getName() -> String {
return name
}
func getYear() -> Int {
return year
}
func getNumLikes() -> Int {
return numLikes
}
func getNumPlays() -> Int {
return numPlays
}
}

Viewing initial data source and filtering with RxSwift using MVVM

I'm starting out with RxSwift and trying to get a simple example of filtering a data source with a UISearchController working.
I have the basic setup of a UISearchController wired into a UITableViewController. Using MVVM I also have a basic view model setup that will drive the table.
self.viewModel.searchText.accept(searchController.searchBar.text ?? "")
viewModel.listItems.bind(to: tableView.rx.items(cellIdentifier: "ItemCell")) { row, item, cell in
cell.textLabel!.text = item.name
}
.disposed(by: disposeBag)
View Model
class ListViewModel {
private let sourceItems: BehaviorRelay<[ListItem]> = BehaviorRelay(value: [
ListItem(name: "abc"),
ListItem(name: "def"),
ListItem(name: "ghi"),
ListItem(name: "jkl"),
ListItem(name: "mno")
])
let searchText = BehaviorRelay<String>(value: "")
var listItems: Observable<[ListItem]> = Observable.just([])
init() {
listItems = sourceItems.asObservable()
}
}
I can add in the search filtering and this works such that only the values matching the filter string will show
let searchObservable = searchText
.throttle(.milliseconds(300), scheduler: MainScheduler.instance)
.distinctUntilChanged()
.filter { query in
return query.count > 2
}
.share(replay: 1)
listItems = Observable.combineLatest(sourceItems.asObservable(), searchObservable) { items, query in
return items.filter({ item in
item.name.lowercased().contains(query.lowercased())
})
}
However, this will not show any values until the filter is matched. What I am trying to do is initially show all the values and then only show the filtered values. I'm not quite sure how to populate the listItems when the searchText changes but is empty or events are filtered out.
You forgot to subscribe for changes, instead of
listItems = Observable.combineLatest(sourceItems.asObservable(), searchObservable) { items, query in
return items.filter({ item in
item.name.lowercased().contains(query.lowercased())
})
}
should be
Observable.combineLatest(sourceItems.asObservable(), searchObservable) { items, query in
return items.filter({ item in
item.name.lowercased().contains(query.lowercased())
})
}.subscribe(onNext: { resultArray in
print(resultArray) // here you can change your listItems
})
.disposed(by: disposeBag)
this is how to change searchText searchText.accept("123")
UPDATED:
to handle any searchBar updates you should implement serachBar.rx
Here is some example how to
import UIKit
import RxSwift
import RxCocoa
class ListItem: NSObject {
var name: String = ""
public init(name str: String) {
super.init()
name = str
}
}
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
private let sourceItems: BehaviorRelay<[ListItem]> = BehaviorRelay(value: [
ListItem(name: "abc"),
ListItem(name: "def"),
ListItem(name: "ghi"),
ListItem(name: "jkl"),
ListItem(name: "mno")
])
let searchText = BehaviorRelay<String>(value: "")
var listItems: Observable<[ListItem]> = Observable.just([])
var disposeBag: DisposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
addSearchBarObserver()
listItems = sourceItems.asObservable()
Observable.combineLatest(sourceItems.asObservable(), searchText) { items, query in
return items.filter({ item in
item.name.lowercased().contains(query.lowercased())
})
}.subscribe(onNext: { resultArray in
print(resultArray)
})
.disposed(by: disposeBag)
}
private func addSearchBarObserver() {
searchBar
.rx
.text
.orEmpty
.debounce(.milliseconds(300), scheduler: MainScheduler.instance)
.distinctUntilChanged()
.subscribe { [weak self] query in
guard
let query = query.element else { return }
self?.searchText.accept(query)
}
.disposed(by: disposeBag)
}
}
my approach will be the following
create such observable class as below
import Foundation
class Observable<Generic> {
var value: Generic {
didSet {
DispatchQueue.main.async {
self.valueChanged?(self.value)
}
}
}
private var valueChanged: ((Generic) -> Void)?
init(_ value: Generic) {
self.value = value
}
/// Add closure as an observer and trigger the closure imeediately if fireNow = true
func addObserver(fireNow: Bool = true, _ onChange: ((Generic) -> Void)?) {
valueChanged = onChange
if fireNow {
onChange?(value)
}
}
func removeObserver() {
valueChanged = nil
}
}
have in your VM the original list
create different filtered list as below
var cellViewModels: Observable<[/* your model */]?>
have the search bar delegate method in the View as below - remember it will vary depending on your implementation -
class TradeDealsViewController: UIViewController, UISearchBarDelegate {
// Mark :- IB Outlets
#IBOutlet weak var collectionView: UICollectionView!
private var viewModel: ViewModel /* your VM */?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewModel = TradeDealsViewModel()
bindViewLiveData()
}
private func bindViewLiveData(){
viewModel?.cellViewModels.addObserver({ [weak self] (responsee) in
self?.collectionView.reloadData()
})
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
/* do your filtration logic here */
}
}
note that this solution does not use RxSwift it uses only foundation
With thanks to #AlexandrKolesnik I managed to tweak his answer and get it working:
class ListViewModel {
private let sourceItems: BehaviorRelay<[ListItem]> = BehaviorRelay(value: [
ListItem(name: "abc"),
ListItem(name: "def"),
ListItem(name: "ghi"),
ListItem(name: "jkl"),
ListItem(name: "mno"),
ListItem(name: "abcdef")
])
private var filteredItems: BehaviorRelay<[ListItem]> = BehaviorRelay(value: [])
let searchText = BehaviorRelay<String>(value: "")
var listItems: Observable<[ListItem]> {
return filteredItems.asObservable()
}
private let disposeBag = DisposeBag()
init() {
searchText
.throttle(.milliseconds(300), scheduler: MainScheduler.instance)
.distinctUntilChanged()
.subscribe({ [unowned self] event in
guard let query = event.element, !query.isEmpty else {
self.filteredItems.accept(self.sourceItems.value)
return
}
self.filteredItems.accept(
self.sourceItems.value.filter {
$0.name.lowercased().contains(query.lowercased())
}
)
})
.disposed(by: disposeBag)
}
}

save entity array under another entity CoreDataRelationships

Im working on an app where a merchant can add a bunch of products into a persons tab.
After selecting the customer from a table view the user can see a list of products and the total amount which I'd like to save under that customers name for him to come pay later. I've done a lot of research about relationships in CoreData but have not found a way to save many items at once.
Here is a screenshot of the view controller showing the customer and the products to add to his tab.
Add to tab view controller
I've created the data models and all and everything works great just can't link the products to each customer. I want to be able to click on a customer and see all the products in his tab. I've spent weeks now trying to find an answer and its getting very frustrating. Just need to be able to save and retrieve the items and my app will be done.
Really looking forward to an answer!
import UIKit
import MapKit
import GoogleSignIn
import CoreData
class addToTabViewController: UIViewController {
// Data Arrays
var myCart = [Cart]()
var myCartUz: [Cart] = []
var selectedIndex: Int!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var amount: String = ""
var transaction: String = ""
#IBOutlet weak var profilePicture: UIImageView!
#IBOutlet weak var customerName: UILabel!
#IBOutlet weak var phoneNumber: UILabel!
#IBOutlet weak var emailAddress: UILabel!
#IBOutlet weak var customerAddress: UILabel!
#IBOutlet weak var profileView: UIView!
#IBOutlet weak var map: MKMapView!
#IBOutlet weak var receiptView: UIView!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var customerProfile: UIImageView!
#IBOutlet weak var customerProfileView: UIView!
#IBOutlet weak var totalAmount: UILabel!
#IBOutlet weak var merchantName: UILabel!
#IBOutlet weak var merchatEmail: UILabel!
// Variable
var customers: Cutomers!
override func viewDidLoad() {
super.viewDidLoad()
// Show data
configureEntryData(entry: customers)
fetchCartData()
totalAmount.text = amount
// Design parameters
hutzilopochtli()
}
// Info profile button
#IBAction func infoButton(_ sender: Any) {
profileView.isHidden = !profileView.isHidden
receiptView.isHidden = !receiptView.isHidden
customerProfileView.isHidden = !customerProfileView.isHidden
}
// Add to tab button
#IBAction func addToTabButton(_ sender: Any) {
}
// Show customer details
func configureEntryData(entry: Cutomers) {
let name = entry.name
let address = entry.address
let phone = entry.phoneNumber
let email = entry.email
customerName!.text = name
customerAddress!.text = address
phoneNumber!.text = phone
emailAddress!.text = email
self.title = name
let image = entry.profileicture as Data?
profilePicture!.image = UIImage(data: image!)
customerProfile!.image = UIImage(data: image!)
}
// Get cart data
func fetchCartData() {
do {
myCart = try context.fetch(Cart.fetchRequest())
myCartUz = myCart
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
}
merchantName?.text = GIDSignIn.sharedInstance().currentUser.profile.name
merchatEmail?.text = GIDSignIn.sharedInstance().currentUser.profile.email
}
// Design parameters function
func hutzilopochtli(){
profilePicture.roundMyCircle()
customerProfile.roundMyCircle()
profileView.layer.cornerRadius = 15
receiptView.layer.cornerRadius = 15
profileView.isHidden = true
map.layer.cornerRadius = 13
}
}
// Table view dataSource and delegates
extension addToTabViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myCartUz.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discountCell", for: indexPath) as! discountTableViewCell
let price = myCartUz[indexPath.row].price
let xNSNumber = price as NSNumber
cell.productName?.text = myCartUz[indexPath.row].product
cell.amountLabel?.text = "IDR \(xNSNumber.stringValue)"
return cell
}
}
Here is the customer class
class constantCustomer: NSObject {
private class func getContext() -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
class func saveObject(customerId: String, name: String, phone: String, address: String, email: String, picture: NSData) -> Bool {
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "Cutomers", in: context)
let managedObject = NSManagedObject(entity: entity!, insertInto: context)
managedObject.setValue(customerId, forKey: "customerID")
managedObject.setValue(NSDate(), forKey: "date")
managedObject.setValue(name, forKey: "name")
managedObject.setValue(phone, forKey: "phoneNumber")
managedObject.setValue(address, forKey: "address")
managedObject.setValue(email, forKey: "email")
managedObject.setValue(picture, forKey: "profileicture")
do {
try context.save()
return true
} catch {
return false
}
}
class func fetchObject() -> [Cutomers]? {
let context = getContext()
var myCustomers: [Cutomers]? = nil
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Cutomers")
let sort = NSSortDescriptor(key: "date", ascending: true)
fetchRequest.sortDescriptors = [sort]
do {
myCustomers = try context.fetch(Cutomers.fetchRequest())
return myCustomers
} catch {
return myCustomers
}
}
}
Without knowing about the Customers class I can only create an example. This is for the saving process:
func saveCustomer(entry: Customers) {
let entity = NSEntityDescription.entity(forEntityName: "EntityName", in: viewContext)
let customer = Customers(entity: entity!, insertInto: viewContext)
// add data to your customer class
customer.price = price
for journalEntry in entry.entry {
/// Your class with the Relationship
let persistent = CustomersDetail(context: viewContext)
persistent.question = journalEntry.question
persistent.answer = journalEntry.answer
customer.addToRelationship(persistent)
}
/// do saving
do {
try viewContext.save()
} catch let error {
print(error.localizedDescription)
}
}
loading Customer for a specific CostumerName:
func loadCustomerData(customerName: String) -> Customers {
let fetch:NSFetchRequest<Customers> = Customers.fetchRequest()
fetch.predicate = NSPredicate(format: "customerName = %#", "\(customerName)")
var customer = [Customers]()
do {
customer = try viewContext.fetch(fetch)
} catch let error {
print(error.localizedDescription)
}
return customer
}
enter image description here

How would I write a test to make sure the UIbutton "Show all Providers" turns up when there's more than 12 or more items in the table view?

So I'm completely new to testing and I just needed some help figuring out for example how I would write a test for each of the three cases in the enum of the View Model (none, dontSeeProvider, showAllProviders).
enum ProvidersButtonType {
case none, dontSeeProvider, showAllProviders
}
I haven't been able to figure out how to write a test for cases "showAllProviders" and "dontSeeProviders".
This is the View Model:
import RxSwift
import RxCocoa
struct TopProvidersPickerItem {
let provider: MVPD
let logoImage: Observable<UIImage>
init(provider: MVPD, imageLoader: DecodableProviding) {
self.init(provider: provider, logoImage: imageLoader.image(fromURL: provider.logoUrl))
}
init(provider: MVPD, logoImage: Observable<UIImage>) {
self.provider = provider
self.logoImage = logoImage.catchErrorJustReturn(UIImage())
}
}
enum ProvidersButtonType {
case none, dontSeeProvider, showAllProviders
}
struct TopProvidersPickerViewModel {
var caption: String {
return "Get access to more full episodes by signing in with your TV Provider"
}
let buttonType = Variable<ProvidersButtonType>(.none)
let items: Observable<[TopProvidersPickerItem]>
let selectedItem: PublishSubject<TopProvidersPickerItem> = PublishSubject()
let showAllProvidersTrigger: PublishSubject<Void> = PublishSubject()
let mvpdPicked: Observable<MVPD>
init(topProviders: Observable<[MVPD]>, imageLoader: DecodableProviding) {
let items = topProviders.map({ mvpds in
return mvpds.map { mvpd in
TopProvidersPickerItem(provider: mvpd, imageLoader: imageLoader)
}
})
self.init(items: items)
}
init(items: Observable<[TopProvidersPickerItem]>) {
self.items = items
mvpdPicked = selectedItem.map { $0.provider }
let buttonType = items.map { (array) -> ProvidersButtonType in
if array.count > 12 {
return .showAllProviders
} else {
return .dontSeeProvider
}
}
buttonType.bind(to: self.buttonType)
}
}
This is the View Controller:
import UIKit
import RxCocoa
import RxSwift
public class ProviderCollectionViewCell: UICollectionViewCell {
#IBOutlet public private(set) weak var imageView: UIImageView!
}
public class TopProvidersPickerViewController: UIViewController,
ViewModelHolder {
var viewModel: TopProvidersPickerViewModel! = nil
private let bag = DisposeBag()
#IBOutlet public private(set) weak var collectionView: UICollectionView!
#IBOutlet public private(set) weak var captionLabel: UILabel!
#IBOutlet weak var viewAllProvidersButton: UIButton!
override public func viewDidLoad() {
super.viewDidLoad()
captionLabel.text = viewModel.caption
setupRx()
}
private func setupRx() {
viewModel.buttonType.asObservable().subscribe(onNext: { [button = self.viewAllProvidersButton] type in
button?.isHidden = false
switch type {
case .none:
button?.isHidden = true
case .dontSeeProvider:
button?.setTitle("Don't see provider", for: .normal)
case .showAllProviders:
button?.setTitle("Show all providers", for: .normal)
}
})
.disposed(by: bag)
viewModel.items
.bind(to: collectionView
.rx
.items(cellIdentifier: "ProviderCell", cellType: ProviderCollectionViewCell.self)) { [ unowned self ] _, item, cell in
item.logoImage.bind(to: cell.imageView.rx.image).addDisposableTo(self.bag)
}
.addDisposableTo(bag)
collectionView
.rx
.modelSelected(TopProvidersPickerItem.self)
.bind(to: self.viewModel.selectedItem)
.addDisposableTo(bag)
viewAllProvidersButton
.rx
.tap
.bind(to: self.viewModel.showAllProvidersTrigger)
.addDisposableTo(bag)
}
}
I wrote a test for the "none" case, but haven't been able to figure out the other two cases:
import FBSnapshotTestCase
import OHHTTPStubs
import RxSwift
#testable import AuthSuite
class TopProvidersPickerViewControllerTests: FBSnapshotTestCase,
ProvidersViewControllerTests {
override func setUp() {
super.setUp()
recordMode = true
}
func testDoesNotShowButtonWhenLoadingProviders() {
let viewModel = TopProvidersPickerViewModel(items: .never())
let controller = TopProvidersPickerViewController.instantiateViewController(with: viewModel)
presentViewController(controller)
FBSnapshotVerifyView(controller.view)
}
I've never used FB Snapshot Tester. I'm going to have to look into that.
Here's how I would do it:
I wouldn't expose the enum to the ViewController. setupRx() would contain this instead:
private func setupRx() {
viewModel.buttonTitle
.bind(to: viewAllProvidersButton.rx.title(for: .normal))
.disposed(by: bag)
viewModel.buttonHidden
.bind(to: viewAllProvidersButton.rx.isHidden)
.disposed(by: bag)
// everything else
}
Then to test the title of the button, for example, I would use these tests:
import XCTest
import RxSwift
#testable import RxPlayground
class TopProvidersPickerViewModelTests: XCTestCase {
func testButtonTitleEmptyItems() {
let topProviders = Observable<[MVPD]>.just([])
let decodableProviding = MockDecodableProviding()
let viewModel = TopProvidersPickerViewModel(topProviders: topProviders, imageLoader: decodableProviding)
var title: String = ""
_ = viewModel.buttonTitle.subscribe(onNext: { title = $0 })
XCTAssertEqual(title, "Don't see provider")
}
func testButtonTitle12Items() {
let topProviders = Observable<[MVPD]>.just(Array(repeating: MVPD(), count: 12))
let decodableProviding = MockDecodableProviding()
let viewModel = TopProvidersPickerViewModel(topProviders: topProviders, imageLoader: decodableProviding)
var title: String = ""
_ = viewModel.buttonTitle.subscribe(onNext: { title = $0 })
XCTAssertEqual(title, "Don't see provider")
}
func testButtonTitle13Items() {
let topProviders = Observable<[MVPD]>.just(Array(repeating: MVPD(), count: 13))
let decodableProviding = MockDecodableProviding()
let viewModel = TopProvidersPickerViewModel(topProviders: topProviders, imageLoader: decodableProviding)
var title: String = ""
_ = viewModel.buttonTitle.subscribe(onNext: { title = $0 })
XCTAssertEqual(title, "Show all providers")
}
}
class MockDecodableProviding: DecodableProviding {
// nothing needed for these tests.
}