From 5ef39db289ba7cc076f4f6692e3bd50eb4a112ac Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Tue, 18 Jun 2019 08:43:02 +0900 Subject: [PATCH] =?utf8?q?TabInformations.Tabs=E3=81=ABKeyedCollection?= =?utf8?q?=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- OpenTween.Tests/Models/TabInformationTest.cs | 2 +- OpenTween/FilterDialog.cs | 2 +- OpenTween/Models/TabCollection.cs | 34 ++++++++++++++++++++ OpenTween/Models/TabInformations.cs | 46 +++++++++++++--------------- OpenTween/OpenTween.csproj | 1 + OpenTween/TabsDialog.cs | 4 +-- OpenTween/Tween.cs | 33 ++++++++++---------- 7 files changed, 77 insertions(+), 45 deletions(-) create mode 100644 OpenTween/Models/TabCollection.cs diff --git a/OpenTween.Tests/Models/TabInformationTest.cs b/OpenTween.Tests/Models/TabInformationTest.cs index cfcc02a6..cd64b22a 100644 --- a/OpenTween.Tests/Models/TabInformationTest.cs +++ b/OpenTween.Tests/Models/TabInformationTest.cs @@ -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] diff --git a/OpenTween/FilterDialog.cs b/OpenTween/FilterDialog.cs index 36eeb861..d2c6fc1d 100644 --- a/OpenTween/FilterDialog.cs +++ b/OpenTween/FilterDialog.cs @@ -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 index 00000000..40934696 --- /dev/null +++ b/OpenTween/Models/TabCollection.cs @@ -0,0 +1,34 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2019 kim_upsilon (@kim_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 , 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 + { + public bool TryGetValue(string tabName, out TabModel tab) + => this.Dictionary.TryGetValue(tabName, out tab); + + protected override string GetKeyForItem(TabModel tab) + => tab.TabName; + } +} diff --git a/OpenTween/Models/TabInformations.cs b/OpenTween/Models/TabInformations.cs index fd4e1c41..d3447c99 100644 --- a/OpenTween/Models/TabInformations.cs +++ b/OpenTween/Models/TabInformations.cs @@ -40,7 +40,7 @@ namespace OpenTween.Models public sealed class TabInformations { //個別タブの情報をDictionaryで保持 - public Dictionary Tabs { get; } = new Dictionary(); + public TabCollection Tabs { get; } = new TabCollection(); public ConcurrentDictionary Posts { get; } = new ConcurrentDictionary(); private Dictionary _quotes = new Dictionary(); @@ -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.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(); - foreach (var tab in this.Tabs.Values.OfType().ToArray()) + foreach (var tab in this.Tabs.OfType().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() where T : TabModel { lock (this.LockObj) - return this.Tabs.Values.OfType().FirstOrDefault(); + return this.Tabs.OfType().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() where T : TabModel { lock (this.LockObj) - return this.Tabs.Values.OfType().ToArray(); + return this.Tabs.OfType().ToArray(); } public TabModel[] GetTabsInnerStorageType() { lock (LockObj) { - return this.Tabs.Values + return this.Tabs .Where(x => x.IsInnerStorageTabType) .ToArray(); } diff --git a/OpenTween/OpenTween.csproj b/OpenTween/OpenTween.csproj index 42e64ac3..7e7cce30 100644 --- a/OpenTween/OpenTween.csproj +++ b/OpenTween/OpenTween.csproj @@ -162,6 +162,7 @@ + diff --git a/OpenTween/TabsDialog.cs b/OpenTween/TabsDialog.cs index 742d5558..b44a44c6 100644 --- a/OpenTween/TabsDialog.cs +++ b/OpenTween/TabsDialog.cs @@ -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, }); } diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index 08b43ecc..4f7f11b4 100644 --- a/OpenTween/Tween.cs +++ b/OpenTween/Tween.cs @@ -1060,7 +1060,7 @@ namespace OpenTween if (this._statuses.GetTabByType() == 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++) { -- 2.11.0