generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Binding<Bool>
and animation in ChatView
for three-dots typing…
… indicator (#6) # Chat Typing Indicator ## ♻️ Current situation & Problem As stated in [#15](StanfordSpezi/SpeziLLM#15), there is currently no visual indicator when a user input is being processed by the LLM on the other end. This can lead to a confusing experience, as the user may prematurely send another message or exit the app, believing that the chat has stopped responding. ## ⚙️ Release Notes - Extended `MessageView` to add the typing indicator animation, which is visible depending on a new variable called `displayTypingIndicator`. - Typing indicator displays three gray dots in a message bubble at the bottom of the chat when an LLM response is pending (i.e., the first token has not yet been streamed). - Each dot appears and disappears in a sequential, wave-like pattern, creating the impression that the dots are bouncing or pulsing one after the other from left to right. The animation loops continuously until `displayTypingIndicator` is set to `false`. ## 📚 Documentation Documentation for the new initializer associated with this update is provided as an in-code comment. ## ✅ Testing Manually tested with SwiftUI Previews. ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md). --------- Co-authored-by: Philipp Zagar <[email protected]>
- Loading branch information
1 parent
9d45c10
commit ea5e21b
Showing
9 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// Creates a typing indicator animation for pending messages. | ||
/// The animation consists of three dots that fade in and out in a sequential, wave-like pattern. | ||
/// It loops continuously as long as `isAnimating` is `true`. | ||
/// | ||
/// Usage: | ||
/// ```swift | ||
/// struct ChatView: View { | ||
/// var body: some View { | ||
/// VStack { | ||
/// MessageView(ChatEntity(role: .user, content: "User Message!")) | ||
/// TypingIndicator() | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
public struct TypingIndicator: View { | ||
@State var isAnimating = false | ||
|
||
public var body: some View { | ||
HStack { | ||
HStack(spacing: 3) { | ||
ForEach(0..<3) { index in | ||
Circle() | ||
.opacity(self.isAnimating ? 1 : 0) | ||
.foregroundStyle(.tertiary) | ||
.animation( | ||
Animation | ||
.easeInOut(duration: 0.6) | ||
.repeatForever(autoreverses: true) | ||
.delay(0.2 * Double(index)), | ||
value: self.isAnimating | ||
) | ||
.frame(width: 10) | ||
} | ||
.accessibilityIdentifier(String(localized: "TYPING_INDICATOR", bundle: .module)) | ||
} | ||
.frame(width: 42, height: 12, alignment: .center) | ||
.padding(.vertical, 4) | ||
.onAppear { | ||
self.isAnimating = true | ||
} | ||
.chatMessageStyle(alignment: .leading) | ||
Spacer(minLength: 32) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
ScrollView { | ||
VStack { | ||
MessageView(ChatEntity(role: .system, content: "System Message!"), hideMessagesWithRoles: []) | ||
MessageView(ChatEntity(role: .system, content: "System Message (hidden)!")) | ||
MessageView(ChatEntity(role: .function(name: "test_function"), content: "Function Message!"), hideMessagesWithRoles: [.system]) | ||
MessageView(ChatEntity(role: .user, content: "User Message!")) | ||
TypingIndicator() | ||
} | ||
.padding() | ||
} | ||
} |
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
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
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
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
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