Created
November 13, 2020 08:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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