Skip to content

Commit

Permalink
Remove selection
Browse files Browse the repository at this point in the history
  • Loading branch information
acwright committed Apr 19, 2018
1 parent fb8dd1a commit aaaa32d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Token/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.2.0</string>
<string>1.2.1</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
2 changes: 1 addition & 1 deletion TokenMobile/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.2.0</string>
<string>1.2.1</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
38 changes: 19 additions & 19 deletions TokenShared/Array+Cacheable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Array+Cacheable.swift
// Array+cacheable.swift
// Token
//
// Created by Paul Foster on 1/31/18.
Expand All @@ -16,55 +16,55 @@ public extension Array where Element == Cacheable {
public func items<T: Cacheable>() -> [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<T: Cacheable>(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) } }

}
80 changes: 2 additions & 78 deletions TokenShared/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,98 +10,22 @@ import Foundation

public struct Cache<T: Cacheable>: State {

var cache: [Cacheable]
var selected: [Cacheable]

public init() {
self.cache = []
self.selected = []
}
var cache: [Cacheable] = []

}

extension Cache {

func selectAndReplace(_ cacheables: [T]) -> Cache<T> { return mutated { $0.selected = cacheables } }
func selectAndMerge(_ cacheables: [T]) -> Cache<T> { return mutated { $0.selected.merge(cacheables) } }
func selectAll() -> Cache<T> { return mutated { $0.selected.merge(cache) } }
func deselect(_ cacheables: [T]) -> Cache<T> { return mutated { $0.selected.remove(cacheables) } }
func deselectAll() -> Cache<T> { return mutated { $0.selected = [] } }
func add(_ cacheables: [T]) -> Cache<T> { return mutated { $0.cache.merge(cacheables) } }
func merge(_ cacheables: [T]) -> Cache<T> { return mutated { $0.cache.merge(cacheables) } }
func remove(_ cacheables: [T]) -> Cache<T> { return mutated { $0.cache.remove(cacheables) } }
func clear() -> Cache<T> { return mutated { $0.cache = [] } }

func selectPrevious() -> Cache<T> {
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<T> {
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)
}
}

}
7 changes: 0 additions & 7 deletions TokenShared/CacheAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
import Foundation

public enum CacheAction<T>: 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
Expand Down
7 changes: 0 additions & 7 deletions TokenShared/CacheReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ public struct CacheReducer<A>: Reducer where A : Cacheable {
guard let newState = state as? Cache<A> 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()
Expand Down

0 comments on commit aaaa32d

Please sign in to comment.