Skip to content

Commit

Permalink
Kotlin Flow API implemented in GalleryViewModel.kt in app-flow module.
Browse files Browse the repository at this point in the history
Removed rx java from app-flow module.
  • Loading branch information
mmobin789 committed Nov 11, 2020
1 parent eec920f commit 62509ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app-flow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {
implementation "com.github.bumptech.glide:glide:$glideVersion"

// reactive programming for Android
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
// implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

// ViewModel - ktx
implementation "androidx.fragment:fragment-ktx:1.2.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@ import android.database.Cursor
import android.provider.MediaStore
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import flow.kt.gallery.model.GalleryPicture
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import java.util.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

class GalleryViewModel : ViewModel() {
private val compositeDisposable = CompositeDisposable()
private var startingRow = 0
private var rowsToLoad = 0
private var allLoaded = false


@ExperimentalCoroutinesApi
fun getImagesFromGallery(
context: Context,
pageSize: Int,
list: (List<GalleryPicture>) -> Unit
) {
compositeDisposable.add(
Single.fromCallable {
fetchGalleryImages(context, pageSize)
}.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe({
list(it)
}, {
it.printStackTrace()
})
)
viewModelScope.launch {
flow {
emit(fetchGalleryImages(context, pageSize))
}.catch {
it.printStackTrace()
}.collect {
list(it)
}
}

}

fun getGallerySize(context: Context): Int {
Expand All @@ -44,12 +46,11 @@ class GalleryViewModel : ViewModel() {
}

private fun fetchGalleryImages(context: Context, rowsPerLoad: Int): List<GalleryPicture> {
val galleryImageUrls = LinkedList<GalleryPicture>()
val cursor = getGalleryCursor(context)

if (cursor != null && !allLoaded) {
val totalRows = cursor.count

val galleryImageUrls = ArrayList<GalleryPicture>(totalRows)
allLoaded = rowsToLoad == totalRows
if (rowsToLoad < rowsPerLoad) {
rowsToLoad = rowsPerLoad
Expand Down Expand Up @@ -79,8 +80,11 @@ class GalleryViewModel : ViewModel() {

cursor.close()
Log.i("PartialGallerySize", " ${galleryImageUrls.size}")

return galleryImageUrls
}
return galleryImageUrls

return emptyList()
}

private fun getGalleryCursor(context: Context): Cursor? {
Expand All @@ -101,8 +105,4 @@ class GalleryViewModel : ViewModel() {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
path.toLong()
)

override fun onCleared() {
compositeDisposable.clear()
}
}

0 comments on commit 62509ce

Please sign in to comment.