- Handled optional gender type - Handled optional gender in profile - Remaining MyListVC changes
330 lines
12 KiB
Swift
330 lines
12 KiB
Swift
//
|
|
// PersistentStorage.swift
|
|
// WOKA
|
|
//
|
|
// Created by MacBook Pro on 05/08/24.
|
|
//
|
|
|
|
import Foundation
|
|
import CoreData
|
|
|
|
/**
|
|
Hey there, I hope you enjoyed the video, if you have any questions then please feel free to ask I will be happy to answer them.
|
|
Do share this with your iOS group on whatsapp or facebook or anyone who wants to learn iOS
|
|
*/
|
|
|
|
enum PersistentStorageEnum : String{
|
|
case UserClicks
|
|
|
|
case click_counts
|
|
case category_id
|
|
case post_id
|
|
case post_type
|
|
}
|
|
|
|
enum PostType: Int {
|
|
case series = 1
|
|
case season = 2
|
|
case episode = 3
|
|
case video = 4
|
|
// case paint = 5
|
|
case game = 6
|
|
case audio = 7
|
|
case karaokeVideo = 8
|
|
case shopProduct = 9
|
|
// case parentalVideo = 10
|
|
// case article = 11
|
|
case liveTV = 12
|
|
case FM = 13
|
|
case teaser = 14
|
|
case others = 15
|
|
case home = 16
|
|
}
|
|
|
|
//enum PostType: String {
|
|
// case series = "1"
|
|
// case season = "2"
|
|
// case episode = "3"
|
|
// case video = "4"
|
|
//// case paint = "5"
|
|
// case game = "6"
|
|
// case audio = "7"
|
|
// case karaokeVideo = "8"
|
|
// case shopProduct = "9"
|
|
//// case parentalVideo = "10"
|
|
//// case article = "11"
|
|
// case liveTV = "12"
|
|
// case FM = "13"
|
|
// case teaser = "14"
|
|
// case others = "15"
|
|
// case home = "16"
|
|
//}
|
|
|
|
struct UserClickData {
|
|
let clickCounts: Int
|
|
let categoryId: Int
|
|
let postId: Int
|
|
let postType: Int
|
|
}
|
|
|
|
final class PersistentStorage
|
|
{
|
|
|
|
private init(){}
|
|
static let shared = PersistentStorage()
|
|
|
|
// MARK: - Core Data stack
|
|
|
|
lazy var persistentContainer: NSPersistentContainer = {
|
|
|
|
let container = NSPersistentContainer(name: "WOKA")
|
|
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
|
|
if let error = error as NSError? {
|
|
|
|
fatalError("Unresolved errorsss \(error), \(error.userInfo)")
|
|
}
|
|
})
|
|
return container
|
|
}()
|
|
|
|
lazy var context = persistentContainer.viewContext
|
|
// MARK: - Core Data Saving support
|
|
|
|
func saveContext() {
|
|
if context.hasChanges {
|
|
do {
|
|
try context.save()
|
|
} catch {
|
|
let nserror = error as NSError
|
|
fatalError("Unresolved errorsss \(nserror), \(nserror.userInfo)")
|
|
}
|
|
}
|
|
}
|
|
|
|
func createData(data : UserClickData){
|
|
|
|
//We need to create a context from this container
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
let share = UserClicks(context: managedContext)
|
|
share.click_counts = Int64(data.clickCounts)
|
|
share.category_id = Int64(data.categoryId)
|
|
share.post_id = Int64(data.postId)
|
|
share.post_type = Int64(data.postType)
|
|
|
|
do {
|
|
try managedContext.save()
|
|
retrieveData(postID: data.postId, catID: data.categoryId, postType: data.postType)
|
|
} catch let error as NSError {
|
|
print("Could not save. \(error), \(error.userInfo)")
|
|
}
|
|
}
|
|
|
|
func checkIfExist( key : PersistentStorageEnum , clicksData : UserClickData) {
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: PersistentStorageEnum.UserClicks.rawValue)
|
|
// fetchRequest.fetchLimit = 1
|
|
// fetchRequest.predicate = NSPredicate(format: "id == %d" ,id)
|
|
fetchRequest.predicate = NSPredicate(format: "\(key.rawValue) == %@" ,clicksData.postType.toString())
|
|
|
|
do {
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
|
if result.isEmpty{
|
|
//create
|
|
PersistentStorage.shared.createData(data: clicksData)
|
|
print("create")
|
|
}else{
|
|
//update
|
|
let objectUpdate = result[0] as NSManagedObject
|
|
print("Update")
|
|
objectUpdate.setValue(result.first!.click_counts + Int64(clicksData.clickCounts), forKey: "click_counts")
|
|
do{
|
|
try managedContext.save()
|
|
retrieveData(postID: clicksData.postId, catID: clicksData.categoryId, postType: clicksData.postType)
|
|
}
|
|
catch
|
|
{
|
|
print(error)
|
|
}
|
|
}
|
|
// result.forEach { clicks in
|
|
// print("Counts" , clicks.click_counts)
|
|
// }
|
|
}catch let error as NSError {
|
|
print("Could not fetch. \(error), \(error.userInfo)")
|
|
}
|
|
}
|
|
|
|
func checkWebSeries(clicksData : UserClickData) {
|
|
let managedContext = PersistentStorage.shared.context
|
|
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: PersistentStorageEnum.UserClicks.rawValue)
|
|
fetchRequest.fetchLimit = 1
|
|
fetchRequest.predicate = NSPredicate(format: "post_id == %@ AND category_id == %@" ,clicksData.postId.toString(), clicksData.categoryId.toString())
|
|
|
|
do {
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
|
if result.isEmpty{
|
|
//create
|
|
PersistentStorage.shared.createData(data: clicksData)
|
|
print("create Main")
|
|
}else{
|
|
//update
|
|
let objectUpdate = result[0] as NSManagedObject
|
|
print("Update Main")
|
|
objectUpdate.setValue(result.first!.click_counts + Int64(clicksData.clickCounts), forKey: "click_counts")
|
|
do{
|
|
try managedContext.save()
|
|
retrieveData(postID: clicksData.postId, catID: clicksData.categoryId, postType: clicksData.postType)
|
|
}
|
|
catch
|
|
{
|
|
print(error)
|
|
}
|
|
}
|
|
}catch let error as NSError {
|
|
print("Could not fetch. \(error), \(error.userInfo)")
|
|
}
|
|
}
|
|
|
|
func retrieveData(postID : Int?, catID : Int?, postType : Int) {
|
|
|
|
//We need to create a context from this container
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
|
// debugPrint(path[0])
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserClicks")
|
|
|
|
fetchRequest.predicate = NSPredicate(format: "post_id == %@ AND category_id == %@ AND post_type == %@" ,postID?.toString() ?? "0", catID?.toString() ?? "0" ,postType.toString())
|
|
|
|
// fetchRequests.fetchLimit = 1
|
|
// fetchRequests.sortDescriptors = [NSSortDescriptor.init(key: "uuid", ascending: false)]
|
|
do {
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
|
result.forEach { clicks in
|
|
print("ID:-" , PostType(rawValue: Int(clicks.post_type))!, "CatID:- ", clicks.category_id, "PostID:- ", clicks.post_id , "Count:-", clicks.click_counts)
|
|
}
|
|
}
|
|
catch let error
|
|
{
|
|
debugPrint(error)
|
|
}
|
|
}
|
|
|
|
// func updateData(){
|
|
// //We need to create a context from this container
|
|
// let managedContext = PersistentStorage.shared.context
|
|
//
|
|
//
|
|
// let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserClicks")
|
|
// fetchRequest.predicate = NSPredicate(format: "uuid = %@", "2")
|
|
// do
|
|
// {
|
|
// let test = try managedContext.fetch(fetchRequest)
|
|
//
|
|
// let objectUpdate = test[0] as! NSManagedObject
|
|
// objectUpdate.setValue("Bilal Ahmed Khan New Name", forKey: "name")
|
|
// do{
|
|
// try managedContext.save()
|
|
// self.retrieveData()
|
|
// }
|
|
// catch
|
|
// {
|
|
// print(error)
|
|
// }
|
|
// }
|
|
// catch
|
|
// {
|
|
// print(error)
|
|
// }
|
|
//
|
|
// }
|
|
|
|
// func deleteData(){
|
|
// //We need to create a context from this container
|
|
// let managedContext = PersistentStorage.shared.context
|
|
//
|
|
// let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserClicks")
|
|
//// fetchRequest.fetchLimit = 1
|
|
//// fetchRequest.predicate = NSPredicate(format: "uuid = %@ AND uuid = %@", "1", "1")
|
|
// fetchRequest.predicate = NSPredicate(format: "post_id = %@", "11")
|
|
//
|
|
// do
|
|
// {
|
|
// let test = try managedContext.fetch(fetchRequest)
|
|
//
|
|
// let objectToDelete = test[0] as! NSManagedObject
|
|
//// test.forEach { obbject in
|
|
//// managedContext.delete(obbject as! NSManagedObject)
|
|
//// }
|
|
// managedContext.delete(test.first as! NSManagedObject)
|
|
//// managedContext.delete(objectToDelete)
|
|
//
|
|
// do{
|
|
// try managedContext.save()
|
|
//// self.createData()
|
|
// self.retrieveData()
|
|
// }
|
|
// catch
|
|
// {
|
|
// print(error)
|
|
// }
|
|
//
|
|
// }
|
|
// catch
|
|
// {
|
|
// print(error)
|
|
// }
|
|
// }
|
|
|
|
// MARK: - Handle Clicks
|
|
|
|
func addOthersCount(){
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: 0, postType: PostType.others.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addKaraokeCount(postID : Int){
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: postID, postType: PostType.karaokeVideo.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addAudioCount(postID : Int){
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: postID, postType: PostType.audio.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addGamesCount(postID : Int, count : Int = 1){
|
|
let userClicks = UserClickData(clickCounts: count, categoryId: 0, postId: postID, postType: PostType.game.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addShopCount(postID : Int){
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: postID, postType: PostType.shopProduct.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addRadioCount(){
|
|
guard let postID = AuthFunc.shareInstance.staticURLs?.liveFmData?.id else{return}
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: postID, postType: PostType.FM.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addLiveTVCount(){
|
|
guard let postID = AuthFunc.shareInstance.staticURLs?.liveData?.first?.id else{return}
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: postID, postType: PostType.liveTV.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addTrailerCount(){
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: 0, postType: PostType.teaser.rawValue)
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
|
}
|
|
|
|
func addWebSeries(catID : Int, postID : Int, postType : PostType){
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: catID, postId: postID, postType: postType.rawValue)
|
|
PersistentStorage.shared.checkWebSeries(clicksData: userClicks)
|
|
}
|
|
}
|