Cosmos icon indicating copy to clipboard operation
Cosmos copied to clipboard

How to modify each cosmosView rating independently in a custom Cell, and afterward save the changes.

Open IlijaMihajlovic opened this issue 4 years ago • 4 comments

Hi @evgenyneu,

First, I want to say thanks so much for such a great star rating control. I'm trying to figure it now for days and maybe you know the answer (at the end you the author of the repository). :) My question is how to modify each cosmosView rating independently in a custom UICollectionViewCell, and afterward save the changes.

I know how saving works using UserDefaults. But I don't know where to call the functions (load & save) after I modify the rating on the cells, so each cell rating is modified independently from the other one. I did tried it in multiple UICollectionView methods like didSelectItemAt indexPath, and cellForItemAt indexPath This is my Custom UICollectionViewCell of course some unnecessary code I left out.

class ProductCell: UICollectionViewCell {

//MARK: - Properties
 fileprivate let kDefaults = "key"

lazy var ratingStar: CosmosView = {
        let cosmosStar = CosmosView()
        cosmosStar.settings.filledImage = UIImage(named: "starFilled")?.withRenderingMode(.alwaysOriginal)
        cosmosStar.settings.emptyImage = UIImage(named: "starEmpty")?.withRenderingMode(.alwaysOriginal)
        cosmosStar.rating = 0
        cosmosStar.settings.fillMode = .half
        cosmosStar.settings.updateOnTouch = true
        return cosmosStar
    }()

override init(frame: CGRect) {
        super.init(frame: frame)
        saveStarRating()
        loadStarRating()
    }

// MARK: - Methods
   func saveStarRating() {
        ratingStar.didFinishTouchingCosmos = { rating in
            UserDefaults.standard.set(rating, forKey: self.kDefaults)
        }
    }
    
func loadStarRating() {
        guard let loadValue = UserDefaults.standard.object(forKey: kDefaults) as? Double else {return}
        ratingStar.rating = loadValue
    }

}

Also, This is how the UICollectionViewCells look in the simulator: Simulator Screen Shot - iPhone 8 - 2019-08-21 at 17 18 58

Thanks in advance. :)

IlijaMihajlovic avatar Aug 21 '19 12:08 IlijaMihajlovic

Hi @IlijaMihajlovic, I don't have time to help you right now, but have a look at the demo app that comes with this repository. It has a table view screen with multiple rating controls. You can see how the ratings for each control are stores in local variable ratingStorage:

https://github.com/evgenyneu/Cosmos/blob/master/Demo/PerformanceTableViewController.swift

For your case, instead of using local variable, you can store your ratings in a local database, like UserDefaults (for small data) or CoreData.

evgenyneu avatar Aug 24 '19 04:08 evgenyneu

Hi @evgenyneu,

Thanks so much for your message. I understand how saving and loading works. But I didn't understand how to save and load the ratings on the cells, so each cell rating is modified independently from the other one on the cosmosView using UserDefaults and a CollectionView programmatically.

If you can show me the sample code for that in your spare time when you not busy, that would mean a lot to me. I'm trying to figure it out now for days without any luck. Thanks

IlijaMihajlovic avatar Aug 24 '19 19:08 IlijaMihajlovic

@IlijaMihajlovic, I'm really sorry I don't have any spare time right now. I'm a student and the workload is insane currenly, I barely have time for sleeping lol. I would suggest trying your luck on stackoverflow.

evgenyneu avatar Aug 25 '19 01:08 evgenyneu

@evgenyneu, Ok cool just keep the issue open, maybe somebody else will know the answer. Thanks!

IlijaMihajlovic avatar Aug 26 '19 15:08 IlijaMihajlovic