You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
371 lines
10 KiB
371 lines
10 KiB
//
|
|
// Created by Marco Schmickler on 18.01.22.
|
|
// Copyright (c) 2022 Marco Schmickler. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import CoreData
|
|
import CoreGraphics
|
|
|
|
class DatabaseManager {
|
|
static let sharedInstance = DatabaseManager()
|
|
|
|
let managedObjectContext : NSManagedObjectContext
|
|
let persistentContainer : NSPersistentContainer
|
|
|
|
var allTags = [String]()
|
|
|
|
init() {
|
|
self.persistentContainer = KPersistentContainer(defaultContainerWithName: "kplayer")
|
|
self.managedObjectContext = self.persistentContainer.viewContext
|
|
|
|
loadAllTags()
|
|
}
|
|
|
|
func enrichItem(_ item: MediaItem) {
|
|
let fetchRequest = KItem.fetchRequest()
|
|
fetchRequest.predicate = NSPredicate(format: "name == %@ AND path == %@ AND root == %@", item.name, item.path, item.root)
|
|
|
|
let results = try! managedObjectContext.fetch(fetchRequest)
|
|
|
|
for r in results {
|
|
print(r.name)
|
|
item.favorite = r.favorite
|
|
|
|
for s in r.snapshots as! Set<KSnapshot> {
|
|
print("DB -- Found snapshot at \(s.index)")
|
|
|
|
for c in item.children {
|
|
if s.index == c.indexId {
|
|
c.objectID = s.objectID
|
|
c.time = s.time
|
|
c.length = s.length
|
|
c.loop = s.loop
|
|
c.size.width = CGFloat(s.sizex)
|
|
c.size.height = CGFloat(s.sizey)
|
|
c.offset.x = CGFloat(s.offx)
|
|
c.offset.y = CGFloat(s.offy)
|
|
c.scale = s.scale
|
|
c.rating = Int(s.rating)
|
|
|
|
for t in s.tags as! Set<KTag> {
|
|
c.tags.append(t.name!)
|
|
}
|
|
}
|
|
}
|
|
print(s)
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
func getKItem(_ item: MediaItem) -> KItem {
|
|
if let oid = item.objectID {
|
|
do {
|
|
let i = try managedObjectContext.existingObject(with: oid)
|
|
|
|
if i != nil {
|
|
return i as! KItem
|
|
}
|
|
} catch {
|
|
}
|
|
}
|
|
|
|
let fetchRequest = KItem.fetchRequest()
|
|
fetchRequest.predicate = NSPredicate(format: "name == %@ AND path == %@ AND root == %@", item.name, item.path, item.root)
|
|
|
|
let results = try! managedObjectContext.fetch(fetchRequest)
|
|
|
|
var saved = false
|
|
|
|
for r in results {
|
|
print(r.name)
|
|
saved = true
|
|
|
|
return r
|
|
}
|
|
|
|
let new = KItem(context: managedObjectContext)
|
|
new.name = item.name
|
|
new.root = item.root
|
|
new.path = item.path
|
|
new.local = item.local
|
|
new.favorite = item.favorite
|
|
new.type = item.type.rawValue
|
|
save()
|
|
|
|
return new;
|
|
}
|
|
|
|
private func save() {
|
|
do {
|
|
try managedObjectContext.save()
|
|
} catch {
|
|
print("Error")
|
|
}
|
|
}
|
|
|
|
func saveItemMetaData(_ item: MediaItem) {
|
|
if (item.type == ItemType.SNAPSHOT) {
|
|
if let oid = item.objectID {
|
|
do {
|
|
let i = try managedObjectContext.existingObject(with: oid)
|
|
|
|
if i != nil {
|
|
let snap = i as! KSnapshot
|
|
updateSnapshot(snap: snap, c: item)
|
|
print("DB -- Update snapshot at \(item.indexId)")
|
|
|
|
}
|
|
} catch {
|
|
}
|
|
}
|
|
}
|
|
|
|
if (item.type == ItemType.VIDEO) {
|
|
let k = getKItem(item)
|
|
|
|
k.favorite = item.favorite
|
|
|
|
var kstimes = Set<Int32>()
|
|
|
|
let ksitems = k.snapshots as! Set<KSnapshot>
|
|
|
|
for snap in ksitems {
|
|
print(snap)
|
|
kstimes.insert(snap.index)
|
|
}
|
|
|
|
for c in item.children {
|
|
if !kstimes.contains(Int32(c.indexId)) {
|
|
let snap = KSnapshot(context: managedObjectContext)
|
|
snap.index = Int32(c.indexId)
|
|
updateSnapshot(snap: snap, c: c)
|
|
k.addToSnapshots(snap)
|
|
|
|
print("DB -- Insert snapshot at \(c.indexId)")
|
|
}
|
|
else {
|
|
for snap in ksitems {
|
|
if snap.index == c.indexId {
|
|
updateSnapshot(snap: snap, c: c)
|
|
print("DB -- Update snapshot at \(c.indexId)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
save()
|
|
}
|
|
|
|
private func updateSnapshot(snap: KSnapshot, c: MediaItem) {
|
|
snap.time = c.time
|
|
snap.length = c.length
|
|
snap.loop = c.loop
|
|
snap.offx = Int16(c.offset.x)
|
|
snap.offy = Int16(c.offset.y)
|
|
snap.sizex = Int16(c.size.width)
|
|
snap.sizey = Int16(c.size.height)
|
|
snap.scale = c.scale
|
|
snap.rating = Int16(c.rating)
|
|
|
|
snap.thumb = c.thumbUrl
|
|
|
|
var ct = [String](c.tags)
|
|
|
|
for t in snap.tags as! Set<KTag> {
|
|
if !c.tags.contains(t.name!) {
|
|
snap.removeFromTags(t)
|
|
print("remove \(t.name!)")
|
|
}
|
|
else {
|
|
ct.removeAll(where: { e in e == t.name!})
|
|
}
|
|
}
|
|
|
|
for a in ct {
|
|
addTag(a, snapshot: snap)
|
|
print("add \(a)")
|
|
}
|
|
|
|
}
|
|
|
|
func createTag(_ answer: String) {
|
|
let tag = KTag(context: managedObjectContext)
|
|
tag.name = answer
|
|
|
|
save()
|
|
|
|
loadAllTags()
|
|
}
|
|
|
|
func loadAllTags() {
|
|
let fetchRequest = KTag.fetchRequest()
|
|
|
|
let results = try! managedObjectContext.fetch(fetchRequest)
|
|
|
|
allTags.removeAll()
|
|
|
|
for t in results {
|
|
allTags.append(t.name!)
|
|
}
|
|
}
|
|
|
|
func searchSnapshots(item: MediaItem) -> Void {
|
|
let fetch = KSnapshot.fetchRequest()
|
|
|
|
if item.tags.isEmpty {
|
|
return
|
|
}
|
|
|
|
let firstTag = item.tags[0]
|
|
item.children.removeAll()
|
|
|
|
fetch.predicate = NSPredicate(format: "ANY tags.name == %@", firstTag)
|
|
|
|
let results = try! managedObjectContext.fetch(fetch)
|
|
|
|
for s in results {
|
|
var accept = true
|
|
|
|
for t in item.tags {
|
|
var hasTag = false
|
|
for has in s.tags as! Set<KTag> {
|
|
if has.name == t {
|
|
hasTag = true
|
|
break
|
|
}
|
|
}
|
|
if !hasTag {
|
|
accept = false
|
|
}
|
|
}
|
|
|
|
if accept {
|
|
let n = loadSnapshot(s: s)
|
|
n.loaded = true
|
|
item.children.append(n)
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadTags(completionHandler: @escaping Weiter) -> Void {
|
|
var res = [MediaItem]()
|
|
|
|
let fetchRequest = KTag.fetchRequest()
|
|
|
|
let results = try! managedObjectContext.fetch(fetchRequest)
|
|
|
|
for t in results {
|
|
let tag = MediaItem(name: t.name!, path: t.name!, root: "tags", type: ItemType.TAG)
|
|
tag.loaded = true
|
|
let snapshots = t.tagged as! Set<KSnapshot>
|
|
|
|
for s in snapshots {
|
|
let sitem = loadSnapshot(s: s)
|
|
sitem.parent = tag
|
|
tag.children.append(sitem)
|
|
}
|
|
|
|
res.append(tag)
|
|
}
|
|
|
|
let m = MediaItem(name: "new", path: "new", root: "tags", type: ItemType.TAG)
|
|
m.local = true
|
|
res.append(m)
|
|
|
|
let c = MediaItem(name: "combine", path: "combine", root: "tags", type: ItemType.TAG)
|
|
c.local = true
|
|
res.append(c)
|
|
|
|
completionHandler(res)
|
|
}
|
|
|
|
private func loadSnapshot(s: KSnapshot) -> MediaItem {
|
|
let i = s.item!
|
|
let sitem = MediaItem(name: i.name!, path: i.path!, root: i.root!, type: ItemType.VIDEO)
|
|
sitem.loaded = true
|
|
sitem.compilation = true
|
|
sitem.objectID = i.objectID
|
|
|
|
let c = MediaItem(name: i.name!, path: i.path!, root: i.root!, type: ItemType.SNAPSHOT)
|
|
c.time = s.time
|
|
c.length = s.length
|
|
c.loop = s.loop
|
|
c.size.width = CGFloat(s.sizex)
|
|
c.size.height = CGFloat(s.sizey)
|
|
c.offset.x = CGFloat(s.offx)
|
|
c.offset.y = CGFloat(s.offy)
|
|
c.scale = s.scale
|
|
c.rating = Int(s.rating)
|
|
|
|
if s.thumb != nil {
|
|
c.thumbUrl = s.thumb
|
|
}
|
|
else {
|
|
c.thumbUrl = c.snapshotDirPathForVideo + "\(s.index)_thumb.jpg"
|
|
print("Thumb " + c.thumbUrl!)
|
|
}
|
|
|
|
c.indexId = Int(s.index)
|
|
c.parent = sitem
|
|
c.objectID = s.objectID
|
|
|
|
for t in s.tags as! Set<KTag> {
|
|
c.tags.append(t.name!)
|
|
}
|
|
|
|
sitem.children.append(c)
|
|
|
|
return sitem
|
|
}
|
|
|
|
func addTag(_ name: String, snapshot: KSnapshot) {
|
|
let kFetch = KTag.fetchRequest()
|
|
kFetch.predicate = NSPredicate(format: "name == %@", name)
|
|
let tags = try! managedObjectContext.fetch(kFetch)
|
|
|
|
if tags.isEmpty {
|
|
return
|
|
}
|
|
|
|
let tag = tags[0]
|
|
|
|
snapshot.addToTags(tag)
|
|
}
|
|
|
|
func addTag(_ name: String, _ item: MediaItem) {
|
|
let kFetch = KTag.fetchRequest()
|
|
kFetch.predicate = NSPredicate(format: "name == %@", name)
|
|
let tags = try! managedObjectContext.fetch(kFetch)
|
|
|
|
if tags.isEmpty {
|
|
return
|
|
}
|
|
|
|
let tag = tags[0]
|
|
|
|
let fetchRequest = KItem.fetchRequest()
|
|
fetchRequest.predicate = NSPredicate(format: "name == %@", item.name)
|
|
|
|
let results = try! managedObjectContext.fetch(fetchRequest)
|
|
|
|
for r in results {
|
|
if "\(item.root)\(item.path)" == "\(r.root!)\(r.path!)" {
|
|
|
|
let ksitems = r.snapshots as! Set<KSnapshot>
|
|
|
|
for snap in ksitems {
|
|
if snap.index == item.indexId {
|
|
snap.addToTags(tag)
|
|
print(item.name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
save()
|
|
}
|
|
}
|