@@ -47,119 +47,132 @@ internal class StatusDetailRemoteMediator(
4747 pageSize : Int ,
4848 request : Request ,
4949 ): Result {
50- if (request != Request .Refresh ) {
51- return Result (
52- endOfPaginationReached = true ,
53- )
54- }
55- if (! database.pagingTimelineDao().existsPaging(accountKey, pagingKey)) {
56- database.statusDao().get(statusKey, AccountType .Specific (accountKey)).firstOrNull()?.let {
57- database
58- .pagingTimelineDao()
59- .insertAll(
60- listOf (
61- DbPagingTimeline (
62- accountType = AccountType .Specific (accountKey),
63- statusKey = statusKey,
64- pagingKey = pagingKey,
65- sortId = 0 ,
66- ),
67- ),
68- )
69- }
70- }
7150 val result =
72- if (statusOnly) {
73- val current =
74- service
75- .getPosts(
76- GetPostsQueryParams (
77- persistentListOf(AtUri (statusKey.id)),
78- ),
79- ).requireResponse()
80- .posts
81- .firstOrNull()
82- listOfNotNull(current).map(::FeedViewPost )
83- } else {
84- val context =
85- service
86- .getPostThread(
87- GetPostThreadQueryParams (
88- AtUri (statusKey.id),
89- ),
90- ).requireResponse()
91- when (val thread = context.thread) {
92- is GetPostThreadResponseThreadUnion .ThreadViewPost -> {
93- val parents = mutableListOf<ThreadViewPost >()
94- var current: ThreadViewPost ? = thread.value
95- while (current != null ) {
96- parents.add(current)
97- current =
98- when (val parent = current.parent) {
99- is ThreadViewPostParentUnion .ThreadViewPost -> parent.value
100- else -> null
51+ when (request) {
52+ is Request .Append -> {
53+ if (statusOnly) {
54+ return Result (
55+ endOfPaginationReached = true ,
56+ )
57+ } else {
58+ val context =
59+ service
60+ .getPostThread(
61+ GetPostThreadQueryParams (
62+ AtUri (statusKey.id),
63+ ),
64+ ).requireResponse()
65+ when (val thread = context.thread) {
66+ is GetPostThreadResponseThreadUnion .ThreadViewPost -> {
67+ val parents = mutableListOf<ThreadViewPost >()
68+ var current: ThreadViewPost ? = thread.value
69+ while (current != null ) {
70+ parents.add(current)
71+ current =
72+ when (val parent = current.parent) {
73+ is ThreadViewPostParentUnion .ThreadViewPost -> parent.value
74+ else -> null
75+ }
10176 }
102- }
103- val replies =
104- thread.value.replies.mapNotNull {
105- when (it) {
106- is ThreadViewPostReplieUnion .ThreadViewPost -> {
107- if (it.value.replies.any()) {
108- val last =
109- it.value.replies.last().let {
110- when (it) {
111- is ThreadViewPostReplieUnion .ThreadViewPost -> it.value.post
112- else -> null
113- }
114- }
115- if (last != null ) {
116- val parents =
117- listOfNotNull(it.value.post) +
118- it.value.replies.toList().dropLast(1 ).mapNotNull {
77+ val replies =
78+ thread.value.replies.mapNotNull {
79+ when (it) {
80+ is ThreadViewPostReplieUnion .ThreadViewPost -> {
81+ if (it.value.replies.any()) {
82+ val last =
83+ it.value.replies.last().let {
11984 when (it) {
12085 is ThreadViewPostReplieUnion .ThreadViewPost -> it.value.post
12186 else -> null
12287 }
12388 }
124- val currentRef =
125- ReplyRef (
126- root = ReplyRefRootUnion .PostView (parents.last()),
127- parent = ReplyRefParentUnion .PostView (parents.last()),
128- )
89+ if (last != null ) {
90+ val parents =
91+ listOfNotNull(it.value.post) +
92+ it.value.replies.toList().dropLast(1 ).mapNotNull {
93+ when (it) {
94+ is ThreadViewPostReplieUnion .ThreadViewPost -> it.value.post
95+ else -> null
96+ }
97+ }
98+ val currentRef =
99+ ReplyRef (
100+ root = ReplyRefRootUnion .PostView (parents.last()),
101+ parent = ReplyRefParentUnion .PostView (parents.last()),
102+ )
129103
130- FeedViewPost (
131- post = last,
132- reply = currentRef,
133- )
134- } else {
135- FeedViewPost (
136- it.value.post,
137- )
104+ FeedViewPost (
105+ post = last,
106+ reply = currentRef,
107+ )
108+ } else {
109+ FeedViewPost (
110+ it.value.post,
111+ )
112+ }
113+ } else {
114+ FeedViewPost (
115+ it.value.post,
116+ )
117+ }
138118 }
139- } else {
140- FeedViewPost (
141- it.value.post,
142- )
119+ else -> null
143120 }
144121 }
145- else -> null
146- }
122+ parents.map { FeedViewPost (it.post) }.reversed() + FeedViewPost (thread.value.post) + replies
147123 }
148- parents.map { FeedViewPost (it.post) }.reversed() + FeedViewPost (thread.value.post) + replies
124+
125+ else -> emptyList()
126+ }
127+ }
128+ }
129+ is Request .Prepend -> {
130+ return Result (
131+ endOfPaginationReached = true ,
132+ )
133+ }
134+ Request .Refresh -> {
135+ if (! database.pagingTimelineDao().existsPaging(accountKey, pagingKey)) {
136+ database.statusDao().get(statusKey, AccountType .Specific (accountKey)).firstOrNull()?.let {
137+ database
138+ .pagingTimelineDao()
139+ .insertAll(
140+ listOf (
141+ DbPagingTimeline (
142+ accountType = AccountType .Specific (accountKey),
143+ statusKey = statusKey,
144+ pagingKey = pagingKey,
145+ sortId = 0 ,
146+ ),
147+ ),
148+ )
149+ }
149150 }
150151
151- else -> emptyList()
152+ val current =
153+ service
154+ .getPosts(
155+ GetPostsQueryParams (
156+ persistentListOf(AtUri (statusKey.id)),
157+ ),
158+ ).requireResponse()
159+ .posts
160+ .firstOrNull()
161+ listOfNotNull(current).map(::FeedViewPost )
152162 }
153163 }
164+
165+ val shouldLoadMore = ! (request is Request .Append || statusOnly)
154166 return Result (
155- endOfPaginationReached = true ,
167+ endOfPaginationReached = ! shouldLoadMore ,
156168 data =
157169 result.toDbPagingTimeline(
158170 accountKey,
159171 pagingKey,
160172 ) {
161173 - result.indexOf(it).toLong()
162174 },
175+ nextKey = if (shouldLoadMore) pagingKey else null ,
163176 )
164177 }
165178}
0 commit comments