Skip to content

Instantly share code, notes, and snippets.

@wangerekaharun
Created November 13, 2020 08:54
Show Gist options
  • Save wangerekaharun/7331bfe2c42625ffcff7d1fcfbe92398 to your computer and use it in GitHub Desktop.
Save wangerekaharun/7331bfe2c42625ffcff7d1fcfbe92398 to your computer and use it in GitHub Desktop.
Getting a list of posts and for each post looping through it to get a subcirbers count.
suspend fun getPostWithSubscribers(): NetworkResult<List<PostsUIModel>> {
return try {
val response = api.loadPosts().body()!!.posts
val postModel = mutableListOf<PostsUIModel>()
response.onEach { post ->
post.url.toUri().host?.let { siteUrl ->
val site = api.loadSubscribersCount(siteUrl)
postModel.add(post.toPostUIModel(site.body()!!.subscribersCount))
}
}
return NetworkResult.Success(postModel)
} catch (exc: Exception) {
NetworkResult.Error(IOException("Could not get posts"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment