[polari] app: Factor out _savedChannelIndex() help method



commit dbe2395feff345b008d824d945bed0304aaf6ecb
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Oct 1 22:36:21 2015 +0200

    app: Factor out _savedChannelIndex() help method
    
    We search the list of saved channels for a particular account/channel
    combination both when adding a saved channel (to avoid duplicate entries)
    and when removing one (to locate the entry to remove).
    Consolidate that code in a single place to make future modifications
    easier.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=755722

 src/application.js |   34 ++++++++++++++++------------------
 1 files changed, 16 insertions(+), 18 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index a41fcc2..3362e5d 100644
--- a/src/application.js
+++ b/src/application.js
@@ -199,35 +199,33 @@ const Application = new Lang.Class({
         this._window.showMessageUserDialog();
     },
 
+    _savedChannelIndex: function(savedChannels, account, channel) {
+        let accountPath = account.get_object_path();
+        for (let i = 0; i < savedChannels.length; i++)
+            if (savedChannels[i].account.deep_unpack() == accountPath &&
+                savedChannels[i].channel.deep_unpack() == channel)
+                return i;
+        return -1;
+    },
+
     _addSavedChannel: function(account, channel) {
         let savedChannels = this._settings.get_value('saved-channel-list').deep_unpack();
-        let savedChannel = {
+        if (this._savedChannelIndex(savedChannels, account, channel) != -1)
+            return;
+        savedChannels.push({
             account: GLib.Variant.new('s', account.get_object_path()),
             channel: GLib.Variant.new('s', channel)
-        };
-        for (let i = 0; i < savedChannels.length; i++)
-            if (savedChannels[i].account.equal(savedChannel.account) &&
-                savedChannels[i].channel.equal(savedChannel.channel))
-                return;
-        savedChannels.push(savedChannel);
+        });
         this._settings.set_value('saved-channel-list',
                                  GLib.Variant.new('aa{sv}', savedChannels));
     },
 
     _removeSavedChannel: function(account, channel) {
         let savedChannels = this._settings.get_value('saved-channel-list').deep_unpack();
-        let savedChannel = {
-            account: GLib.Variant.new('s', account.get_object_path()),
-            channel: GLib.Variant.new('s', channel)
-        };
-        let i;
-        for (i = 0; i < savedChannels.length; i++)
-            if (savedChannels[i].account.equal(savedChannel.account) &&
-                savedChannels[i].channel.equal(savedChannel.channel))
-                break;
-        if (!savedChannels[i])
+        let pos = this._savedChannelIndex(savedChannels, account, channel);
+        if (pos < 0)
             return;
-        savedChannels.splice(i, 1);
+        savedChannels.splice(pos, 1);
         this._settings.set_value('saved-channel-list',
                                  GLib.Variant.new('aa{sv}', savedChannels));
     },


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]