Skip to content

Commit 277158a

Browse files
authored
Merge pull request #1282 from DimensionDev/bugfix/status_detail_loading
update the way to load status
2 parents 8dc23bc + 0b1b6fa commit 277158a

File tree

5 files changed

+318
-289
lines changed

5 files changed

+318
-289
lines changed

shared/src/commonMain/kotlin/dev/dimension/flare/data/database/cache/mapper/Misskey.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ private fun Notification.toDbStatus(accountKey: MicroBlogKey): DbStatus {
7979
)
8080
}
8181

82-
internal fun List<Note>.toDbPagingTimeline(
82+
internal suspend fun List<Note>.toDbPagingTimeline(
8383
accountKey: MicroBlogKey,
8484
pagingKey: String,
85-
sortIdProvider: (Note) -> Long = { Instant.parse(it.createdAt).toEpochMilliseconds() },
85+
sortIdProvider: suspend (Note) -> Long = { Instant.parse(it.createdAt).toEpochMilliseconds() },
8686
): List<DbPagingTimelineWithStatus> =
8787
this.map {
8888
createDbPagingTimelineWithStatus(

shared/src/commonMain/kotlin/dev/dimension/flare/data/datasource/bluesky/StatusDetailRemoteMediator.kt

Lines changed: 102 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

shared/src/commonMain/kotlin/dev/dimension/flare/data/datasource/mastodon/StatusDetailRemoteMediator.kt

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,59 +36,69 @@ internal class StatusDetailRemoteMediator(
3636
pageSize: Int,
3737
request: Request,
3838
): Result {
39-
if (request != Request.Refresh) {
40-
return Result(
41-
endOfPaginationReached = true,
42-
)
43-
}
44-
val exists = database.pagingTimelineDao().existsPaging(accountKey, pagingKey)
45-
if (!exists) {
46-
val status = database.statusDao().get(statusKey, AccountType.Specific(accountKey)).firstOrNull()
47-
status?.let {
48-
database.connect {
49-
database
50-
.pagingTimelineDao()
51-
.insertAll(
52-
listOf(
53-
DbPagingTimeline(
54-
accountType = AccountType.Specific(accountKey),
55-
statusKey = statusKey,
56-
pagingKey = pagingKey,
57-
sortId = 0,
58-
),
59-
),
39+
val result =
40+
when (request) {
41+
is Request.Append -> {
42+
if (statusOnly) {
43+
return Result(
44+
endOfPaginationReached = true,
6045
)
46+
} else {
47+
val context =
48+
service.context(
49+
statusKey.id,
50+
)
51+
val current =
52+
service.lookupStatus(
53+
statusKey.id,
54+
)
55+
context.ancestors.orEmpty() + listOf(current) + context.descendants.orEmpty()
56+
}
6157
}
62-
}
63-
}
64-
val result =
65-
if (statusOnly) {
66-
val current =
67-
service.lookupStatus(
68-
statusKey.id,
69-
)
70-
listOf(current)
71-
} else {
72-
val context =
73-
service.context(
74-
statusKey.id,
75-
)
76-
val current =
77-
service.lookupStatus(
78-
statusKey.id,
58+
is Request.Prepend ->
59+
return Result(
60+
endOfPaginationReached = true,
7961
)
80-
context.ancestors.orEmpty() + listOf(current) + context.descendants.orEmpty()
62+
Request.Refresh -> {
63+
val exists = database.pagingTimelineDao().existsPaging(accountKey, pagingKey)
64+
if (!exists) {
65+
val status = database.statusDao().get(statusKey, AccountType.Specific(accountKey)).firstOrNull()
66+
status?.let {
67+
database.connect {
68+
database
69+
.pagingTimelineDao()
70+
.insertAll(
71+
listOf(
72+
DbPagingTimeline(
73+
accountType = AccountType.Specific(accountKey),
74+
statusKey = statusKey,
75+
pagingKey = pagingKey,
76+
sortId = 0,
77+
),
78+
),
79+
)
80+
}
81+
}
82+
}
83+
val current =
84+
service.lookupStatus(
85+
statusKey.id,
86+
)
87+
listOf(current)
88+
}
8189
}
90+
val shouldLoadMore = !(request is Request.Append || statusOnly)
8291

8392
return Result(
84-
endOfPaginationReached = true,
93+
endOfPaginationReached = !shouldLoadMore,
8594
data =
8695
result.toDbPagingTimeline(
8796
accountKey = accountKey,
8897
pagingKey = pagingKey,
8998
) {
9099
-result.indexOf(it).toLong()
91100
},
101+
nextKey = if (shouldLoadMore) pagingKey else null,
92102
)
93103
}
94104
}

0 commit comments

Comments
 (0)