Simplify things.
This commit is contained in:
3 files changed
+28
-84
No files matched your search
@@ -160,19 +160,19 @@ final public class TheTVDBService {
|
||||
|
||||
private var token: Token? {
|
||||
get {
|
||||
var token: Token?
|
||||
tokenQueue.sync {
|
||||
return tokenQueue.sync {
|
||||
if let result = savedToken, result.timestamp + 60 * 60 * 4 > Date.timeIntervalSinceReferenceDate {
|
||||
token = result
|
||||
return result
|
||||
}
|
||||
else if let result = login() {
|
||||
UserDefaults.standard.set(result.key, forKey: "SBTheTVBDToken")
|
||||
UserDefaults.standard.set(result.timestamp, forKey: "SBTheTVBDTokenTimestamp")
|
||||
savedToken = result
|
||||
token = result
|
||||
return result
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return token
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
-52
@@ -186,42 +186,29 @@ final class Queue {
|
||||
|
||||
var status: Status {
|
||||
get {
|
||||
var status: Status = .completed
|
||||
arrayQueue.sync {
|
||||
status = self.statusInternal
|
||||
}
|
||||
return status
|
||||
return arrayQueue.sync { self.statusInternal }
|
||||
}
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
get {
|
||||
var count = 0
|
||||
arrayQueue.sync {
|
||||
count = items.count
|
||||
}
|
||||
return count
|
||||
return arrayQueue.sync { items.count }
|
||||
}
|
||||
}
|
||||
|
||||
var readyCount: Int {
|
||||
get {
|
||||
var count = 0
|
||||
arrayQueue.sync {
|
||||
count = items.filter { $0.status == .ready }.count
|
||||
}
|
||||
return count
|
||||
return arrayQueue.sync { items.filter { $0.status == .ready }.count }
|
||||
}
|
||||
}
|
||||
|
||||
private var firstItemInQueue: QueueItem? {
|
||||
get {
|
||||
var first: QueueItem?
|
||||
arrayQueue.sync {
|
||||
first = items.first(where: { $0.status == .ready })
|
||||
return arrayQueue.sync {
|
||||
let first = items.first(where: { $0.status == .ready })
|
||||
first?.status = .working
|
||||
return first
|
||||
}
|
||||
return first
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,53 +221,31 @@ final class Queue {
|
||||
}
|
||||
|
||||
func item(at index: Int) -> QueueItem {
|
||||
var result: QueueItem?
|
||||
arrayQueue.sync {
|
||||
result = items[index]
|
||||
}
|
||||
return result!
|
||||
return arrayQueue.sync { items[index] }
|
||||
}
|
||||
|
||||
func items(at indexes: IndexSet) -> [QueueItem] {
|
||||
var result: [QueueItem] = []
|
||||
arrayQueue.sync {
|
||||
result = indexes.map { items[$0] }
|
||||
}
|
||||
return result
|
||||
return arrayQueue.sync { indexes.map { items[$0] } }
|
||||
}
|
||||
|
||||
func index(of item: QueueItem) -> Int {
|
||||
var index = -1
|
||||
arrayQueue.sync {
|
||||
index = items.firstIndex(of: item) ?? -1
|
||||
}
|
||||
return index
|
||||
return arrayQueue.sync { items.firstIndex(of: item) ?? -1 }
|
||||
}
|
||||
|
||||
func indexesOfItems(with status: QueueItem.Status) -> IndexSet {
|
||||
var indexes: [Int] = []
|
||||
arrayQueue.sync {
|
||||
indexes = items.enumerated().filter { $0.element.status == status } .map { $0.offset }
|
||||
}
|
||||
return IndexSet(indexes)
|
||||
return arrayQueue.sync { IndexSet(items.enumerated().filter { $0.element.status == status } .map { $0.offset }) }
|
||||
}
|
||||
|
||||
func insert(_ item: QueueItem, at index: Int) {
|
||||
arrayQueue.sync {
|
||||
items.insert(item, at: index)
|
||||
}
|
||||
arrayQueue.sync { items.insert(item, at: index) }
|
||||
}
|
||||
|
||||
func remove(at indexes: IndexSet) {
|
||||
arrayQueue.sync {
|
||||
items = IndexSet(items.indices).subtracting(indexes).map { items[$0] }
|
||||
}
|
||||
arrayQueue.sync { items = IndexSet(items.indices).subtracting(indexes).map { items[$0] } }
|
||||
}
|
||||
|
||||
func swapAt(_ i: Int, _ j: Int) {
|
||||
arrayQueue.sync {
|
||||
items.swapAt(i, j)
|
||||
}
|
||||
arrayQueue.sync { items.swapAt(i, j) }
|
||||
}
|
||||
|
||||
func remove(_ item: QueueItem) {
|
||||
@@ -292,12 +257,11 @@ final class Queue {
|
||||
}
|
||||
|
||||
func removeCompletedItems() -> IndexSet {
|
||||
var indexes = IndexSet()
|
||||
arrayQueue.sync {
|
||||
indexes = IndexSet(items.enumerated().filter { $0.element.status == .completed } .map { $0.offset })
|
||||
return arrayQueue.sync {
|
||||
let indexes = IndexSet(items.enumerated().filter { $0.element.status == .completed } .map { $0.offset })
|
||||
items = IndexSet(items.indices).subtracting(indexes).map { items[$0] }
|
||||
return indexes
|
||||
}
|
||||
return indexes
|
||||
}
|
||||
|
||||
//MARK: Sleep
|
||||
|
||||
+7
-27
@@ -31,16 +31,10 @@ import MP42Foundation
|
||||
|
||||
@objc dynamic var destURL: URL {
|
||||
get {
|
||||
var result: URL = fileURL
|
||||
queue.sync {
|
||||
result = destURLInternal
|
||||
}
|
||||
return result
|
||||
return queue.sync { destURLInternal }
|
||||
}
|
||||
set (value) {
|
||||
queue.sync {
|
||||
destURLInternal = value
|
||||
}
|
||||
queue.sync { destURLInternal = value }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,16 +107,10 @@ import MP42Foundation
|
||||
|
||||
@objc dynamic var status: Status {
|
||||
get {
|
||||
var status: Status = .ready
|
||||
queue.sync {
|
||||
status = statusInternal
|
||||
}
|
||||
return status
|
||||
return queue.sync { statusInternal }
|
||||
}
|
||||
set (newStatus) {
|
||||
queue.sync {
|
||||
statusInternal = newStatus
|
||||
}
|
||||
queue.sync { statusInternal = newStatus }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,27 +118,19 @@ import MP42Foundation
|
||||
|
||||
@objc dynamic var actions: [QueueActionProtocol] {
|
||||
get {
|
||||
var result: [QueueActionProtocol] = []
|
||||
queue.sync {
|
||||
result = actionsInternal
|
||||
}
|
||||
return result
|
||||
return queue.sync { actionsInternal }
|
||||
}
|
||||
}
|
||||
|
||||
func addAction(_ action: QueueActionProtocol) {
|
||||
willChangeValue(for: \.actions)
|
||||
queue.sync {
|
||||
actionsInternal.append(action)
|
||||
}
|
||||
queue.sync { actionsInternal.append(action) }
|
||||
didChangeValue(for: \.actions)
|
||||
}
|
||||
|
||||
func removeAction(at index: Int) {
|
||||
willChangeValue(for: \.actions)
|
||||
_ = queue.sync {
|
||||
actionsInternal.remove(at: index)
|
||||
}
|
||||
_ = queue.sync { actionsInternal.remove(at: index) }
|
||||
didChangeValue(for: \.actions)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user