Optimized Timeline context menu so that it isn't constantly scanning for article location in array

This commit is contained in:
Maurice Parker
2020-07-20 16:21:48 -05:00
parent 4bf4c6d6c2
commit e7a68f433d
4 changed files with 102 additions and 78 deletions

View File

@@ -101,28 +101,30 @@ extension Array where Element == Article {
}
func articlesAbove(article: Article) -> [Article] {
guard let position = firstIndex(of: article) else {
return []
}
guard let position = firstIndex(of: article) else { return [] }
return articlesAbove(position: position)
}
func articlesAbove(position: Int) -> [Article] {
guard position < count else { return [] }
let articlesAbove = self[..<position]
return Array(articlesAbove)
}
func articlesBelow(article: Article) -> [Article] {
guard let position = firstIndex(of: article) else {
return []
}
guard let position = firstIndex(of: article) else { return [] }
return articlesBelow(position: position)
}
func articlesBelow(position: Int) -> [Article] {
guard position < count else { return [] }
var articlesBelow = Array(self[position...])
guard !articlesBelow.isEmpty else {
return []
}
articlesBelow.removeFirst()
return articlesBelow
}
}