From e773c1d4183448896f47cadc337a0537ec5cfe2b Mon Sep 17 00:00:00 2001 From: wendyyanto Date: Sun, 31 May 2020 15:42:36 +0700 Subject: [PATCH 1/2] Implements map lambda when transforming ApiUser to User --- .../com/mindorks/kotlinFlow/learn/room/RoomDBViewModel.kt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/mindorks/kotlinFlow/learn/room/RoomDBViewModel.kt b/app/src/main/java/com/mindorks/kotlinFlow/learn/room/RoomDBViewModel.kt index fbdef08..cfee7b3 100644 --- a/app/src/main/java/com/mindorks/kotlinFlow/learn/room/RoomDBViewModel.kt +++ b/app/src/main/java/com/mindorks/kotlinFlow/learn/room/RoomDBViewModel.kt @@ -29,17 +29,14 @@ class RoomDBViewModel(private val apiHelper: ApiHelper, private val dbHelper: Da if (usersFromDb.isEmpty()) { return@flatMapConcat apiHelper.getUsers() .map { apiUserList -> - val userList = mutableListOf() - for (apiUser in apiUserList) { - val user = User( + apiUserList.map { apiUser -> + User( apiUser.id, apiUser.name, apiUser.email, apiUser.avatar ) - userList.add(user) } - userList } .flatMapConcat { usersToInsertInDB -> dbHelper.insertAll(usersToInsertInDB) From 4a46e0e20218a53399104277e203303563ba25a1 Mon Sep 17 00:00:00 2001 From: wendyyanto Date: Sun, 31 May 2020 15:48:32 +0700 Subject: [PATCH 2/2] Implements add operator to merge list --- .../learn/retrofit/parallel/ParallelNetworkCallsViewModel.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/com/mindorks/kotlinFlow/learn/retrofit/parallel/ParallelNetworkCallsViewModel.kt b/app/src/main/java/com/mindorks/kotlinFlow/learn/retrofit/parallel/ParallelNetworkCallsViewModel.kt index 2d92677..08bd082 100644 --- a/app/src/main/java/com/mindorks/kotlinFlow/learn/retrofit/parallel/ParallelNetworkCallsViewModel.kt +++ b/app/src/main/java/com/mindorks/kotlinFlow/learn/retrofit/parallel/ParallelNetworkCallsViewModel.kt @@ -31,10 +31,7 @@ class ParallelNetworkCallsViewModel( users.postValue(Resource.loading(null)) apiHelper.getUsers() .zip(apiHelper.getMoreUsers()) { usersFromApi, moreUsersFromApi -> - val allUsersFromApi = mutableListOf() - allUsersFromApi.addAll(usersFromApi) - allUsersFromApi.addAll(moreUsersFromApi) - return@zip allUsersFromApi + usersFromApi + moreUsersFromApi } .flowOn(Dispatchers.Default) .catch { e ->