P4DepotArea.swift #3

  • //
  • main/
  • guest/
  • tjuricek/
  • DocHub/
  • DocHub/
  • P4DepotArea.swift
  • View
  • Commits
  • Open Download .zip Download (3 KB)
//
//  P4DepotArea.swift
//  DocHub
//
//  Created by Tristan Juricek on 6/24/14.
//  Copyright (c) 2014 Perforce. All rights reserved.
//

import Foundation

class P4DepotArea : NSObject, Area {
    
    var p4Library: P4Library
    var p4Depot: P4Depot
    
    init(p4Library p4Lib:P4Library, p4Depot p4Dep:P4Depot) {
        p4Library = p4Lib
        p4Depot = p4Dep
        
        super.init()
        
        // TODO I think I hit a bug with the compiler which causes me to avoid
        //      using lazy initialization here
        children = loadChildren()
        files = loadFiles()
        sorted = sortItems()
    }
    
    var name:String {
        return p4Depot.name
    }

    var library: Library {
        return p4Library
    }
    
    var children: Directory[] = []
    
    func loadChildren() -> Directory[] {
        var depotDirs = Array<P4DepotDir>()
        let depotPath = "\(p4Depot.depotPath)/*"
        p4Library.withConnection({
            (api:P4ClientApi) -> () in
            
            let (err, dirs) = api.dirs(depotPath)
            
            if let e = err {
                NSException(name: "DirectoryListFailed",
                    reason: e.localizedDescription, userInfo: nil).raise()
            }
            
            for p4dir in dirs {
                depotDirs.append(P4DepotDir(depotArea:self, dir:p4dir))
            }
        })
        return depotDirs
    }
    
    var files: File[] = []
    
    func loadFiles() -> File[] {
        var depotFiles = Array<P4DepotFile>()
        let depotPath = "\(p4Depot.depotPath)/*"

        p4Library.withConnection({
            (api:P4ClientApi) -> () in
            
            let (err, p4Files) = api.files(depotPath)
            
            if let e = err {
                NSException(name: "FileListFailed",
                    reason: e.localizedDescription, userInfo: nil).raise()
            }
            
            for p4file in p4Files {
                let file = P4DepotFile(depotArea:self, p4File:p4file)
                depotFiles.append(file)
            }
        })

        return depotFiles
    }
    
    var sorted:Namable[] = []
    
    func sortItems() -> Namable[] {
        var namables = Array<Namable>()
        for child in children {
            namables.append(child)
        }
        for file in files {
            namables.append(file)
        }
        let newArr : Array<Namable> = sort(namables, {
            (n1, n2) -> Bool in
            return n1.name.compare(n2.name) < 0
            })
        return newArr
    }
    
    func itemAt(index:Int) -> Namable {
        return sorted[index]
    }
}
# Change User Description Committed
#3 9583 tjuricek Set up basic directory browsing.
#2 9581 tjuricek Basic listing backend for Perforce models
#1 9560 tjuricek Setup parent reference to Library protocol, and handle using a separate model class.
Adds a "library" ID to the title bar, which may make sense.