Skip to content

Commit 308cebe

Browse files
committed
Add WatchOS App
1 parent ea0ca7c commit 308cebe

File tree

12 files changed

+462
-38
lines changed

12 files changed

+462
-38
lines changed

Shared/ContentView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ struct ContentView: View {
3636
}
3737
}
3838

39+
#if !os(watchOS)
3940
Divider()
4041
bottomView(image: "profile", proxy: proxy)
4142
Spacer()
43+
#endif
4244
}
4345
.onChange(of: vm.messages.last?.responseText) { _ in scrollToBottom(proxy: proxy)
4446
}
@@ -64,7 +66,9 @@ struct ContentView: View {
6466
}
6567

6668
TextField("Send message", text: $vm.inputMessage, axis: .vertical)
69+
#if !os(watchOS)
6770
.textFieldStyle(.roundedBorder)
71+
#endif
6872
.focused($isTextFieldFocused)
6973
.disabled(vm.isInteractingWithChatGPT)
7074

Shared/MessageRowView.swift

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,81 @@ struct MessageRowView: View {
1515

1616
var body: some View {
1717
VStack(spacing: 0) {
18-
1918
messageRow(text: message.sendText, image: message.sendImage, bgColor: colorScheme == .light ? .white : Color(red: 52/255, green: 53/255, blue: 65/255, opacity: 0.5))
2019

2120
if let text = message.responseText {
2221
Divider()
2322
messageRow(text: text, image: message.responseImage, bgColor: colorScheme == .light ? .gray.opacity(0.1) : Color(red: 52/255, green: 53/255, blue: 65/255, opacity: 1), responseError: message.responseError, showDotLoading: message.isInteractingWithChatGPT)
2423
Divider()
2524
}
26-
2725
}
28-
2926
}
3027

3128
func messageRow(text: String, image: String, bgColor: Color, responseError: String? = nil, showDotLoading: Bool = false) -> some View {
29+
#if os(watchOS)
30+
VStack(alignment: .leading, spacing: 8) {
31+
messageRowContent(text: text, image: image, responseError: responseError, showDotLoading: showDotLoading)
32+
}
33+
34+
.padding(16)
35+
.frame(maxWidth: .infinity, alignment: .leading)
36+
.background(bgColor)
37+
38+
#else
3239
HStack(alignment: .top, spacing: 24) {
33-
if image.hasPrefix("http"), let url = URL(string: image) {
34-
AsyncImage(url: url) { image in
35-
image
36-
.resizable()
37-
.frame(width: 25, height: 25)
38-
} placeholder: {
39-
ProgressView()
40-
}
41-
42-
} else {
43-
Image(image)
40+
messageRowContent(text: text, image: image, responseError: responseError, showDotLoading: showDotLoading)
41+
}
42+
.padding(16)
43+
.frame(maxWidth: .infinity, alignment: .leading)
44+
.background(bgColor)
45+
#endif
46+
}
47+
48+
@ViewBuilder
49+
func messageRowContent(text: String, image: String, responseError: String? = nil, showDotLoading: Bool = false) -> some View {
50+
if image.hasPrefix("http"), let url = URL(string: image) {
51+
AsyncImage(url: url) { image in
52+
image
4453
.resizable()
4554
.frame(width: 25, height: 25)
55+
} placeholder: {
56+
ProgressView()
57+
}
58+
59+
} else {
60+
Image(image)
61+
.resizable()
62+
.frame(width: 25, height: 25)
63+
}
64+
65+
VStack(alignment: .leading) {
66+
if !text.isEmpty {
67+
Text(text)
68+
.multilineTextAlignment(.leading)
69+
#if !os(watchOS)
70+
.textSelection(.enabled)
71+
#endif
4672
}
4773

48-
VStack(alignment: .leading) {
49-
if !text.isEmpty {
50-
Text(text)
51-
.multilineTextAlignment(.leading)
52-
.textSelection(.enabled)
53-
}
54-
55-
if let error = responseError {
56-
Text("Error: \(error)")
57-
.foregroundColor(.red)
58-
.multilineTextAlignment(.leading)
59-
60-
Button("Regenerate response") {
61-
retryCallback(message)
62-
}
63-
.foregroundColor(.accentColor)
64-
.padding(.top)
65-
}
74+
if let error = responseError {
75+
Text("Error: \(error)")
76+
.foregroundColor(.red)
77+
.multilineTextAlignment(.leading)
6678

67-
if showDotLoading {
68-
DotLoadingView()
69-
.frame(width: 60, height: 30)
79+
Button("Regenerate response") {
80+
retryCallback(message)
7081
}
82+
.foregroundColor(.accentColor)
83+
.padding(.top)
84+
}
85+
86+
if showDotLoading {
87+
DotLoadingView()
88+
.frame(width: 60, height: 30)
7189
}
7290
}
73-
.padding(16)
74-
.frame(maxWidth: .infinity, alignment: .leading)
75-
.background(bgColor)
91+
92+
7693
}
7794

7895
}

0 commit comments

Comments
 (0)