From 571ec9a2b4abee1d4abf56cb425d19a83b1086d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 15 Aug 2022 09:29:24 -0400
Subject: [PATCH] switchToggled: use toggle() instead checked=value

This forces the settings to get the correct status.
Also do some cleanup in currentaccount

Change-Id: I0f1bff7e54d902b25afd3c95a804400c4e6b6b52
GitLab: #801
---
 src/app/currentaccount.cpp                     | 18 +++---------------
 src/app/currentaccount.h                       |  9 +++------
 .../components/CurrentAccountSettings.qml      |  2 +-
 .../settingsview/components/ToggleSwitch.qml   |  2 +-
 4 files changed, 8 insertions(+), 23 deletions(-)

diff --git a/src/app/currentaccount.cpp b/src/app/currentaccount.cpp
index a80c8b683..98fba2c13 100644
--- a/src/app/currentaccount.cpp
+++ b/src/app/currentaccount.cpp
@@ -41,21 +41,9 @@ CurrentAccount::CurrentAccount(LRCInstance* lrcInstance,
 }
 
 void
-CurrentAccount::set_enabled(bool enabled, bool initialize)
+CurrentAccount::enableAccount(bool enabled)
 {
-    if (enabled_ != enabled) {
-        enabled_ = enabled;
-        if (!initialize)
-            lrcInstance_->accountModel().setAccountEnabled(lrcInstance_->get_currentAccountId(),
-                                                           enabled);
-        Q_EMIT enabledChanged();
-    }
-}
-
-bool
-CurrentAccount::get_enabled()
-{
-    return enabled_;
+    lrcInstance_->accountModel().setAccountEnabled(lrcInstance_->get_currentAccountId(), enabled);
 }
 
 void
@@ -120,7 +108,7 @@ CurrentAccount::updateData()
         set_status(accInfo.status);
         set_type(accInfo.profileInfo.type);
 
-        set_enabled(accInfo.enabled, true);
+        set_enabled(accInfo.enabled);
         set_managerUri(accConfig.managerUri);
         set_keepAliveEnabled(accConfig.keepAliveEnabled, true);
         set_peerDiscovery(accConfig.peerDiscovery, true);
diff --git a/src/app/currentaccount.h b/src/app/currentaccount.h
index 8b8c0558c..5f4337aa7 100644
--- a/src/app/currentaccount.h
+++ b/src/app/currentaccount.h
@@ -98,7 +98,6 @@ class CurrentAccount final : public QObject
 {
     Q_OBJECT
     // Basic settings
-    Q_PROPERTY(bool enabled READ get_enabled WRITE set_enabled NOTIFY enabledChanged);
 
     QML_RO_PROPERTY(QString, id)
     QML_RO_PROPERTY(QString, uri)
@@ -108,6 +107,7 @@ class CurrentAccount final : public QObject
     QML_RO_PROPERTY(QString, bestName)
     QML_RO_PROPERTY(QString, managerUri)
     QML_RO_PROPERTY(bool, hasAvatarSet)
+    QML_PROPERTY(bool, enabled)
     QML_RO_PROPERTY(lrc::api::account::Status, status)
     QML_RO_PROPERTY(lrc::api::profile::Type, type)
 
@@ -198,17 +198,15 @@ public:
                             QObject* parent = nullptr);
     ~CurrentAccount() = default;
 
-    void set_enabled(bool enabled = false, bool initialize = false);
-    bool get_enabled();
-
     void set_isAllModeratorsEnabled(bool enabled, bool initialize = false);
     bool get_isAllModeratorsEnabled();
 
     void set_isLocalModeratorsEnabled(bool enabled, bool initialize = false);
     bool get_isLocalModeratorsEnabled();
 
+    Q_INVOKABLE void enableAccount(bool enabled);
+
 Q_SIGNALS:
-    void enabledChanged();
     void isAllModeratorsEnabledChanged();
     void isLocalModeratorsEnabledChanged();
 
@@ -217,7 +215,6 @@ private Q_SLOTS:
     void onAccountUpdated(const QString& id);
 
 private:
-    bool enabled_;
     bool isAllModeratorsEnabled_;
     bool isLocalModeratorsEnabled_;
 
diff --git a/src/app/settingsview/components/CurrentAccountSettings.qml b/src/app/settingsview/components/CurrentAccountSettings.qml
index 47623dce3..31aca86a6 100644
--- a/src/app/settingsview/components/CurrentAccountSettings.qml
+++ b/src/app/settingsview/components/CurrentAccountSettings.qml
@@ -173,7 +173,7 @@ Rectangle {
 
             checked: CurrentAccount.enabled
 
-            onSwitchToggled: CurrentAccount.enabled = checked
+            onSwitchToggled: CurrentAccount.enableAccount(checked)
         }
 
         AccountProfile {
diff --git a/src/app/settingsview/components/ToggleSwitch.qml b/src/app/settingsview/components/ToggleSwitch.qml
index 73857190a..418402b7e 100644
--- a/src/app/settingsview/components/ToggleSwitch.qml
+++ b/src/app/settingsview/components/ToggleSwitch.qml
@@ -76,7 +76,7 @@ RowLayout {
         enabled: parent.visible
         onTapped: function onTapped(eventPoint) {
             // switchToggled should be emitted as onToggled is not called (because it's only called if the user click on the switch)
-            switchOfLayout.checked = !switchOfLayout.checked
+            switchOfLayout.toggle()
             switchToggled()
         }
     }
-- 
GitLab