Skip to content

Commit

Permalink
Merge pull request #133 from kiwicom/109-speed-up-toastqueue-typecheck
Browse files Browse the repository at this point in the history
Speed up `ToastQueue` typecheck
  • Loading branch information
sjavora authored May 18, 2022
2 parents 503de8b + 5356214 commit 545d115
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
7 changes: 4 additions & 3 deletions Sources/Orbit/Components/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ struct ToastPreviews: PreviewProvider {

static let description = "Toast shows a brief message that's clear & understandable."
static let toastQueue = ToastQueue()
static let toastLiveQueue = ToastQueue()

static var previews: some View {
PreviewWrapper {
Expand All @@ -211,7 +212,7 @@ struct ToastPreviews: PreviewProvider {

PreviewWrapper {
storybookLive
.overlay(Toast(toastQueue: toastQueue), alignment: .top)
.overlay(Toast(toastQueue: toastLiveQueue), alignment: .top)
}
}

Expand Down Expand Up @@ -244,8 +245,8 @@ struct ToastPreviews: PreviewProvider {

Spacer()

Button("Add toast 1") { toastQueue.add(description)}
Button("Add toast 2") { toastQueue.add("Another toast message.")}
Button("Add toast 1") { toastLiveQueue.add(description) }
Button("Add toast 2") { toastLiveQueue.add("Another toast message.") }
}
.padding(.medium)
.previewDisplayName("Live Preview")
Expand Down
49 changes: 25 additions & 24 deletions Sources/Orbit/Support/Toast/ToastQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,34 +96,24 @@ public final class ToastQueue: ObservableObject {
.map { _ in () }
.eraseToAnyPublisher()
}

func incrementProgress(forAction action: ToastAction) -> TimeInterval {
switch action {
case .run: return 0.1
case .pause: return 0
case .dismiss: return 1
}

var progressPublisher: AnyPublisher<TimeInterval, Never> {
Publishers.CombineLatest(timerPublisher, currentToastActionSubject)
.map(\.1.incrementProgress)
.scan(0, +)
.map { (progress: Double) -> Double in
progress / Self.dismissTimeout
}
.prepend(0)
.eraseToAnyPublisher()
}

return Publishers.CombineLatest(timerPublisher, currentToastActionSubject)
.map(\.1)
.map(incrementProgress(forAction:))
.scan(0, +)
.map { (progress: Double) -> Double in
progress / Self.dismissTimeout
}
.prepend(0)
.combineLatest(currentToastActionSubject)
.prefix { (_, action: ToastAction) -> Bool in
action != .dismiss
return Publishers.CombineLatest(progressPublisher, currentToastActionSubject)
.prefix { (progress: Double, action: ToastAction) in
progress <= 1 && action != .dismiss
}
.map(\.0)
.prefix { (progress: Double) -> Bool in
progress <= 1
}
.map { (progress: Double) -> Toast? in
toast.withProgress(progress)
}
.map(toast.withProgress)
.append(Just(nil))
.eraseToAnyPublisher()
}
Expand Down Expand Up @@ -158,3 +148,14 @@ public final class ToastQueue: ObservableObject {
}
}
}

private extension ToastQueue.ToastAction {

var incrementProgress: TimeInterval {
switch self {
case .run: return 0.1
case .pause: return 0
case .dismiss: return 1
}
}
}

0 comments on commit 545d115

Please sign in to comment.