OSDN Git Service

Forward/Backメソッドから現在の履歴を更新する操作を分離
authorKimura Youichi <kim.upsilon@bucyou.net>
Mon, 8 Jan 2024 00:54:28 +0000 (09:54 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Mon, 8 Jan 2024 00:59:35 +0000 (09:59 +0900)
OpenTween.Tests/Models/StatusTextHistoryTest.cs
OpenTween/Models/StatusTextHistory.cs
OpenTween/Tween.cs

index c33fe44..01e2400 100644 (file)
@@ -35,25 +35,35 @@ namespace OpenTween.Models
         }
 
         [Fact]
-        public void Back_NoItemsTest()
+        public void SetCurrentItem_Test()
         {
             var history = new StatusTextHistory();
-            history.Back("@hoge aaa", (new TwitterStatusId("111"), "hoge"));
+            history.SetCurrentItem("@hoge aaa", (new TwitterStatusId("111"), "hoge"));
             Assert.Single(history.Items);
             Assert.Equal(new("@hoge aaa", (new TwitterStatusId("111"), "hoge")), history.Items[0]);
             Assert.Equal(0, history.HistoryIndex);
         }
 
         [Fact]
+        public void Back_NoItemsTest()
+        {
+            var history = new StatusTextHistory();
+            history.Back();
+            Assert.Single(history.Items);
+            Assert.Equal(new("", null), history.Items[0]);
+            Assert.Equal(0, history.HistoryIndex);
+        }
+
+        [Fact]
         public void Back_HasItemsTest()
         {
             var history = new StatusTextHistory();
             history.AddLast("@hoge aaa", (new TwitterStatusId("111"), "hoge"));
-            history.Back("@foo bbb", (new TwitterStatusId("222"), "foo"));
+            history.Back();
 
             Assert.Equal(2, history.Items.Count);
             Assert.Equal(new("@hoge aaa", (new TwitterStatusId("111"), "hoge")), history.Items[0]);
-            Assert.Equal(new("@foo bbb", (new TwitterStatusId("222"), "foo")), history.Items[1]);
+            Assert.Equal(new("", null), history.Items[1]);
             Assert.Equal(0, history.HistoryIndex);
         }
 
@@ -61,9 +71,9 @@ namespace OpenTween.Models
         public void Forward_NoItemsTest()
         {
             var history = new StatusTextHistory();
-            history.Forward("@hoge aaa", (new TwitterStatusId("111"), "hoge"));
+            history.Forward();
             Assert.Single(history.Items);
-            Assert.Equal(new("@hoge aaa", (new TwitterStatusId("111"), "hoge")), history.Items[0]);
+            Assert.Equal(new("", null), history.Items[0]);
             Assert.Equal(0, history.HistoryIndex);
         }
 
@@ -72,12 +82,12 @@ namespace OpenTween.Models
         {
             var history = new StatusTextHistory();
             history.AddLast("@hoge aaa", (new TwitterStatusId("111"), "hoge"));
-            history.Back("@foo bbb", (new TwitterStatusId("222"), "foo"));
-            history.Forward("@hoge aaa 123", (new TwitterStatusId("111"), "hoge"));
+            history.Back();
+            history.Forward();
 
             Assert.Equal(2, history.Items.Count);
-            Assert.Equal(new("@hoge aaa 123", (new TwitterStatusId("111"), "hoge")), history.Items[0]);
-            Assert.Equal(new("@foo bbb", (new TwitterStatusId("222"), "foo")), history.Items[1]);
+            Assert.Equal(new("@hoge aaa", (new TwitterStatusId("111"), "hoge")), history.Items[0]);
+            Assert.Equal(new("", null), history.Items[1]);
             Assert.Equal(1, history.HistoryIndex);
         }
 
index f84c66d..91cca92 100644 (file)
@@ -44,36 +44,35 @@ namespace OpenTween.Models
         public StatusTextHistory()
             => this.items.Add(new(""));
 
-        public HistoryItem Back(string text, (PostId StatusId, string ScreenName)? inReplyTo)
+        public void SetCurrentItem(string text, (PostId StatusId, string ScreenName)? inReplyTo)
         {
             if (!string.IsNullOrWhiteSpace(text))
                 this.items[this.historyIndex] = new(text, inReplyTo);
+        }
 
-            this.historyIndex -= 1;
-            if (this.historyIndex < 0)
-                this.historyIndex = 0;
+        public HistoryItem Back()
+        {
+            if (this.historyIndex > 0)
+                this.historyIndex--;
 
             return this.items[this.historyIndex];
         }
 
-        public HistoryItem Forward(string text, (PostId StatusId, string ScreenName)? inReplyTo)
+        public HistoryItem Forward()
         {
-            if (!string.IsNullOrWhiteSpace(text))
-                this.items[this.historyIndex] = new(text, inReplyTo);
-
-            this.historyIndex += 1;
-            if (this.historyIndex > this.items.Count - 1)
-                this.historyIndex = this.items.Count - 1;
+            if (this.historyIndex < this.items.Count - 1)
+                this.historyIndex++;
 
             return this.items[this.historyIndex];
         }
 
         public void AddLast(string text, (PostId StatusId, string ScreenName)? inReplyTo)
         {
-            this.items[this.items.Count - 1] = new(text, inReplyTo);
+            this.historyIndex = this.items.Count - 1;
+            this.SetCurrentItem(text, inReplyTo);
 
             this.items.Add(new(""));
-            this.historyIndex = this.items.Count - 1;
+            this.historyIndex++;
         }
 
         public HistoryItem? Peek()
index 51974e0..f2d9080 100644 (file)
@@ -1088,7 +1088,8 @@ namespace OpenTween
 
         private void StatusTextHistoryBack()
         {
-            var historyItem = this.history.Back(this.StatusText.Text, this.inReplyTo);
+            this.history.SetCurrentItem(this.StatusText.Text, this.inReplyTo);
+            var historyItem = this.history.Back();
             this.inReplyTo = historyItem.InReplyTo;
             this.StatusText.Text = historyItem.Status;
             this.StatusText.SelectionStart = this.StatusText.Text.Length;
@@ -1096,7 +1097,8 @@ namespace OpenTween
 
         private void StatusTextHistoryForward()
         {
-            var historyItem = this.history.Forward(this.StatusText.Text, this.inReplyTo);
+            this.history.SetCurrentItem(this.StatusText.Text, this.inReplyTo);
+            var historyItem = this.history.Forward();
             this.inReplyTo = historyItem.InReplyTo;
             this.StatusText.Text = historyItem.Status;
             this.StatusText.SelectionStart = this.StatusText.Text.Length;