OSDN Git Service

Twitter.GetHomeTimelineApiメソッドからTabModelへの依存を除去
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 7 May 2024 15:12:59 +0000 (00:12 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Tue, 7 May 2024 16:15:50 +0000 (01:15 +0900)
OpenTween/Models/HomeTabModel.cs
OpenTween/Models/TimelineResponse.cs [new file with mode: 0644]
OpenTween/Twitter.cs

index bb9651e..53705ce 100644 (file)
@@ -75,16 +75,27 @@ namespace OpenTween.Models
             progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, backward ? -1 : 1));
 
             var firstLoad = !this.IsFirstLoadCompleted;
+            var count = Twitter.GetApiResultCount(MyCommon.WORKERTYPE.Timeline, backward, firstLoad);
+            var cursor = backward ? this.CursorBottom : this.CursorTop;
 
-            await twAccount.Legacy.GetHomeTimelineApi(this, backward, firstLoad)
+            var response = await twAccount.Legacy.GetHomeTimelineApi(count, cursor, firstLoad)
                 .ConfigureAwait(false);
 
+            foreach (var post in response.Posts)
+                TabInformations.GetInstance().AddPost(post);
+
             // 新着時未読クリア
             if (SettingManager.Instance.Common.ReadOldPosts)
                 TabInformations.GetInstance().SetReadHomeTab();
 
             TabInformations.GetInstance().DistributePosts();
 
+            if (response.CursorTop != null && !backward)
+                this.CursorTop = response.CursorTop;
+
+            if (response.CursorBottom != null)
+                this.CursorBottom = response.CursorBottom;
+
             if (firstLoad)
                 this.IsFirstLoadCompleted = true;
 
diff --git a/OpenTween/Models/TimelineResponse.cs b/OpenTween/Models/TimelineResponse.cs
new file mode 100644 (file)
index 0000000..5747587
--- /dev/null
@@ -0,0 +1,31 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2024 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.
+
+#nullable enable
+
+namespace OpenTween.Models
+{
+    public record TimelineResponse(
+        PostClass[] Posts,
+        IQueryCursor? CursorTop,
+        IQueryCursor? CursorBottom
+    );
+}
index ca7373f..b1e424e 100644 (file)
@@ -623,16 +623,15 @@ namespace OpenTween
             return Math.Min(count, GetMaxApiResultCount(type));
         }
 
-        public async Task GetHomeTimelineApi(HomeTabModel tab, bool more, bool firstLoad)
+        public async Task<TimelineResponse> GetHomeTimelineApi(int count, IQueryCursor? cursor, bool firstLoad)
         {
             this.CheckAccountState();
 
-            var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, firstLoad);
-
             TwitterStatus[] statuses;
+            IQueryCursor? cursorTop = null, cursorBottom = null;
+
             if (this.Api.AuthType == APIAuthType.TwitterComCookie)
             {
-                var cursor = more ? tab.CursorBottom : tab.CursorTop;
                 var request = new HomeLatestTimelineRequest
                 {
                     Count = count,
@@ -643,30 +642,36 @@ namespace OpenTween
 
                 statuses = response.ToTwitterStatuses();
 
-                tab.CursorBottom = response.CursorBottom;
-
-                if (!more)
-                    tab.CursorTop = response.CursorTop;
+                cursorTop = response.CursorTop;
+                cursorBottom = response.CursorBottom;
             }
             else
             {
-                var maxId = more ? tab.CursorBottom?.As<TwitterStatusId>() : null;
+                TwitterStatusId? maxId = null, sinceId = null;
 
-                statuses = await this.Api.StatusesHomeTimeline(count, maxId)
+                if (cursor is QueryCursor<TwitterStatusId> statusIdCursor)
+                {
+                    if (statusIdCursor.Type == CursorType.Top)
+                        sinceId = statusIdCursor.Value;
+                    else if (statusIdCursor.Type == CursorType.Bottom)
+                        maxId = statusIdCursor.Value;
+                }
+
+                statuses = await this.Api.StatusesHomeTimeline(count, maxId, sinceId)
                     .ConfigureAwait(false);
 
                 if (statuses.Length > 0)
                 {
-                    var min = statuses.Select(x => new TwitterStatusId(x.IdStr)).Min();
-                    tab.CursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
+                    var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
+                    cursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
+                    cursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
                 }
             }
 
             var posts = this.CreatePostsFromJson(statuses, firstLoad);
             posts = this.FilterNoRetweetUserPosts(posts);
 
-            foreach (var post in posts)
-                TabInformations.GetInstance().AddPost(post);
+            return new(posts, cursorTop, cursorBottom);
         }
 
         public async Task GetMentionsTimelineApi(MentionsTabModel tab, bool more, bool firstLoad)