Created
May 29, 2020 18:13
-
-
Save AlexanderBollbach/40c5c87a0f6de0000e7d8e70a3a281a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import Foundation | |
//import ComposableArchitecture | |
// | |
//protocol Selectable { | |
// var isSelected: Bool { get set } | |
//} | |
// | |
// | |
//protocol Creatable { | |
// associatedtype ID | |
// static func create(id: ID) -> Self | |
//} | |
// | |
//extension Array where Element: Selectable { | |
// | |
// var selected: [Element] { | |
// filter { $0.isSelected } | |
// } | |
// | |
// mutating func selectAll() { | |
// for i in 0..<count { | |
// self[i].isSelected = true | |
// } | |
// } | |
// | |
// mutating func deselectAll() { | |
// for i in 0..<count { | |
// self[i].isSelected = false | |
// } | |
// } | |
// | |
// mutating func removeSelected() { | |
// self = filter { !$0.isSelected } | |
// } | |
//} | |
// | |
//extension Array where Element: Selectable & Identifiable { | |
// | |
// mutating func selectOnly(at id: Element.ID) { | |
// deselectAll() | |
// select(at: id, isSelected: true) | |
// } | |
// | |
// mutating func select(at id: Element.ID) { | |
// select(at: id, isSelected: true) | |
// } | |
// | |
// mutating func deselect(at id: Element.ID) { | |
// select(at: id, isSelected: false) | |
// } | |
// | |
// private mutating func select(at id: Element.ID, isSelected: Bool) { | |
// update(pred: { $0.id == id }) { | |
// $0.isSelected = isSelected | |
// } | |
// } | |
// | |
// mutating func toggleSelected(at id: Element.ID) { | |
// let isSelected = first { $0.id == id }?.isSelected ?? false | |
// select(at: id, isSelected: !isSelected) | |
// } | |
// | |
// mutating func swapSelected(_ rightLeft: Bool) { | |
// | |
// if let firstSelected = selected.first?.id { | |
// rightLeft ? self.swapRight(firstSelected) : self.swapLeft(firstSelected) | |
// } | |
// } | |
//} | |
// | |
//extension Array where Element: Identifiable & Selectable { | |
// | |
// mutating func update(pred: (Element) -> Bool, t: (inout Element) -> Void) { | |
// for i in 0..<count { | |
// if pred(self[i]) { t(&self[i]) } | |
// } | |
// } | |
// | |
// mutating func update(t: (inout Element) -> Void) { | |
// for i in 0..<count { | |
// t(&self[i]) | |
// } | |
// } | |
//} | |
// | |
//extension Array where Element: Identifiable { | |
// | |
// mutating func swap(id1: Element.ID, id2: Element.ID) { | |
// guard | |
// let i1 = firstIndex(where: { $0.id == id1 }), | |
// let i2 = firstIndex(where: { $0.id == id2 }) else { fatalError() } | |
// | |
// swapAt(i1, i2) | |
// } | |
// | |
// mutating func swapRight(_ id: Element.ID) { | |
// guard | |
// let i1 = firstIndex(where: { $0.id == id }) else { fatalError() } | |
// | |
// if i1 == self.count - 1 { | |
// swapAt(i1, 0) | |
// return | |
// } | |
// | |
// swapAt(i1, i1 + 1) | |
// } | |
// | |
// mutating func swapLeft(_ id: Element.ID) { | |
// guard | |
// let i1 = firstIndex(where: { $0.id == id }) else { fatalError() } | |
// | |
// if i1 == 0 { | |
// swapAt(i1, self.count - 1) | |
// return | |
// } | |
// | |
// swapAt(i1, i1 - 1) | |
// } | |
//} | |
// | |
//enum ListAction<Entity: Identifiable, SubAction> { | |
// | |
// // creation | |
// case insert(Entity) | |
// case insertAndSelect(Entity) | |
// case load([Entity]) | |
// case create(id: Entity.ID) | |
// case createNewAndSelect(Entity.ID) | |
// | |
// // selection | |
// case select(id: Entity.ID) | |
// case selectOnly(id: Entity.ID) | |
// case toggleSelected(id: Entity.ID) | |
// case deselect(id: Entity.ID) | |
// case removeSelected | |
// case deselectAll | |
// case selectAll | |
// case selected(SubAction) | |
// | |
// // updates | |
// case all(SubAction) | |
// case update(id: Entity.ID, action: SubAction) | |
// case replace(with: [Entity]) | |
// case updateByIds(ids: [Entity.ID], action: SubAction) | |
// | |
// // reordering | |
// case reorder(id1: Entity.ID, id2: Entity.ID) | |
// case swapRight(id: Entity.ID) | |
// case swapLeft(id: Entity.ID) | |
// case swapSelected(rightLeft: Bool) | |
// | |
// // deletion | |
// case removeAll | |
//} | |
// | |
//typealias ListElement = Identifiable & Selectable & Creatable | |
// | |
//extension Project { | |
// | |
// mutating func list<Element: ListElement, Entity, EntityAction> ( | |
// elements: inout [Element], | |
// action: ListAction<Entity, EntityAction> | |
// ) { | |
// | |
// switch action { | |
// | |
// case let .createNewAndSelect(id): | |
// let e = Element.create(id: id) | |
// elements.append(e) | |
// elements.deselectAll() | |
// elements.select(at: id) | |
// | |
// case let .toggleSelected(id): | |
// elements.toggleSelected(at: id) | |
// | |
// case let .updateByIds(ids, action): | |
// elements.update( | |
// pred: { ids.contains($0.id) }) { | |
// self.callAsFunction(&$0, action, toLocalEnvironment(globalEnvironment)) | |
// } | |
// | |
// case .removeAll: | |
// elements = [] | |
// | |
// case let .load(entities): | |
// elements = entities | |
// | |
// case let .insert(e): | |
// elements.append(e) | |
// | |
// case let .insertAndSelect(e): | |
// elements.append(e) | |
// elements.deselectAll() | |
// elements.select(at: e.id) | |
// | |
// case let .create(id): | |
// elements.append(Element.create(id: id)) | |
// | |
// case let .select(id): | |
// elements.select(at: id) | |
// | |
// case let .selectOnly(id): | |
// elements.deselectAll() | |
// elements.select(at: id) | |
// | |
// case let .deselect(id): | |
// elements.deselect(at: id) | |
// | |
// case .deselectAll: | |
// elements.deselectAll() | |
// | |
// case .removeSelected: | |
// elements.removeSelected() | |
// | |
// case let .selected(action): | |
// | |
// // TODO: collect effects for sub reducer calls | |
// elements | |
// .update(pred: { $0.isSelected }) { | |
// self.callAsFunction(&$0, action, toLocalEnvironment(globalEnvironment)) | |
// } | |
// | |
// case let .update(id, action): | |
// elements | |
// .update(pred: { $0.id == id }, t: { self.callAsFunction(&$0, action, toLocalEnvironment(globalEnvironment)) }) | |
// | |
// case let .replace(entities): | |
// elements = entities | |
// | |
// case .selectAll: | |
// elements.selectAll() | |
// | |
// case let .all(action): | |
// elements | |
// .update(pred: { _ in true }) { | |
// self.callAsFunction(&$0, action, toLocalEnvironment(globalEnvironment)) | |
// } | |
// | |
// case let .reorder(id1, id2): | |
// elements.swap(id1: id1, id2: id2) | |
// | |
// case let .swapRight(id): | |
// elements.swapRight(id) | |
// | |
// case let .swapLeft(id): | |
// elements.swapLeft(id) | |
// | |
// case let .swapSelected(rightLeft): | |
// elements.swapSelected(rightLeft) | |
// | |
// } | |
// } | |
//} | |
// | |
//import SwiftUI | |
// | |
///// A structure that computes views on demand from a store on a collection of data. | |
//struct ListStore<EachState, EachAction, Data, Content>: DynamicViewContent | |
// where | |
// Data: Collection, | |
// EachState: ListElement, | |
// // ID: Hashable, | |
// Content: View | |
//{ | |
// | |
// let data: Data | |
// private let content: () -> Content | |
// | |
// /// Initializes a structure that computes views on demand from a store on an array of data and an | |
// /// indexed action. | |
// /// | |
// /// - Parameters: | |
// /// - store: A store on an array of data and an indexed action. | |
// /// - id: A key path identifying an element. | |
// /// - content: A function that can generate content given a store of an element. | |
// public init<EachContent>( | |
// _ store: Store<Data, ListAction<EachState, EachAction>>, | |
// pred: @escaping (EachState) -> Bool = { _ in true }, | |
// content: @escaping (Store<EachState, EachAction>) -> EachContent | |
// ) | |
// where | |
// Data == [EachState], | |
// EachContent: View, | |
// Content == WithViewStore< | |
// Data, | |
// ListAction<EachState, EachAction>, | |
// ForEach<[EachState], EachState.ID, EachContent> | |
// > | |
// { | |
// self.data = ViewStore(store, removeDuplicates: { _, _ in false }).state | |
// | |
// self.content = { | |
// WithViewStore( | |
// store, | |
// removeDuplicates: { _, _ in false } // TODO (from ForEachStore) | |
// ) { viewStore in | |
// | |
// ForEach( | |
// viewStore.state.filter(pred), | |
// id: \.id | |
// ) { element in | |
// content( | |
// store.scope( | |
// state: { val in | |
// | |
// val.filter { $0.id == element.id }.first ?? element | |
// // index < $0.endIndex ? $0[index] : element | |
// }, | |
// action: { val in | |
// // fatalError() | |
// ListAction.update(id: element.id, action: val) | |
// } | |
// ) | |
// ) | |
// } | |
// } | |
// } | |
// } | |
// | |
// public var body: some View { | |
// self.content() | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment