How to randomize cards data from model array without repetition - swift

I am displaying cards data from my model array, how do i randomize card data everytime i start the app and should not be repetitive. I am using ZLSwipeable view library for creating cards fro swipe feature. this is the function in which i have write all my code for swipe cards, and in the code wordsdata is the model array that i have declaredlike this var wordsdata = ModelRsult.
CardView is the view i am displaying my cards into
screenshot of my card display screen
in the image it is shown banana as the first data, now when i run the app again it should show me another data first and so on
my source code:
func nextCardView() -> UIView?
{
let arrayData = wordsData
if (self.colorIndex >= arrayData.count)
{
return nil
}
let cardView = CardView(frame: swipeableView.bounds)
if loadCardsFromXib
{
let contentView = Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)?.first! as! CardView
contentView.superVC = self
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.backgroundColor = cardView.backgroundColor
cardView.addSubview(contentView)
let data = arrayData[(self.colorIndex)] as ModelResult
contentView.wordModel = data
let savedValue = UserDefaults.standard.bool(forKey: "isLangChanged")
tempLangChanged = savedValue
if tempLangChanged == false
{
if let spanishName = data.cardWordEnglish, spanishName.count != 0
{
contentView.wordSpanish.text = spanishName
print(spanishName)
}
}
else
{
if let dict = data.cardWordImage, dict.count != 0
{
let url = WS_wordIconUrl + dict
contentView.wordImage.kf.indicatorType = .activity
contentView.wordImage.kf.setImage(with: URL(string: url))
}
if let spanishName = data.cardWordSpanish, spanishName.count != 0
{
contentView.wordSpanish.text = spanishName
print(spanishName)
}
}
contentView.soundBtn.addTarget(self, action:#selector(self.soundBtn(sender:)), for: .touchUpInside)
contentView.hideCard.addTarget(self, action:#selector(self.hideCard(sender:)), for: .touchUpInside)
contentView.soundBtn.tag = self.colorIndex
contentView.hideCard.tag = self.colorIndex
cardView.tag = self.colorIndex
constrain(contentView, cardView) { view1, view2 in
view1.left == view2.left
view1.top == view2.top
view1.width == cardView.bounds.width
view1.height == cardView.bounds.height
}
}
colorIndex += 1
return cardView
}

The best way for this problem is to shuffle the array where your cards are and then use it. In your case, I would shuffle it at the app start.
array.shuffle()

Related

AdMob Native ad loading but not showing

I'm using Google AdMob to display native ads on my app. I made everything as in the Google Documentation. After testing, I figured out that test ad values are loading successfully since they aren't nil and when I print them, I can see them as they should display. However data is not displaying on my GADNativeAdView. Here is some of my code:
var nativeAdView: GADNativeAdView! {
didSet {
nativeAdView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(nativeAdView)
nativeAdView.leftAnchor.constraint(equalTo: collectionView4.leftAnchor).isActive = true
nativeAdView.rightAnchor.constraint(equalTo: collectionView4.rightAnchor).isActive = true
nativeAdView.topAnchor.constraint(equalTo: collectionView4.bottomAnchor, constant: 10).isActive = true
nativeAdView.heightAnchor.constraint(equalToConstant: 310).isActive = true
}
}
var adLoader: GADAdLoader!
// MARK: - Google Mobile Ads
extension HomeController: GADNativeAdLoaderDelegate, GADNativeAdDelegate {
private func setupAd() {
let multipleAdsOptions = GADMultipleAdsAdLoaderOptions()
multipleAdsOptions.numberOfAds = 1
adLoader = GADAdLoader(adUnitID: testNativeId, rootViewController: self,
adTypes: [.native],
options: [multipleAdsOptions])
adLoader.delegate = self
adLoader.load(GADRequest())
}
func imageOfStars(from starRating: NSDecimalNumber?) -> UIImage? {
guard let rating = starRating?.doubleValue else {
return nil
}
if rating >= 5 {
return UIImage(named: "stars_5")
} else if rating >= 4.5 {
return UIImage(named: "stars_4_5")
} else if rating >= 4 {
return UIImage(named: "stars_4")
} else if rating >= 3.5 {
return UIImage(named: "stars_3_5")
} else {
return nil
}
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
// Create and place ad in view hierarchy.
let nibView = Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil)?.first
guard let nativeAdView = nibView as? GADNativeAdView else {
return
}
self.nativeAdView = nativeAdView
nativeAd.delegate = self
(nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
(nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
nativeAdView.bodyView?.isHidden = nativeAd.body == nil
(nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
nativeAdView.callToActionView?.isHidden = nativeAd.callToAction == nil
(nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
nativeAdView.iconView?.isHidden = nativeAd.icon == nil
(nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(from: nativeAd.starRating)
nativeAdView.starRatingView?.isHidden = nativeAd.starRating == nil
(nativeAdView.storeView as? UILabel)?.text = nativeAd.store
nativeAdView.storeView?.isHidden = nativeAd.store == nil
(nativeAdView.priceView as? UILabel)?.text = nativeAd.price
nativeAdView.priceView?.isHidden = nativeAd.price == nil
(nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil
nativeAdView.callToActionView?.isUserInteractionEnabled = false
nativeAdView.nativeAd = nativeAd
}
func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
print("ERROR: \(error.localizedDescription)")
}
}
Here is my .xib file:
The views custom class on the .xib file are set.
After trial and error I figured out that using #IBOutlet for views in the .xib file and calling them on the adLoader(_:) worked.

UIImage and UILabel disappearing after doing UIRefreshControl

UIImages and UILabel are disappearing in some collectionViews after refreshing inside my app.
here is my code. You can see what is wrong in the picture, the left image is before refreshing and there is a discount price label and after refreshing the simulator, it is gone. getProductLatestData(), getBestSellingData() are the Alamofire get request for API.
//For Refresh Controller
//----------------------
var player : AVAudioPlayer
var refreshController : UIRefreshControl = {
let refreshController = UIRefreshControl()
refreshController.addTarget(self, action: #selector(updateData), for: .valueChanged)
return refreshController
}()
//----------------------
//For Update Data when scroll to top
//----------------------------------
#objc func updateData(){
//To play sound when scroll top
//-----------------------------
let pathToSound = Bundle.main.path(forResource: "Flick", ofType: "wav")
let url = URL(fileURLWithPath: pathToSound!)
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch let error {
print(error.localizedDescription)
}
//-----------------------------
//Fetch Latest Products Data
getProductLatestData(pageNumber: 0, pageSize: 0)
//Fetch BestSelling Products Data
getBestSellingData(pageNumber: 0, pageSize: 0)
//Fetch Promotion Products Data
getPromotionProductsData(pageNumber: 0, pageSize: 0)
//Fetch Products By Category
getProductsByCat(pageNumber: 0, pageSize: 0)
//Fetch Reward Products
getRewardProducts()
self.refreshController.endRefreshing()
}
if collectionView == latestItemCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "latestItemCell", for: indexPath) as! PromotionItemsCollectionViewCell
if let percent = latestItemData[indexPath.row].promotePercent {
if percent != 0 {
cell.percentOff.isHidden = false
cell.percentOff.text = " \(percent) % Off "
cell.percentOffBackground.isHidden = false
}
else{
cell.percentOff.isHidden = true
cell.percentOffBackground.isHidden = true
}
}
if let name = latestItemData[indexPath.row].name {
cell.itemsName.text = name
}
else{
cell.itemsName.text = "-"
}
if let url = latestItemData[indexPath.row].url {
cell.itemsImageView.sd_setImage(with: URL(string: url), placeholderImage: UIImage(named: "placeholder"))
}
//----------------------------------
Thanks to #larme I recheck the code here is how I fixed it, I added those disappeared labels isHidden = false also as I did isHidden = true for them.
if let promotionPrice = latestItemData[indexPath.row].promotePrice {
if promotionPrice != 0 {
cell.originalItemsPrice.text = "\(setCommaForPrice(price: Int(promotionPrice))) Ks"
cell.promotionPercent.isHidden = false
cell.promotionLine.isHidden = false
}
else{
if let orPrice = latestItemData[indexPath.row].originalPrice {
cell.originalItemsPrice.text = "\(setCommaForPrice(price: Int(orPrice))) Ks"
}
cell.promotionPercent.isHidden = true
cell.promotionLine.isHidden = true
}
}

Reload TableView only at new rows once user scrolls to bottom

I am trying to append new data to Tableview and only reload the Tableview for the new data added.
My app has been crashing giving me this error: "attempt to delete row 19 from section 0 which only contains 10 rows before the update'"
I would like to reload those rows once the asynchronous function contentQueryContinuous() is completed.
Here is my code:
//Once User Scrolls all the way to bottom, beginContentBatchFetch() is called
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentSize.height
let contentHeight = scrollView.contentSize.height
if offsetY > contentHeight - scrollView.frame.height {
beginContentBatchFetch()
}
}
//Content Batch Fetch looks up to 10 new elements, tries to append to current array's, and then reload's tableview only for those new elements
func beginContentBatchFetch() {
contentFetchMore = true
ProgressHUD.show()
let oldcount = contentObjectIdArray.count
var IndexPathsToReload = [IndexPath]()
var startIndex = Int()
var endIndex = Int()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
//Calls self.contentQueryContinous which adds new elements
self.contentQueryContinous()
let newElements = self.contentObjectIdArray.count - oldcount
startIndex = oldcount
endIndex = self.contentObjectIdArray.count
for index in startIndex..<endIndex {
let indexPath = IndexPath(row: index, section: 0)
IndexPathsToReload.append(indexPath)
}
if newElements > 0 {
self.MyTableView.reloadRows(at: IndexPathsToReload, with: .fade)
}
ProgressHUD.dismiss()
}
}
Here is contentQueryContinous()
func contentQueryContinous() {
if contentObjectIdArray.count != 0 {
contentSkip = contentObjectIdArray.count
let query = PFQuery(className: "ContentPost")
query.whereKey("Spot", equalTo: SpotText)
query.limit = 10
query.skip = contentSkip
query.addDescendingOrder("createdAt")
query.findObjectsInBackground(block: { (objects: [PFObject]?,error: Error?) in
if let objects = objects {
for object in objects {
let ProfileImageFile = object["ProfileImage"] as? PFFileObject
let urlString = ProfileImageFile?.url as! String
if let url = URL(string: urlString) {
let data = try? Data(contentsOf: url)
if let imageData = data {
self.contentPostProPicUrlArray.append(urlString as NSString)
self.contentPostProPicImageCache.setObject(UIImage(data: imageData)!, forKey: urlString as NSString)
}
}
if object["Post"] != nil && object["UserLikes"] != nil && object["Username"] != nil && object["UserTime"] != nil {
self.contentPostArray.append(object["Post"] as! String)
self.contentLikeArray.append(object["UserLikes"] as! Int)
self.contentUsernameArray.append(object["Username"] as! String)
self.contentTimeArray.append(object["UserTime"] as! String)
self.contentObjectIdArray.append(object.objectId!)
}
}
print(self.contentPostArray)
}
})
}
}
You are trying to reload a cell that is not on the table. Try inserting it. Also, you are risking a race condition.
From the docs
Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it animates the old row out. Call this method if you want to alert the user that the value of a cell is changing.

Button is Showing GMSMarkers but isn't Hiding it, How do I get it to do both

Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.
This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.
func showCarIcon() {
ref = Database.database().reference()
ref.child("location").observe(.childAdded) { (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject] {
if dict["Activity"] as! String == "Car" {
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
let n = self.carIconArray.count
let heightWidth = self.mapView.frame.height
for marker in 1...n {
let carMarker = GMSMarker()
let carIconView = UIImage(named: "carPin")
let image = carIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
carMarker.position = location
carMarker.icon = image
carMarker.title = title
carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
func displayIt() {
if self.carNumber == "1" {
carMarker.map = self.mapView
} else {
carMarker.map = nil
}
}
displayIt()
}
}
}
}
}
So this is the action function for when button is pressed.
var carNumber = String()
#IBAction func showCar(_ sender: Any) {
if motorisedVehicleButtonActive {
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
} else {
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
}
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
showCarIcon()
}
Let me explain what is issue with your code.
You are creating new marker every time when button press. So, new marker have different object id than older.
When you try to remove it, it will not works just because of it's different marker than you placed on map.
So you need to store marker in array and on remove time, get icon from array and remove it from map.
First you need to create array of GMSMarker, because you have to store every marker which is placed on map.
So, write following line of code at top of your class.
var arrCarMarkers = [GMSMarker]()
Then after, store every marker in this array which are you placing on map.
So, update your code as follow:
func displayIt() {
if self.carNumber == "1" {
carMarker.map = self.mapView
arrCarMarkers.append(carMarker) // Here is store marker in array
} else {
carMarker.map = nil
}
}
Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:
#IBAction func showCar(_ sender: Any) {
if motorisedVehicleButtonActive {
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
showCarIcon()
} else {
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
self.arrCarMarkers.forEach { $0.map = nil }
}
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
}
Above code will remove all markers from map.
I hope this will works for you.

If-statements with UIImageViews

I have made an array of UIImagesViews and I have made a code to assign them random pictures. I would like to make an if-statement if more than or 5 pictures have the same picture shown.
var images: [UIImageView] = [Image1, Image2, Image3, Image4, Image5, Image6, Image7, Image8, Image9]
The different pictures are named card1, card2, card3
let firstRandomNumber = arc4random_uniform(3) + 1
let firstRandomString:String = String(format: "card%i", firstRandomNumber)
self.Image1.image = UIImage(named: firstRandomString)
let secondRandomNumber = arc4random_uniform(3) + 1
let secondRandomString:String = String(format: "card%i", secondRandomNumber)
self.Image2.image = UIImage(named: secondRandomString)
All images have their own specific code like the ones above. Now, as I said, I would like to make an if-statement or switch-case if five or more of the Images have the same picture shown.
Edit: Here is the updated code;
let imageNames:NSArray = [firstRandomString, secondRandomString, thirdRandomString, fourthRandomString, fifthRandomString, sixthRandomString, seventhRandomString, eighthRandomString, ninthRandomString]
let maxRepeating = imageNames
.map {img in imageNames.reduce(0) { $1 == img ? $0 + 1 : $0} }
.reduce(0){ $1 > $0 ? $1 : $0 }
The code only has one error at .map {img in imageNames.reduce(0) { $1 == img ? $0 + 1 : $0} } saying;
Binary operator '==' cannot be applied to two 'Element' (aka 'AnyObject') operands
Edit: Here is another code I have tried from Christopher Kevin;
var correct = 0
func checkImageDuplication() {
let imageArray : NSArray = [firstRandomString, secondRandomString, thirdRandomString, fourthRandomString, fifthRandomString, sixthRandomString, seventhRandomString, eighthRandomString, ninthRandomString]
let imageDataArray = imageArray.map { (image) -> NSData in
return UIImagePNGRepresentation(image as! UIImage)!
}
let countSet = NSCountedSet(array: imageDataArray)
for imageData in imageDataArray {
let count = countSet.countForObject(imageData)
if count > 5 {
correct = 5
}
}
}
#IBAction func PlayerPRESSED(sender: AnyObject) {
if correct == 5 {
view.backgroundColor = UIColor.greenColor()
}
}
I tried the code but it will not execute the action. (view.backgroundColor) Anyone have any Idea what is wrong with this code?
Your logic here needs to be separated from the views.
Create a model in the same class that holds an array of UIImage. Use this array to populate your cards, and to check whether more than 5 images are the same. Example untested and unoptimised code:
var images = [UIImage]()
private func populateImages() {
// Fill images array, and perhaps populate imageViews
}
private func checkImageDuplication() {
let imageDataArray = imageArray.map { (image) -> NSData in
return UIImagePNGRepresentation(image)!
}
let countSet = NSCountedSet(array: imageDataArray)
for imageData in imageDataArray {
let count = countSet.countForObject(imageData)
if count > 5 {
// Handle here
}
}
}
I don't think you can compare UIImage objects, as each call to UIImage(named:) will generate another UIImage instance, even if the name is the same. I'd recommend generating an array of strings, and testing that array for repetitions, with something like this:
let maxRepeating = imageNames
.map {img in imageNames.reduce(0) { $1 == img ? $0 + 1 : $0} }
.reduce(0){ $1 > $0 ? $1 : $0 }