blob: 5adb8d1d30cce2bed60e91b094a73bf847d7ddfe [file] [log] [blame]
Nicolas Jager8a85e1f2016-08-15 15:11:06 -04001/**************************************************************************
2* Copyright (C) 2016 by Savoir-faire Linux *
3* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
4* *
5* This program is free software; you can redistribute it and/or modify *
6* it under the terms of the GNU General Public License as published by *
7* the Free Software Foundation; either version 3 of the License, or *
8* (at your option) any later version. *
9* *
10* This program is distributed in the hope that it will be useful, *
11* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13* GNU General Public License for more details. *
14* *
15* You should have received a copy of the GNU General Public License *
16* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17**************************************************************************/
18#include "pch.h"
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040019#include "ContactsViewModel.h"
Nicolas Jagerc551c362016-10-01 19:24:50 -040020
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040021#include "MainPage.xaml.h"
22
23#include "MessageTextPage.xaml.h"
24
25using namespace RingClientUWP::Views;
26using namespace RingClientUWP::ViewModel;
27
28using namespace Platform;
29using namespace Windows::Foundation;
30using namespace Windows::Foundation::Collections;
31using namespace Windows::UI::Xaml;
32using namespace Windows::UI::Xaml::Controls;
33using namespace Windows::UI::Xaml::Controls::Primitives;
34using namespace Windows::UI::Xaml::Data;
35using namespace Windows::UI::Xaml::Documents;
36using namespace Windows::UI::Xaml::Input;
37using namespace Windows::UI::Xaml::Media;
38using namespace Windows::UI::Xaml::Navigation;
Nicolas Jager7bef1492016-09-14 11:23:29 -040039using namespace Windows::ApplicationModel::Core;
40using namespace Platform;
41using namespace Windows::UI::Core;
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040042
43MessageTextPage::MessageTextPage()
44{
45 InitializeComponent();
Nicolas Jager93abfea2016-08-30 12:33:07 -040046
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040047 /* bind the source to account only able to be used to contact the contact */
48 _associableAccountsList_->ItemsSource = AccountsViewModel::instance->accountsList;
49 _associableAccountsList_->SelectionChanged += ref new Windows::UI::Xaml::Controls::SelectionChangedEventHandler(this, &RingClientUWP::Views::MessageTextPage::OnSelectionChanged);
50
Nicolas Jagerc551c362016-10-01 19:24:50 -040051 /* connect to delegates */
Nicolas Jager93abfea2016-08-30 12:33:07 -040052 RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
Nicolas Jagerc551c362016-10-01 19:24:50 -040053 String^ fromRingId, String^ payload) {
54 scrollDown();
Nicolas Jager7bef1492016-09-14 11:23:29 -040055 });
Nicolas Jager8f805cd2016-10-05 11:54:40 -040056 RingD::instance->incomingMessage += ref new RingClientUWP::IncomingMessage(this, &RingClientUWP::Views::MessageTextPage::OnincomingMessage);
57
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040058}
59
60void
61RingClientUWP::Views::MessageTextPage::updatePageContent()
62{
Nicolas Jagerc551c362016-10-01 19:24:50 -040063 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
64 auto contact = item->_contact;
65
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040066
67
68 if (!contact) /* should never happen */
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040069 return;
70
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040071
72 /* show the name of contact on the page */
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040073 _title_->Text = contact->name_;
74
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040075 /* show messages */
Nicolas Jager93abfea2016-08-30 12:33:07 -040076 _messagesList_->ItemsSource = contact->_conversation->_messages;
77
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040078 /* select the associated accountId stored with the contact */
79 auto accountIdAssociated = contact->_accountIdAssociated;
80 auto list = AccountsViewModel::instance->accountsList;
81 unsigned int index = 0;
82 bool found = true;
83
84 for (auto item : list)
85 if (item->accountID_ == accountIdAssociated) {
86 found = list->IndexOf(item, &index);
87 break;
88 }
89
90
91 if (found)
92 _associableAccountsList_->SelectedIndex = index;
93 else
94 ERR_("mismatch between accountIdAssociated and associable accounts!");
95
96 /* scroll to the last message on the page*/
Nicolas Jager7bef1492016-09-14 11:23:29 -040097 scrollDown();
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040098
Nicolas Jager7bef1492016-09-14 11:23:29 -040099}
100
101void RingClientUWP::Views::MessageTextPage::scrollDown()
102{
103 _scrollView_->UpdateLayout();
104 _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400105}
106
107void
108RingClientUWP::Views::MessageTextPage::_sendBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
109{
110 sendMessage();
111}
112
113void
114RingClientUWP::Views::MessageTextPage::_messageTextBox__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
115{
116 if (e->Key == Windows::System::VirtualKey::Enter) {
117 sendMessage();
118 }
119}
120
121void
122RingClientUWP::Views::MessageTextPage::sendMessage()
123{
Nicolas Jagerc551c362016-10-01 19:24:50 -0400124 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
125 auto contact = item->_contact;
126
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400127 auto txt = _messageTextBox_->Text;
128
129 /* empty the textbox */
130 _messageTextBox_->Text = "";
131
132 if (!contact || txt->IsEmpty())
133 return;
134
Nicolas Jager655df542016-08-31 10:24:47 -0400135 RingD::instance->sendAccountTextMessage(txt);
Nicolas Jager7bef1492016-09-14 11:23:29 -0400136 scrollDown();
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400137}
Nicolas Jager7bef1492016-09-14 11:23:29 -0400138
139Object ^ RingClientUWP::Views::BubbleBackground::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
140{
141 auto settings = ref new Windows::UI::ViewManagement::UISettings();
142 auto color = settings->GetColorValue(Windows::UI::ViewManagement::UIColorType::Accent);
143
144 return ((bool)value) ? ref new SolidColorBrush(color) : ref new SolidColorBrush(Windows::UI::Colors::LightBlue);
145}
146
147// we only do OneWay so the next function is not used
148Object ^ RingClientUWP::Views::BubbleBackground::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
149{
150 throw ref new Platform::NotImplementedException();
151}
152
153RingClientUWP::Views::BubbleBackground::BubbleBackground()
154{}
155
156Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
157{
158 return ((bool)value) ? Windows::UI::Xaml::HorizontalAlignment::Left : Windows::UI::Xaml::HorizontalAlignment::Right;
159}
160
161// we only do OneWay so the next function is not used
162Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
163{
164 throw ref new Platform::NotImplementedException();
165}
166
167RingClientUWP::Views::BubbleHorizontalAlignement::BubbleHorizontalAlignement()
168{}
Nicolas Jager8f805cd2016-10-05 11:54:40 -0400169
170
Nicolas Jagerd57809f2016-10-06 11:31:55 -0400171void RingClientUWP::Views::MessageTextPage::OnincomingMessage(Platform::String ^callId, Platform::String ^payload)
Nicolas Jager8f805cd2016-10-05 11:54:40 -0400172{
173 scrollDown();
174}
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -0400175
176
177void RingClientUWP::Views::MessageTextPage::OnSelectionChanged(Platform::Object ^sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs ^e)
178{
179 auto account = dynamic_cast<Account^>(_associableAccountsList_->SelectedItem);
180 SmartPanelItemsViewModel::instance->_selectedItem->_contact->_accountIdAssociated = account->accountID_;
Nicolas Jagere72856b2016-10-30 15:06:17 -0400181}
182
183void RingClientUWP::Views::MessageTextPage::_deleteContact__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
184{
185 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
186 auto contact = item->_contact;
187
188 closeMessageTextPage();
189
190 ContactsViewModel::instance->deleteContact(contact);
191 SmartPanelItemsViewModel::instance->removeItem(item);
192}