OSDN Git Service

TabInformations.TabsにKeyedCollectionを使用する
authorKimura Youichi <kim.upsilon@bucyou.net>
Mon, 17 Jun 2019 23:43:02 +0000 (08:43 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 22 Jun 2019 14:10:48 +0000 (23:10 +0900)
OpenTween.Tests/Models/TabInformationTest.cs
OpenTween/FilterDialog.cs
OpenTween/Models/TabCollection.cs [new file with mode: 0644]
OpenTween/Models/TabInformations.cs
OpenTween/OpenTween.csproj
OpenTween/TabsDialog.cs
OpenTween/Tween.cs

index cfcc02a..cd64b22 100644 (file)
@@ -56,7 +56,7 @@ namespace OpenTween.Models
             var ret = this.tabinfo.AddTab(tab);
 
             Assert.True(ret);
-            Assert.Same(tab, this.tabinfo.Tabs.Values.Last());
+            Assert.Same(tab, this.tabinfo.Tabs.Last());
         }
 
         [Fact]
index 36eeb86..d2c6fc1 100644 (file)
@@ -907,7 +907,7 @@ namespace OpenTween
         {
             _sts = TabInformations.GetInstance();
             ListTabs.Items.Clear();
-            foreach (var tab in this._sts.Tabs.Values)
+            foreach (var tab in this._sts.Tabs)
             {
                 if (tab.TabType == MyCommon.TabUsageType.Mute)
                     continue;
diff --git a/OpenTween/Models/TabCollection.cs b/OpenTween/Models/TabCollection.cs
new file mode 100644 (file)
index 0000000..4093469
--- /dev/null
@@ -0,0 +1,34 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2019 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System.Collections.ObjectModel;
+
+namespace OpenTween.Models
+{
+    public class TabCollection : KeyedCollection<string, TabModel>
+    {
+        public bool TryGetValue(string tabName, out TabModel tab)
+            => this.Dictionary.TryGetValue(tabName, out tab);
+
+        protected override string GetKeyForItem(TabModel tab)
+            => tab.TabName;
+    }
+}
index fd4e1c4..d3447c9 100644 (file)
@@ -40,7 +40,7 @@ namespace OpenTween.Models
     public sealed class TabInformations
     {
         //個別タブの情報をDictionaryで保持
-        public Dictionary<string, TabModel> Tabs { get; } = new Dictionary<string, TabModel>();
+        public TabCollection Tabs { get; } = new TabCollection();
         public ConcurrentDictionary<long, PostClass> Posts { get; } = new ConcurrentDictionary<long, PostClass>();
 
         private Dictionary<long, PostClass> _quotes = new Dictionary<long, PostClass>();
@@ -113,10 +113,10 @@ namespace OpenTween.Models
         {
             lock (this.LockObj)
             {
-                if (this.Tabs.ContainsKey(tab.TabName))
+                if (this.Tabs.Contains(tab.TabName))
                     return false;
 
-                this.Tabs.Add(tab.TabName, tab);
+                this.Tabs.Add(tab);
                 tab.SetSortMode(this.SortMode, this.SortOrder);
 
                 return true;
@@ -145,7 +145,7 @@ namespace OpenTween.Models
                         var exist = false;
                         var Id = tb.GetStatusIdAt(idx);
                         if (Id < 0) continue;
-                        foreach (var tab in this.Tabs.Values)
+                        foreach (var tab in this.Tabs)
                         {
                             if (tab != tb && tab != dmTab)
                             {
@@ -165,14 +165,14 @@ namespace OpenTween.Models
         }
 
         public bool ContainsTab(string TabText)
-            => this.Tabs.ContainsKey(TabText);
+            => this.Tabs.Contains(TabText);
 
         public bool ContainsTab(TabModel ts)
-            => this.Tabs.ContainsValue(ts);
+            => this.Tabs.Contains(ts);
 
         public void SelectTab(string tabName)
         {
-            if (!this.Tabs.ContainsKey(tabName))
+            if (!this.Tabs.Contains(tabName))
                 throw new ArgumentException($"{tabName} does not exist.", nameof(tabName));
 
             this.SelectedTabName = tabName;
@@ -212,9 +212,6 @@ namespace OpenTween.Models
             throw new TabException(message);
         }
 
-        public Dictionary<string, TabModel>.KeyCollection KeysTab
-            => this.Tabs.Keys;
-
         public SortOrder SortOrder { get; private set; }
 
         public ComparerMode SortMode { get; private set; }
@@ -224,7 +221,7 @@ namespace OpenTween.Models
             this.SortMode = mode;
             this.SortOrder = sortOrder;
 
-            foreach (var tab in this.Tabs.Values)
+            foreach (var tab in this.Tabs)
                 tab.SetSortMode(mode, sortOrder);
         }
 
@@ -302,7 +299,7 @@ namespace OpenTween.Models
 
         public void RemovePostFromAllTabs(long statusId, bool setIsDeleted)
         {
-            foreach (var tab in this.Tabs.Values)
+            foreach (var tab in this.Tabs)
             {
                 tab.EnqueueRemovePost(statusId, setIsDeleted);
             }
@@ -334,7 +331,7 @@ namespace OpenTween.Models
 
                 var currentNotifyPriority = -1;
 
-                foreach (var tab in this.Tabs.Values)
+                foreach (var tab in this.Tabs)
                 {
                     // 振分確定 (各タブに反映)
                     var addedIds = tab.AddSubmit();
@@ -388,7 +385,7 @@ namespace OpenTween.Models
                 foreach (var removedId in removedIdsAll.Distinct())
                 {
                     var orphaned = true;
-                    foreach (var tab in this.Tabs.Values)
+                    foreach (var tab in this.Tabs)
                     {
                         if (tab.Contains(removedId))
                         {
@@ -585,7 +582,7 @@ namespace OpenTween.Models
         {
             lock (LockObj)
             {
-                foreach (var tab in this.Tabs.Values)
+                foreach (var tab in this.Tabs)
                 {
                     if (!tab.Contains(statusId))
                         continue;
@@ -656,7 +653,7 @@ namespace OpenTween.Models
                 var tb = this.Tabs[Original];
                 this.Tabs.Remove(Original);
                 tb.TabName = NewName;
-                this.Tabs.Add(NewName, tb);
+                this.Tabs.Add(tb);
             }
         }
 
@@ -667,7 +664,7 @@ namespace OpenTween.Models
                 var homeTab = GetTabByType(MyCommon.TabUsageType.Home);
                 var detachedIdsAll = Enumerable.Empty<long>();
 
-                foreach (var tab in this.Tabs.Values.OfType<FilterTabModel>().ToArray())
+                foreach (var tab in this.Tabs.OfType<FilterTabModel>().ToArray())
                 {
                     if (tab.TabType == MyCommon.TabUsageType.Mute)
                         continue;
@@ -736,7 +733,7 @@ namespace OpenTween.Models
                 foreach (var id in detachedIdsAll)
                 {
                     var hit = false;
-                    foreach (var tbTemp in this.Tabs.Values.ToArray())
+                    foreach (var tbTemp in this.Tabs.ToArray())
                     {
                         if (!tbTemp.IsDistributableTabType)
                             continue;
@@ -768,7 +765,7 @@ namespace OpenTween.Models
                     foreach (var Id in tb.StatusIds)
                     {
                         var Hit = false;
-                        foreach (var tab in this.Tabs.Values)
+                        foreach (var tab in this.Tabs)
                         {
                             if (tab.Contains(Id))
                             {
@@ -822,22 +819,21 @@ namespace OpenTween.Models
             //合致しなければnullを返す
             lock (LockObj)
             {
-                return this.Tabs.Values
-                    .FirstOrDefault(x => x.TabType.HasFlag(tabType));
+                return this.Tabs.FirstOrDefault(x => x.TabType.HasFlag(tabType));
             }
         }
 
         public T GetTabByType<T>() where T : TabModel
         {
             lock (this.LockObj)
-                return this.Tabs.Values.OfType<T>().FirstOrDefault();
+                return this.Tabs.OfType<T>().FirstOrDefault();
         }
 
         public TabModel[] GetTabsByType(MyCommon.TabUsageType tabType)
         {
             lock (LockObj)
             {
-                return this.Tabs.Values
+                return this.Tabs
                     .Where(x => x.TabType.HasFlag(tabType))
                     .ToArray();
             }
@@ -846,14 +842,14 @@ namespace OpenTween.Models
         public T[] GetTabsByType<T>() where T : TabModel
         {
             lock (this.LockObj)
-                return this.Tabs.Values.OfType<T>().ToArray();
+                return this.Tabs.OfType<T>().ToArray();
         }
 
         public TabModel[] GetTabsInnerStorageType()
         {
             lock (LockObj)
             {
-                return this.Tabs.Values
+                return this.Tabs
                     .Where(x => x.IsInnerStorageTabType)
                     .ToArray();
             }
index 42e64ac..7e7cce3 100644 (file)
     <Compile Include="Models\PostClass.cs" />
     <Compile Include="Models\PublicSearchTabModel.cs" />
     <Compile Include="Models\RelatedPostsTabModel.cs" />
+    <Compile Include="Models\TabCollection.cs" />
     <Compile Include="Models\TabModel.cs" />
     <Compile Include="Models\TabInformations.cs" />
     <Compile Include="Models\TabUsageTypeExt.cs" />
index 742d555..b44a44c 100644 (file)
@@ -78,13 +78,13 @@ namespace OpenTween
                 });
             }
 
-            foreach (var (name, tab) in this.TabInfo.Tabs)
+            foreach (var tab in this.TabInfo.Tabs)
             {
                 if (!tab.IsDistributableTabType) continue;
 
                 this.TabList.Items.Add(new TabListItem
                 {
-                    Label = name,
+                    Label = tab.TabName,
                     Tab = tab,
                 });
             }
index 08b43ec..4f7f11b 100644 (file)
@@ -1060,7 +1060,7 @@ namespace OpenTween
             if (this._statuses.GetTabByType<MuteTabModel>() == null)
                 this._statuses.AddTab(new MuteTabModel());
 
-            foreach (var tab in _statuses.Tabs.Values)
+            foreach (var tab in _statuses.Tabs)
             {
                 // ミュートタブは表示しない
                 if (tab.TabType == MyCommon.TabUsageType.Mute)
@@ -6730,7 +6730,7 @@ namespace OpenTween
             var inReplyToId = currentPost.InReplyToStatusId.Value;
             var inReplyToUser = currentPost.InReplyToUser;
 
-            var inReplyToPosts = from tab in _statuses.Tabs.Values
+            var inReplyToPosts = from tab in _statuses.Tabs
                                  orderby tab != curTabClass
                                  from post in tab.Posts.Values
                                  where post.StatusId == inReplyToId
@@ -6797,13 +6797,13 @@ namespace OpenTween
                 if (currentPost.InReplyToStatusId != null)
                 {
                     var posts = from t in _statuses.Tabs
-                                from p in t.Value.Posts
+                                from p in t.Posts
                                 where p.Value.StatusId != currentPost.StatusId && p.Value.InReplyToStatusId == currentPost.InReplyToStatusId
-                                let indexOf = t.Value.IndexOf(p.Value.StatusId)
+                                let indexOf = t.IndexOf(p.Value.StatusId)
                                 where indexOf > -1
                                 orderby isForward ? indexOf : indexOf * -1
-                                orderby t.Value != curTabClass
-                                select new {Tab = t.Value, Post = p.Value, Index = indexOf};
+                                orderby t != curTabClass
+                                select new {Tab = t, Post = p.Value, Index = indexOf};
                     try
                     {
                         var postList = posts.ToList();
@@ -6835,13 +6835,13 @@ namespace OpenTween
                 if (replyChains == null || replyChains.Count < 1)
                 {
                     var posts = from t in _statuses.Tabs
-                                from p in t.Value.Posts
+                                from p in t.Posts
                                 where p.Value.InReplyToStatusId == currentPost.StatusId
-                                let indexOf = t.Value.IndexOf(p.Value.StatusId)
+                                let indexOf = t.IndexOf(p.Value.StatusId)
                                 where indexOf > -1
                                 orderby indexOf
-                                orderby t.Value != curTabClass
-                                select new {Tab = t.Value, Index = indexOf};
+                                orderby t != curTabClass
+                                select new {Tab = t, Index = indexOf};
                     try
                     {
                         var post = posts.First();
@@ -6861,7 +6861,7 @@ namespace OpenTween
                     if (chainHead.InReplyToId == currentPost.StatusId)
                     {
                         var tab = chainHead.OriginalTab;
-                        if (!this._statuses.Tabs.ContainsValue(tab))
+                        if (!this._statuses.Tabs.Contains(tab))
                         {
                             replyChains = null;
                         }
@@ -6912,7 +6912,7 @@ namespace OpenTween
                         this.selectPostChains.Pop();
                         var (tab, post) = this.selectPostChains.Peek();
 
-                        if (!this._statuses.Tabs.ContainsValue(tab))
+                        if (!this._statuses.Tabs.Contains(tab))
                             continue; // 該当タブが存在しないので無視
 
                         if (post != null)
@@ -6993,7 +6993,7 @@ namespace OpenTween
         {
             if (statusId == 0) return false;
 
-            var tab = this._statuses.Tabs.Values
+            var tab = this._statuses.Tabs
                 .Where(x => x.TabType != MyCommon.TabUsageType.DirectMessage)
                 .Where(x => x.Contains(statusId))
                 .FirstOrDefault();
@@ -8598,7 +8598,7 @@ namespace OpenTween
                 SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.Ver &&
                 SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.OwnStatus)
             {
-                foreach (var tab in _statuses.Tabs.Values)
+                foreach (var tab in _statuses.Tabs)
                 {
                     ur += tab.UnreadCount;
                     al += tab.AllCount;
@@ -8661,7 +8661,7 @@ namespace OpenTween
             StringBuilder slbl = new StringBuilder(256);
             try
             {
-                foreach (var tab in _statuses.Tabs.Values)
+                foreach (var tab in _statuses.Tabs)
                 {
                     ur += tab.UnreadCount;
                     al += tab.AllCount;
@@ -10572,7 +10572,8 @@ namespace OpenTween
                         // 関連発言なら既存のタブを置き換える
                         tb.TabName = relatedTab.TabName;
                         this.ClearTab(tb.TabName, false);
-                        _statuses.Tabs[tb.TabName] = tb;
+                        _statuses.Tabs.Remove(tb.TabName);
+                        _statuses.Tabs.Add(tb);
 
                         for (int i = 0; i < ListTab.TabPages.Count; i++)
                         {