Skip to content

Commit

Permalink
Merge pull request #20 from ch8n/swipe_refresh
Browse files Browse the repository at this point in the history
Add Swipe refresh
  • Loading branch information
Spikeysanju authored Oct 5, 2020
2 parents c4c3377 + 5dc4b6b commit 08fd5c9
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 58 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

package www.thecodemonks.techbytes.repo

import android.util.Log
import org.jsoup.Jsoup
import www.thecodemonks.techbytes.db.ArticleDatabase
import www.thecodemonks.techbytes.model.Article
Expand Down Expand Up @@ -74,7 +75,6 @@ class Repo(private val db: ArticleDatabase) {
articles.add(article)
}
}

return articles
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ class ArticlesFragment : Fragment(R.layout.fragment_articles) {
viewModel.currentTopic.observe(viewLifecycleOwner, Observer {
article_rv.animate().alpha(0f)
.withStartAction {
progress_view.isVisible = true
progress_view.animate().alpha(1f)
if (viewModel.networkObserver.value == true){
refresh_articles.isRefreshing = true
}
}
.withEndAction {
viewModel.crawlFromNYTimes(it.toString())
Expand All @@ -110,12 +111,9 @@ class ArticlesFragment : Fragment(R.layout.fragment_articles) {

// observe the articles
viewModel.articles.observe(viewLifecycleOwner, Observer {
refresh_articles.isRefreshing = false
newsAdapter.differ.submitList(it)
progress_view.animate().alpha(0f)
.withEndAction {
article_rv.animate().alpha(1f)
progress_view.isVisible = false
}
article_rv.animate().alpha(1f)
})

// onclick to select source & post value to liveData
Expand All @@ -135,18 +133,24 @@ class ArticlesFragment : Fragment(R.layout.fragment_articles) {
)
}

var lastOnlineStatus = true // this flag is required to block showing of onlineStatus on startup
var lastOnlineStatus =
true // this flag is required to block showing of onlineStatus on startup
viewModel.networkObserver.observe(viewLifecycleOwner, Observer { isConnected ->
if (lastOnlineStatus != isConnected) {
lastOnlineStatus = isConnected
if (isConnected) {
container_network_status.setOnlineBehaviour()
} else {
container_network_status.setOfflineBehaviour()
}
container_network_status.applyNetworkStatusTheme(isConnected)
container_network_status.applyNetworkStatusAnimations(isConnected)
container_network_status.applyNetworkStatusVisibilityBehaviour(isConnected)
refresh_articles.isEnabled = isConnected
}
})

refresh_articles.setOnRefreshListener {
viewModel.reCrawlFromNYTimes {
refresh_articles.isRefreshing = true
}
}

}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand Down Expand Up @@ -278,7 +282,62 @@ class ArticlesFragment : Fragment(R.layout.fragment_articles) {
}
}

}

fun LinearLayout.applyNetworkStatusTheme(isConnected: Boolean) {

setBackgroundColor(
ContextCompat.getColor(
requireContext(),
if (isConnected) R.color.colorStatusConnected else R.color.colorStatusNotConnected
)
)

val onlineDrawable =
ContextCompat.getDrawable(
requireContext(),
if (isConnected) R.drawable.ic_internet_on else R.drawable.ic_internet_off
)

text_network_status.setCompoundDrawablesWithIntrinsicBounds(
onlineDrawable,
null,
null,
null
)

text_network_status.text = if (isConnected) {
getString(R.string.text_connectivity)
} else {
getString(R.string.text_no_connectivity)
}
}

fun LinearLayout.applyNetworkStatusAnimations(isConnected: Boolean) {
if (!isVisible) {
//play expanding animation
Animations.expand(container_network_status)
applyNetworkStatusTheme(isConnected)
} else {
//play fade out and in animation
Animations.fadeOutFadeIn(text_network_status) {
//on fadeInStarted
applyNetworkStatusTheme(isConnected)
}
}
}

fun LinearLayout.applyNetworkStatusVisibilityBehaviour(isConnected: Boolean) {
if (isConnected) {
networkAutoDismissHandler.postDelayed({
if (viewModel.networkObserver.value == true) {
Animations.collapse(this)
}
}, 3000)
} else {
networkAutoDismissHandler.removeCallbacksAndMessages(null)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package www.thecodemonks.techbytes.ui.viewmodel

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
Expand Down Expand Up @@ -73,10 +74,26 @@ class ArticleViewModel(
repo.deleteArticle(article)
}


private var currentQueryUrl = ""

// crawl data from NY times
fun crawlFromNYTimes(url: String) {
viewModelScope.launch(IO) {
_articles.postValue(repo.crawlFromNYTimes(url))
currentQueryUrl = url
if (networkObserver.value == true) {
viewModelScope.launch(IO) {
_articles.postValue(repo.crawlFromNYTimes(url))
}
}
}

fun reCrawlFromNYTimes(refreshFailed: () -> Unit = {}) {
if (networkObserver.value == true) {
viewModelScope.launch(IO) {
_articles.postValue(repo.crawlFromNYTimes(currentQueryUrl))
}
} else {
refreshFailed.invoke()
}
}

Expand Down
83 changes: 41 additions & 42 deletions app/src/main/res/layout/fragment_articles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,65 +52,64 @@
tools:text="@string/text_no_connectivity" />
</LinearLayout>

<FrameLayout
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh_articles"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/parent_layout"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/fragment_articles_scene"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/container_network_status">
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/article_rv"
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?listPreferredItemHeightLarge"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toTopOf="@+id/bottom_chips"
app:layoutDescription="@xml/fragment_articles_scene"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_post_article" />

<com.google.android.material.card.MaterialCardView
android:id="@+id/bottom_chips"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeightLarge"
android:backgroundTint="?colorSurface"
app:cardElevation="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
app:layout_constraintTop_toBottomOf="@+id/container_network_status">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/category_rv"
android:id="@+id/article_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_marginBottom="?listPreferredItemHeightLarge"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_post_category" />
app:layout_constraintBottom_toTopOf="@+id/bottom_chips"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_post_article" />

</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/bottom_chips"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeightLarge"
android:backgroundTint="?colorSurface"
app:cardElevation="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

</androidx.constraintlayout.motion.widget.MotionLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/category_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_post_category" />

<ProgressBar
android:id="@+id/progress_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible" />
</com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.motion.widget.MotionLayout>

</FrameLayout>
</FrameLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

0 comments on commit 08fd5c9

Please sign in to comment.