|
1 | 1 | package com.jdroid.android.googleplay.publisher |
2 | 2 |
|
3 | 3 | import com.google.api.services.androidpublisher.model.Review |
| 4 | +import com.google.api.services.androidpublisher.model.ReviewsListResponse |
4 | 5 |
|
5 | 6 | class ReviewsService : AbstractGooglePlayPublisher() { |
6 | 7 |
|
7 | | - fun getReviews(app: App, maxResults: Long? = null, paginationToken: String? = null, startIndex: Long? = null, translationLanguage: String? = null): List<Review> { |
| 8 | + fun getReviews(app: App, maxResults: Long? = null, paginationToken: String? = null, startIndex: Long? = null, translationLanguage: String? = null): ReviewsListResponse { |
8 | 9 | val list = init(app.appContext).reviews().list(app.applicationId) |
9 | 10 | list.maxResults = maxResults |
10 | 11 | list.token = paginationToken |
11 | 12 | list.startIndex = startIndex |
12 | 13 | list.translationLanguage = translationLanguage |
13 | | - return list.execute().reviews |
| 14 | + return list.execute() |
| 15 | + } |
| 16 | + |
| 17 | + fun getFilteredReviews(app: App, appVersionCode: Int? = null, maxRating: Int = 5, translationLanguage: String? = null): List<Review> { |
| 18 | + return internalGetFilteredReviews(app, appVersionCode, maxRating, null, null, null, translationLanguage) |
| 19 | + } |
| 20 | + |
| 21 | + private fun internalGetFilteredReviews(app: App, appVersionCode: Int?, maxRating: Int, maxResults: Long?, paginationToken: String?, startIndex: Long?, translationLanguage: String?): List<Review> { |
| 22 | + val reviews = mutableListOf<Review>() |
| 23 | + val reviewsListResponse = getReviews(app, maxResults, paginationToken, startIndex, translationLanguage) |
| 24 | + reviewsListResponse.reviews.forEach { |
| 25 | + it.comments.forEach { comment -> |
| 26 | + if (comment.userComment != null) { |
| 27 | + if (appVersionCode == null || comment.userComment.appVersionCode == appVersionCode) { |
| 28 | + if (comment.userComment.starRating <= maxRating) { |
| 29 | + if (!reviews.contains(it)) { |
| 30 | + reviews.add(it) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + // TODO Pagination is not working as expected |
| 38 | + if (reviewsListResponse.pageInfo != null) { |
| 39 | + if (reviewsListResponse.pageInfo.startIndex + reviewsListResponse.pageInfo.resultPerPage < reviewsListResponse.pageInfo.totalResults) { |
| 40 | + reviews.addAll(internalGetFilteredReviews(app, appVersionCode, maxRating, maxResults, reviewsListResponse.tokenPagination.nextPageToken, |
| 41 | + reviewsListResponse.pageInfo.startIndex.toLong() + reviewsListResponse.pageInfo.resultPerPage, translationLanguage)) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return reviews |
14 | 46 | } |
15 | 47 | } |
0 commit comments