Skip to content

Update libs and use of onStart operator. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ apply plugin: 'kotlin-kapt'


android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.mindorks.kotlinFlow"
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -32,28 +32,28 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

// Added Dependencies
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
implementation "androidx.room:room-ktx:2.2.5"
implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
implementation "androidx.room:room-ktx:2.2.6"

// Testing
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13.1'
testImplementation "org.mockito:mockito-core:3.3.3"
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.flow
class DatabaseHelperImpl(private val appDatabase: AppDatabase) : DatabaseHelper {

override fun getUsers(): Flow<List<User>> =
flow { emit(appDatabase.userDao().getAll()) }
appDatabase.userDao().getAll()

override fun insertAll(users: List<User>): Flow<Unit> = flow {
appDatabase.userDao().insertAll(users)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.mindorks.kotlinFlow.data.local.entity.User
import kotlinx.coroutines.flow.Flow

@Dao
interface UserDao {

@Query("SELECT * FROM user")
fun getAll(): List<User>
fun getAll(): Flow<List<User>>

@Insert
fun insertAll(users: List<User>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch

class CompletionViewModel(
Expand All @@ -24,8 +25,8 @@ class CompletionViewModel(
}
private fun fetchUsers() {
viewModelScope.launch {
status.postValue(Resource.loading(null))
apiHelper.getUsers()
.onStart { status.postValue(Resource.loading(null)) }
.catch { e ->
status.postValue(Resource.error(e.toString(), null))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.mindorks.kotlinFlow.data.model.ApiUser
import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch

class CatchViewModel(
Expand All @@ -25,8 +26,9 @@ class CatchViewModel(

private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))

apiHelper.getUsersWithError()
.onStart { users.postValue(Resource.loading(null)) }
.catch { e ->
users.postValue(Resource.error(e.toString(), null))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class EmitAllViewModel(

private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))

apiHelper.getUsers()
.onStart { users.postValue(Resource.loading(null)) }
.zip(
apiHelper.getUsersWithError()
.catch { emitAll(flowOf(emptyList())) }) { usersFromApi, moreUsersFromApi ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.mindorks.kotlinFlow.data.local.DatabaseHelper
import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch

Expand All @@ -20,9 +21,10 @@ class FilterViewModel(

fun startFilterTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))

val result = mutableListOf<Int>()
(1..5).asFlow()
.onStart { status.postValue(Resource.loading(null)) }
.filter {
it % 2 == 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch


Expand All @@ -27,8 +28,9 @@ class MapViewModel(

private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))

apiHelper.getUsers()
.onStart { users.postValue(Resource.loading(null)) }
.map { apiUserList ->
val userList = mutableListOf<User>()
for (apiUser in apiUserList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.mindorks.kotlinFlow.data.api.ApiHelper
import com.mindorks.kotlinFlow.data.local.DatabaseHelper
import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.reduce
import kotlinx.coroutines.launch

Expand All @@ -20,8 +21,8 @@ class ReduceViewModel(

fun startReduceTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))
val result = (1..5).asFlow()
.onStart { status.postValue(Resource.loading(null)) }
.reduce { a, b -> a + b }

status.postValue(Resource.success(result.toString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import com.mindorks.kotlinFlow.data.local.DatabaseHelper
import com.mindorks.kotlinFlow.data.model.ApiUser
import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.zip
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

class ParallelNetworkCallsViewModel(
private val apiHelper: ApiHelper,
private val dbHelper: DatabaseHelper
private val apiHelper: ApiHelper,
private val dbHelper: DatabaseHelper
) : ViewModel() {

private val users = MutableLiveData<Resource<List<ApiUser>>>()
Expand All @@ -28,21 +25,21 @@ class ParallelNetworkCallsViewModel(

private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))
apiHelper.getUsers()
.zip(apiHelper.getMoreUsers()) { usersFromApi, moreUsersFromApi ->
val allUsersFromApi = mutableListOf<ApiUser>()
allUsersFromApi.addAll(usersFromApi)
allUsersFromApi.addAll(moreUsersFromApi)
return@zip allUsersFromApi
}
.flowOn(Dispatchers.Default)
.catch { e ->
users.postValue(Resource.error(e.toString(), null))
}
.collect {
users.postValue(Resource.success(it))
}
.onStart { users.postValue(Resource.loading(null)) }
.zip(apiHelper.getMoreUsers()) { usersFromApi, moreUsersFromApi ->
val allUsersFromApi = mutableListOf<ApiUser>()
allUsersFromApi.addAll(usersFromApi)
allUsersFromApi.addAll(moreUsersFromApi)
return@zip allUsersFromApi
}
.flowOn(Dispatchers.Default)
.catch { e ->
users.postValue(Resource.error(e.toString(), null))
}
.collect {
users.postValue(Resource.success(it))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import com.mindorks.kotlinFlow.data.local.DatabaseHelper
import com.mindorks.kotlinFlow.data.model.ApiUser
import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

class SeriesNetworkCallsViewModel(
Expand All @@ -28,9 +25,10 @@ class SeriesNetworkCallsViewModel(

private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))

val allUsersFromApi = mutableListOf<ApiUser>()
apiHelper.getUsers()
.onStart { users.postValue(Resource.loading(null)) }
.flatMapConcat { usersFromApi ->
allUsersFromApi.addAll(usersFromApi)
apiHelper.getMoreUsers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch

class SingleNetworkCallViewModel(
Expand All @@ -26,8 +27,8 @@ class SingleNetworkCallViewModel(

private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))
apiHelper.getUsers()
.onStart { users.postValue(Resource.loading(null)) }
.catch { e ->
users.postValue(Resource.error(e.toString(), null))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class RetryViewModel(

fun startTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))
// do a long running task
doLongRunningTask()
.onStart { status.postValue(Resource.loading(null)) }
.flowOn(Dispatchers.Default)
.retry(retries = 3) { cause ->
if (cause is IOException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class RetryExponentialBackoffModel(

fun startTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))
// do a long running task
var currentDelay = 1000L
val delayFactor = 2
doLongRunningTask()
.onStart { status.postValue(Resource.loading(null)) }
.flowOn(Dispatchers.Default)
.retry(retries = 3) { cause ->
if (cause is IOException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class RetryWhenViewModel(

fun startTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))

// do a long running task
doLongRunningTask()
.onStart { status.postValue(Resource.loading(null)) }
.flowOn(Dispatchers.Default)
.retryWhen { cause, attempt ->
if (cause is IOException && attempt < 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class RoomDBViewModel(private val apiHelper: ApiHelper, private val dbHelper: Da

private fun fetchUsers() {
viewModelScope.launch {
users.postValue(Resource.loading(null))

dbHelper.getUsers()
.onStart { users.postValue(Resource.loading(null)) }
.flatMapConcat { usersFromDb ->
if (usersFromDb.isEmpty()) {
return@flatMapConcat apiHelper.getUsers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class LongRunningTaskViewModel(

fun startLongRunningTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))
// do a long running task
doLongRunningTask()
.onStart { status.postValue(Resource.loading(null)) }
.flowOn(Dispatchers.Default)
.catch {
status.postValue(Resource.error("Something Went Wrong", null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class TwoLongRunningTasksViewModel(

fun startLongRunningTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))
doLongRunningTaskOne()
.onStart { status.postValue(Resource.loading(null)) }
.zip(doLongRunningTaskTwo()) { resultOne, resultTwo ->
return@zip resultOne + resultTwo
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.61'
ext.kotlin_version = '1.4.21'
ext.versions = [
lifecycle: "2.2.0-rc03"
]
Expand All @@ -11,7 +11,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jan 07 18:56:30 IST 2020
#Thu Jan 14 09:33:25 IST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip