Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Lint.pt1
  • Loading branch information
eonist committed Jul 11, 2019
1 parent 26ad8b7 commit 48927e8
Show file tree
Hide file tree
Showing 50 changed files with 646 additions and 690 deletions.
64 changes: 32 additions & 32 deletions Sources/GitSyncMac/library/CommitDP.swift
@@ -1,61 +1,61 @@
import Foundation
@testable import Utils
/**
* DISCUSSION: The reason we have prevCommits, is to provide an easy way to find which commit from a repo was added to CommitDB
* - DISCUSSION: The reason we have prevCommits, is to provide an easy way to find which commit from a repo was added to CommitDB
* CommitDB is a wrapper for git repos. Instead of querrying the many git repos at all time, we rather inteligently cache the data because some parts of the GUI frequently asks for an updated state of the last 100 commits -> this would be cpu instensive to recalculate often so we cache the data instead, and only ask the repos for data that isnt cached
* TODO: it would be significatly faster if we knew the freshesht commit for each repo. -> store a Dict of repoHash, descChronoDate -> and assert on each add wether to store a new freshest item or not
* - TODO: it would be significatly faster if we knew the freshesht commit for each repo. -> store a Dict of repoHash, descChronoDate -> and assert on each add wether to store a new freshest item or not
*/
class CommitDP:DataProvider{
static var max:Int = 100
class CommitDP: DataProvider {
static var max: Int = 100
}
extension CommitDP{
/**
* New
*/
func add(rawCommitData:String,_ repoTitle:String){
let processedCommitData:ProcessedCommitData = ProcessedCommitData(rawCommitData:rawCommitData,repoTitle)
let commitDict:[String:String] = processedCommitData.dict
func add(rawCommitData: String, _ repoTitle: String){
let processedCommitData: ProcessedCommitData = ProcessedCommitData(rawCommitData:rawCommitData,repoTitle)
let commitDict: [String: String] = processedCommitData.dict
addCommitItem(commitDict)
}
/**
* Adds An item to the sortedArr (at the correct index according to descending chronology, by using a custom binarySearch method)
* NOTE: Items must be added one after the other. A Bulk add method wouldn't work, unless you iterate one by one I guess???
* - NOTE: Items must be added one after the other. A Bulk add method wouldn't work, unless you iterate one by one I guess???
*/
func addCommitItem(_ item:[String:String]){
let closestIdx:Int = CommitDP.closestIndex(items, item, 0, items.endIndex)
if(!Utils.existAtOrBefore(items,closestIdx,item)){//TODO: ⚠️️ ideally this should be handled in the binarySearch algo, but this is a quick fix, that doesn't hurt performance
// Swift.print("📝 insert at: \(closestIdx) item.date: \(GitDateUtils.gitTime(item["sortableDate"]!))" )
self.add(item, closestIdx,false)
self.add(item, closestIdx, false)
//_ = items.insertAt(item, closestIdx)
}
if(items.count > CommitDP.max){_ = items.popLast()}/*keeps the array at max items*/
if items.count > CommitDP.max { _ = items.popLast() }/*keeps the array at max items*/
}
/**
* This binarySearch finds a suitable index to insert an item in a sorted list (a regular binarySearch would return nil if no match is found, this implmentation returns the closestIndex)
* NOTE: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array.
* NOTE: Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.
* NOTE: Binary search runs in at worst logarithmic time, making O(log n) comparisons, where n is the number of elements in the array and log is the logarithm. Binary search takes only constant (O(1)) space, meaning that the space taken by the algorithm is the same for any number of elements in the array.[5] Although specialized data structures designed for fast searching—such as hash tables—can be searched more efficiently, binary search applies to a wider range of search problems.
* NOTE: This implementation of binary search is recursive (it calls it self) (Binary search is recursive in nature because you apply the same logic over and over again to smaller and smaller subarrays.)
* IMPORTANT: ⚠️️ Although the idea is simple, implementing binary search correctly requires attention to some subtleties about its exit conditions and midpoint calculation.
* IMPORTANT: ⚠️️ Note that the numbers array is sorted. The binary search algorithm does not work otherwise!
* DISCUSSION: Is it a problem that the array must be sorted first? It depends. Keep in mind that sorting takes time -- the combination of binary search plus sorting may be slower than doing a simple linear search. Binary search shines in situations where you sort just once and then do many searches.
* TRIVIA: YOu can also implement binary serach as iterative implementation by using a while loop
* TODO: use range instead of start and end int?!?
* - NOTE: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array.
* - NOTE: Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.
* - NOTE: Binary search runs in at worst logarithmic time, making O(log n) comparisons, where n is the number of elements in the array and log is the logarithm. Binary search takes only constant (O(1)) space, meaning that the space taken by the algorithm is the same for any number of elements in the array.[5] Although specialized data structures designed for fast searching—such as hash tables—can be searched more efficiently, binary search applies to a wider range of search problems.
* - NOTE: This implementation of binary search is recursive (it calls it self) (Binary search is recursive in nature because you apply the same logic over and over again to smaller and smaller subarrays.)
* - IMPORTANT: ⚠️️ Although the idea is simple, implementing binary search correctly requires attention to some subtleties about its exit conditions and midpoint calculation.
* - IMPORTANT: ⚠️️ Note that the numbers array is sorted. The binary search algorithm does not work otherwise!
* - DISCUSSION: Is it a problem that the array must be sorted first? It depends. Keep in mind that sorting takes time -- the combination of binary search plus sorting may be slower than doing a simple linear search. Binary search shines in situations where you sort just once and then do many searches.
* - TRIVIA: YOu can also implement binary serach as iterative implementation by using a while loop
* - Fixme: use range instead of start and end int?!?
*/
static func closestIndex(_ arr:[[String:String]],_ i:[String:String],_ start:Int,_ end:Int) -> Int{
static func closestIndex(_ arr: [[String: String]], _ i: [String: String], _ start: Int, _ end: Int) -> Int {
if(start == end){
//Swift.print("❤️️ i doesn't exist, this is the closest at: \(start) ")
return start
}
let mid:Int = start + ((end - start) / 2)/*start + middle of the distance between start and end*/
let mid: Int = start + ((end - start) / 2)/*start + middle of the distance between start and end*/
//Swift.print("mid: " + "\(mid)")
//Swift.print("arr[mid]: " + "\(arr[mid])")
if(i["sortableDate"]!.int > arr[mid]["sortableDate"]!.int){/*index is in part1*/
//Swift.print("a")
return closestIndex(arr,i,start,mid)
}else if(i["sortableDate"]!.int < arr[mid]["sortableDate"]!.int){/*index is in part2*/
//Swift.print("b")
return closestIndex(arr,i,mid+1,end)
return closestIndex(arr, i, mid + 1, end)
}else{/*index is at middleIndex*/
//Swift.print("at middle: \(mid)")
return mid
Expand All @@ -65,27 +65,27 @@ extension CommitDP{
private class Utils{
/**
* Asserts if an item is at or before PARAM: idx
* NOTE: Usefull in conjunction with ArrayModifier.insertAt()// to assert if an item already exists at that idx or not. to avoid dups
* - NOTE: Usefull in conjunction with ArrayModifier.insertAt()// to assert if an item already exists at that idx or not. to avoid dups
*/
static func existAtOrBefore(_ arr:[[String:String]],_ idx:Int, _ item:[String:String]) -> Bool {
func itemAlreadyExistAtIdx()->Bool {return (arr.valid(idx) && arr[idx]["hash"] == item["hash"]) }
func itemExistsAtIdxBefore()->Bool {return (arr.valid(idx-1) && arr[idx-1]["hash"] == item["hash"])}
static func existAtOrBefore(_ arr: [[String: String]], _ idx: Int, _ item: [String: String]) -> Bool {
func itemAlreadyExistAtIdx() -> Bool { return (arr.valid(idx) && arr[idx]["hash"] == item["hash"]) }
func itemExistsAtIdxBefore() -> Bool { return (arr.valid(idx - 1) && arr[idx - 1]["hash"] == item["hash"]) }
return itemAlreadyExistAtIdx() || itemExistsAtIdxBefore()
}
/**
* New
*/
static func existAtOrAfter(_ arr:[[String:String]],_ idx:Int, _ item:[String:String]) -> Bool {
func itemAlreadyExistAtIdx()->Bool {return (arr.valid(idx) && arr[idx]["hash"] == item["hash"]) }
func itemExistsAtIdxAfter()->Bool {return (arr.valid(idx+1) && arr[idx+1]["hash"] == item["hash"])}
static func existAtOrAfter(_ arr: [[String: String]], _ idx: Int, _ item: [String: String]) -> Bool {
func itemAlreadyExistAtIdx() -> Bool {return (arr.valid(idx) && arr[idx]["hash"] == item["hash"]) }
func itemExistsAtIdxAfter() -> Bool {return (arr.valid(idx + 1) && arr[idx + 1]["hash"] == item["hash"])}
return itemAlreadyExistAtIdx() || itemExistsAtIdxAfter()
}
}
//this makes CommitDB unwrappable (XML->CommitDB)
extension CommitDP:UnWrappable{
static func unWrap<T>(_ xml:XML) -> T? {
extension CommitDP: UnWrappable{
static func unWrap<T>(_ xml: XML) -> T? {
//Swift.print("xml.xmlString.count: " + "\(xml.xmlString.count)")
let items:[[String:String]?] = unWrap(xml, "items")
let items:[[String: String]?] = unWrap(xml, "items")
//let prevCommits:[Int:String] = unWrap(xml,"prevCommits")
return CommitDP(items.flatMap{$0}/*,prevCommits*/) as? T/*flatMap is used to remove any nil values*/
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/GitSyncMac/library/Config.swift
Expand Up @@ -6,22 +6,22 @@ import Foundation
enum Config {
enum Bundle{
/*The root of the asset bundle*/
static let assets:String = FilePathParser.resourcePath + "/assets.bundle/"//rename to user maybe?
static let styles:String = FilePathParser.resourcePath + "/styles.bundle/"
static let repo:String = {/*Stores the repo details*/
static let assets: String = FilePathParser.resourcePath + "/assets.bundle/"//rename to user maybe?
static let styles: String = FilePathParser.resourcePath + "/styles.bundle/"
static let repo: String = {/*Stores the repo details*/
return assets + (Config.release == .dev ? "user/dev/" : "user/pub/") + "repos.xml"
}()
static let structure:String = assets + "structure.json"/*UI structure of the app*/
static let prefsURL:String = {/*The app prefs*/
static let structure: String = assets + "structure.json"/*UI structure of the app*/
static let prefsURL: String = {/*The app prefs*/
return assets + (Config.release == .dev ? "user/dev/" : "user/pub/") + "prefs.xml"
}()
static let commitCacheURL:String = {/*Cache.swift uses this url*/
static let commitCacheURL: String = {/*Cache.swift uses this url*/
return assets + (Config.release == .dev ? "user/dev/" : "user/pub/") + "sortedcommits.xml"
}()
static let commitCountsURL:String = {
static let commitCountsURL: String = {
return assets + (Config.release == .dev ? "user/dev/" : "user/pub/") + "commitCounts.json"
}()
}
enum ReleaseType {case dev,pub}
static let release:ReleaseType = .dev/*Toggle between development and public release*/
enum ReleaseType { case dev, pub }
static let release: ReleaseType = .dev/*Toggle between development and public release*/
}
8 changes: 4 additions & 4 deletions Sources/GitSyncMac/library/Proxy.swift
Expand Up @@ -9,7 +9,7 @@ class Proxy {
/**
* New
*/
static var styleTestView:StyleTestView? {
static var styleTestView: StyleTestView? {
do {
return try getStyleTestView()
} catch let error as NSError {
Expand All @@ -20,9 +20,9 @@ class Proxy {
/**
* New
*/
private static func getStyleTestView() throws -> StyleTestView{
guard let win:NSWindow = WinParser.focusedWindow() ?? NSApp.windows[safe:0] else {throw ProxyError.windowNotAvailable}
guard let styleTestView = win.contentView as? StyleTestView else {throw ProxyError.styleTestViewNotAvailable}
private static func getStyleTestView() throws -> StyleTestView {
guard let win: NSWindow = WinParser.focusedWindow() ?? NSApp.windows[safe: 0] else { throw ProxyError.windowNotAvailable }
guard let styleTestView = win.contentView as? StyleTestView else { throw ProxyError.styleTestViewNotAvailable }
return styleTestView
}
}
23 changes: 11 additions & 12 deletions Sources/GitSyncMac/library/repo/RepoItem+Extensions.swift
Expand Up @@ -4,24 +4,24 @@ import Foundation
* Accessors
*/
extension RepoItem{
var localPath:String {get {return local} set{local = newValue}}
var remotePath:String {get {return remote} set{remote = newValue}}
var localPath: String { get { return local } set { local = newValue } }
var remotePath: String { get { return remote } set { remote = newValue } }
}

/**
/**
* Parsers
*/
extension RepoItem{
/**
* Converts GitRepo to RepoItem
*/
var gitRepo:GitRepo {
var gitRepo: GitRepo {
return GitRepo.gitRepo(self.local, remotePath, self.branch)
}
/**
* Converts GitRepo to RepoItem
*/
static func repoItem(_ gitRepo:GitRepo) -> RepoItem {
static func repoItem(_ gitRepo: GitRepo) -> RepoItem {
var repoItem = RepoItem()
repoItem.local = gitRepo.localPath
repoItem.remote = "https://" + gitRepo.remotePath
Expand All @@ -36,8 +36,8 @@ extension RepoItem{
/**
* Basic
*/
static func repoItem(local:String,branch:String,title:String,remote:String = "") -> RepoItem{
var repoItem:RepoItem = RepoItem()
static func repoItem(local: String, branch: String, title: String, remote: String = "") -> RepoItem {
var repoItem: RepoItem = RepoItem()
repoItem.local = local
repoItem.branch = branch
repoItem.title = title
Expand All @@ -47,8 +47,8 @@ extension RepoItem{
/**
* DummyData
*/
static var dummyData:RepoItem {
return RepoItem.repoItem(local: "user file path",branch: "master",title: "Element iOS")
static var dummyData: RepoItem {
return RepoItem.repoItem(local: "user file path", branch: "master", title: "Element iOS")
}
}

Expand All @@ -59,7 +59,7 @@ extension RepoItem{
extension RepoItem{
subscript<T>(key: String) -> T? {
get {
switch key{
switch key {
case "local": return local as? T
case "branch": return branch as? T
case "title": return title as? T
Expand All @@ -73,7 +73,7 @@ extension RepoItem{
}
}
set {
switch key{
switch key {
case "local": local = newValue as! String //⚠️️ use if assert first
case "branch": branch = newValue as! String
case "title": title = newValue as! String
Expand All @@ -88,4 +88,3 @@ extension RepoItem{
}
}
}

12 changes: 2 additions & 10 deletions Sources/GitSyncMac/library/repo/RepoItem+Key.swift
@@ -1,15 +1,7 @@
import Foundation

extension RepoItem {
enum Key{
static let title = "title"
static let local = "local"
static let remote = "remote"
static let branch = "branch"
static let auto = "auto"
static let message = "message"
static let active = "active"
static let template = "template"
static let notification = "notification"
enum Key: String {
case title, local, remote, branch, auto, message, active, template, notification
}
}
22 changes: 11 additions & 11 deletions Sources/GitSyncMac/library/repo/RepoItem.swift
@@ -1,19 +1,19 @@
import Foundation

/**
* TODO: ⚠️️ rename to RepoData
* TODO: ⚠️️ Needs a unique identifier value
* Fixme: ⚠️️ rename to RepoData
* Fixme: ⚠️️ Needs a unique identifier value
*/
struct RepoItem {
var local:String/*Local path*/
var branch:String/*Repo branch, Master is default*/
var title:String/*The title displayed in the app*/
var active:Bool/*Active means that auto and pull will sync the repo*/
var remote:String/*Remote path to repository*/
var message:Bool/*Auto-created commit message*///TODO: ⚠️️ rename
var auto:Bool/*Automatically syncs on an interval*///TODO: ⚠️️ rename to interval?
var template:String/*template message for commitmessages*/
var notification:Bool/*toggle Notifications per repo*/
var local: String/*Local path*/
var branch: String/*Repo branch, Master is default*/
var title: String/*The title displayed in the app*/
var active: Bool/*Active means that auto and pull will sync the repo*/
var remote: String/*Remote path to repository*/
var message: Bool/*Auto-created commit message*///TODO: ⚠️️ rename
var auto: Bool/*Automatically syncs on an interval*///TODO: ⚠️️ rename to interval?
var template: String/*template message for commitmessages*/
var notification: Bool/*toggle Notifications per repo*/
init(){/*Default RepoItem*/
self.local = ""/*Local path*/
self.branch = ""/*Repo branch, Master is default*/
Expand Down
5 changes: 2 additions & 3 deletions Sources/GitSyncMac/library/repo/utils/RepoFolderType.swift
@@ -1,7 +1,6 @@
import Foundation

enum RepoFolderType:String{
case isOpen = "isOpen"
case hasChildren = "hasChildren"
enum RepoFolderType: String{
case isOpen, hasChildren
}
//move this to Key inside RepoItem

0 comments on commit 48927e8

Please sign in to comment.