Skip to content

Commit

Permalink
Support to Automatic load posts on connectivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
PatilShreyas committed Mar 21, 2020
1 parent 540fd67 commit 4d4cf88
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
Binary file modified apk/Foodium.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ class PostsRepository @Inject constructor(

// Retrieve posts from persistence storage and emit
emitAll(fetchFromDatabase().map {
if (!it.isNullOrEmpty()) {
// Posts retrieved. Emit Success state
State.success<List<Post>>(it)
State.success<List<Post>>(it)
} else {
// No posts found in persistence storage.
State.error<List<Post>>("Can't get latest posts!")
}
})
}.flowOn(Dispatchers.IO)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
showLoading(false)
}
}
println("STATE CHANGED = $state")
})

mViewBinding.swipeRefreshLayout.setOnRefreshListener {
Expand Down Expand Up @@ -93,8 +94,10 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
.setListener(null)
}
} else {
if (mViewModel.postsLiveData.value is Error) {
println("CURRENT STATE = ${mViewModel.postsLiveData.value}")
if (mViewModel.postsLiveData.value is State.Error) {
getPosts()
println("CURRENT STATE: GETTING POSTS CALLED")
}
mViewBinding.textViewNetworkStatus.text = getString(R.string.text_connectivity)
mViewBinding.networkStatusLayout.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MainViewModel @Inject constructor(private val postsRepository: PostsReposi
get() = _postsLiveData

fun getPosts() {
println("CURRENT STATE: IN VM")
viewModelScope.launch {
postsRepository.getAllPosts().collect {
_postsLiveData.value = it
Expand Down

0 comments on commit 4d4cf88

Please sign in to comment.