2024-08-05 19:43:30 +05:30
|
|
|
//
|
|
|
|
|
// PersistentStorage.swift
|
|
|
|
|
// WOKA
|
|
|
|
|
//
|
|
|
|
|
// Created by MacBook Pro on 05/08/24.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import CoreData
|
|
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
enum PersistentStorageEnum : String{
|
|
|
|
|
case UserClicks
|
|
|
|
|
case click_counts
|
|
|
|
|
case category_id
|
|
|
|
|
case post_id
|
|
|
|
|
case post_type
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 20:21:47 +05:30
|
|
|
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
|
2024-08-06 21:17:00 +05:30
|
|
|
}
|
2024-08-08 18:47:13 +05:30
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
struct UserClickData {
|
|
|
|
|
let clickCounts: Int
|
|
|
|
|
let categoryId: Int
|
|
|
|
|
let postId: Int
|
|
|
|
|
let postType: Int
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 20:17:33 +05:30
|
|
|
// MARK: - Clicks
|
|
|
|
|
struct ClicksAnalytics : Codable {
|
|
|
|
|
let postID, postType, numberOfClicks, deviceType: Int?
|
|
|
|
|
let categoryID: Int?
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case postID = "post_id"
|
|
|
|
|
case postType = "post_type"
|
|
|
|
|
case numberOfClicks = "number_of_clicks"
|
|
|
|
|
case deviceType = "device_type"
|
|
|
|
|
case categoryID = "category_id"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - ADs Impressions & Clicks
|
|
|
|
|
struct AdsClickImpressionsData : Codable {
|
|
|
|
|
let adID, noOfClick, noOfOmpression: Int
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case adID = "ad_id"
|
|
|
|
|
case noOfClick = "no_of_click"
|
|
|
|
|
case noOfOmpression = "no_of_impression"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
final class PersistentStorage
|
|
|
|
|
{
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
private init(){}
|
|
|
|
|
static let shared = PersistentStorage()
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
// MARK: - Core Data stack
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
lazy var persistentContainer: NSPersistentContainer = {
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
let container = NSPersistentContainer(name: "WOKA")
|
|
|
|
|
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
|
|
|
|
|
if let error = error as NSError? {
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
fatalError("Unresolved errorsss \(error), \(error.userInfo)")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return container
|
|
|
|
|
}()
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
lazy var context = persistentContainer.viewContext
|
|
|
|
|
// MARK: - Core Data Saving support
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
func saveContext() {
|
|
|
|
|
if context.hasChanges {
|
|
|
|
|
do {
|
|
|
|
|
try context.save()
|
|
|
|
|
} catch {
|
|
|
|
|
let nserror = error as NSError
|
|
|
|
|
fatalError("Unresolved errorsss \(nserror), \(nserror.userInfo)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
func createData(data : UserClickData){
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
//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)
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
do {
|
|
|
|
|
try managedContext.save()
|
2024-08-07 20:21:47 +05:30
|
|
|
retrieveData(postID: data.postId, catID: data.categoryId, postType: data.postType)
|
2024-08-06 21:17:00 +05:30
|
|
|
} catch let error as NSError {
|
|
|
|
|
print("Could not save. \(error), \(error.userInfo)")
|
|
|
|
|
}
|
2024-08-05 19:43:30 +05:30
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
func checkIfExist( key : PersistentStorageEnum , clicksData : UserClickData) {
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: PersistentStorageEnum.UserClicks.rawValue)
|
2024-08-07 20:21:47 +05:30
|
|
|
// fetchRequest.fetchLimit = 1
|
|
|
|
|
// fetchRequest.predicate = NSPredicate(format: "id == %d" ,id)
|
2024-08-17 01:34:28 +05:30
|
|
|
fetchRequest.predicate = NSPredicate(format: "\(key.rawValue) == %@ AND post_id == %@" ,clicksData.postType.toString(),clicksData.postId.toString())
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-08-06 21:17:00 +05:30
|
|
|
do {
|
|
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
|
|
|
|
if result.isEmpty{
|
|
|
|
|
//create
|
|
|
|
|
PersistentStorage.shared.createData(data: clicksData)
|
2024-08-17 01:34:28 +05:30
|
|
|
print("create, In Exist")
|
2024-08-06 21:17:00 +05:30
|
|
|
}else{
|
|
|
|
|
//update
|
|
|
|
|
let objectUpdate = result[0] as NSManagedObject
|
2024-08-17 01:34:28 +05:30
|
|
|
print("Update, In Exist")
|
2024-08-06 21:17:00 +05:30
|
|
|
objectUpdate.setValue(result.first!.click_counts + Int64(clicksData.clickCounts), forKey: "click_counts")
|
|
|
|
|
do{
|
|
|
|
|
try managedContext.save()
|
2024-08-07 20:21:47 +05:30
|
|
|
retrieveData(postID: clicksData.postId, catID: clicksData.categoryId, postType: clicksData.postType)
|
2024-08-06 21:17:00 +05:30
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-07 20:21:47 +05:30
|
|
|
// 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)
|
|
|
|
|
}
|
2024-08-06 21:17:00 +05:30
|
|
|
}
|
|
|
|
|
}catch let error as NSError {
|
|
|
|
|
print("Could not fetch. \(error), \(error.userInfo)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 20:21:47 +05:30
|
|
|
func retrieveData(postID : Int?, catID : Int?, postType : Int) {
|
2024-08-06 21:17:00 +05:30
|
|
|
|
|
|
|
|
//We need to create a context from this container
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
|
2024-08-05 19:43:30 +05:30
|
|
|
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
2024-08-07 20:21:47 +05:30
|
|
|
// debugPrint(path[0])
|
|
|
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserClicks")
|
2024-08-06 21:17:00 +05:30
|
|
|
|
2024-08-07 20:21:47 +05:30
|
|
|
fetchRequest.predicate = NSPredicate(format: "post_id == %@ AND category_id == %@ AND post_type == %@" ,postID?.toString() ?? "0", catID?.toString() ?? "0" ,postType.toString())
|
2024-08-06 21:17:00 +05:30
|
|
|
|
2024-08-07 20:21:47 +05:30
|
|
|
// fetchRequests.fetchLimit = 1
|
|
|
|
|
// fetchRequests.sortDescriptors = [NSSortDescriptor.init(key: "uuid", ascending: false)]
|
2024-08-05 19:43:30 +05:30
|
|
|
do {
|
2024-08-07 20:21:47 +05:30
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
2024-08-06 21:17:00 +05:30
|
|
|
result.forEach { clicks in
|
2024-08-07 20:21:47 +05:30
|
|
|
print("ID:-" , PostType(rawValue: Int(clicks.post_type))!, "CatID:- ", clicks.category_id, "PostID:- ", clicks.post_id , "Count:-", clicks.click_counts)
|
2024-08-06 21:17:00 +05:30
|
|
|
}
|
2024-08-08 18:47:13 +05:30
|
|
|
}
|
|
|
|
|
catch let error
|
2024-08-05 19:43:30 +05:30
|
|
|
{
|
|
|
|
|
debugPrint(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-23 19:45:24 +05:30
|
|
|
|
|
|
|
|
//Sending Clicks data to our server
|
2024-09-24 20:10:45 +05:30
|
|
|
func sendDataToServer(isLogout : Bool = false) {
|
2024-08-14 20:06:28 +05:30
|
|
|
//We need to create a context from this container
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
2024-09-24 20:10:45 +05:30
|
|
|
|
2024-08-14 20:06:28 +05:30
|
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserClicks")
|
2024-08-17 01:34:28 +05:30
|
|
|
fetchRequest.fetchLimit = 15
|
2024-08-14 20:06:28 +05:30
|
|
|
do {
|
|
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
|
|
|
|
var userClicks = [ClicksAnalytics]()
|
2024-09-23 19:45:24 +05:30
|
|
|
|
|
|
|
|
print("UserClicks Count from SendData to server :- ", result.count)
|
|
|
|
|
|
|
|
|
|
//if data is less , dont keep sending the data to server
|
2024-09-24 20:10:45 +05:30
|
|
|
if isLogout == false,result.count < 3 {
|
|
|
|
|
print("Not Enough Clicks Data")
|
2024-09-23 19:45:24 +05:30
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 20:06:28 +05:30
|
|
|
result.forEach { clicks in
|
|
|
|
|
// device type 1- android , 2 - iOS
|
|
|
|
|
userClicks.append(ClicksAnalytics(postID: Int(clicks.post_id), postType: Int(clicks.post_type), numberOfClicks: Int(clicks.click_counts), deviceType: 2, categoryID: Int(clicks.category_id)))
|
|
|
|
|
|
|
|
|
|
print("ID:-" , PostType(rawValue: Int(clicks.post_type))!, "CatID:- ", clicks.category_id, "PostID:- ", clicks.post_id , "Count:-", clicks.click_counts)
|
|
|
|
|
}
|
2024-09-23 19:45:24 +05:30
|
|
|
|
|
|
|
|
// send data to server
|
2024-08-14 20:06:28 +05:30
|
|
|
NetworkManager.shareInstance.nwCallRawJSON(clicksData: userClicks) { isDone in
|
2024-09-23 19:45:24 +05:30
|
|
|
if isDone{ // if data is send to server and we get success callback then delete that data from coredata entity.
|
2024-09-24 20:10:45 +05:30
|
|
|
self.deleteData(result,isLogout: isLogout)
|
2024-08-14 20:06:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch let error
|
|
|
|
|
{
|
|
|
|
|
debugPrint(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-04 20:17:33 +05:30
|
|
|
|
2024-09-23 19:45:24 +05:30
|
|
|
//Delete data from CoreData
|
2024-09-24 20:10:45 +05:30
|
|
|
func deleteData(_ data: [UserClicks],isLogout : Bool = false) {
|
2024-08-14 20:06:28 +05:30
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
|
|
|
|
|
data.forEach { clicks in
|
|
|
|
|
managedContext.delete(clicks)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try managedContext.save()
|
2024-09-23 19:45:24 +05:30
|
|
|
|
|
|
|
|
//after deleting check if more data exist.
|
2024-09-24 20:10:45 +05:30
|
|
|
getAllData(isLogout: isLogout)
|
2024-08-14 20:06:28 +05:30
|
|
|
print("Deleted data")
|
|
|
|
|
} catch let error {
|
|
|
|
|
debugPrint("Failed to delete data:", error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-07 20:21:47 +05:30
|
|
|
|
2024-09-23 19:45:24 +05:30
|
|
|
//Get all data from DB
|
2024-09-24 20:10:45 +05:30
|
|
|
func getAllData(isLogout : Bool = false) {
|
2024-09-23 19:45:24 +05:30
|
|
|
//We need to create a context from this container
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserClicks")
|
|
|
|
|
fetchRequest.fetchLimit = 15
|
|
|
|
|
do {
|
|
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [UserClicks] else {return}
|
|
|
|
|
|
|
|
|
|
print("UserClicks Count from getAllData :- ", result.count)
|
|
|
|
|
//if data is less , dont keep sending the data to server
|
2024-09-24 20:10:45 +05:30
|
|
|
if isLogout == false,result.count < 5 {
|
2024-09-23 19:45:24 +05:30
|
|
|
print("Not Enough Data")
|
|
|
|
|
return
|
|
|
|
|
} else{
|
|
|
|
|
// send data again to server
|
2024-10-01 19:57:04 +05:30
|
|
|
if result.count != 0{
|
|
|
|
|
sendDataToServer(isLogout: isLogout)
|
|
|
|
|
}
|
2024-09-23 19:45:24 +05:30
|
|
|
print("Data sent Again.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.forEach { clicks in
|
|
|
|
|
// device type 1- android , 2 - iOS
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-04 20:17:33 +05:30
|
|
|
|
2024-08-17 01:34:28 +05:30
|
|
|
// MARK: - Handle Clicks For UserClicks
|
2024-08-07 20:21:47 +05:30
|
|
|
|
|
|
|
|
func addOthersCount(){
|
|
|
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: 0, postId: 0, postType: PostType.others.rawValue)
|
|
|
|
|
PersistentStorage.shared.checkIfExist( key: .post_type,clicksData: userClicks)
|
2024-08-06 21:17:00 +05:30
|
|
|
}
|
|
|
|
|
|
2024-08-12 19:58:58 +05:30
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 20:02:41 +05:30
|
|
|
func addGamesCount(postID : Int, count : Int = 1){
|
|
|
|
|
let userClicks = UserClickData(clickCounts: count, categoryId: 0, postId: postID, postType: PostType.game.rawValue)
|
2024-08-12 19:58:58 +05:30
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 20:21:47 +05:30
|
|
|
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)
|
2024-08-06 21:17:00 +05:30
|
|
|
}
|
|
|
|
|
|
2024-08-07 20:21:47 +05:30
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 19:58:58 +05:30
|
|
|
func addWebSeries(catID : Int, postID : Int, postType : PostType){
|
|
|
|
|
let userClicks = UserClickData(clickCounts: 1, categoryId: catID, postId: postID, postType: postType.rawValue)
|
2024-08-07 20:21:47 +05:30
|
|
|
PersistentStorage.shared.checkWebSeries(clicksData: userClicks)
|
|
|
|
|
}
|
2024-09-24 20:10:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Handling ADS
|
|
|
|
|
|
|
|
|
|
extension PersistentStorage{
|
2024-08-17 01:34:28 +05:30
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
func sendAdsData(isLogout : Bool = false) {
|
2024-09-04 20:17:33 +05:30
|
|
|
//create a context from this container
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
|
|
|
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "AdClicksImpressions")
|
|
|
|
|
fetchRequest.fetchLimit = 15
|
|
|
|
|
do {
|
|
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [AdClicksImpressions] else {return}
|
2024-09-23 19:45:24 +05:30
|
|
|
|
|
|
|
|
print("userImpressions Count from SendAdsData to server :- ", result.count)
|
|
|
|
|
|
|
|
|
|
//if data is less , dont keep sending the data to server
|
2024-09-24 20:10:45 +05:30
|
|
|
if isLogout == false,result.count < 2 {
|
|
|
|
|
print("Not Enough Ads Data")
|
2024-09-23 19:45:24 +05:30
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 20:17:33 +05:30
|
|
|
var userImpressions = [AdsClickImpressionsData]()
|
2024-09-24 20:10:45 +05:30
|
|
|
//map the impressions
|
2024-09-04 20:17:33 +05:30
|
|
|
result.forEach { ads in
|
|
|
|
|
// device type 1- android , 2 - iOS
|
|
|
|
|
userImpressions.append(AdsClickImpressionsData(adID: Int(ads.ad_id), noOfClick: Int(ads.no_of_click), noOfOmpression: Int(ads.no_of_impression)))
|
|
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
print("ADs Data :- ","ID:-" , ads.ad_id, "No Of Clicks :- ", ads.no_of_click, "Impressions :- ", ads.no_of_impression)
|
2024-09-04 20:17:33 +05:30
|
|
|
}
|
2024-09-23 19:45:24 +05:30
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
//Send it to server and delete from our DB
|
2024-09-04 20:17:33 +05:30
|
|
|
NetworkManager.shareInstance.nwCallRawJSONAds(adsData: userImpressions) { isDone in
|
|
|
|
|
if isDone{
|
|
|
|
|
self.deleteAdsData(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch let error
|
|
|
|
|
{
|
|
|
|
|
debugPrint(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-17 01:34:28 +05:30
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
func deleteAdsData(_ data: [AdClicksImpressions],isLogout : Bool = false) {
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
|
|
|
|
|
data.forEach { clicks in
|
|
|
|
|
managedContext.delete(clicks)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try managedContext.save()
|
|
|
|
|
getAllAdsData(isLogout: isLogout)
|
|
|
|
|
print("Deleted data")
|
|
|
|
|
} catch let error {
|
|
|
|
|
debugPrint("Failed to delete data:", error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getAllAdsData(isLogout : Bool = false) {
|
|
|
|
|
//We need to create a context from this container
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "AdClicksImpressions")
|
|
|
|
|
do {
|
|
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [AdClicksImpressions] else {return}
|
|
|
|
|
|
|
|
|
|
print("UserClicks Count from getAllData :- ", result.count)
|
|
|
|
|
//if data is less , dont keep sending the data to server
|
|
|
|
|
if isLogout == false,result.count < 2 {
|
|
|
|
|
print("Not Enough Ads Data")
|
|
|
|
|
return
|
|
|
|
|
}else{
|
|
|
|
|
// send ads data again to server
|
2024-10-01 19:57:04 +05:30
|
|
|
if result.count != 0{
|
|
|
|
|
sendAdsData()
|
|
|
|
|
}
|
2024-09-24 20:10:45 +05:30
|
|
|
print("Ads Data sent Again.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// result.forEach { clicks in
|
|
|
|
|
// // device type 1- android , 2 - iOS
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 20:17:33 +05:30
|
|
|
func checkIfAdExist(adsData : AdsClickImpressionsData) {
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "AdClicksImpressions")
|
|
|
|
|
fetchRequest.predicate = NSPredicate(format: "ad_id == %@" ,adsData.adID.toString())
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [AdClicksImpressions] else {return}
|
|
|
|
|
if result.isEmpty{
|
|
|
|
|
//create
|
|
|
|
|
PersistentStorage.shared.createAdsData(data: adsData)
|
|
|
|
|
print("create, In Exist")
|
|
|
|
|
}else{
|
|
|
|
|
//update
|
|
|
|
|
let objectUpdate = result[0] as NSManagedObject
|
|
|
|
|
print("Update, In Exist")
|
|
|
|
|
objectUpdate.setValue(result.first!.no_of_click + Int64(adsData.noOfClick), forKey: "no_of_click")
|
|
|
|
|
objectUpdate.setValue(result.first!.no_of_impression + Int64(adsData.noOfOmpression), forKey: "no_of_impression")
|
|
|
|
|
do{
|
|
|
|
|
try managedContext.save()
|
|
|
|
|
retrieveAdsData(adID: adsData.adID)
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch let error as NSError {
|
|
|
|
|
print("Could not fetch. \(error), \(error.userInfo)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
// MARK: - Add Ads to CoreData
|
|
|
|
|
|
2024-09-04 20:17:33 +05:30
|
|
|
func createAdsData(data : AdsClickImpressionsData){
|
|
|
|
|
//We need to create a context from this container
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
|
|
|
|
|
let share = AdClicksImpressions(context: managedContext)
|
|
|
|
|
share.ad_id = Int64(data.adID)
|
|
|
|
|
share.no_of_click = Int64(data.noOfClick)
|
|
|
|
|
share.no_of_impression = Int64(data.noOfOmpression)
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try managedContext.save()
|
2024-09-24 20:10:45 +05:30
|
|
|
//show the ad saved in cored data
|
2024-09-04 20:17:33 +05:30
|
|
|
retrieveAdsData(adID: data.adID)
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
print("Could not save. \(error), \(error.userInfo)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func retrieveAdsData(adID : Int?) {
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "AdClicksImpressions")
|
|
|
|
|
|
|
|
|
|
fetchRequest.predicate = NSPredicate(format: "ad_id == %@",adID?.toString() ?? 0)
|
|
|
|
|
do {
|
|
|
|
|
guard let result = try managedContext.fetch(fetchRequest) as? [AdClicksImpressions] else {return}
|
|
|
|
|
result.forEach { ads in
|
|
|
|
|
print("ID:-" , ads.ad_id, "No Of Clicks :- ", ads.no_of_click, "Impressions :- ", ads.no_of_impression)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch let error
|
|
|
|
|
{
|
|
|
|
|
debugPrint(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addAdsCount(adID : Int, impressions : Int = 0, clicks : Int = 0){
|
|
|
|
|
let adsData = AdsClickImpressionsData(adID: adID, noOfClick: clicks, noOfOmpression: impressions)
|
|
|
|
|
PersistentStorage.shared.checkIfAdExist( adsData: adsData)
|
|
|
|
|
}
|
2024-09-24 20:10:45 +05:30
|
|
|
|
|
|
|
|
}
|
2024-09-04 20:17:33 +05:30
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
// MARK: - Sync CoreData to server
|
|
|
|
|
|
|
|
|
|
extension PersistentStorage{
|
2024-09-04 20:17:33 +05:30
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
func getEntityDataCount() -> (Int,Int){
|
|
|
|
|
let managedContext = PersistentStorage.shared.context
|
|
|
|
|
let adsFetchReq:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "AdClicksImpressions")
|
|
|
|
|
let clicksFetchReq:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserClicks")
|
|
|
|
|
|
2024-09-04 20:17:33 +05:30
|
|
|
do {
|
2024-09-24 20:10:45 +05:30
|
|
|
// Perform the count request
|
|
|
|
|
let adsCount = try managedContext.count(for: adsFetchReq)
|
|
|
|
|
let clicksCount = try managedContext.count(for: clicksFetchReq)
|
|
|
|
|
return (adsCount,clicksCount)
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
print("Error fetching count: \(error), \(error.userInfo)")
|
|
|
|
|
return (0,0)
|
2024-09-04 20:17:33 +05:30
|
|
|
}
|
|
|
|
|
}
|
2024-09-23 19:45:24 +05:30
|
|
|
|
2024-09-24 20:10:45 +05:30
|
|
|
func checkLastSync(){
|
|
|
|
|
/*
|
|
|
|
|
check ad timestamp from gvar
|
|
|
|
|
show ads if the saved timestamp time and current time has diffrenece of 30 minutes
|
|
|
|
|
*/
|
|
|
|
|
if let timeStamp = K.GVar.lastDataSync{
|
|
|
|
|
/*
|
|
|
|
|
check if timestamp difference is equal and greater than 60 minutes i.e 3600 seconds
|
|
|
|
|
*/
|
|
|
|
|
let duration = DateFormatterLib.dateDifferenceINT(date1: timeStamp, date2: Date())
|
|
|
|
|
print("Last Sync Duration :- ", duration, " Seconds.")
|
|
|
|
|
|
|
|
|
|
if duration <= 3600{ //if last data sync has not
|
|
|
|
|
// dont sync data.
|
|
|
|
|
print("Last sync has not passed 1 hour")
|
|
|
|
|
}else{
|
|
|
|
|
//Sync data to server if the last sync time has been changed.
|
|
|
|
|
syncData()
|
2024-09-23 19:45:24 +05:30
|
|
|
}
|
2024-09-24 20:10:45 +05:30
|
|
|
}else{
|
|
|
|
|
/*
|
|
|
|
|
Assign the date and sync the data to server
|
|
|
|
|
*/
|
|
|
|
|
syncData()
|
2024-09-23 19:45:24 +05:30
|
|
|
}
|
|
|
|
|
}
|
2024-09-24 20:10:45 +05:30
|
|
|
|
|
|
|
|
func syncData(){
|
|
|
|
|
K.GVar.lastDataSync = Date()
|
|
|
|
|
self.sendAdsData()
|
|
|
|
|
self.sendDataToServer()
|
|
|
|
|
print("Data Synced to Server.")
|
|
|
|
|
}
|
2024-08-05 19:43:30 +05:30
|
|
|
}
|