From 72b429265250c7d5dc38069d58bcc2a78729f704 Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Tue, 25 Oct 2016 07:52:42 -0400
Subject: [PATCH] internal : add logic and ui to manage account deletion

Change-Id: I95fd4583575c3fdb1e826f92f10a28d03c74683f
Tulesp: #1244
---
 AccountListItem.cpp |  1 +
 AccountListItem.h   |  2 ++
 RingD.cpp           | 21 +++++++++++++++++++++
 RingD.h             |  4 +++-
 SmartPanel.xaml     |  4 ++--
 SmartPanel.xaml.cpp | 33 +++++++++++++++++++++++++++++----
 6 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/AccountListItem.cpp b/AccountListItem.cpp
index 0e3e99d..2d20a89 100644
--- a/AccountListItem.cpp
+++ b/AccountListItem.cpp
@@ -33,6 +33,7 @@ AccountListItem::AccountListItem(Account^ a)
 {
     _account = a;
     _editionMode = false;
+
 }
 
 void
diff --git a/AccountListItem.h b/AccountListItem.h
index e9c0fa8..4bbb2c0 100644
--- a/AccountListItem.h
+++ b/AccountListItem.h
@@ -22,6 +22,8 @@ using namespace Windows::Data::Json;
 using namespace Windows::UI::Xaml;
 using namespace Windows::UI::Xaml::Data;
 
+#include <RingDebug.h>
+
 namespace RingClientUWP
 {
 namespace Controls {
diff --git a/RingD.cpp b/RingD.cpp
index 794cc11..5fe326e 100644
--- a/RingD.cpp
+++ b/RingD.cpp
@@ -293,6 +293,19 @@ void RingClientUWP::RingD::updateAccount(String^ accountId)
     tasksList_.push(task);
 }
 
+void RingClientUWP::RingD::deleteAccount(String ^ accountId)
+{
+    editModeOn_ = true;
+
+    auto frame = dynamic_cast<Frame^>(Window::Current->Content);
+    dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(true, true);
+
+    auto task = ref new RingD::Task(Request::DeleteAccount);
+    task->_accountId = accountId;
+
+    tasksList_.push(task);
+}
+
 void
 RingClientUWP::RingD::startDaemon()
 {
@@ -733,6 +746,14 @@ RingD::dequeueTasks()
             DRing::setAccountDetails(Utils::toString(account->accountID_), accountDetails);
             break;
         }
+        case Request::DeleteAccount:
+        {
+            auto accountId = task->_accountId;
+            auto accountId2 = Utils::toString(accountId);
+
+            DRing::removeAccount(accountId2);
+            break;
+        }
         default:
             break;
         }
diff --git a/RingD.h b/RingD.h
index 6689a27..a304139 100644
--- a/RingD.h
+++ b/RingD.h
@@ -98,6 +98,7 @@ internal:
     void askToExportOnRing(String^ accountId, String^ password);
     void eraseCacheFolder();
     void updateAccount(String^ accountId);
+    void deleteAccount(String^ accountId);
 
     /* TODO : move members */
     ///bool hasConfig; // replaced by startingStatus
@@ -130,7 +131,8 @@ private:
         RegisterDevice,
         GetKnownDevices,
         ExportOnRing,
-        UpdateAccount
+        UpdateAccount,
+        DeleteAccount
     };
 
 
diff --git a/SmartPanel.xaml b/SmartPanel.xaml
index bb82ec5..abab1f7 100644
--- a/SmartPanel.xaml
+++ b/SmartPanel.xaml
@@ -541,14 +541,14 @@
                     <ToggleSwitch x:Name="_upnpState_"
                                   Margin="20,10"/>
                     <!-- delete account sub menu. -->
-                    <!--<StackPanel Orientation="Horizontal" Margin="10">
+                    <StackPanel Orientation="Horizontal" Margin="10">
                         <TextBlock Style="{StaticResource TextSegoeStyle-20pt-black}"
                                    Text="&#xE74D;"/>
                         <TextBlock Text="Delete account" Margin="10"/>
                     </StackPanel>
                     <ToggleButton x:Name="_deleteAccountBtnEditionMenu_"
                             Margin="20,10"
-                            Content="Delete"/>-->
+                            Content="Delete"/>
                 </StackPanel>
                 <!-- buttons yes/no to accept the modification. -->
                 <Grid Grid.Row="1">
diff --git a/SmartPanel.xaml.cpp b/SmartPanel.xaml.cpp
index 0153000..7a9029b 100644
--- a/SmartPanel.xaml.cpp
+++ b/SmartPanel.xaml.cpp
@@ -63,7 +63,13 @@ SmartPanel::SmartPanel()
 
     /* connect delegates */
     Configuration::UserPreferences::instance->selectIndex += ref new SelectIndex([this](int index) {
-        _accountsList_->SelectedIndex = index;
+        if (_accountsList_) {
+            auto accountsListSize = dynamic_cast<Vector<AccountListItem^>^>(_accountsList_->ItemsSource)->Size;
+            if (accountsListSize > index)
+                _accountsList_->SelectedIndex = index;
+            else
+                _accountsList_->SelectedIndex = 0;
+        }
     });
     Configuration::UserPreferences::instance->loadProfileImage += ref new LoadProfileImage([this]() {
         StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
@@ -728,11 +734,30 @@ void RingClientUWP::Views::SmartPanel::_acceptAccountModification__Click(Platfor
 {
     auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
     auto accountId = account->accountID_;
-    account->name_ = _aliasTextBoxEditionMenu_->Text;
-    account->_upnpState = _upnpState_->IsOn;
 
 
-    RingD::instance->updateAccount(accountId);
+    // mettre ca en visibility du bouton delete
+    auto accountsListSize = dynamic_cast<Vector<AccountListItem^>^>(_accountsList_->ItemsSource)->Size;
+
+    if (_deleteAccountBtnEditionMenu_->IsChecked && accountsListSize > 1) {
+        RingD::instance->deleteAccount(accountId);
+
+        /* rebuild a new list of accounts without the one to delete */
+        auto newAccountList = ref new Vector<AccountListItem^>();
+        for each (AccountListItem^ item in AccountListItemsViewModel::instance->itemsList) {
+            if (item->_account->accountID_ != accountId)
+                newAccountList->Append(item);
+        }
+
+        _accountsList_->ItemsSource = newAccountList;
+
+    } else {
+
+        account->name_ = _aliasTextBoxEditionMenu_->Text;
+        account->_upnpState = _upnpState_->IsOn;
+
+        RingD::instance->updateAccount(accountId);
+    }
 
     _accountEditionMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
     _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
-- 
GitLab