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