Files
omnivore/apple/OmnivoreKit/Sources/Services/InternalModels/InternalUserProfile.swift
2022-12-19 17:07:27 +08:00

80 lines
2.0 KiB
Swift

import CoreData
import Foundation
import Models
public struct InternalUserProfile: Identifiable, Encodable {
let userID: String
public let name: String
public let username: String
public let profileImageURL: String?
public var id: String {
userID
}
func persist(context: NSManagedObjectContext) -> NSManagedObjectID? {
var objectID: NSManagedObjectID?
context.performAndWait {
let user = asManagedObject(inContext: context)
do {
try context.save()
logger.debug("User saved succesfully")
objectID = user.objectID
} catch {
context.rollback()
logger.debug("Failed to save User: \(error.localizedDescription)")
}
}
return objectID
}
func asManagedObject(inContext context: NSManagedObjectContext) -> UserProfile {
let existing = UserProfile.lookup(byID: userID, inContext: context)
let userProfile = existing ?? UserProfile(entity: UserProfile.entity(), insertInto: context)
userProfile.userID = userID
userProfile.name = name
userProfile.username = username
userProfile.profileImageURL = profileImageURL
return userProfile
}
public static func makeSingle(_ user: UserProfile?) -> InternalUserProfile? {
if let user = user,
let userID = user.userID,
let name = user.name,
let username = user.username
{
return InternalUserProfile(
userID: userID,
name: name,
username: username,
profileImageURL: user.profileImageURL
)
}
return nil
}
public static func make(_ users: NSSet?) -> [InternalUserProfile] {
users?
.compactMap { user in
if let user = user as? UserProfile,
let userID = user.userID,
let name = user.name,
let username = user.username
{
return InternalUserProfile(
userID: userID,
name: name,
username: username,
profileImageURL: user.profileImageURL
)
}
return nil
} ?? []
}
}