Browse Source

Updated message view

Sergey 2 years ago
parent
commit
7827b7561e

+ 1 - 1
Chat.xcodeproj/xcuserdata/sertrsv.xcuserdatad/xcschemes/xcschememanagement.plist

@@ -7,7 +7,7 @@
 		<key>Chat (iOS).xcscheme_^#shared#^_</key>
 		<dict>
 			<key>orderHint</key>
-			<integer>1</integer>
+			<integer>0</integer>
 		</dict>
 		<key>Chat (macOS).xcscheme_^#shared#^_</key>
 		<dict>

+ 5 - 9
Chat/UI/Chat/ChatView.swift

@@ -26,15 +26,11 @@ struct ChatView: View, KeyboardReadable {
                     VStack {
                         withAnimation {
 							ForEach(viewModel.messages, id: \.id) { message in
-                                HStack {
-                                    if message.fromID == XMPPController.shared.xmppStream.myJID?.bare {
-                                        Spacer(minLength: 40)
-										MessageView(text: message.body, time: "", isSelf: true)
-                                    } else {
-										MessageView(text: message.body, time: "", isSelf: false)
-                                        Spacer(minLength: 40)
-                                    }
-                                }
+								MessageView(
+									text: message.body,
+									time: "",
+									isSelf: message.fromID == XMPPController.shared.xmppStream.myJID?.bare
+								)
                             }
                             .onChange(of: viewModel.messages) { _ in
 								DispatchQueue.main.asyncAfter(deadline: .now() + 1) {

+ 54 - 29
Chat/UI/Helpers/MessageView.swift

@@ -8,36 +8,61 @@
 import SwiftUI
 
 struct MessageView: View {
-    let text: String
-    let time: String
-    let isSelf: Bool
-    var body: some View {
-        HStack(alignment: .bottom) {
-            Text(text)
-                .foregroundColor(.primary)
-                .padding(.vertical, 6)
-                .padding(.horizontal, 12)
-            Text(time)
-                .font(.caption)
-                .foregroundColor(.secondary)
-                .padding(.vertical, 6)
-                .padding(.trailing, 12)
-                .padding(.leading, -12)
-        }
-        .background(
-            RoundedRectangle(cornerRadius: 17)
-                .foregroundColor(isSelf ? .mint.opacity(0.1) : .secondary.opacity(0.1))
-        )
-    }
+	let text: String
+	let time: String
+	let isSelf: Bool
+
+	var body: some View {
+		HStack {
+			if isSelf {
+				Spacer(minLength: 40)
+			}
+			HStack(alignment: .bottom) {
+				Text(text)
+					.foregroundColor(.primary)
+					.padding(.vertical, 6)
+					.padding(.horizontal, 12)
+				Text(time)
+					.font(.caption)
+					.foregroundColor(.secondary)
+					.padding(.vertical, 6)
+					.padding(.trailing, 12)
+					.padding(.leading, -12)
+			}
+			.background(
+				RoundedRectangle(cornerRadius: 17)
+					.foregroundColor(isSelf ? .mint.opacity(0.1) : .secondary.opacity(0.1))
+			)
+			if !isSelf {
+				Spacer(minLength: 40)
+			}
+		}
+	}
 }
 
 struct MessageView_Previews: PreviewProvider {
-    static var previews: some View {
-        MessageView(text: "Малое пробное сообщение", time: "23:05", isSelf: true)
-            .padding()
-            .previewLayout(.sizeThatFits)
-        MessageView(text: "Пробное сообщение на несколько строк для проверки того, как выглядит сообщение и его фон", time: "23:08", isSelf: false)
-            .padding()
-            .previewLayout(.sizeThatFits)
-    }
+	static var previews: some View {
+		MessageTestView()
+	}
+
+	struct MessageTestView: View {
+		@State var messageText1: String = "Малое пробное сообщение"
+		@State var messageText2: String = "Пробное сообщение на несколько строк для проверки того, как выглядит сообщение и его фон"
+
+		var body: some View {
+			VStack {
+				ScrollView {
+					VStack(spacing: 6) {
+						MessageView(text: messageText1, time: "23:05", isSelf: true)
+						MessageView(text: messageText2, time: "23:08", isSelf: false)
+					}
+				}
+				TextField("Message", text: $messageText1)
+					.textFieldStyle(.roundedBorder)
+				TextField("Message", text: $messageText2)
+					.textFieldStyle(.roundedBorder)
+			}
+			.padding()
+		}
+	}
 }