Skip to content

Instantly share code, notes, and snippets.

@marchyuu
Created June 19, 2021 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marchyuu/8843fee8fc43ee512622ac9ca5cb5e8b to your computer and use it in GitHub Desktop.
Save marchyuu/8843fee8fc43ee512622ac9ca5cb5e8b to your computer and use it in GitHub Desktop.
//
// MyProducts.swift
// Glame
//
// Created by Jenifer Yolanda on 12/04/21.
//
import UIKit
import CloudKit
class MyProducts: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var myProductsCollectionView: UICollectionView!
var productNames = ""
var productPrices = ""
var productImages : CKAsset?
var records = [CKRecord]()
//let privateDatabase = CKContainer(identifier: "iCloud.com.example.Glame").privateCloudDatabase
override func viewDidLoad() {
super.viewDidLoad()
myProductsCollectionView.delegate = self
myProductsCollectionView.dataSource = self
let database = CKContainer(identifier: "iCloud.com.example.Glame").privateCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Product", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "modificationDate", ascending: false)]
database.perform(query, inZoneWith: nil){ (records, error) in
if let fetchedRecords = records {
self.records = fetchedRecords
print("Recordnya adalah \(self.records)")
DispatchQueue.main.async{
self.myProductsCollectionView.reloadData()
}
}
print("Erronya adalah \(error)")
}
}
@IBAction func AddTapped(_ sender: Any) {
}
@IBAction func EditTapped(_ sender: Any) {
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let selectedItem = sender as? Product else { // ganti product ke cloudkit punya
return
}
if segue.identifier == "EditProduct" {
guard let destinationVC = segue.destination as? ProductForm else {
return
}
//selecteditem.(cloudkit punya)
destinationVC.fetchedBrandName = selectedItem.model
destinationVC.fetchedBrandType = selectedItem.model
destinationVC.fetchedDesc = selectedItem.type.typeName
destinationVC.fetchedFrameType = selectedItem.productDesc
destinationVC.fetchedPrice = selectedItem.price
destinationVC.fetchedProductImage = selectedItem.url
destinationVC.fetchedStoreURL = selectedItem.url
}
}
}
@IBAction func DeleteTapped(_ sender: Any) {
//DELETE RECORD
}
// EDIT!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return records.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as? MyProductsCollectionViewCell
cell!.layer.cornerRadius = 15
let record = records[indexPath.row]
cell?.productImage.image = record.object(forKey: "productImage") as? UIImage
cell?.productName.text = record.object(forKey:"productName") as? String
cell?.productPrice.text = record.object(forKey: "productPrice") as? String
print("ini price \(record.object(forKey: "productPrice") as? String)")
return cell!
}
// func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// let selectedData = productData.filter({ $0.type.typeName == toDisplayType[indexPath.row].typeName }) // Ganti ke cloud punya
// self.performSegue(withIdentifier: "EditProduct", sender: selectedData)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment