MessageView.swift 2.32 KB
Newer Older
 Joel  Oksanen's avatar
Joel Oksanen committed
1
2
3
4
5
6
7
8
9
10
11
//
//  MessageView.swift
//  ADAbot
//
//  Created by Joel Oksanen on 24.2.2020.
//  Copyright © 2020 Joel Oksanen. All rights reserved.
//

import SwiftUI

struct MessageView: View {
12
13
  @ObservedObject var chatManager: ChatManager
  
 Joel  Oksanen's avatar
Joel Oksanen committed
14
15
16
17
18
19
20
  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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  let query: ArgumentQuery?
  @State var sent: Bool
  
  init(chatManager: ChatManager, message: Message) {
    self.chatManager = chatManager
    self.message = message
    self.query = nil
    _sent = State(initialValue: true)
  }
  
  init(chatManager: ChatManager, query: ArgumentQuery) {
    self.chatManager = chatManager
    self.message = query.message
    self.query = query
    _sent = State(initialValue: false)
  }
 Joel  Oksanen's avatar
Joel Oksanen committed
37
38
  
  var body: some View {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    VStack(spacing: 0) {
      if !chatManager.sent || self.sent {
        HStack(spacing: 0) {
          if message.sender == .USER {
            Spacer()
          }
          
          Text(message.text)
            .foregroundColor(Color.white)
            .font(Font.custom("Helvetica Neue", size: 14))
            .fixedSize(horizontal: false, 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)
            .onTapGesture {
 Joel  Oksanen's avatar
Joel Oksanen committed
54
55
56
57
              withAnimation(.easeInOut(duration: 0.3)) {
                if self.message.sender == .ADA {
                  self.chatManager.showOptionsFor(self.message as! ADAMessage)
                } else if !self.sent {
58
59
60
61
62
63
64
65
66
67
68
                  self.chatManager.sendQuery(query: self.query!)
                  self.sent = true
                }
              }
            }
          
          if message.sender == .ADA {
            Spacer()
          }
        }
        .padding(EdgeInsets(top: 0, leading: 20, bottom: 15, trailing: 20))
 Joel  Oksanen's avatar
Joel Oksanen committed
69
70
      }
      
71
72
      if chatManager.showingOptionsFor(message) {
        FeatureView(chatManager: chatManager, arguments: message.arguments)
 Joel  Oksanen's avatar
Joel Oksanen committed
73
74
      }
    }
75
    .frame(width: UIScreen.main.bounds.width)
 Joel  Oksanen's avatar
Joel Oksanen committed
76
77
  }
}