From e72856b765aa80f5cc7ce1a5f40e20a3fa89c8a0 Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Sun, 30 Oct 2016 15:06:17 -0400
Subject: [PATCH] contact : add capability to delete contact and conversation
 file

Change-Id: Ia2781ffcbbf1929b5535cb7fd16e2f36eff5eb33
Tuleap: #1285
---
 Contact.cpp                  | 13 +++++++++++++
 Contact.h                    |  1 +
 ContactsViewModel.cpp        | 14 ++++++++++++++
 ContactsViewModel.h          |  3 +++
 MainPage.xaml.cpp            | 10 ++++++++++
 MainPage.xaml.h              |  1 +
 MessageTextPage.xaml         |  5 +++++
 MessageTextPage.xaml.cpp     | 13 ++++++++++++-
 MessageTextPage.xaml.h       |  6 ++++++
 SmartPanel.xaml.cpp          |  5 +++++
 SmartPanel.xaml.h            |  1 +
 SmartPanelItemsViewModel.cpp | 10 +++++++++-
 SmartPanelItemsViewModel.h   |  1 +
 13 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/Contact.cpp b/Contact.cpp
index 50c142c..a151cc6 100644
--- a/Contact.cpp
+++ b/Contact.cpp
@@ -136,6 +136,19 @@ Contact::DestringifyConversation(String^ data)
     }
 }
 
+void RingClientUWP::Contact::deleteConversationFile()
+{
+    StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
+    String^ messagesFile = ".messages\\" + GUID_ + ".json";
+
+    // refacto : Utils::fileExists fails here if the file doesn't exist, code below should replace fileExist everywhere
+    create_task(localfolder->TryGetItemAsync(messagesFile)).then([](IStorageItem^ storageFile) {
+        if (storageFile)
+            storageFile->DeleteAsync();
+    });
+
+}
+
 void
 Contact::saveConversationToFile()
 {
diff --git a/Contact.h b/Contact.h
index aa2671d..8bf4164 100644
--- a/Contact.h
+++ b/Contact.h
@@ -95,6 +95,7 @@ internal:
     void        saveConversationToFile();
     String^     StringifyConversation();
     void        DestringifyConversation(String^ data);
+    void        deleteConversationFile();
 
 protected:
     void NotifyPropertyChanged(String^ propertyName);
diff --git a/ContactsViewModel.cpp b/ContactsViewModel.cpp
index f19f44f..9721c7d 100644
--- a/ContactsViewModel.cpp
+++ b/ContactsViewModel.cpp
@@ -173,6 +173,20 @@ ContactsViewModel::Destringify(String^ data)
     }
 }
 
+void RingClientUWP::ViewModel::ContactsViewModel::deleteContact(Contact ^ contact)
+{
+    unsigned int index;
+    auto itemsList = SmartPanelItemsViewModel::instance->itemsList;
+    auto item = SmartPanelItemsViewModel::instance->_selectedItem;
+
+    if (contactsList_->IndexOf(contact, &index)) {
+        contact->deleteConversationFile();
+        contactsList_->RemoveAt(index);
+    }
+
+    saveContactsToFile();
+}
+
 
 void RingClientUWP::ViewModel::ContactsViewModel::OnincomingMessage(Platform::String ^callId, Platform::String ^payload)
 {
diff --git a/ContactsViewModel.h b/ContactsViewModel.h
index b6ee05f..bd78b1e 100644
--- a/ContactsViewModel.h
+++ b/ContactsViewModel.h
@@ -27,6 +27,7 @@ namespace RingClientUWP
 
 /* delegates */
 delegate void ContactAdded(Contact^);
+delegate void ContactDeleted(Contact^);
 
 namespace ViewModel {
 public ref class ContactsViewModel sealed
@@ -49,6 +50,7 @@ internal:
     void        openContactsFromFile();
     String^     Stringify();
     void        Destringify(String^ data);
+    void        deleteContact(Contact^ contact);
 
     /* properties */
     property Vector<Contact^>^ contactsList
@@ -61,6 +63,7 @@ internal:
 
     /* events */
     event ContactAdded^ contactAdded;
+    event ContactDeleted^ contactDeleted;
 
 private:
     ContactsViewModel(); // singleton
diff --git a/MainPage.xaml.cpp b/MainPage.xaml.cpp
index 70f8418..02057e2 100644
--- a/MainPage.xaml.cpp
+++ b/MainPage.xaml.cpp
@@ -70,6 +70,9 @@ MainPage::MainPage()
     smartPanel->summonVideoPage += ref new RingClientUWP::SummonVideoPage(this, &RingClientUWP::MainPage::OnsummonVideoPage);
     auto videoPage = dynamic_cast<VideoPage^>(_videoFrame_->Content);
     videoPage->pressHangUpCall += ref new RingClientUWP::PressHangUpCall(this, &RingClientUWP::MainPage::OnpressHangUpCall);
+    auto messageTextFrame = dynamic_cast<MessageTextPage^>(_messageTextFrame_->Content);
+    messageTextFrame->closeMessageTextPage += ref new RingClientUWP::CloseMessageTextPage(this, &RingClientUWP::MainPage::OncloseMessageTextPage);
+
 
     DisplayInformation^ displayInformation = DisplayInformation::GetForCurrentView();
     dpiChangedtoken = (displayInformation->DpiChanged += ref new TypedEventHandler<DisplayInformation^,
@@ -398,3 +401,10 @@ MainPage::BeginExtendedExecution()
         }
     });
 }
+
+
+void RingClientUWP::MainPage::OncloseMessageTextPage()
+{
+    auto smartPanel = dynamic_cast<SmartPanel^>(_smartPanel_->Content);
+    smartPanel->unselectContact();
+}
diff --git a/MainPage.xaml.h b/MainPage.xaml.h
index a8b1934..821efbf 100644
--- a/MainPage.xaml.h
+++ b/MainPage.xaml.h
@@ -74,5 +74,6 @@ private:
     void OnsummonVideoPage();
     void OnpressHangUpCall();
     void OnstateChange(Platform::String ^callId, CallStatus state, int code);
+    void OncloseMessageTextPage();
 };
 }
diff --git a/MessageTextPage.xaml b/MessageTextPage.xaml
index 3435963..92767ea 100644
--- a/MessageTextPage.xaml
+++ b/MessageTextPage.xaml
@@ -134,6 +134,7 @@
                                VerticalAlignment="Center"
                                FontSize="20"
                                Margin="20,0" />
+                <StackPanel Orientation="Horizontal">
                 <ComboBox x:Name="_associableAccountsList_">
                     <ComboBox.ItemTemplate>
                         <DataTemplate x:DataType="local:Account">
@@ -143,6 +144,10 @@
                         </DataTemplate>
                     </ComboBox.ItemTemplate>
                 </ComboBox>
+                <Button x:Name="_deleteContact_"
+                        Content="delete"
+                        Click="_deleteContact__Click"/>
+                </StackPanel>
             </StackPanel>
 
         </StackPanel>
diff --git a/MessageTextPage.xaml.cpp b/MessageTextPage.xaml.cpp
index e018cbd..5adb8d1 100644
--- a/MessageTextPage.xaml.cpp
+++ b/MessageTextPage.xaml.cpp
@@ -178,4 +178,15 @@ void RingClientUWP::Views::MessageTextPage::OnSelectionChanged(Platform::Object
 {
     auto account = dynamic_cast<Account^>(_associableAccountsList_->SelectedItem);
     SmartPanelItemsViewModel::instance->_selectedItem->_contact->_accountIdAssociated = account->accountID_;
-}
\ No newline at end of file
+}
+
+void RingClientUWP::Views::MessageTextPage::_deleteContact__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
+{
+    auto item = SmartPanelItemsViewModel::instance->_selectedItem;
+    auto contact = item->_contact;
+
+    closeMessageTextPage();
+
+    ContactsViewModel::instance->deleteContact(contact);
+    SmartPanelItemsViewModel::instance->removeItem(item);
+}
diff --git a/MessageTextPage.xaml.h b/MessageTextPage.xaml.h
index 8b90277..ffc701f 100644
--- a/MessageTextPage.xaml.h
+++ b/MessageTextPage.xaml.h
@@ -21,6 +21,8 @@
 
 namespace RingClientUWP
 {
+delegate void CloseMessageTextPage();
+
 namespace Views
 {
 
@@ -45,6 +47,9 @@ public:
     void updatePageContent();
     void scrollDown();
 
+internal:
+    event CloseMessageTextPage^ closeMessageTextPage;
+
 private:
     void _sendBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
     void _messageTextBox__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e);
@@ -52,6 +57,7 @@ private:
     void OnincomingMessage(Platform::String ^callId, Platform::String ^payload);
     void OnSelectionChanged(Platform::Object ^sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs ^e);
 
+    void _deleteContact__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
 };
 }
 }
diff --git a/SmartPanel.xaml.cpp b/SmartPanel.xaml.cpp
index 8ffbbcb..6b5bc15 100644
--- a/SmartPanel.xaml.cpp
+++ b/SmartPanel.xaml.cpp
@@ -173,6 +173,11 @@ RingClientUWP::Views::SmartPanel::updatePageContent()
     _upnpState_->IsOn = accountListItem->_account->_upnpState;
 }
 
+void RingClientUWP::Views::SmartPanel::unselectContact()
+{
+    _smartList_->SelectedItem = nullptr;
+}
+
 void RingClientUWP::Views::SmartPanel::_accountsMenuButton__Checked(Object^ sender, RoutedEventArgs^ e)
 {
     _shareMenuButton_->IsChecked = false;
diff --git a/SmartPanel.xaml.h b/SmartPanel.xaml.h
index 8cf5f7e..cc53de7 100644
--- a/SmartPanel.xaml.h
+++ b/SmartPanel.xaml.h
@@ -83,6 +83,7 @@ public ref class SmartPanel sealed
 public:
     SmartPanel();
     void updatePageContent();
+    void unselectContact();
 
 internal:
     enum class Mode { Minimized, Normal };
diff --git a/SmartPanelItemsViewModel.cpp b/SmartPanelItemsViewModel.cpp
index e567398..8147ff0 100644
--- a/SmartPanelItemsViewModel.cpp
+++ b/SmartPanelItemsViewModel.cpp
@@ -75,4 +75,12 @@ SmartPanelItemsViewModel::getIndex(Contact^ contact)
             break;
     }
     return i;
-}
\ No newline at end of file
+}
+
+void RingClientUWP::ViewModel::SmartPanelItemsViewModel::removeItem(SmartPanelItem ^ item)
+{
+    unsigned int index;
+
+    if (itemsList->IndexOf(item, &index))
+        itemsList->RemoveAt(index);
+}
diff --git a/SmartPanelItemsViewModel.h b/SmartPanelItemsViewModel.h
index f77d3a0..184120b 100644
--- a/SmartPanelItemsViewModel.h
+++ b/SmartPanelItemsViewModel.h
@@ -46,6 +46,7 @@ internal:
     SmartPanelItem^ findItem(Contact^ contact);
     unsigned int getIndex(String^ callId);
     unsigned int getIndex(Contact^ contact);
+    void removeItem(SmartPanelItem^ item);
 
     property Vector<SmartPanelItem^>^ itemsList
     {
-- 
GitLab