Skip to content

Commit 78f9fba

Browse files
committed
fix app crash on loading comment with no internet
1 parent 9bd9a5c commit 78f9fba

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ class CommentsSource(private val commentInfo: CommentInfo) : PagingSource<Page,
2020
if (params.key == null) {
2121
return LoadResult.Page(commentInfo.comments, null, commentInfo.nextPage)
2222
} else {
23-
val info = withContext(Dispatchers.IO) {
24-
CommentsInfo.getMoreItems(service, commentInfo.url, params.key)
23+
return try {
24+
val info = withContext(Dispatchers.IO) {
25+
CommentsInfo.getMoreItems(service, commentInfo.url, params.key)
26+
}
27+
LoadResult.Page(info.items, null, info.nextPage)
28+
} catch (exception: Exception) {
29+
LoadResult.Error(exception)
2530
}
26-
27-
return LoadResult.Page(info.items, null, info.nextPage)
2831
}
2932
}
3033

app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentSection.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ private fun CommentSection(
139139
items(comments.itemCount) {
140140
Comment(comment = comments[it]!!) {}
141141
}
142+
143+
// handle append (next page) errors
144+
if (comments.loadState.append is LoadState.Error) {
145+
item {
146+
ErrorPanel(
147+
errorInfo = ErrorInfo(
148+
throwable = (comments.loadState.append as LoadState.Error).error,
149+
userAction = UserAction.REQUESTED_COMMENTS,
150+
request = "comments"
151+
),
152+
onRetry = { comments.retry() },
153+
modifier = Modifier.fillMaxWidth()
154+
)
155+
}
156+
}
157+
//
158+
// show loading indicator while appending
159+
if (comments.loadState.append is LoadState.Loading) {
160+
item {
161+
LoadingIndicator(modifier = Modifier.padding(top = 8.dp))
162+
}
163+
}
142164
}
143165
}
144166
}

0 commit comments

Comments
 (0)