Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Jun 26, 2024
1 parent 1783eef commit f5425c0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.9
// swift-tools-version:5.10

import PackageDescription

Expand Down
4 changes: 4 additions & 0 deletions Sources/Intramodular/Interactivity/_ChatItemViewActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import Swallow
import SwiftUIX
import SwiftUIZ

extension ChatView {

}

public struct _ChatViewActions: Initiable, MergeOperatable {
var onEdit: ((AnyChatItemIdentifier, AnyChatItemContent) -> Void)?
var onDelete: ((AnyChatItemIdentifier) -> Void)?
Expand Down
20 changes: 10 additions & 10 deletions Sources/Intramodular/Message List/ChatMessageList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ public struct ChatMessageList<Data: RandomAccessCollection>: View where Data.Ele
private var _chatViewActions = _ChatViewActions()

private enum ImplementationStrategy {
case a
case b
case c
case a // LazyVStack + ScrollView
case b // SwiftUI.List
case c // SwiftUIX.CocoaList
}

/// Whether `SwiftUI`'s official `List` implementation is garbage.
private var _isOfficialImplementationStillFucked: Bool {
true
false
}

private var implementationStrategy: ImplementationStrategy {
#if os(macOS)
#if os(macOS)
if _isOfficialImplementationStillFucked {
ImplementationStrategy.c // SwiftUIX.CocoaList
} else {
ImplementationStrategy.b // SwiftUI.List
}
#elseif os(iOS) || os(visionOS)
#elseif os(iOS) || os(visionOS)
ImplementationStrategy.a // LazyVStack + ScrollView
#endif
#endif
}

init<C: View>(_content: () -> C) {
self._content = _content().eraseToAnyView()
}

public var body: some View {
switch implementationStrategy {
case .a:
Expand Down Expand Up @@ -80,7 +80,7 @@ extension ChatMessageList {
}
}
}

public init<Content: View>(
_ data: Data,
content: @escaping (Data.Element) -> Content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Vatsal Manot
//

import SwiftUI
import SwiftUIZ

public struct _AnyChatItemPlaceholderContent: View {
@Environment(\.userInterfaceIdiom) var userInterfaceIdiom
Expand Down
99 changes: 63 additions & 36 deletions Sources/Intramodular/Message List/_ChatMessageListB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,74 @@ public struct _ChatMessageListB<Content: View>: View {
self.content = content()
}

private enum Subviews {
case lastItem
}

public var body: some View {
ScrollViewReader { scrollView in
_VariadicViewAdapter(content) { (content: _SwiftUI_VariadicView<Content>) in
let lastItem: AnyChatItemIdentifier? = content.children.last?[trait: \._chatItemConfiguration]?.id

makeList(content: content, scrollView: scrollView)
.modifier(
__LazyMessagesVStackScrollBehavior(
scrollView: scrollView,
lastItem: lastItem
)
)
_VariadicViewAdapter(content) { (content: _SwiftUI_VariadicView<Content>) in
let lastItem: AnyChatItemIdentifier? = content.children.last?[trait: \._chatItemConfiguration]?.id

ScrollViewReader { scrollView in
List {
_ForEachSubview(
enumerating: content,
trait: \._chatItemConfiguration,
id: \.id
) { (index: Int, subview: _VariadicViewChildren.Subview, item: _ChatItemIdentity) in
_SwiftUI_UnaryViewAdaptor {
subview
.modifier(
__LazyMessagesVStackStackItem(
index: index,
id: item.id,
role: item.role.erasedAsAnyHashable,
isLast: index == content.children.count,
scrollView: scrollView
)
)
.modifier(_ExpandAndAlignChatItem(item: item))
}
.modifier(_StipAllListItemStyling(insetsIncluded: false))
.id(item.id)
}
}
._stripAllListOrListItemStyling()
.modifier(_ChatMessageListB_ScrollBehavior(scrollView: scrollView, lastItem: lastItem))
}
}
._SwiftUIX_defaultScrollAnchor(.bottom)
}
}

fileprivate struct _ChatMessageListB_ScrollBehavior: ViewModifier {
let scrollView: ScrollViewProxy
let lastItem: AnyChatItemIdentifier?

private func makeList(
content: _SwiftUI_VariadicView<Content>,
scrollView: ScrollViewProxy
) -> some View {
List {
_ForEachSubview(
enumerating: content,
trait: \._chatItemConfiguration
) { (index: Int, subview: _VariadicViewChildren.Subview, item: _ChatItemIdentity) in
subview
.modifier(
__LazyMessagesVStackStackItem(
index: index,
id: item.id,
role: item.role.erasedAsAnyHashable,
isLast: index == content.children.count,
scrollView: scrollView
)
)
.modifier(_ExpandAndAlignChatItem(item: item))
._noListItemModification()
func body(content: Content) -> some View {
content
.onAppearOnce {
guard let lastItem else {
return
}

withoutAnimation(after: .milliseconds(200)) {
scrollView.scrollTo(lastItem, anchor: .bottom)

withoutAnimation(after: .milliseconds(200)) {
scrollView.scrollTo(lastItem, anchor: .bottom)
}
}
}
.withChangePublisher(for: lastItem) { lastItem in
lastItem
.compactMap({ $0 })
.removeDuplicates()
.debounce(for: .milliseconds(200), scheduler: DispatchQueue.main)
.sink { id in
withAnimation {
scrollView.scrollTo(id, anchor: .bottom)
}
}
}
}
.listStyle(.plain)
._noListItemModification()
}
}
8 changes: 5 additions & 3 deletions Sources/Intramodular/Message List/_LazyMessagesVStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//

@_spi(Internal) import SwiftUIX
import SwiftUIZ

@View(.dynamic)
public struct _LazyMessagesVStack<Content: View>: View {
@Environment(\._chatViewPreferences) var chatView

Expand Down Expand Up @@ -109,14 +111,14 @@ struct __LazyMessagesVStackStackItem: Identifiable, ViewModifier {
let isLast: Bool?
let scrollView: ScrollViewProxy?

@ViewStorage var isActive: Bool = false

func body(content: Content) -> some View {
content
.environment(\._chatItemConfiguration, _ChatItemConfiguration(id: id, actions: _chatViewActions))
.contentShape(Rectangle())
.padding(.top, index == 0 ? 12 : 0)
.padding(.bottom, 4)
.onChangeOfFrame { _ in
scrollView?.scrollTo(id, anchor: .bottom)
}
.geometryGroup()
}
}

0 comments on commit f5425c0

Please sign in to comment.