Show image in Xcode based on Firestore boolean value - ios

I'm new to coding and could really use your help!
I am trying to show a 'bestseller' image on a product based on a boolean.
I am using Firestore for the database.
I have managed to get the value of the 'bestseller' field on all the documents, but I don't know what to do next.
This is my code so far. This shows the bestsellerImg on all of the products - instead of only the ones where the value = "True"
Here are two pictures to show what i mean :)
the swift file/class "ProductsVC" is controlling the ViewController with the collectionView in it.
Code from "ProductsVC"
import UIKit
import Firebase
class ProductsVC: UIViewController, ProductCellDelegate {
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var categoryName: UILabel!
var products = [Product]()
var category: Category!
var db : Firestore!
var listener : ListenerRegistration!
override func viewDidLoad() {
super.viewDidLoad()
db = Firestore.firestore()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: Identifiers.ProductCell, bundle: nil), forCellWithReuseIdentifier: Identifiers.ProductCell)
setQuery()
categoryName.text = category.name
}
func setQuery() {
var ref: Query!
ref = db.products(category: category.id)
listener = ref.addSnapshotListener({ (snap, error) in
if let error = error {
debugPrint(error.localizedDescription)
}
snap?.documentChanges.forEach({ (change) in
let data = change.document.data()
let product = Product.init(data: data)
switch change.type {
case .added:
self.onDocumentAdded(change: change, product: product)
case .modified:
self.onDocumentModified(change: change, product: product)
case .removed:
self.onDoucmentRemoved(change: change)
}
})
})
}
func productAddToCart(product: Product) {
if UserService.isGuest {
self.simpleAlert(title: "Hej!", msg: "Man kan kun tilføje ting til sin kurv hvis man er oprettet som Fender-bruger. ")
return
}
PaymentCart.addItemToCart(item: product)
self.addedtocart(title: "Tilføjet til kurv!", msg: "")
}
}
extension ProductsVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func onDocumentAdded(change: DocumentChange, product: Product) {
let newIndex = Int(change.newIndex)
products.insert(product, at: newIndex)
collectionView.insertItems(at: [IndexPath(item: newIndex, section: 0)])
}
func onDocumentModified(change: DocumentChange, product: Product) {
if change.oldIndex == change.newIndex {
let index = Int(change.newIndex)
products[index] = product
collectionView.reloadItems(at: [IndexPath(item: index, section: 0)])
} else {
let oldIndex = Int(change.oldIndex)
let newIndex = Int(change.newIndex)
products.remove(at: oldIndex)
products.insert(product, at: newIndex)
collectionView.moveItem(at: IndexPath(item: oldIndex, section: 0), to: IndexPath(item: newIndex, section: 0))
}
}
func onDoucmentRemoved(change: DocumentChange) {
let oldIndex = Int(change.oldIndex)
products.remove(at: oldIndex)
collectionView.deleteItems(at: [IndexPath(item: oldIndex, section: 0)])
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
products.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ProductCell, for: indexPath) as? ProductCell {
cell.configureCell(product: products[indexPath.item], delegate: self)
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = DetailProductVC()
let selectedProduct = products[indexPath.item]
vc.product = selectedProduct
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = view.frame.width
let cellWidth = (width - 30) / 3
let cellHeight = cellWidth * 2.1
return CGSize(width: cellWidth, height: cellHeight)
}
}
My struct
import Foundation
import FirebaseFirestore
struct Product {
var name: String
var id: String
var category: String
var price: Double
var productDescription: String
var imageUrl: String
var timeStamp: Timestamp
var inStore: Int
var bestseller: Bool
var quantity: Int
init(
name: String,
id: String,
category: String,
price: Double,
productDescription: String,
imageUrl: String,
timeStamp: Timestamp = Timestamp(),
inStore: Int,
bestseller: Bool,
quantity: Int) {
self.name = name
self.id = id
self.category = category
self.price = price
self.productDescription = productDescription
self.imageUrl = imageUrl
self.timeStamp = timeStamp
self.inStore = inStore
self.bestseller = bestseller
self.quantity = quantity
}
init(data: [String: Any]) {
name = data["name"] as? String ?? ""
id = data["id"] as? String ?? ""
category = data["category"] as? String ?? ""
price = data["price"] as? Double ?? 0.0
productDescription = data["productDescription"] as? String ?? ""
imageUrl = data["imageUrl"] as? String ?? ""
timeStamp = data["timeStamp"] as? Timestamp ?? Timestamp()
inStore = data["inStore"] as? Int ?? 0
bestseller = data["bestseller"] as? Bool ?? true
quantity = data["quantity"] as? Int ?? 0
}
static func modelToData(product: Product) -> [String: Any] {
let data : [String: Any] = [
"name" : product.name,
"id" : product.id,
"category" : product.category,
"price" : product.price,
"productDescription" : product.productDescription,
"imageUrl" : product.imageUrl,
"timeStamp" : product.timeStamp,
"inStore" : product.inStore,
"bestseller" : product.bestseller,
"quantity" : product.quantity
]
return data
}
}
extension Product : Equatable {
static func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.id == rhs.id
}
}
Code from "ProductCell"
import UIKit
import Kingfisher
import Firebase
protocol ProductCellDelegate : class {
func productAddToCart(product: Product)
}
class ProductCell: UICollectionViewCell{
#IBOutlet weak var imgView: UIImageView!
#IBOutlet weak var titleLbl: UILabel!
#IBOutlet weak var priceLbl: UILabel!
#IBOutlet weak var bestsellerImg: UIImageView!
var db: Firestore?
weak var delegate : ProductCellDelegate?
private var product: Product!
override func awakeFromNib() {
super.awakeFromNib()
imgView.layer.cornerRadius = 5
getInStore()
}
func getInStore() {
Firestore.firestore().collection("products").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents{
var isBestseller = document.get("bestseller")
}
}
}
}
func configureCell(product: Product, delegate: ProductCellDelegate) {
self.product = product
self.delegate = delegate
titleLbl.text = product.name
if let url = URL(string: product.imageUrl) {
let placeholder = UIImage(named: "Fender")
imgView.kf.indicatorType = .activity
let options : KingfisherOptionsInfo =
[KingfisherOptionsInfoItem.transition(.fade(0.1))]
imgView.kf.setImage(with: url, placeholder: placeholder, options: options)
}
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = "DKK"
if let price = formatter.string(from: product.price as NSNumber) {
priceLbl.text = price
}
}
#IBAction func addToCart(_ sender: Any) {
delegate?.productAddToCart(product: product)
}
}

Looking at the code, there may be a pretty simple solution that would simplify what you're trying to do.
Let me walk through it and then make a suggestion:
The tableView datasource is populated in the setQuery function with
func setQuery() {
...
snap?.documentChanges.forEach({ (change) in
let data = change.document.data()
let product = Product.init(data: data)
Each product knows if it's a best seller because it has a best seller property which is either true or false. So when the products are loaded from firebase, that property is set with the Product.init.
As your tableView delegate is creating each cell
cell.configureCell(product: products[indexPath.item]
why don't you just have a piece of code in ProductCell that says if the product is a bestSeller then use the bestSeller image, else use the regular image?
func configureCell(product: Product, delegate: ProductCellDelegate) {
self.product = product
self.delegate = delegate
if self.product.bestSeller == true {
//set the image the bestseller image
} else {
//set the image to the regular image
}

Related

How to pass data from ViewModel to UICollectionViewCell?

I have ViewModel for getting data from API. I want to pass this data to my UICollectionViewCell and show it on my ViewController but I don't know how.
I'm trying to delete extra information and leave useful information in code below:
My ViewModel:
class DayWeatherViewModel {
let url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid=APIKEY"
func viewDidLoad() {
getData(from: url)
}
func getData(from url: String) {
guard let url = URL(string: url) else {
print("Failed to parse URL")
return
}
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
print("something went wrong")
return
}
var result: CitiesWeather?
do {
result = try JSONDecoder().decode(CitiesWeather.self, from: data)
self.weatherDidChange?(result!)
}
catch {
print("failed to convert \(error)")
}
guard let json = result else {
return
}
print(json.coord?.latitude)
print(json.coord?.longitude)
print(json.weather)
print(json.wind?.speed)
}
task.resume()
}
var weatherDidChange: ((CitiesWeather) -> Void)?
}
My UICollectionViewCell:
class DayWeatherCell: UICollectionViewCell, UIScrollViewDelegate {
struct Model {
let mainTemperatureLabel: Double
}
var mainTemperatureLabel: UILabel = {
let label = UILabel()
label.font = UIFont(name: "Rubik-Medium", size: 36)
label.text = "10"
label.textColor = .white
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func configure(with model: Model) {
mainTemperatureLabel.text = String(model.mainTemperatureLabel)
}
My ViewController:
class MainScrenenViewController: UIViewController {
let viewModel: DayWeatherViewModel
private var main: Double? {
didSet {
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.register(DayWeatherCell.self, forCellWithReuseIdentifier: "sliderCell")
collectionView.layer.cornerRadius = 5
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = UIColor(red: 0.125, green: 0.306, blue: 0.78, alpha: 1)
return collectionView
}()
init(viewModel: DayWeatherViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.dataSource = self
collectionView.delegate = self
setupConstraints()
viewModel.weatherDidChange = { result in
self.main = result.main?.temp
}
viewModel.viewDidLoad()
}
extension MainScrenenViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sliderCell", for: indexPath) as! DayWeatherCell
if let myd: String? = String(main ?? 1.1) {
cell.mainTemperatureLabel.text = myd
}
return cell
}
}
extension MainScrenenViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
}
My Struct for parse JSON:
struct CitiesWeather: Decodable {
let coord : Coordinate?
let cod, visibility, id : Int?
let name : String?
let base : String?
let weather: [Weather]?
let sys: Sys?
let main: Main?
let wind: Wind?
let clouds: Clouds?
let dt: Date?
var timezone: Int?
}
struct Coordinate: Decodable {
var longitude: Double?
var latitude: Double?
}
struct Weather: Decodable {
let id: Int?
let main: MainEnum?
let description: String?
let icon: String?
}
struct Sys : Decodable {
let type, id : Int?
let sunrise, sunset : Date?
let message : Double?
let country : String?
}
struct Main : Decodable {
let temp, tempMin, tempMax : Double?
let pressure, humidity : Int?
}
struct Wind : Decodable {
let speed : Double?
let deg : Int?
}
struct Clouds: Decodable {
let all: Int?
}
enum MainEnum: String, Decodable {
case clear = "Clear"
case clouds = "Clouds"
case rain = "Rain"
}

Saving document ID so it can be used throughout the ViewController

I am new to Swift development, so sorry if this is a stupid question. I'm having issues with saving the Firestore document ID to the cell of my to do.
My goal:
Save the document ID of the to-do so it can be used in my ChangeButton protocol.
The app is a to-do list-style app. The changeButton refers to changing the button from an empty circle to a filled circle.
My cellForRowAt in my mainViewController:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 && indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "inputCell", for: indexPath) as! InputCell
cell.delegate = self
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskCell
let current = sections[indexPath.section].items[indexPath.row]
cell.taskNameLabel.text = current.name
if current.checked {
cell.checkBoxOutlet.setBackgroundImage(#imageLiteral(resourceName: "checkBoxFILLED "), for: UIControl.State.normal)
} else {
cell.checkBoxOutlet.setBackgroundImage(#imageLiteral(resourceName: "checkBoxOUTLINE "), for: UIControl.State.normal)
}
cell.delegate = self
cell.items = sections[indexPath.section].items
cell.indexSection = indexPath.section
cell.indexRow = indexPath.row
cell.itemID = sections[indexPath.section].items[indexPath.row].itemID
// print("cell.itemID is \(cell.itemID)")
// print("sections.itemID is \(sections[indexPath.section].items[indexPath.row].itemID)")
return cell
}
}
My changeButton function in mainViewController
func changeButton(state: Bool, indexSection: Int?, indexRow: Int?, itemID: String?) {
print("The item ID is \(itemID)")
print("The item ID section is \(sections[indexSection!].items[indexRow!].itemID)")
sections[indexSection!].items[indexRow!].checked = state
print("Line 175 ID is \(itemID)")
if let itemID = itemID {
let itemRef = db.collection(K.FStore.lists).document(currentListID!).collection(K.FStore.sections).document("\(indexSection!)").collection(K.FStore.items).document(itemID)
if sections[indexSection!].items[indexRow!].checked {
itemRef.updateData([
K.FStore.isChecked : true,
K.FStore.checkedBy: currentUserID!
]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
} else {
itemRef.updateData([
K.FStore.isChecked : false
]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
}
} else {
print("No item ID")
}
tableView.reloadData()
}
My loadItems and loadFunctions in my mainViewController
func loadItems(listID: String, section: Int) {
let itemRef = db.collection(K.FStore.lists).document(listID).collection(K.FStore.sections).document("(section)").collection(K.FStore.items)
var itemArray = Task
itemRef.getDocuments() { (querySnapshot, error) in
if let error = error {
print("Error getting documents: \(error)")
} else {
for document in querySnapshot!.documents {
let name = document.data()["name"] as? String
let isChecked : Bool = (document.data()["isChecked"] != nil)
let newItem = Task(name: name ?? "FIREBASE ERROR", isChecked: isChecked)
itemArray.append(newItem)
// print(newItem.checked)
}
}
// print(itemArray)
self.sections[section].items = itemArray
self.tableView.reloadData()
}
}
//MARK: - Load sections
func loadSections(listID: String) {
let listRef = db.collection(K.FStore.lists).document(listID)
listRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
let sectionNames = document.data()!["sections"] as? [String]
if let sectionNames = sectionNames {
for (index, item) in sectionNames.enumerated() {
let newSection = Section(name: item, isExpanded: true, items: [])
self.sections.append(newSection)
self.loadItems(listID: listID, section: index)
}
}
self.tableView.reloadData()
} else {
print("Document does not exist")
}
}
}
My Task class
class Task {
var name = ""
var checked = false
var date = Date()
var category: String
var number: Int
var itemID: String?
My TaskCell
protocol ChangeButton {
func changeButton(state: Bool, indexSection: Int?, indexRow: Int?, itemID: String?)
}
class TaskCell: UITableViewCell {
#IBAction func checkBoxAction(_ sender: Any) {
// print("The item ID is \(itemID)")
if items![indexRow!].checked {
delegate?.changeButton(state: false, indexSection: indexSection!, indexRow: indexRow!, itemID: itemID)
print("Line 22 \(itemID)")
} else {
delegate?.changeButton(state: true, indexSection: indexSection!, indexRow: indexRow!, itemID: itemID)
print("Line 25 \(itemID)")
}
}
#IBOutlet weak var taskNameLabel: UILabel!
#IBOutlet weak var checkBoxOutlet: UIButton!
var delegate: ChangeButton?
var indexSection: Int?
var indexRow: Int?
var tasks: [[Task]]?
var items: [Task]?
var itemID: String?
}
I am completely lost in how I can fix this. As you can see, I've tried a lot of print statements to figure out where the itemID can be loaded.
This is what I get back from those print statements:
Line 25 nil
The item ID is nil
The item ID section is nil
Line 175 ID is nil
No item ID
Line 22 nil
Please let me know if I forgot to include anything, and sorry for this extremely long post.
Thanks a ton,
Matt
Firstly,
We don't trust cells because it's reusable so we need a static class.
When cell will be reuse you can lost your data
I create some examples. Maybe it'll be helpful
// This is our model
class Task {
var name = ""
var checked = false
var date = Date()
var category: String
var number: Int
var itemID: String?
public init() {
self.category = ""
self.number = 0
}
}
// Extension for init from firebase response
extension Task {
convenience init(with firebase: [String: Any]) {
self.init()
self.name = (firebase["name"] as? String) ?? ""
}
}
// We create service for document
// We use this service like an API
final class DocumentService {
static let shared = DocumentService()
private let database: FirebaseDatabase
private var tasks: [[Task]] = []
public init(database: FirebaseDatabase = FirebaseDatabase()) {
self.database = database
}
func load(in section: Int, completion: #escaping (([Task]) -> Void)) {
database.loadData(section: section) { [unowned self] tasks in
self.tasks[section] = tasks.map(Task.init)
completion(self.tasks[section])
}
}
func check(at indexPath: IndexPath, isChecked: Bool) {
tasks[indexPath.section][indexPath.row].checked = isChecked
}
}
// We create firebase database class we can add some features in here
final class FirebaseDatabase {
func loadData(section: Int, completion: #escaping (([[String: Any]]) -> Void)) {
// TODO: firebase load data
let response: [[String: Any]] = [
["name": "Stackoverflow"]
]
completion(response)
}
}
final class TestController: UIViewController {
private let service = DocumentService.shared
override func viewDidLoad() {
super.viewDidLoad()
service.load(in: 0) { tasks in
// TODO
}
}
}
Thank you for your answer, #Vicaren. Fortunately, the solution was more simple than that. In the end, I found that I forgot to pass in the itemID argument in the loadItems() function. Thank you.

why my method from protocol delegate pattern triggered automatically when updating stepper inside collection view cell?

I am trying to make an app that can save a product to a cart (order) using collection view, there is a stepper inside each collection view cell.
here is the project file in the google drive: https://drive.google.com/file/d/1Ax7VLpI8Vb2jFSJxn_Ss_TjEECbDt-fs/view?usp=sharing
and here is the screenshot of the app (please ignore the ugly UI):
there are 6 products displayed in the collection view cell. each collection view cell has name label, add to cart button and a stepper.
add to cart button and the stepper has the same size and located at the same place, once the add to cart button is tapped, then it will hide the add to cart button and it will show up the stepper like the gif in here:
http://g.recordit.co/DAKMvFwQs4.gif
I use the code below for my Collection view cell, I pass the data using protocol and delegate pattern :
protocol OrderCellDelegate {
func addToCartButtonDidTapped(at selectedIndexPath: IndexPath)
func stepperDidTapped(at selectedIndexPath:IndexPath, counterValue: Int)
}
class OrderCell: UICollectionViewCell {
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var stepper: GMStepper!
#IBOutlet var addToCartButton: UIButton!
var productData : Product? {
didSet {
setStepper()
updateUI()
}
}
var indexPath: IndexPath?
var delegate: OrderCellDelegate?
#IBAction func AddToCartButtonDidTapped(_ sender: Any) {
guard let selectedIndexPath = indexPath else {return}
self.delegate?.addToCartButtonDidTapped(at: selectedIndexPath)
}
#IBAction func stepperButtonDidTapped(_ sender: Any) {
guard let selectedIndexPath = indexPath else {return}
let value = Int(stepper.value)
self.delegate?.stepperDidTapped(at: selectedIndexPath, counterValue: value)
}
func setStepper() {
guard let product = productData else {return}
stepper.value = Double(product.quantityInCart)
stepper.autorepeat = false
stepper.minimumValue = 0
stepper.maximumValue = 100
stepper.stepValue = 1.0
}
func updateUI() {
guard let product = productData else {return}
nameLabel.text = product.name
setCartAndStepperButton()
}
private func setCartAndStepperButton() {
guard let selectedProduct = productData else {return}
func showStepperButton(status: Bool) {
// to decide whether to show stepper or add to cart button.
stepper.isHidden = !status
stepper.isEnabled = status
addToCartButton.isHidden = status
addToCartButton.isEnabled = !status
}
if selectedProduct.quantityInCart == 0 {
showStepperButton(status: false)
} else {
showStepperButton(status: true)
stepper.value = Double(selectedProduct.quantityInCart)
}
}
}
and here is the code for my view controller:
class ViewController: UIViewController, OrderCellDelegate {
#IBOutlet weak var collectionView: UICollectionView!
var order : Order!
var allProducts = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
order = Order.getOrderFromRealmDatabase()
allProducts = Product.fetchProductData()
}
func addToCartButtonDidTapped(at selectedIndexPath: IndexPath) {
print("A")
let selectedProduct = allProducts[selectedIndexPath.item]
Order.addProductToOrderRealmDatabase(userOrder: order, selectedProduct: selectedProduct)
collectionView.reloadData()
}
func stepperDidTapped(at selectedIndexPath: IndexPath, counterValue: Int) {
print("B")
}
}
// DATA SOURCE
extension ViewController : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return allProducts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "orderCell", for: indexPath) as! OrderCell
cell.productData = allProducts[indexPath.item]
cell.delegate = self
cell.indexPath = indexPath
return cell
}
}
and the problem is .....
when I tap the add to cart button, it will trigger addToCartButtonDidTapped on my view controller. but... I don't understand why when I tap the add to cart button, it will also trigger the stepperDidTapped method
so when addToCartButtonDidTapped , it will automatically triggered stepperDidTapped method. print("A") and print("B") are triggered at the same time.
as you can see the stepperDidTapped method should only triggred when I tap the stepper not the add to cart button, I use 2 differents method on OrderCellDelegate
I don't understand why. Do I have a wrong way to implement stepper inside the collection view cell like that ? to be honest, I am not sure with my code to implement stepper inside the collection view above.
here is the file of the project: https://drive.google.com/file/d/1Ax7VLpI8Vb2jFSJxn_Ss_TjEECbDt-fs/view?usp=sharing
here is the Product model:
import RealmSwift
class Product : Object {
#objc dynamic var productID : Int = 0
#objc dynamic var name : String = ""
#objc dynamic var unitPrice: Double = 0.0
#objc dynamic var quantityInCart : Int = 0
#objc dynamic var descriptionProduct : String = ""
#objc dynamic var hasBeenAddedToCart : Bool = false
override static func primaryKey() -> String? {
return "productID"
}
convenience init(productID: Int, name: String, unitPrice: Double, descriptionProduct: String) {
self.init()
self.productID = productID
self.name = name
self.unitPrice = unitPrice
self.descriptionProduct = descriptionProduct
}
static func fetchProductData() -> [Product] {
let product1 = Product(productID: 1, name: "Product1", unitPrice: 1000, descriptionProduct: "Description of Product 1")
let product2 = Product(productID: 2, name: "Product2", unitPrice: 2000, descriptionProduct: "Description of Product 2")
let product3 = Product(productID: 3, name: "Product3", unitPrice: 3000, descriptionProduct: "Description of Product 3")
let product4 = Product(productID: 4, name: "Product4", unitPrice: 4000, descriptionProduct: "Description of Product 4")
let product5 = Product(productID: 5, name: "Product5", unitPrice: 5000, descriptionProduct: "Description of Product 5")
let product6 = Product(productID: 6, name: "Product6", unitPrice: 6000, descriptionProduct: "Description of Product 6")
return [product1,product2,product3,product4,product5,product6]
}
static func changeProductQuantityInRealmDatabase(selectedProduct: Product, quantity: Int) {
guard let selectedProductInRealmDatabase = RealmService.shared.realm.objects(Product.self).filter("productID == %#", selectedProduct.productID).first else {return}
RealmService.shared.updateOnCertainKey(object: selectedProductInRealmDatabase, for: ["quantityInCart": quantity])
}
}
and here is the Order model:
import RealmSwift
class Order : Object {
#objc dynamic var userID: String = ""
var products = List<Product>()
convenience init (userID: String, products: List<Product>) {
self.init()
self.userID = "1"
self.products = products
}
//MARK: - Realm Related
static func getOrderFromRealmDatabase() -> Order {
let userID = "1"
let realmService = RealmService.shared.realm
let allOrder = realmService.objects(Order.self)
let theOrder = allOrder.filter("userID CONTAINS[cd] %#", userID).first
if let userOrder = theOrder {
return userOrder
} else {
// Order never setted up before in Realm database container
// then create Order in realm database
let newOrder = Order()
newOrder.userID = userID
newOrder.products = List<Product>()
RealmService.shared.save(object: newOrder)
return newOrder
}
}
static func addProductToOrderRealmDatabase(userOrder: Order, selectedProduct: Product) {
// to check wheter the selected product from user is already in Order or not
if userOrder.products.filter("productID == %#", selectedProduct.productID).first == nil {
// check if the selected product has already available in Product.self database or not
if let matchingProduct = RealmService.shared.realm.objects(Product.self).filter("productID == %#", selectedProduct.productID).first {
RealmService.shared.save(expression: {
userOrder.products.append(matchingProduct)
matchingProduct.hasBeenAddedToCart = true
matchingProduct.quantityInCart = 1
})
} else {
RealmService.shared.save(expression: {
userOrder.products.append(selectedProduct)
selectedProduct.hasBeenAddedToCart = true
selectedProduct.quantityInCart = 1
})
}
}
}
}

iOS / Swift - Appending a filter to retrieving data from Firebase

What I got so far is a tableView and custom Cells about hookah tobacco. Those include an image, name, brand and ID. Now what I try to reach is basically a tableview that contains only the cells with attributes based on a "filter". For example the tableView that appears at the beginning has only the following two settings to make it simple: PriceRange and BrandName. At the first time loading the tableView those are PriceRange: 0 - 100 and Brands: all brands. Then imagine a user restricting those like 0 - 15 Euros and only brand called "7 Days". How exactly would I do that with reloading the tableView?
import UIKit
import Firebase
class ShopViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var button_filter: UIBarButtonItem!
#IBOutlet weak var searchBar_shop: UISearchBar!
#IBOutlet weak var view_navigator: UIView!
#IBOutlet weak var tableView_shop: UITableView!
var ShopCells: [ShopCell] = []
var databaseRef: DatabaseReference!
var storageRef: StorageReference!
override func viewDidLoad() {
super.viewDidLoad()
self.databaseRef = Database.database().reference()
self.storageRef = Storage.storage().reference()
createArray() { shopCells in
for item in shopCells {
self.ShopCells.append(item)
}
DispatchQueue.main.async {
self.tableView_shop.reloadData()
}
}
self.navigationItem.title = "Shop"
self.tableView_shop.delegate = self
self.tableView_shop.dataSource = self
self.searchBar_shop.delegate = self
self.searchBar_shop.barTintColor = UIColor(hexString: "#1ABC9C")
self.view_navigator.backgroundColor = UIColor(hexString: "#1ABC9C")
self.tableView_shop.separatorColor = UIColor.clear
self.searchBar_shop.isTranslucent = false
self.searchBar_shop.backgroundImage = UIImage()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ShopViewController.viewTapped(gestureRecognizer:)))
view.addGestureRecognizer(tapGesture)
}
#objc func viewTapped(gestureRecognizer: UITapGestureRecognizer) {
view.endEditing(true)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.searchBar_shop.resignFirstResponder()
}
func createArray(completion: #escaping ([ShopCell]) -> () ) {
var tempShopCells: [ShopCell] = []
let rootRef = Database.database().reference()
let query = rootRef.child("tobaccos").queryOrdered(byChild: "name")
query.observeSingleEvent(of: .value) { (snapshot) in
let dispatchGroup = DispatchGroup()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let value = child.value as? [String: Any];
let name = value?["name"] as? String ?? "";
let brand = value?["brand"] as? String ?? "";
let iD = value?["iD"] as? String ?? "";
dispatchGroup.enter()
let imageReference = Storage.storage().reference().child("tobaccoPictures").child("\(iD).jpg")
imageReference.getData(maxSize: (1 * 1024 * 1024)) { (data, error) in
if let _error = error{
print(_error)
} else {
if let _data = data {
let image: UIImage! = UIImage(data: _data)
tempShopCells.append(ShopCell(productName: name, brandName: brand, productImage: image, iD: iD))
}
}
dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
completion(tempShopCells)
}
}
}
}
extension ShopViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.ShopCells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let shopCell = ShopCells[indexPath.row]
let cell = tableView_shop.dequeueReusableCell(withIdentifier: "ShopCell") as! ShopTableViewCell
cell.setShopCell(shopCell: shopCell)
return cell
}
}

Why is this Swift struct instantiation crashing?

I have a Swift struct Reflection like this:
struct Reflection {
let title: String
let body: String
let author: String
let favorite: Bool
let creationDate: Date
let id: UUID
}
extension Reflection {
var plistRepresentation: [String: AnyObject] {
return [
"title": title as AnyObject,
"body": body as AnyObject,
"author": author as AnyObject,
"favorite": favorite as AnyObject,
"creationDate": creationDate as AnyObject,
"id": id as AnyObject
]
}
init(plist: [String: AnyObject]) {
title = plist["title"] as! String
body = plist["body"] as! String
author = plist["author"] as! String
favorite = plist["favorite"] as! Bool
creationDate = plist["creationDate"] as! Date
id = plist["id"] as! UUID
}
}
class StorageController {
fileprivate let documentsDirectoryURL = FileManager.default
.urls(for: .documentDirectory, in: .userDomainMask)
.first!
fileprivate var notesFileURL: URL {
return documentsDirectoryURL
.appendingPathComponent("Notes")
.appendingPathExtension("plist")
}
func save(_ notes: [Reflection]) {
let notesPlist = notes.map { $0.plistRepresentation } as NSArray
notesPlist.write(to: notesFileURL, atomically: true)
}
func fetchNotes() -> [Reflection] {
guard let notePlists = NSArray(contentsOf: notesFileURL) as? [[String: AnyObject]] else {
return []
}
return notePlists.map(Reflection.init(plist:))
}
}
class StateController {
fileprivate let storageController: StorageController
fileprivate(set) var notes: [Reflection]
init(storageController: StorageController) {
self.storageController = storageController
self.notes = storageController.fetchNotes()
}
func add(_ note: Reflection) {
notes.append(note)
storageController.save(notes)
}
func update(_ note: Reflection) {
for (index, storedNote) in notes.enumerated() {
guard storedNote.id == note.id else {
continue
}
notes[index] = note
storageController.save(notes)
break
}
}
}
Instantiating a Reflection like this in viewWillAppear crashes my app:
import UIKit
class NotesViewController: UIViewController {
var stateController: StateController!
fileprivate var dataSource: FeedDataSource!
#IBOutlet var tableView: UITableView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let reflection = Reflection(title: "Hello", body: "world", author: "Alex", favorite: true, creationDate: Date(), id: UUID())
//stateController.add(reflection)
dataSource = FeedDataSource(notes: stateController.notes)
tableView.dataSource = dataSource
tableView.reloadData()
}
class FeedDataSource: NSObject {
var notes: [Reflection]!
init(notes: [Reflection]) {
self.notes = notes
}
}
extension FeedDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reflectionCell", for: indexPath) as! ReflectionCell
let index = indexPath.row
let note = notes[index]
cell.model = ReflectionCell.Model(data: note)
return cell
}
}
The cell class:
class ReflectionCell: UITableViewCell {
#IBOutlet weak fileprivate var titleLabel: UILabel!
#IBOutlet weak fileprivate var bodyLabel: UILabel!
#IBOutlet weak fileprivate var authorLabel: UILabel!
#IBOutlet weak fileprivate var bookmarkButton: UIButton!
fileprivate var id: UUID!
var model: Model? {
didSet {
guard let model = model else {
return
}
titleLabel.text = model.title
bodyLabel.text = model.body
authorLabel.text = model.author
bookmarkButton.isSelected = model.favorite
id = model.id
}
}
override func awakeFromNib() {
super.awakeFromNib()
bookmarkButton.setImage(#imageLiteral(resourceName: "Bookmark-Highlighted"), for: .selected)
}
}
extension ReflectionCell {
struct Model {
let title: String
let body: String
let author: String
let favorite: Bool
let id: UUID
init(data: Reflection) {
title = data.title
body = data.body
author = data.author
favorite = data.favorite
id = data.id
}
}
}
I get no console output, just a main thread SIGABRT error. What could be going on?
Like an idiot I was cleaning up my code and commented out the line registering the nib for the reuse identifier. However, I do think it would help if Xcode could print out a useful error message for such a mistake.

Resources