Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 1 | /**************************************************************************
|
| 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 Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 19 | #include "ContactsViewModel.h"
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 20 |
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 21 | #include "MainPage.xaml.h"
|
| 22 |
|
| 23 | #include "MessageTextPage.xaml.h"
|
| 24 |
|
| 25 | using namespace RingClientUWP::Views;
|
| 26 | using namespace RingClientUWP::ViewModel;
|
| 27 |
|
| 28 | using namespace Platform;
|
| 29 | using namespace Windows::Foundation;
|
| 30 | using namespace Windows::Foundation::Collections;
|
| 31 | using namespace Windows::UI::Xaml;
|
| 32 | using namespace Windows::UI::Xaml::Controls;
|
| 33 | using namespace Windows::UI::Xaml::Controls::Primitives;
|
| 34 | using namespace Windows::UI::Xaml::Data;
|
| 35 | using namespace Windows::UI::Xaml::Documents;
|
| 36 | using namespace Windows::UI::Xaml::Input;
|
| 37 | using namespace Windows::UI::Xaml::Media;
|
| 38 | using namespace Windows::UI::Xaml::Navigation;
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 39 | using namespace Windows::ApplicationModel::Core;
|
| 40 | using namespace Platform;
|
| 41 | using namespace Windows::UI::Core;
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 42 |
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 43 | using namespace Windows::UI::Popups;
|
| 44 |
|
Nicolas Jager | 7f34b77 | 2016-12-09 12:25:25 -0500 | [diff] [blame] | 45 | // refacto : the message text page should be
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 46 | MessageTextPage::MessageTextPage()
|
| 47 | {
|
| 48 | InitializeComponent();
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 49 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 50 | /* bind the source to account only able to be used to contact the contact */
|
| 51 | _associableAccountsList_->ItemsSource = AccountsViewModel::instance->accountsList;
|
| 52 | _associableAccountsList_->SelectionChanged += ref new Windows::UI::Xaml::Controls::SelectionChangedEventHandler(this, &RingClientUWP::Views::MessageTextPage::OnSelectionChanged);
|
| 53 |
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 54 | /* connect to delegates */
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 55 | RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 56 | String^ fromRingId, String^ payload) {
|
| 57 | scrollDown();
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 58 | });
|
Nicolas Jager | 8f805cd | 2016-10-05 11:54:40 -0400 | [diff] [blame] | 59 | RingD::instance->incomingMessage += ref new RingClientUWP::IncomingMessage(this, &RingClientUWP::Views::MessageTextPage::OnincomingMessage);
|
| 60 |
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 61 | }
|
| 62 |
|
| 63 | void
|
| 64 | RingClientUWP::Views::MessageTextPage::updatePageContent()
|
| 65 | {
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 66 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 67 | auto contact = item->_contact;
|
| 68 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 69 | if (!contact) /* should never happen */
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 70 | return;
|
| 71 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 72 | /* show the name of contact on the page */
|
Nicolas Jager | 7f34b77 | 2016-12-09 12:25:25 -0500 | [diff] [blame] | 73 | _title_->Text = contact->_name;
|
| 74 | _profilName_->Text = contact->_displayName;
|
Nicolas Jager | 8e8787f | 2016-12-11 17:25:17 -0500 | [diff] [blame] | 75 | contact->_unreadMessages = 0;
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 76 |
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 77 | String^ image_path = Utils::toPlatformString(RingD::instance->getLocalFolder()) + ".vcards\\" + contact->_vcardUID + ".png";
|
atraczyk | 890921f | 2016-12-02 16:26:31 -0500 | [diff] [blame] | 78 | if (Utils::fileExists(Utils::toString(image_path))) {
|
| 79 | auto uri = ref new Windows::Foundation::Uri(image_path);
|
| 80 | _contactBarAvatar_->ImageSource = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
|
| 81 | }
|
Nicolas Jager | 7f34b77 | 2016-12-09 12:25:25 -0500 | [diff] [blame] | 82 | else {
|
| 83 | auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/TESTS/contactAvatar.png");
|
| 84 | _contactBarAvatar_->ImageSource = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
|
| 85 | }
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 86 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 87 | /* show messages */
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 88 | _messagesList_->ItemsSource = contact->_conversation->_messages;
|
| 89 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 90 | /* select the associated accountId stored with the contact */
|
| 91 | auto accountIdAssociated = contact->_accountIdAssociated;
|
| 92 | auto list = AccountsViewModel::instance->accountsList;
|
| 93 | unsigned int index = 0;
|
| 94 | bool found = true;
|
| 95 |
|
| 96 | for (auto item : list)
|
| 97 | if (item->accountID_ == accountIdAssociated) {
|
| 98 | found = list->IndexOf(item, &index);
|
| 99 | break;
|
| 100 | }
|
| 101 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 102 | if (found)
|
| 103 | _associableAccountsList_->SelectedIndex = index;
|
| 104 | else
|
| 105 | ERR_("mismatch between accountIdAssociated and associable accounts!");
|
| 106 |
|
| 107 | /* scroll to the last message on the page*/
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 108 | scrollDown();
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 109 |
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 110 | }
|
| 111 |
|
| 112 | void RingClientUWP::Views::MessageTextPage::scrollDown()
|
| 113 | {
|
| 114 | _scrollView_->UpdateLayout();
|
| 115 | _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 116 | }
|
| 117 |
|
| 118 | void
|
| 119 | RingClientUWP::Views::MessageTextPage::_sendBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
|
| 120 | {
|
| 121 | sendMessage();
|
| 122 | }
|
| 123 |
|
| 124 | void
|
| 125 | RingClientUWP::Views::MessageTextPage::_messageTextBox__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
|
| 126 | {
|
| 127 | if (e->Key == Windows::System::VirtualKey::Enter) {
|
| 128 | sendMessage();
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | void
|
| 133 | RingClientUWP::Views::MessageTextPage::sendMessage()
|
| 134 | {
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 135 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 136 | auto contact = item->_contact;
|
| 137 |
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 138 | auto txt = _messageTextBox_->Text;
|
| 139 |
|
| 140 | /* empty the textbox */
|
| 141 | _messageTextBox_->Text = "";
|
| 142 |
|
| 143 | if (!contact || txt->IsEmpty())
|
| 144 | return;
|
| 145 |
|
Nicolas Jager | 655df54 | 2016-08-31 10:24:47 -0400 | [diff] [blame] | 146 | RingD::instance->sendAccountTextMessage(txt);
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 147 | scrollDown();
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 148 | }
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 149 |
|
| 150 | Object ^ RingClientUWP::Views::BubbleBackground::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
|
| 151 | {
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 152 | auto color1 = Windows::UI::ColorHelper::FromArgb(255, 0, 76, 96);
|
Nicolas Jager | 9d85be9 | 2016-12-10 16:10:31 -0500 | [diff] [blame] | 153 | auto color2 = Windows::UI::ColorHelper::FromArgb(255, 58, 192, 210); // refacto : defines colors used by the application as globals
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 154 | return ((bool)value) ? ref new SolidColorBrush(color1) : ref new SolidColorBrush(color2);
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 155 | }
|
| 156 |
|
| 157 | // we only do OneWay so the next function is not used
|
| 158 | Object ^ RingClientUWP::Views::BubbleBackground::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
|
| 159 | {
|
| 160 | throw ref new Platform::NotImplementedException();
|
| 161 | }
|
| 162 |
|
| 163 | RingClientUWP::Views::BubbleBackground::BubbleBackground()
|
| 164 | {}
|
| 165 |
|
| 166 | Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
|
| 167 | {
|
| 168 | return ((bool)value) ? Windows::UI::Xaml::HorizontalAlignment::Left : Windows::UI::Xaml::HorizontalAlignment::Right;
|
| 169 | }
|
| 170 |
|
| 171 | // we only do OneWay so the next function is not used
|
| 172 | Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
|
| 173 | {
|
| 174 | throw ref new Platform::NotImplementedException();
|
| 175 | }
|
| 176 |
|
| 177 | RingClientUWP::Views::BubbleHorizontalAlignement::BubbleHorizontalAlignement()
|
| 178 | {}
|
Nicolas Jager | 8f805cd | 2016-10-05 11:54:40 -0400 | [diff] [blame] | 179 |
|
| 180 |
|
Nicolas Jager | d57809f | 2016-10-06 11:31:55 -0400 | [diff] [blame] | 181 | void RingClientUWP::Views::MessageTextPage::OnincomingMessage(Platform::String ^callId, Platform::String ^payload)
|
Nicolas Jager | 8f805cd | 2016-10-05 11:54:40 -0400 | [diff] [blame] | 182 | {
|
| 183 | scrollDown();
|
| 184 | }
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 185 |
|
| 186 |
|
| 187 | void RingClientUWP::Views::MessageTextPage::OnSelectionChanged(Platform::Object ^sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs ^e)
|
| 188 | {
|
| 189 | auto account = dynamic_cast<Account^>(_associableAccountsList_->SelectedItem);
|
| 190 | SmartPanelItemsViewModel::instance->_selectedItem->_contact->_accountIdAssociated = account->accountID_;
|
Nicolas Jager | e72856b | 2016-10-30 15:06:17 -0400 | [diff] [blame] | 191 | }
|
| 192 |
|
| 193 | void RingClientUWP::Views::MessageTextPage::_deleteContact__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
|
| 194 | {
|
| 195 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 196 | auto contact = item->_contact;
|
| 197 |
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 198 | auto messageDialog = ref new MessageDialog("Are you sure you want to remove this contact?", "Remove Contact");
|
Nicolas Jager | e72856b | 2016-10-30 15:06:17 -0400 | [diff] [blame] | 199 |
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 200 | messageDialog->Commands->Append(ref new UICommand("Cancel", ref new UICommandInvokedHandler([this](IUICommand^ command)
|
| 201 | {})));
|
| 202 | messageDialog->Commands->Append(ref new UICommand("Remove", ref new UICommandInvokedHandler([=](IUICommand^ command)
|
| 203 | {
|
| 204 | closeMessageTextPage();
|
| 205 | ContactsViewModel::instance->deleteContact(contact);
|
| 206 | SmartPanelItemsViewModel::instance->removeItem(item);
|
| 207 | })));
|
| 208 |
|
| 209 | messageDialog->DefaultCommandIndex = 1;
|
| 210 |
|
| 211 | messageDialog->ShowAsync();
|
Nicolas Jager | e72856b | 2016-10-30 15:06:17 -0400 | [diff] [blame] | 212 | }
|
Nicolas Jager | 1d4ab4a | 2016-10-30 15:18:26 -0400 | [diff] [blame] | 213 |
|
| 214 |
|
| 215 | void RingClientUWP::Views::MessageTextPage::_clearConversation__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
|
| 216 | {
|
| 217 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 218 | auto contact = item->_contact;
|
atraczyk | aeb4a5c | 2016-12-01 19:17:18 -0500 | [diff] [blame] | 219 |
|
| 220 | auto messageDialog = ref new MessageDialog("Are you sure you want to clear the conversation?", "Clear conversation");
|
| 221 |
|
| 222 | messageDialog->Commands->Append(ref new UICommand("Cancel", ref new UICommandInvokedHandler([this](IUICommand^ command)
|
| 223 | {})));
|
| 224 | messageDialog->Commands->Append(ref new UICommand("Clear", ref new UICommandInvokedHandler([=](IUICommand^ command)
|
| 225 | {
|
| 226 | contact->_conversation->_messages->Clear();
|
| 227 | contact->saveConversationToFile();
|
| 228 | })));
|
| 229 |
|
| 230 | messageDialog->DefaultCommandIndex = 1;
|
| 231 |
|
| 232 | messageDialog->ShowAsync();
|
Nicolas Jager | 1d4ab4a | 2016-10-30 15:18:26 -0400 | [diff] [blame] | 233 | }
|
Nicolas Jager | 61e6f0e | 2016-11-04 15:12:08 -0400 | [diff] [blame] | 234 |
|
| 235 |
|
| 236 | void RingClientUWP::Views::MessageTextPage::_audioCall__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
|
| 237 | {
|
| 238 | auto button = dynamic_cast<Button^>(e->OriginalSource);
|
| 239 | if (button) {
|
| 240 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 241 | if (item) {
|
| 242 | auto contact = item->_contact;
|
| 243 | if (contact)
|
| 244 | RingD::instance->placeCall(contact);
|
| 245 | }
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 |
|
| 250 | void RingClientUWP::Views::MessageTextPage::_videoCall__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
|
| 251 | {
|
| 252 | auto button = dynamic_cast<Button^>(e->OriginalSource);
|
| 253 | if (button) {
|
| 254 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 255 | if (item) {
|
| 256 | auto contact = item->_contact;
|
| 257 | if (contact)
|
| 258 | RingD::instance->placeCall(contact);
|
| 259 | }
|
| 260 | }
|
| 261 | }
|