Commit a4fc153b authored by  Joel  Oksanen's avatar Joel Oksanen
Browse files

Started implementing chat

parent 727acebd
......@@ -18,6 +18,13 @@
9449FE332402CBD400025F70 /* ProductInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE322402CBD400025F70 /* ProductInfo.swift */; };
9449FE352402CCDA00025F70 /* ConnectionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE342402CCDA00025F70 /* ConnectionManager.swift */; };
9449FE372402D29E00025F70 /* Product.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE362402D29E00025F70 /* Product.swift */; };
9449FE392403E9A400025F70 /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE382403E9A400025F70 /* Message.swift */; };
9449FE3B2403EC3800025F70 /* ADAMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE3A2403EC3800025F70 /* ADAMessage.swift */; };
9449FE3D2403ED0200025F70 /* UserMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE3C2403ED0200025F70 /* UserMessage.swift */; };
9449FE412403EF5A00025F70 /* MessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE402403EF5A00025F70 /* MessageView.swift */; };
9449FE432403F02500025F70 /* MessageBubble.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE422403F02500025F70 /* MessageBubble.swift */; };
9449FE452403F0A600025F70 /* Sender.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE442403F0A600025F70 /* Sender.swift */; };
9449FE472404057D00025F70 /* BubbleTail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE462404057D00025F70 /* BubbleTail.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
......@@ -34,6 +41,13 @@
9449FE322402CBD400025F70 /* ProductInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductInfo.swift; sourceTree = "<group>"; };
9449FE342402CCDA00025F70 /* ConnectionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionManager.swift; sourceTree = "<group>"; };
9449FE362402D29E00025F70 /* Product.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Product.swift; sourceTree = "<group>"; };
9449FE382403E9A400025F70 /* Message.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Message.swift; path = ADAbot/Chat/Message.swift; sourceTree = SOURCE_ROOT; };
9449FE3A2403EC3800025F70 /* ADAMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ADAMessage.swift; sourceTree = "<group>"; };
9449FE3C2403ED0200025F70 /* UserMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserMessage.swift; sourceTree = "<group>"; };
9449FE402403EF5A00025F70 /* MessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageView.swift; sourceTree = "<group>"; };
9449FE422403F02500025F70 /* MessageBubble.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageBubble.swift; sourceTree = "<group>"; };
9449FE442403F0A600025F70 /* Sender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sender.swift; sourceTree = "<group>"; };
9449FE462404057D00025F70 /* BubbleTail.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BubbleTail.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -69,11 +83,9 @@
9449FE1A2402C84F00025F70 /* AppDelegate.swift */,
9449FE1C2402C84F00025F70 /* SceneDelegate.swift */,
9449FE1E2402C84F00025F70 /* ContentView.swift */,
9449FE2E2402C8C500025F70 /* ProductView.swift */,
9449FE342402CCDA00025F70 /* ConnectionManager.swift */,
9449FE302402C90800025F70 /* ChatView.swift */,
9449FE322402CBD400025F70 /* ProductInfo.swift */,
9449FE362402D29E00025F70 /* Product.swift */,
9449FE3F2403EDD300025F70 /* Product */,
9449FE3E2403EDB400025F70 /* Chat */,
9449FE202402C85300025F70 /* Assets.xcassets */,
9449FE252402C85300025F70 /* LaunchScreen.storyboard */,
9449FE282402C85300025F70 /* Info.plist */,
......@@ -90,6 +102,32 @@
path = "Preview Content";
sourceTree = "<group>";
};
9449FE3E2403EDB400025F70 /* Chat */ = {
isa = PBXGroup;
children = (
9449FE442403F0A600025F70 /* Sender.swift */,
9449FE382403E9A400025F70 /* Message.swift */,
9449FE3A2403EC3800025F70 /* ADAMessage.swift */,
9449FE3C2403ED0200025F70 /* UserMessage.swift */,
9449FE302402C90800025F70 /* ChatView.swift */,
9449FE402403EF5A00025F70 /* MessageView.swift */,
9449FE422403F02500025F70 /* MessageBubble.swift */,
9449FE462404057D00025F70 /* BubbleTail.swift */,
);
name = Chat;
path = ADAbot/Chat;
sourceTree = SOURCE_ROOT;
};
9449FE3F2403EDD300025F70 /* Product */ = {
isa = PBXGroup;
children = (
9449FE2E2402C8C500025F70 /* ProductView.swift */,
9449FE322402CBD400025F70 /* ProductInfo.swift */,
9449FE362402D29E00025F70 /* Product.swift */,
);
path = Product;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
......@@ -161,9 +199,16 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9449FE412403EF5A00025F70 /* MessageView.swift in Sources */,
9449FE3B2403EC3800025F70 /* ADAMessage.swift in Sources */,
9449FE3D2403ED0200025F70 /* UserMessage.swift in Sources */,
9449FE2F2402C8C500025F70 /* ProductView.swift in Sources */,
9449FE372402D29E00025F70 /* Product.swift in Sources */,
9449FE472404057D00025F70 /* BubbleTail.swift in Sources */,
9449FE432403F02500025F70 /* MessageBubble.swift in Sources */,
9449FE1B2402C84F00025F70 /* AppDelegate.swift in Sources */,
9449FE392403E9A400025F70 /* Message.swift in Sources */,
9449FE452403F0A600025F70 /* Sender.swift in Sources */,
9449FE312402C90800025F70 /* ChatView.swift in Sources */,
9449FE352402CCDA00025F70 /* ConnectionManager.swift in Sources */,
9449FE1D2402C84F00025F70 /* SceneDelegate.swift in Sources */,
......
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "91D35F8C-89D0-4137-A5A5-FAFC5B6FE3BA"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "01922009-364B-4C8F-87E5-58886FB47FAA"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ADAbot/Chat/MessageView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "34"
endingLineNumber = "34"
landmarkName = "body"
landmarkType = "24">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
//
// ADAMessage.swift
// ADAbot
//
// Created by Joel Oksanen on 24.2.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
import UIKit
struct ADAMessage: Message, Decodable {
let id = UUID()
let sender = Sender.ADA
let text: String
}
//
// BubbleTail.swift
// ADAbot
//
// Created by Joel Oksanen on 24.2.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
//import SwiftUI
//import UIKit
//
//struct MessageBubble: Shape {
//
// let cornerRadius: CGFloat = 10
// let sender: Sender
//
// func path(in rect: CGRect) -> Path {
// let roundedCorners: UIRectCorner =
// sender == .ADA ? [.topLeft, .topRight, .bottomRight] : [.topLeft, .topRight, .bottomLeft]
// let path = UIBezierPath(roundedRect: rect, byRoundingCorners: roundedCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
// return Path(path.cgPath)
// }
//
//}
......@@ -9,11 +9,22 @@
import SwiftUI
struct ChatView: View {
@ObservedObject var connectionManager = ConnectionManager()
var body: some View {
ZStack {
Rectangle()
.foregroundColor(Color(red: 231/255, green: 234/255, blue: 239/255))
Text("Chat")
ScrollView {
VStack {
ForEach(connectionManager.messages, id: \.id) { message in
MessageView(message: message)
.padding(EdgeInsets(top: 0, leading: 0, bottom: 15, trailing: 0))
}
}
.padding(EdgeInsets(top: 30, leading: 20, bottom: 30, trailing: 20))
}
}
}
}
//
// Message.swift
// ADAbot
//
// Created by Joel Oksanen on 24.2.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
import UIKit
protocol Message {
var id: UUID { get }
var sender: Sender { get }
var text: String { get }
}
//
// MessageBubble.swift
// ADAbot
//
// Created by Joel Oksanen on 24.2.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
import SwiftUI
import UIKit
struct MessageBubble: Shape {
let cornerRadius: CGFloat = 10
let sender: Sender
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let curveCenter = CGPoint(x: rect.width - cornerRadius, y: rect.height - cornerRadius)
var tailPath = UIBezierPath()
tailPath.move(to: curveCenter)
tailPath.addLine(to: CGPoint(x: curveCenter.x, y: rect.height))
tailPath.addQuadCurve(to: CGPoint(x: rect.width + cornerRadius / 2, y: curveCenter.y),
controlPoint: CGPoint(x: rect.width + cornerRadius / 2, y: rect.height))
tailPath.close()
if (sender == .ADA) {
tailPath.apply(CGAffineTransform(scaleX: -1, y: 1))
tailPath.apply(CGAffineTransform(translationX: rect.width, y: 0))
} else {
tailPath = tailPath.reversing()
}
path.append(tailPath)
return Path(path.cgPath)
}
}
//
// MessageView.swift
// ADAbot
//
// Created by Joel Oksanen on 24.2.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
import SwiftUI
struct MessageView: View {
let bubbleColors = [
Sender.ADA: Color(red: 242/255, green: 159/255, blue: 31/255),
Sender.USER: Color(red: 35/255, green: 45/255, blue: 62/255)
]
let maxWidth: CGFloat = 300
let message: Message
var body: some View {
HStack(spacing: 0) {
if message.sender == .USER {
Spacer()
}
Text(message.text)
.foregroundColor(Color.white)
.font(Font.custom("Helvetica Neue", size: 14))
.padding(EdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20))
.background(MessageBubble(sender: message.sender).foregroundColor(bubbleColors[message.sender]))
.frame(minWidth: 0, maxWidth: maxWidth, alignment: message.sender == .ADA ? .leading : .trailing)
if message.sender == .ADA {
Spacer()
}
}
}
}
//
// Sender.swift
// ADAbot
//
// Created by Joel Oksanen on 24.2.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
enum Sender: String, Decodable {
case ADA
case USER
}
//
// UserMessage.swift
// ADAbot
//
// Created by Joel Oksanen on 24.2.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
import UIKit
struct UserMessage: Message {
let id = UUID()
let sender = Sender.USER
let text: String
}
......@@ -10,6 +10,8 @@ import SwiftUI
class ConnectionManager: ObservableObject {
@Published var product = Product()
@Published var messages: [Message] = [UserMessage(text: "Why was the camera highly rated?"),
ADAMessage(text: "The camera was highly rated because the lens was good, although the battery was poor.")]
init() {
requestProduct(id: "B00RTGK0N0")
......
......@@ -12,9 +12,9 @@ struct ContentView: View {
var body: some View {
VStack(spacing: 0) {
ProductView()
.zIndex(2)
.zIndex(10)
ChatView()
.zIndex(1)
.zIndex(0)
}
.edgesIgnoringSafeArea(.all)
.background(Color.black)
......
......@@ -17,12 +17,13 @@ struct ProductView: View {
Rectangle()
.foregroundColor(Color.white)
.frame(height: height)
.shadow(color: Color(.sRGB, white: 0, opacity: 0.05), radius: 20, x: 0, y: 8)
.shadow(color: Color(.sRGB, white: 0, opacity: 0.1), radius: 10, x: 0, y: 0)
VStack {
Spacer()
HStack(alignment: .top) {
Text(connectionManager.product.name)
.font(Font.custom("Helvetica Neue", size: 14))
.foregroundColor(Color.gray)
Spacer()
Image(uiImage: connectionManager.product.image)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment