From aaaa32de01e05d0c5eb9b84cc2997acead097fac Mon Sep 17 00:00:00 2001 From: Aaron Wright Date: Thu, 19 Apr 2018 04:17:43 -0500 Subject: [PATCH] Remove selection --- Token/Info.plist | 2 +- TokenMobile/Info.plist | 2 +- TokenShared/Array+Cacheable.swift | 38 +++++++-------- TokenShared/Cache.swift | 80 +------------------------------ TokenShared/CacheAction.swift | 7 --- TokenShared/CacheReducer.swift | 7 --- 6 files changed, 23 insertions(+), 113 deletions(-) diff --git a/Token/Info.plist b/Token/Info.plist index 40dc975..cc10ad7 100644 --- a/Token/Info.plist +++ b/Token/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.2.0 + 1.2.1 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSHumanReadableCopyright diff --git a/TokenMobile/Info.plist b/TokenMobile/Info.plist index db4caab..c6732b7 100644 --- a/TokenMobile/Info.plist +++ b/TokenMobile/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.2.0 + 1.2.1 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/TokenShared/Array+Cacheable.swift b/TokenShared/Array+Cacheable.swift index 62fc18e..fb0d38c 100644 --- a/TokenShared/Array+Cacheable.swift +++ b/TokenShared/Array+Cacheable.swift @@ -1,5 +1,5 @@ // -// Array+Cacheable.swift +// Array+cacheable.swift // Token // // Created by Paul Foster on 1/31/18. @@ -16,55 +16,55 @@ public extension Array where Element == Cacheable { public func items() -> [T] { return compactMap { $0 as? T } } public func items(for uuids: [UUID]) -> [Cacheable] { return filter { uuids.contains($0.uuid) } } - public func contains(_ Cacheable: Cacheable) -> Bool { return uuids().contains(Cacheable.uuid) } + public func contains(_ cacheable: Cacheable) -> Bool { return uuids().contains(cacheable.uuid) } public func item(for UUID: UUID) -> T? { - return reduce(nil, { (result, Cacheable) -> T? in + return reduce(nil, { (result, cacheable) -> T? in if let result = result { return result } - if Cacheable.uuid != UUID { return nil } - return Cacheable as? T + if cacheable.uuid != UUID { return nil } + return cacheable as? T }) } public func item(for UUID: UUID) -> Cacheable? { - return reduce(nil, { (result, Cacheable) -> Cacheable? in + return reduce(nil, { (result, cacheable) -> Cacheable? in if let result = result { return result } - if Cacheable.uuid != UUID { return nil } - return Cacheable + if cacheable.uuid != UUID { return nil } + return cacheable }) } // MARK: - Maps - internal func merging(_ Cacheable: Cacheable) -> [Cacheable] { + internal func merging(_ cacheable: Cacheable) -> [Cacheable] { var found: Bool = false let result: [Cacheable] = map { - if $0 == Cacheable { + if $0 == cacheable { found = true - return Cacheable + return cacheable } else { return $0 } } - if !found { return result.appending(Cacheable) } else { return result } + if !found { return result.appending(cacheable) } else { return result } } - internal func merging(_ Cacheables: [Cacheable]) -> [Cacheable] { + internal func merging(_ cacheable: [Cacheable]) -> [Cacheable] { var result = self - result.merge(Cacheables) + result.merge(cacheable) return result } internal func removing(_ Cacheable: Cacheable) -> [Cacheable] { return filter { $0 != Cacheable } } - internal func removing(_ Cacheables: [Cacheable]) -> [Cacheable] { return filter { !Cacheables.contains($0) } } + internal func removing(_ cacheable: [Cacheable]) -> [Cacheable] { return filter { !cacheable.contains($0) } } // MARK: - Mutations - internal mutating func remove(_ Cacheables: [Cacheable]) { self = removing(Cacheables) } - internal mutating func remove(_ Cacheable: Cacheable) { self = removing(Cacheable) } - internal mutating func merge(_ Cacheable: Cacheable) { self = merging(Cacheable) } - internal mutating func merge(_ Cacheables: [Cacheable]) { Cacheables.forEach { self.merge($0) } } + internal mutating func remove(_ cacheable: [Cacheable]) { self = removing(cacheable) } + internal mutating func remove(_ cacheable: Cacheable) { self = removing(cacheable) } + internal mutating func merge(_ cacheable: Cacheable) { self = merging(cacheable) } + internal mutating func merge(_ cacheable: [Cacheable]) { cacheable.forEach { self.merge($0) } } } diff --git a/TokenShared/Cache.swift b/TokenShared/Cache.swift index 951761d..ad20ba7 100644 --- a/TokenShared/Cache.swift +++ b/TokenShared/Cache.swift @@ -10,98 +10,22 @@ import Foundation public struct Cache: State { - var cache: [Cacheable] - var selected: [Cacheable] - - public init() { - self.cache = [] - self.selected = [] - } + var cache: [Cacheable] = [] } extension Cache { - func selectAndReplace(_ cacheables: [T]) -> Cache { return mutated { $0.selected = cacheables } } - func selectAndMerge(_ cacheables: [T]) -> Cache { return mutated { $0.selected.merge(cacheables) } } - func selectAll() -> Cache { return mutated { $0.selected.merge(cache) } } - func deselect(_ cacheables: [T]) -> Cache { return mutated { $0.selected.remove(cacheables) } } - func deselectAll() -> Cache { return mutated { $0.selected = [] } } func add(_ cacheables: [T]) -> Cache { return mutated { $0.cache.merge(cacheables) } } func merge(_ cacheables: [T]) -> Cache { return mutated { $0.cache.merge(cacheables) } } func remove(_ cacheables: [T]) -> Cache { return mutated { $0.cache.remove(cacheables) } } func clear() -> Cache { return mutated { $0.cache = [] } } - func selectPrevious() -> Cache { - var indexes: [Int] = [] - for (index, item) in self.cache.enumerated() { - if self.selected.contains(item) { - indexes.append(index) - } - } - - return mutated { - $0.selected = indexes.map({ (index) -> Cacheable in - let newIndex = index - 1 == -1 ? self.cache.endIndex - 1 : index - 1 - return self.cache[newIndex] - }) - } - } - - func selectNext() -> Cache { - var indexes: [Int] = [] - for (index, item) in self.cache.enumerated() { - if self.selected.contains(item) { - indexes.append(index) - } - } - - return mutated { - $0.selected = indexes.map({ (index) -> Cacheable in - let newIndex = index + 1 == self.cache.count ? self.cache.startIndex : index + 1 - return self.cache[newIndex] - }) - } - } - } extension Cache { - public func cachedItems() -> [T] { return cache as! [T] } + public func items() -> [T] { return cache as! [T] } public func item(for UUID: UUID) -> T? { return cache.item(for: UUID) } - public func selectedUUIDs() -> [UUID] { return selected.uuids() } - public func deselectedUUIDs() -> [UUID] { return deselectedItems().map { $0.uuid } } - public func selectedItems() -> [T] { return selected as! [T] } - public func selectedItem() -> T? { return selectedItems().first } - public func deselectedItems() -> [T] { return cache.removing(selected) as! [T] } - - public func selectedIndexes(in sequence: [T]) -> IndexSet { - return sequence.enumerated().reduce(IndexSet()) { - guard selected.contains($1.element) else { return $0 } - return $0.inserting($1.offset) - } - } - - public func deselectedIndexes(in sequence: [T]) -> IndexSet { - return sequence.enumerated().reduce(IndexSet()) { - guard !selected.contains($1.element) else { return $0 } - return $0.inserting($1.offset) - } - } - - public func selectedIndexes() -> IndexSet { - return cache.enumerated().reduce(IndexSet()) { - guard selected.contains($1.element) else { return $0 } - return $0.inserting($1.offset) - } - } - - public func deselectedIndexes() -> IndexSet { - return cache.enumerated().reduce(IndexSet()) { - guard !selected.contains($1.element) else { return $0 } - return $0.inserting($1.offset) - } - } } diff --git a/TokenShared/CacheAction.swift b/TokenShared/CacheAction.swift index d7c110c..20ea56e 100644 --- a/TokenShared/CacheAction.swift +++ b/TokenShared/CacheAction.swift @@ -9,13 +9,6 @@ import Foundation public enum CacheAction: Action where T: Cacheable { - case selectAndMerge([T]) - case selectAndReplace([T]) - case selectPrevious - case selectNext - case selectAll - case deselect([T]) - case deselectAll case add([T]) case remove([T]) case clear diff --git a/TokenShared/CacheReducer.swift b/TokenShared/CacheReducer.swift index a75e587..65d248f 100644 --- a/TokenShared/CacheReducer.swift +++ b/TokenShared/CacheReducer.swift @@ -17,13 +17,6 @@ public struct CacheReducer: Reducer where A : Cacheable { guard let newState = state as? Cache else { return state } switch action { - case .selectAndReplace(let cacheables): return newState.selectAndReplace(cacheables) - case .selectAndMerge(let cacheables): return newState.selectAndMerge(cacheables) - case .selectPrevious: return newState.selectPrevious() - case .selectNext: return newState.selectNext() - case .selectAll: return newState.selectAll() - case .deselect(let cacheables): return newState.deselect(cacheables) - case .deselectAll: return newState.deselectAll() case .add(let cacheables): return newState.add(cacheables) case .remove(let cacheables): return newState.remove(cacheables) case .clear: return newState.clear()