Swift Media Player
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.
 
 
 

336 lines
12 KiB

//
// DetailViewController.swift
// kplayer
//
// Created by Marco Schmickler on 24.05.15.
// Copyright (c) 2015 Marco Schmickler. All rights reserved.
//
import UIKit
import Alamofire
class DetailViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
@IBOutlet weak var detailDescriptionLabel: UILabel!
var collectionView: UICollectionView!
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)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("refreshItems:"), name: "loadedItems", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: "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.registerClass(ItemCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.lightGrayColor()
collectionView.registerClass(HeaderCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView");
view.addSubview(collectionView)
view.autoresizesSubviews = true
collectionView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.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 = 3; //seconds
lpgr.delaysTouchesBegan = true
self.collectionView.addGestureRecognizer(lpgr);
let barbutton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: Selector("overview"));
navigationItem.rightBarButtonItems = [barbutton]
if detailItem != nil {
print("Details \(detailItem!.children)")
}
}
func overview() {
let pc = MediaPhotoController()
var i = [MediaItem]()
if let d = detailItem {
for it in d.children {
for c in it.children {
i.append(c)
}
}
pc.items = i
pc.completionHandler = {
self.dismissViewControllerAnimated(true, completion: nil);
}
let navController = UINavigationController(rootViewController: pc) // Creating a navigation controller with pc at the root of the navigation stack.
presentViewController(navController, animated: false, completion: nil)
}
}
func refreshItems(notification: NSNotification) {
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.parent! !== detail {
return
}
print("Object: \(oid.hashValue) Index \(index) Item: \(i) Parent: \(i.parent) Detail: \(detailItem)")
collectionView.performBatchUpdates({
var newItems = [NSIndexPath]()
var j = 0
if detail.children.count > index {
let path = NSIndexPath(forItem: 0, inSection: index)
self.collectionView.reloadItemsAtIndexPaths([path])
for _ in detail.children[index].children {
if j >= 1 {
newItems.append(NSIndexPath(forItem: j, inSection: index))
}
j++
}
self.collectionView.insertItemsAtIndexPaths(newItems)
}
return
}, completion: nil)
}
}
func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
if (gestureRecognizer.state != UIGestureRecognizerState.Ended) {
return;
}
let p = gestureRecognizer.locationInView(self.collectionView);
let indexPath = self.collectionView.indexPathForItemAtPoint(p);
if (indexPath == nil) {
print("couldn't find index path");
} else {
if let detail: MediaItem = self.detailItem {
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.removeAtIndex(indexPath!.item)
if let t = c.time {
let ms = Int(t * 1000)
let p = c.snapshotDirPathForVideo + "\(ms).jpg"
let pt = c.snapshotDirPathForVideo + "\(ms)_thumb.jpg"
Alamofire.request(.GET, NetworkManager.sharedInstance.baseurl + "/service/deletethumb\(p)")
Alamofire.request(.GET, NetworkManager.sharedInstance.baseurl + "/service/deletethumb\(pt)")
}
self.collectionView.reloadData()
}
}
}
}
}
}
func numberOfSectionsInCollectionView(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 == 0 {
return 1
}
return n
}
return 0
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: 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,
atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView =
collectionView.dequeueReusableSupplementaryViewOfKind(kind,
withReuseIdentifier: "HeaderView",
forIndexPath: indexPath)
as! HeaderCell
let items = detailItem!.children[indexPath.section]
headerView.setItem(items)
return headerView
default:
assert(false, "Unexpected element kind")
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if let detail: MediaItem = self.detailItem {
var items = detail.children[indexPath.section]
if (items.loaded || items.type == ItemType.VIDEO) {
if indexPath.item >= items.children.count {
print(items.name)
} else {
items = items.children[indexPath.item]
print(items.name)
}
self.currentItem = items
performSegueWithIdentifier("showVideo", sender: self)
} else {
let len = items.root.characters.count
let url = NetworkManager.sharedInstance.baseurl + "/service/listfiles" + items.fullPath
print(items)
print(url)
Alamofire.request(.GET, url).responseJSON {
(_,_,response) in
if let json = response.value {
var im = [MediaItem]()
for s in json as! [String] {
if s.lowercaseString.hasSuffix(".jpg") {
let l = s.length
let name = NSURL(fileURLWithPath: s).lastPathComponent!
var pathlen = l - len - name.length
// if (pathlen > 1000) {
print(pathlen)
print(name)
print(s)
// }
if (pathlen < 2) {
pathlen = 2
}
let path = (s as NSString).substringWithRange(NSMakeRange(len + 1, pathlen - 2))
let folderName = NSURL(fileURLWithPath: path).lastPathComponent!
let fl = path.length
let pfl = fl - folderName.length
print("\(folderName) \(pfl)")
let fpath = (s as NSString).substringWithRange(NSMakeRange(0, pfl))
let i = MediaItem(name: folderName, path: fpath, root: items.root, type: ItemType.PICS)
i.thumbUrl = "\(s)?preview=true"
if !name.hasPrefix(".") {
im.append(i)
}
}
}
let pc = MediaPhotoController()
pc.items = im
pc.completionHandler = {
self.dismissViewControllerAnimated(true, completion: nil);
}
let navController = UINavigationController(rootViewController: pc) // Creating a navigation controller with pc at the root of the navigation stack.
self.presentViewController(navController, animated: false, completion: nil)
}
}
}
}
}
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showVideo" {
let nc = segue.destinationViewController as! UINavigationController
let controller = nc.topViewController as! VideoPlayerController
controller.currentItem = self.currentItem
// controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
nc.navigationBar.barTintColor = UIColor.blackColor()
controller.completionHandler = {
() in
self.collectionView.reloadData()
self.collectionView.collectionViewLayout.invalidateLayout()
if self.currentItem!.type == ItemType.SNAPSHOT {
self.currentItem = self.currentItem!.parent
}
NetworkManager.sharedInstance.saveItem(self.currentItem!)
self.dismissViewControllerAnimated(true, completion: nil);
}
}
}
}