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.
533 lines
20 KiB
533 lines
20 KiB
//
|
|
// DetailViewController.swift
|
|
// kplayer
|
|
//
|
|
// Created by Marco Schmickler on 24.05.15.
|
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Alamofire
|
|
import FileBrowser
|
|
import AVFoundation
|
|
import ZIPFoundation
|
|
import SwiftUI
|
|
|
|
protocol DetailDelegate {
|
|
func loadDetails(selectedItem: MediaItem, completionHandler: @escaping () -> Void)
|
|
func deleteThumb(selectedItem: MediaItem)
|
|
func deleteLocal(selectedItem: MediaItem)
|
|
func saveItem(selectedItem: MediaItem)
|
|
}
|
|
|
|
class DetailViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDragDelegate {
|
|
|
|
var delegate : DetailDelegate?
|
|
@IBOutlet weak var detailDescriptionLabel: UILabel!
|
|
|
|
var showFavoritesOnly = false
|
|
var collectionView: UICollectionView!
|
|
|
|
var videoplayer = false
|
|
|
|
var currentItem: MediaItem?
|
|
|
|
var defaultItemSize = CGSize(width: (15 * 16) - 6, height: 15 * 9)
|
|
|
|
var detailItem: MediaItem? {
|
|
didSet {
|
|
print(detailItem!.children)
|
|
if collectionView != nil {
|
|
collectionView.reloadData()
|
|
}
|
|
}
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
super.init(coder: aDecoder)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(refreshItems(_:)), name: NSNotification.Name(rawValue: "loadedItems"), object: nil)
|
|
}
|
|
|
|
deinit {
|
|
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "loadedItems"), object: nil)
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
|
|
|
|
layout.sectionInset = UIEdgeInsets(top: 1, left: 0, bottom: 1, right: 0)
|
|
layout.minimumInteritemSpacing = 0.0;
|
|
|
|
layout.itemSize = defaultItemSize
|
|
layout.headerReferenceSize = CGSize(width: 50, height: 50)
|
|
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
|
|
|
|
collectionView.dataSource = self
|
|
collectionView.delegate = self
|
|
collectionView.register(ItemCell.self, forCellWithReuseIdentifier: "Cell")
|
|
collectionView.backgroundColor = UIColor.lightGray
|
|
collectionView.register(HeaderCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderView");
|
|
collectionView.dragInteractionEnabled = true
|
|
collectionView.dragDelegate = self
|
|
|
|
view.addSubview(collectionView)
|
|
view.autoresizesSubviews = true
|
|
collectionView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
|
|
// Do any additional setup after loading the view, typically from a nib.
|
|
|
|
collectionView.reloadData()
|
|
|
|
// attach long press gesture to collectionView
|
|
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
|
|
lpgr.minimumPressDuration = 1;
|
|
//seconds
|
|
lpgr.delaysTouchesBegan = true
|
|
self.collectionView.addGestureRecognizer(lpgr);
|
|
|
|
let settingsButton = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(settings));
|
|
let overviewButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(overview));
|
|
let favButton = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(favorites));
|
|
let vidButton = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(vplayer));
|
|
let browserButton = UIBarButtonItem(barButtonSystemItem: .organize, target: self, action: #selector(fileBrowser));
|
|
navigationItem.rightBarButtonItems = [settingsButton, vidButton, favButton, overviewButton,browserButton]
|
|
if detailItem != nil {
|
|
print("Details \(detailItem!.children)")
|
|
}
|
|
}
|
|
|
|
// https://github.com/marmelroy/FileBrowser
|
|
@objc func fileBrowser() {
|
|
let d = FileHelper.getDocumentsDirectory()
|
|
|
|
do {
|
|
let i = try FileHelper.createDir(name: "incoming")
|
|
|
|
let files = FileHelper.listFiles(name: "")
|
|
for f in files {
|
|
if f.pathExtension == "mp4" {
|
|
var dest = i.appendingPathComponent(f.lastPathComponent)
|
|
try FileManager.default.moveItem(at: f, to: dest)
|
|
try FileHelper.setExcludeFromiCloudBackup(&dest, isExcluded: true)
|
|
}
|
|
}
|
|
} catch {
|
|
}
|
|
|
|
do {
|
|
let i = try FileHelper.createDir(name: "images")
|
|
|
|
let files = FileHelper.listFiles(name: "download")
|
|
for f in files {
|
|
print(f)
|
|
if f.pathExtension == "zip" {
|
|
var dest = i.appendingPathComponent(f.lastPathComponent)
|
|
|
|
do {
|
|
try FileManager.default.createDirectory(at: dest, withIntermediateDirectories: true, attributes: nil)
|
|
try FileManager.default.unzipItem(at: f, to: dest)
|
|
} catch {
|
|
print("Extraction of ZIP archive failed with error:\(error)")
|
|
}
|
|
try FileManager.default.removeItem(at: f)
|
|
try FileHelper.setExcludeFromiCloudBackup(&dest, isExcluded: true)
|
|
}
|
|
}
|
|
} catch {
|
|
}
|
|
|
|
let fileBrowser = FileBrowser(initialPath: d, allowEditing: true)
|
|
fileBrowser.modalPresentationStyle = .fullScreen
|
|
|
|
present(fileBrowser, animated: true, completion: nil)
|
|
}
|
|
|
|
@objc func vplayer() {
|
|
videoplayer = !videoplayer
|
|
}
|
|
|
|
@objc func favorites() {
|
|
showFavoritesOnly = !showFavoritesOnly
|
|
collectionView.reloadData()
|
|
}
|
|
|
|
@objc func settings() {
|
|
let kv = KSettingsView(kSettings: NetworkManager.sharedInstance.settings, completionHandler: {
|
|
self.dismiss(animated: true, completion: nil);
|
|
} )
|
|
let pc = UIHostingController(rootView: kv)
|
|
let navController = UINavigationController(rootViewController: pc) // Creating a navigation controller with pc at the root of the navigation stack.
|
|
navController.modalPresentationStyle = .fullScreen
|
|
|
|
present(navController, animated: false, completion: nil)
|
|
}
|
|
|
|
@objc func overview() {
|
|
|
|
var i = [MediaItem]()
|
|
|
|
if let d = detailItem {
|
|
if (d.local) {
|
|
showComposition(d)
|
|
return
|
|
}
|
|
let pc = MediaPhotoController()
|
|
|
|
for it in d.children {
|
|
if it.children.count > 1 || !showFavoritesOnly {
|
|
for c in it.children {
|
|
i.append(c)
|
|
}
|
|
}
|
|
}
|
|
|
|
pc.items = i
|
|
pc.completionHandler = {
|
|
self.dismiss(animated: true, completion: nil);
|
|
}
|
|
let navController = UINavigationController(rootViewController: pc) // Creating a navigation controller with pc at the root of the navigation stack.
|
|
navController.modalPresentationStyle = .fullScreen
|
|
|
|
present(navController, animated: false, completion: nil)
|
|
}
|
|
}
|
|
|
|
private func showComposition(_ item: MediaItem) {
|
|
var assets = [URL]()
|
|
|
|
for d in item.children {
|
|
assets.append(d.playerURL!)
|
|
}
|
|
|
|
let vc = VideoController()
|
|
|
|
let item = MediaItem(name: item.name, path: item.path, root: item.root, type: ItemType.VIDEO)
|
|
|
|
vc.setItems(items: [item])
|
|
vc.setCurrentItem(item: item)
|
|
vc.urls = assets
|
|
|
|
vc.setCompletionHandler(handler: {
|
|
self.dismiss(animated: true, completion: nil);
|
|
})
|
|
|
|
let navController = UINavigationController(rootViewController: (vc as UIViewController))
|
|
navController.modalPresentationStyle = .fullScreen
|
|
navController.modalPresentationCapturesStatusBarAppearance = true
|
|
navController.navigationBar.barTintColor = UIColor.black
|
|
(vc as UIViewController).navigationItem.leftItemsSupplementBackButton = true
|
|
|
|
self.present(navController, animated: false, completion: nil)
|
|
|
|
}
|
|
|
|
@objc func refreshItems(_ notification: Notification) {
|
|
|
|
if notification.object == nil {
|
|
if self.collectionView != nil {
|
|
self.collectionView.reloadData()
|
|
}
|
|
return
|
|
}
|
|
|
|
let i = notification.object as! MediaItem
|
|
let index = i.index
|
|
|
|
let oid = ObjectIdentifier(self)
|
|
|
|
if let detail: MediaItem = self.detailItem {
|
|
if i.type == ItemType.VIDEO {
|
|
let set = IndexSet(integer: index)
|
|
if index < detail.children.count {
|
|
if (collectionView != nil) {
|
|
collectionView.reloadSections(set)
|
|
}
|
|
}
|
|
} else {
|
|
if i.parent! !== detail {
|
|
return
|
|
}
|
|
|
|
print("Object: \(oid.hashValue) Index \(index) Item: \(i) Parent: \(i.parent) Detail: \(detailItem)")
|
|
|
|
collectionView.performBatchUpdates({
|
|
var newItems = [IndexPath]()
|
|
var j = 0
|
|
|
|
if detail.children.count > index {
|
|
let path = IndexPath(item: 0, section: index)
|
|
self.collectionView.reloadItems(at: [path])
|
|
|
|
for _ in detail.children[index].children {
|
|
if j >= 1 {
|
|
newItems.append(IndexPath(item: j, section: index))
|
|
}
|
|
j += 1
|
|
}
|
|
self.collectionView.insertItems(at: newItems)
|
|
}
|
|
return
|
|
}, completion: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
|
|
if (gestureRecognizer.state != UIGestureRecognizer.State.ended) {
|
|
return;
|
|
}
|
|
let p = gestureRecognizer.location(in: self.collectionView);
|
|
|
|
let indexPath = self.collectionView.indexPathForItem(at: p);
|
|
if (indexPath == nil) {
|
|
print("couldn't find index path");
|
|
} else {
|
|
if let detail: MediaItem = self.detailItem {
|
|
let refreshAlert = UIAlertController(title: "Delete", message: "All data will be lost.", preferredStyle: UIAlertController.Style.alert)
|
|
|
|
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
|
|
|
|
let items = detail.children[indexPath!.section]
|
|
if (items.loaded) {
|
|
if items.children.count == 0 {
|
|
} else {
|
|
if indexPath!.item >= items.children.count {
|
|
} else {
|
|
let c = items.children.remove(at: indexPath!.item)
|
|
|
|
self.delegate!.deleteThumb(selectedItem: c)
|
|
self.collectionView.reloadData()
|
|
}
|
|
}
|
|
}
|
|
else if items.local {
|
|
self.delegate!.deleteLocal(selectedItem: items)
|
|
}
|
|
}))
|
|
|
|
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
|
|
print("Handle Cancel Logic here")
|
|
}))
|
|
|
|
present(refreshAlert, animated: true, completion: nil)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
|
if let detail: MediaItem = self.detailItem {
|
|
let cnt = detail.children.count
|
|
return cnt
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
if let detail: MediaItem = self.detailItem {
|
|
let n = detail.children[section].children.count
|
|
|
|
if n < 2 && showFavoritesOnly {
|
|
return 0
|
|
}
|
|
|
|
if n == 0 && !showFavoritesOnly {
|
|
return 1
|
|
}
|
|
return n
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ItemCell
|
|
|
|
if let detail: MediaItem = self.detailItem {
|
|
let items = detail.children[indexPath.section]
|
|
if items.children.count == 0 {
|
|
cell.setItem(items)
|
|
} else {
|
|
if indexPath.item >= items.children.count {
|
|
cell.setItem(items)
|
|
} else {
|
|
let item = items.children[indexPath.item]
|
|
cell.setItem(item)
|
|
}
|
|
}
|
|
}
|
|
return cell
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView,
|
|
viewForSupplementaryElementOfKind kind: String,
|
|
at indexPath: IndexPath) -> UICollectionReusableView {
|
|
switch kind {
|
|
case UICollectionView.elementKindSectionHeader:
|
|
let headerView =
|
|
collectionView.dequeueReusableSupplementaryView(ofKind: kind,
|
|
withReuseIdentifier: "HeaderView",
|
|
for: indexPath)
|
|
as! HeaderCell
|
|
let items = detailItem!.children[indexPath.section]
|
|
|
|
headerView.setItem(items)
|
|
return headerView
|
|
default:
|
|
fatalError("Unexpected element kind")
|
|
}
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
if let detail: MediaItem = self.detailItem {
|
|
print(detail.toJSON())
|
|
// NetworkManager.sharedInstance.saveFavDir(name: "fav.json", item: detail)
|
|
|
|
var sectionItem = detail.children[indexPath.section]
|
|
currentItem = sectionItem
|
|
var selectedItem = currentItem!
|
|
|
|
let weiter: () -> Void = {
|
|
|
|
if (sectionItem.type == ItemType.VIDEO || sectionItem.loaded) {
|
|
if indexPath.item >= sectionItem.children.count {
|
|
print(sectionItem.name)
|
|
} else {
|
|
selectedItem = sectionItem.children[indexPath.item]
|
|
print(selectedItem.name)
|
|
}
|
|
}
|
|
|
|
if sectionItem.isVideo() {
|
|
self.showVideo(selectedItem: selectedItem)
|
|
} else if sectionItem.isPic() {
|
|
self.showPhotos(sectionItem.children)
|
|
} else if sectionItem.isWeb() {
|
|
self.showWeb(selectedItem: selectedItem)
|
|
}
|
|
|
|
if sectionItem.type == ItemType.DOWNLOAD {
|
|
let cancelAlert = UIAlertController(title: "Abort Download", message: "All data will be lost.", preferredStyle: UIAlertController.Style.alert)
|
|
|
|
cancelAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
|
|
sectionItem.cancelled = true
|
|
if sectionItem.length == 1.0 {
|
|
NetworkManager.sharedInstance.currentDownloads.removeValue(forKey: sectionItem.name)
|
|
}
|
|
}))
|
|
|
|
cancelAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
|
|
print("Handle Cancel Logic here")
|
|
}))
|
|
|
|
self.present(cancelAlert, animated: true)
|
|
}
|
|
}
|
|
|
|
delegate!.loadDetails(selectedItem: currentItem!, completionHandler: weiter)
|
|
|
|
}
|
|
}
|
|
|
|
func showPhotos(_ im: [MediaItem]) {
|
|
let pc = MediaPhotoController()
|
|
|
|
pc.items = im
|
|
pc.completionHandler = {
|
|
self.dismiss(animated: true, completion: nil);
|
|
}
|
|
let navController = UINavigationController(rootViewController: pc) // Creating a navigation controller with pc at the root of the navigation stack.
|
|
navController.modalPresentationStyle = .fullScreen
|
|
|
|
self.present(navController, animated: false, completion: nil)
|
|
}
|
|
|
|
func showWeb(selectedItem: MediaItem) {
|
|
if selectedItem.name.contains("strip") {
|
|
|
|
let chromeURL = selectedItem.name.replacingOccurrences(of: "https://", with: "googlechrome://")
|
|
UIApplication.shared.openURL(URL(string: chromeURL)!)
|
|
|
|
return
|
|
}
|
|
let pc = BrowserController()
|
|
pc.setCurrentItem(item: selectedItem)
|
|
|
|
pc.completionHandler = {
|
|
self.dismiss(animated: true, completion: nil);
|
|
}
|
|
let navController = UINavigationController(rootViewController: pc) // Creating a navigation controller with pc at the root of the navigation stack.
|
|
navController.modalPresentationStyle = .fullScreen
|
|
|
|
self.present(navController, animated: false, completion: nil)
|
|
}
|
|
|
|
func showVideo(selectedItem: MediaItem) {
|
|
var se = selectedItem
|
|
var children = detailItem!.children
|
|
if videoplayer {
|
|
if selectedItem.local {
|
|
let jfile = selectedItem.playerURL!.appendingPathExtension("json")
|
|
do {
|
|
let jsonData = try Data(contentsOf: jfile)
|
|
|
|
let items = try JSONDecoder().decode(MediaModel.self, from: jsonData)
|
|
se = MediaItem(model: items)
|
|
children = se.children
|
|
|
|
print (String(data: jsonData, encoding: .utf8))
|
|
} catch {
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
var pc: ItemController?
|
|
|
|
pc = VideoController()
|
|
|
|
pc!.setCurrentItem(item: se)
|
|
pc!.setItems(items: children)
|
|
pc!.setCompletionHandler(handler: {
|
|
self.collectionView.reloadData()
|
|
self.collectionView.collectionViewLayout.invalidateLayout()
|
|
|
|
if !se.local {
|
|
self.delegate!.saveItem(selectedItem: se)
|
|
}
|
|
self.dismiss(animated: true, completion: nil);
|
|
})
|
|
let navController = UINavigationController(rootViewController: (pc! as! UIViewController))
|
|
navController.modalPresentationStyle = .fullScreen
|
|
navController.modalPresentationCapturesStatusBarAppearance = true
|
|
navController.navigationBar.barTintColor = UIColor.black
|
|
(pc! as! UIViewController).navigationItem.leftItemsSupplementBackButton = true
|
|
|
|
self.present(navController, animated: false, completion: nil)
|
|
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
|
if let detail: MediaItem = self.detailItem {
|
|
var selectedItem = detail.children[indexPath.section]
|
|
|
|
if selectedItem.children.count > indexPath.item {
|
|
selectedItem = selectedItem.children[indexPath.count]
|
|
}
|
|
|
|
if !selectedItem.local {
|
|
return []
|
|
}
|
|
|
|
let itemProvider = NSItemProvider(object: selectedItem.toJSON() as NSString)
|
|
return [UIDragItem(itemProvider: itemProvider)]
|
|
}
|
|
return []
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd session: UIDragSession) {
|
|
collectionView.reloadData()
|
|
}
|
|
}
|
|
|