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 |
|
| 43 | MessageTextPage::MessageTextPage()
|
| 44 | {
|
| 45 | InitializeComponent();
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 46 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 47 | /* 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 Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 51 | /* connect to delegates */
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 52 | RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 53 | String^ fromRingId, String^ payload) {
|
| 54 | scrollDown();
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 55 | });
|
Nicolas Jager | 8f805cd | 2016-10-05 11:54:40 -0400 | [diff] [blame] | 56 | RingD::instance->incomingMessage += ref new RingClientUWP::IncomingMessage(this, &RingClientUWP::Views::MessageTextPage::OnincomingMessage);
|
| 57 |
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 58 | }
|
| 59 |
|
| 60 | void
|
| 61 | RingClientUWP::Views::MessageTextPage::updatePageContent()
|
| 62 | {
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 63 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 64 | auto contact = item->_contact;
|
| 65 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 66 |
|
| 67 |
|
| 68 | if (!contact) /* should never happen */
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 69 | return;
|
| 70 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 71 |
|
| 72 | /* show the name of contact on the page */
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 73 | _title_->Text = contact->name_;
|
| 74 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 75 | /* show messages */
|
Nicolas Jager | 93abfea | 2016-08-30 12:33:07 -0400 | [diff] [blame] | 76 | _messagesList_->ItemsSource = contact->_conversation->_messages;
|
| 77 |
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 78 | /* 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 Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 97 | scrollDown();
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 98 |
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 99 | }
|
| 100 |
|
| 101 | void RingClientUWP::Views::MessageTextPage::scrollDown()
|
| 102 | {
|
| 103 | _scrollView_->UpdateLayout();
|
| 104 | _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 105 | }
|
| 106 |
|
| 107 | void
|
| 108 | RingClientUWP::Views::MessageTextPage::_sendBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
|
| 109 | {
|
| 110 | sendMessage();
|
| 111 | }
|
| 112 |
|
| 113 | void
|
| 114 | RingClientUWP::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 |
|
| 121 | void
|
| 122 | RingClientUWP::Views::MessageTextPage::sendMessage()
|
| 123 | {
|
Nicolas Jager | c551c36 | 2016-10-01 19:24:50 -0400 | [diff] [blame] | 124 | auto item = SmartPanelItemsViewModel::instance->_selectedItem;
|
| 125 | auto contact = item->_contact;
|
| 126 |
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 127 | auto txt = _messageTextBox_->Text;
|
| 128 |
|
| 129 | /* empty the textbox */
|
| 130 | _messageTextBox_->Text = "";
|
| 131 |
|
| 132 | if (!contact || txt->IsEmpty())
|
| 133 | return;
|
| 134 |
|
Nicolas Jager | 655df54 | 2016-08-31 10:24:47 -0400 | [diff] [blame] | 135 | RingD::instance->sendAccountTextMessage(txt);
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 136 | scrollDown();
|
Nicolas Jager | 8a85e1f | 2016-08-15 15:11:06 -0400 | [diff] [blame] | 137 | }
|
Nicolas Jager | 7bef149 | 2016-09-14 11:23:29 -0400 | [diff] [blame] | 138 |
|
| 139 | Object ^ 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
|
| 148 | Object ^ 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 |
|
| 153 | RingClientUWP::Views::BubbleBackground::BubbleBackground()
|
| 154 | {}
|
| 155 |
|
| 156 | Object ^ 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
|
| 162 | Object ^ 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 |
|
| 167 | RingClientUWP::Views::BubbleHorizontalAlignement::BubbleHorizontalAlignement()
|
| 168 | {}
|
Nicolas Jager | 8f805cd | 2016-10-05 11:54:40 -0400 | [diff] [blame] | 169 |
|
| 170 |
|
Nicolas Jager | d57809f | 2016-10-06 11:31:55 -0400 | [diff] [blame] | 171 | void RingClientUWP::Views::MessageTextPage::OnincomingMessage(Platform::String ^callId, Platform::String ^payload)
|
Nicolas Jager | 8f805cd | 2016-10-05 11:54:40 -0400 | [diff] [blame] | 172 | {
|
| 173 | scrollDown();
|
| 174 | }
|
Nicolas Jager | d8e22fd | 2016-10-30 09:41:10 -0400 | [diff] [blame] | 175 |
|
| 176 |
|
| 177 | void 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 Jager | e72856b | 2016-10-30 15:06:17 -0400 | [diff] [blame^] | 181 | }
|
| 182 |
|
| 183 | void 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 | }
|