aboutsummaryrefslogtreecommitdiffstats
path: root/src/message_view.rs
blob: 4709dd6f4c531a2fa3fe6d0150aa0a48b1474635 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::borrow::Cow;

use chrono::NaiveDate;
use iced::{
    alignment::Horizontal::{self, Right},
    border::Radius,
    color,
    theme::Palette,
    widget::{button, column, container, row, scrollable, text, text_input, Column},
    Border, Color, Element,
    Length::{Fill, Shrink},
    Theme,
};
use indexmap::IndexMap;
use jid::JID;
use luz::chat::Message as ChatMessage;
use uuid::Uuid;

pub struct MessageView {
    pub jid: JID,
    pub message_history: IndexMap<Uuid, ChatMessage>,
    pub new_message: String,
}

#[derive(Debug, Clone)]
pub enum Message {
    MessageHistory(Vec<ChatMessage>),
    Message(ChatMessage),
    MessageCompose(String),
    SendMessage(String),
}

pub enum Action {
    None,
    SendMessage(String),
}

impl MessageView {
    pub fn new(jid: JID) -> Self {
        Self {
            jid,
            // TODO: save position in message history
            message_history: IndexMap::new(),
            // TODO: save draft (as part of chat struct?)
            new_message: String::new(),
        }
    }
    pub fn update(&mut self, message: Message) -> Action {
        match message {
            Message::MessageHistory(messages) => {
                if self.message_history.is_empty() {
                    self.message_history = messages
                        .into_iter()
                        .map(|message| (message.id.clone(), message))
                        .collect();
                }
                Action::None
            }
            Message::Message(message) => {
                let i = self
                    .message_history
                    .iter()
                    .position(|(_id, m)| m.timestamp > message.timestamp);
                if let Some(i) = i {
                    self.message_history.insert_before(i, message.id, message);
                } else {
                    self.message_history.insert(message.id, message);
                }
                Action::None
            }
            Message::MessageCompose(s) => {
                self.new_message = s;
                Action::None
            }
            Message::SendMessage(m) => {
                self.new_message = String::new();
                Action::SendMessage(m)
            }
        }
    }
    pub fn view(&self) -> Element<Message> {
        let mut messages_view = column![].spacing(8);
        let mut latest_date = NaiveDate::MIN;
        for (_id, message) in &self.message_history {
            let message_date = message.timestamp.naive_local().date();
            if message_date > latest_date {
                latest_date = message_date;
                messages_view = messages_view.push(date(latest_date));
            }
            messages_view = messages_view.push(self.message(message));
        }
        let message_send_input = row![
            text_input("new message", &self.new_message).on_input(Message::MessageCompose),
            button("send").on_press(Message::SendMessage(self.new_message.clone()))
        ];
        column![
            scrollable(messages_view)
                .height(Fill)
                .width(Fill)
                .spacing(1)
                .anchor_bottom(),
            message_send_input
        ]
        .into()
    }

    pub fn message<'a>(&'a self, message: &'a ChatMessage) -> Element<'a, Message> {
        let timestamp = message.timestamp.naive_local();
        let timestamp = timestamp.time().format("%H:%M").to_string();

        if self.jid == message.from.as_bare() {
            container(
                container(
                    column![
                        text(message.body.body.as_str()),
                        container(text(timestamp).wrapping(text::Wrapping::None).size(12))
                            .align_right(Fill)
                    ]
                    .width(Shrink)
                    .max_width(500),
                )
                .padding(16)
                .style(|theme: &Theme| {
                    let palette = theme.extended_palette();
                    container::Style::default()
                        .background(palette.primary.weak.color)
                        .border(Border {
                            color: Color::BLACK,
                            width: 4.,
                            radius: Radius::new(16),
                        })
                }),
            )
            .align_left(Fill)
            .into()
        } else {
            let element: Element<Message> = container(
                container(
                    column![
                        text(message.body.body.as_str()),
                        container(text(timestamp).wrapping(text::Wrapping::None).size(12))
                            .align_right(Fill)
                    ]
                    .width(Shrink)
                    .max_width(500),
                )
                .padding(16)
                .style(|theme: &Theme| {
                    let palette = theme.extended_palette();
                    container::Style::default()
                        .background(palette.primary.base.color)
                        .border(Border {
                            color: Color::BLACK,
                            width: 4.,
                            radius: Radius::new(16),
                        })
                }),
            )
            .align_right(Fill)
            .into();
            // element.explain(Color::BLACK)
            element
        }
    }
}

pub fn date(date: NaiveDate) -> Element<'static, Message> {
    container(text(date.to_string())).center_x(Fill).into()
}