Skip to content

Commit

Permalink
153
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jul 19, 2021
1 parent 3bb6040 commit 20e705a
Show file tree
Hide file tree
Showing 16 changed files with 923 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ struct DerivedBehaviorApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
AppView(
store: .init(initialState: AppState.init(counters: []), reducer: appReducer, environment: AppEnvironment(fact: .live, mainQueue: .main, uuid: UUID.init))
)
// AppView(
// store: .init(initialState: AppState.init(counters: []), reducer: appReducer, environment: AppEnvironment(fact: .live, mainQueue: .main, uuid: UUID.init))
// )
VanillaAppView(viewModel: AppViewModel(fact: .live, mainQueue: .main, uuid: UUID.init))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,29 @@ struct VanillaCounterView: View {
class CounterRowViewModel: ObservableObject, Identifiable {
@Published var counter: CounterViewModel
let id: UUID
let onRemove: () -> Void

init(counter: CounterViewModel, id: UUID) {
init(
counter: CounterViewModel,
id: UUID,
onRemove: @escaping () -> Void
) {
self.counter = counter
self.id = id
self.onRemove = onRemove
}

func removeButtonTapped() {
// TODO: track analytics
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.onRemove()
}
}
}

struct VanillaCounterRowView: View {
let viewModel: CounterRowViewModel
let onRemoveTapped: () -> Void


var body: some View {
HStack {
VanillaCounterView(
Expand All @@ -105,7 +113,6 @@ struct VanillaCounterRowView: View {

Button("Remove") {
withAnimation {
self.onRemoveTapped()
self.viewModel.removeButtonTapped()
}
}
Expand Down Expand Up @@ -197,6 +204,11 @@ struct VanillaFactPrompt: View {
}
}

struct IdentifiedArray<Element: Identifiable> {
var ids: [Element.ID]
var lookup: [Element.ID: Element]
}

class AppViewModel: ObservableObject {
@Published var counters: [CounterRowViewModel] = []
@Published var factPrompt: FactPromptViewModel?
Expand Down Expand Up @@ -237,10 +249,28 @@ class AppViewModel: ObservableObject {
}
)

let id = self.uuid()
// let index = self.counters.endIndex
self.counters.append(
.init(
counter: counterViewModel,
id: self.uuid()
id: id,
onRemove: { [weak self] in

// - quick lookup / in-place mutation
// - handles invariants for duplicate identities
// - simpler than ordered set (requires hashable elements)

// self?.counters.removeAll(where: { $0.id == id })
guard let self = self else { return }
for (index, counter) in zip(self.counters.indices, self.counters) {
if counter.id == id {
self.counters.remove(at: index)
return
}
}
// self?.counters.remove(at: index)
}
)
)

Expand All @@ -249,10 +279,6 @@ class AppViewModel: ObservableObject {
.store(in: &self.cancellables)
}

func removeButtonTapped(id: UUID) {
self.counters.removeAll(where: { $0.id == id })
}

func dismissFactPrompt() {
self.factPrompt = nil
}
Expand All @@ -268,10 +294,7 @@ struct VanillaAppView: View {

ForEach(self.viewModel.counters) { counterRow in
VanillaCounterRowView(
viewModel: counterRow,
onRemoveTapped: {
self.viewModel.removeButtonTapped(id: counterRow.id)
}
viewModel: counterRow
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
//
// DerivedBehaviorApp.swift
// DerivedBehavior
//
// Created by Point-Free on 5/12/21.
//

import ComposableArchitecture
import SwiftUI

@main
struct DerivedBehaviorApp: App {
var body: some Scene {
WindowGroup {
VanillaContentView(
viewModel: .init()
// counterViewModel: .init(),
// profileViewModel: .init()
)
}
var body: some Scene {
WindowGroup {
TcaContentView(
store: Store(
initialState: .init(),
reducer: appReducer,
environment: .init()
)
)
}
}
}
7 changes: 7 additions & 0 deletions 0152-case-paths-performance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## [Point-Free](https://www.pointfree.co)

> #### This directory contains code from Point-Free Episode: [Composable Architecture Performance: Case Paths](https://www.pointfree.co/episodes/ep152-composable-architecture-performance-case-paths)
>
> This week we improve the performance of another part of the Composable Architecture ecosystem: case paths! We will benchmark the reflection mechanism that powers case paths and speed things up with the help of a Swift runtime function.
For the section of the episode where we refactor the Case Paths, see [this GitHub pull request](https://github.com/pointfreeco/swift-case-paths/pull/35).
Loading

0 comments on commit 20e705a

Please sign in to comment.