Commit d72dd740 authored by  Joel  Oksanen's avatar Joel Oksanen

Implemented text bolding in front-end

parent 4ac66e6b
......@@ -29,6 +29,7 @@
9449FE4D2404561400025F70 /* ArgumentQuery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE4C2404561400025F70 /* ArgumentQuery.swift */; };
9449FE4F240533FD00025F70 /* QueryOptionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE4E240533FD00025F70 /* QueryOptionView.swift */; };
9449FE5124053DA500025F70 /* ChatManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9449FE5024053DA500025F70 /* ChatManager.swift */; };
945E6BBD2493931300C0DCAC /* ArgumentText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945E6BBC2493931300C0DCAC /* ArgumentText.swift */; };
94BE1EEB2407E26900741749 /* RatingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94BE1EEA2407E26900741749 /* RatingView.swift */; };
94BE1EED240800D800741749 /* InitResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94BE1EEC240800D800741749 /* InitResponse.swift */; };
/* End PBXBuildFile section */
......@@ -58,6 +59,7 @@
9449FE4C2404561400025F70 /* ArgumentQuery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentQuery.swift; sourceTree = "<group>"; };
9449FE4E240533FD00025F70 /* QueryOptionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryOptionView.swift; sourceTree = "<group>"; };
9449FE5024053DA500025F70 /* ChatManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatManager.swift; sourceTree = "<group>"; };
945E6BBC2493931300C0DCAC /* ArgumentText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentText.swift; sourceTree = "<group>"; };
94BE1EEA2407E26900741749 /* RatingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RatingView.swift; sourceTree = "<group>"; };
94BE1EEC240800D800741749 /* InitResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InitResponse.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
......@@ -122,6 +124,7 @@
9449FE442403F0A600025F70 /* Sender.swift */,
9449FE382403E9A400025F70 /* Message.swift */,
9449FE3A2403EC3800025F70 /* ADAMessage.swift */,
945E6BBC2493931300C0DCAC /* ArgumentText.swift */,
9449FE4A24042E8800025F70 /* Argument.swift */,
9449FE4C2404561400025F70 /* ArgumentQuery.swift */,
9449FE3C2403ED0200025F70 /* UserMessage.swift */,
......@@ -232,6 +235,7 @@
9449FE4924042D6500025F70 /* FeatureView.swift in Sources */,
9449FE312402C90800025F70 /* ChatView.swift in Sources */,
9449FE5124053DA500025F70 /* ChatManager.swift in Sources */,
945E6BBD2493931300C0DCAC /* ArgumentText.swift in Sources */,
9449FE352402CCDA00025F70 /* ConnectionManager.swift in Sources */,
9449FE1D2402C84F00025F70 /* SceneDelegate.swift in Sources */,
9449FE1F2402C84F00025F70 /* ContentView.swift in Sources */,
......
......@@ -11,12 +11,33 @@ import UIKit
struct ADAMessage: Message, Decodable, Equatable {
let id = UUID()
let sender = Sender.ADA
let text: String
let text: ArgumentText
let arguments: [Argument]
let sender: Sender = .ADA
static func == (lhs: ADAMessage, rhs: ADAMessage) -> Bool {
lhs.id == rhs.id
}
func getAttributedText() -> NSAttributedString {
let components = text.template.components(separatedBy: "*")
var argumentLocations = [(Int,Int)]()
var textWithArguments = ""
for (i, component) in components.enumerated() {
textWithArguments += component
if i < components.count - 1 {
argumentLocations.append((textWithArguments.count, text.arguments[i].count))
textWithArguments += text.arguments[i]
}
}
let attributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica Neue", size: 14)!,
NSAttributedString.Key.foregroundColor: UIColor.white]
let attributedString = NSMutableAttributedString(string: textWithArguments, attributes: attributes)
print(attributedString)
for (start, len) in argumentLocations {
attributedString.addAttribute(NSAttributedString.Key.font, value: text.style.getFont(), range: NSMakeRange(start, len))
}
return attributedString
}
}
//
// ADAText.swift
// ADAbot
//
// Created by Joel Oksanen on 12.6.2020.
// Copyright © 2020 Joel Oksanen. All rights reserved.
//
import UIKit
struct ArgumentText: Decodable {
let template: String
let arguments: [String]
let style: TextStyle
}
enum TextStyle: String, Decodable {
case ARG
case QUOT
func getFont() -> UIFont {
switch self {
case .ARG: return UIFont(name: "HelveticaNeue-Bold", size: 14)!
case .QUOT: return UIFont(name: "HelveticaNeue-Italic", size: 14)!
}
}
}
......@@ -9,10 +9,12 @@
import UIKit
protocol Message {
var id: UUID { get }
var sender: Sender { get }
var text: String { get }
var arguments: [Argument] { get }
func getAttributedText() -> NSAttributedString
}
......@@ -43,13 +43,13 @@ struct MessageView: View {
Spacer()
}
Text(message.text)
Label(maxWidth: maxWidth, attributedText: self.message.getAttributedText())
.foregroundColor(Color.white)
.font(Font.custom("Helvetica Neue", size: 14))
.fixedSize(horizontal: false, vertical: true)
// .font(Font.custom("Helvetica Neue", size: 14))
.fixedSize(horizontal: true, vertical: true)
.padding(EdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20))
.background(MessageBubble(sender: message.sender).foregroundColor(sent ? bubbleColors[message.sender] : FeatureView.bubbleColor))
.frame(minWidth: 0, maxWidth: maxWidth, alignment: message.sender == .ADA ? .leading : .trailing)
// .frame(minWidth: 0, maxWidth: maxWidth, alignment: message.sender == .ADA ? .leading : .trailing)
.onTapGesture {
withAnimation(.easeInOut(duration: 0.3)) {
if self.message.sender == .ADA {
......@@ -75,3 +75,29 @@ struct MessageView: View {
.frame(width: UIScreen.main.bounds.width)
}
}
struct Label: UIViewRepresentable {
// typealias TheUIView = UILabel
// fileprivate var configuration = { (view: TheUIView) in }
//
// func makeUIView(context: UIViewRepresentableContext<Self>) -> TheUIView { TheUIView() }
// func updateUIView(_ uiView: TheUIView, context: UIViewRepresentableContext<Self>) {
// configuration(uiView)
// }
var maxWidth: CGFloat
var attributedText: NSAttributedString
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.lineBreakMode = .byWordWrapping
label.attributedText = attributedText
label.numberOfLines = 0
label.preferredMaxLayoutWidth = maxWidth
return label
}
func updateUIView(_ view: UILabel, context: Context) {
}
}
......@@ -9,12 +9,12 @@
import UIKit
struct UserMessage: Message, Equatable {
let id = UUID()
let sender = Sender.USER
let text: String
let arguments = [Argument]()
let sent: Bool
let sender: Sender = .USER
init(text: String) {
self.text = text
......@@ -30,4 +30,10 @@ struct UserMessage: Message, Equatable {
lhs.id == rhs.id
}
func getAttributedText() -> NSAttributedString {
let attributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica Neue", size: 14)!,
NSAttributedString.Key.foregroundColor: UIColor.white]
return NSAttributedString(string: text, attributes: attributes)
}
}
......@@ -17,20 +17,24 @@ class ConnectionManager: ObservableObject {
private var messageQueue: [Message]? = nil
init() {
requestProduct(id: "B000AYW0M2", type: "watch")
requestProduct(id: "B00005UP2N")
// B00RTGK0N0 - red Canon camera
// B004J3V90Y - Canon T3i
// B0012YA85A - Canon Rebel XSI
// B003ZYF3LO - Nikon D3100
// B0075SUK14 - Backpack
// B000AYW0M2 - Watch
// B000ZKA0J6 - Starcraft game
// B00005UP2N - Mixer
}
private func requestProduct(id: String, type: String) {
let url = URL(string: "http://" + ip + ":" + port + "/ios_server/product/?id=" + id + "&type=" + type)!
private func requestProduct(id: String) {
let url = URL(string: "http://" + ip + ":" + port + "/ios_server/product/?id=" + id)!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
do {
print(data)
let resp = try JSONDecoder().decode(InitResponse.self, from: data)
DispatchQueue.main.async {
self.requestImage(at: resp.productInfo.imageURL)
......@@ -86,6 +90,7 @@ class ConnectionManager: ObservableObject {
mimeType == "application/json",
let receivedData = receivedData {
do {
print(receivedData)
let resp = try JSONDecoder().decode(ADAMessage.self, from: receivedData)
DispatchQueue.main.async {
self.addMessage(resp)
......
Markdown is supported
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