From 68dd97fe8d119b8896b499354c46264152cadddb Mon Sep 17 00:00:00 2001 From: Chetan Gupta Date: Mon, 5 Oct 2020 11:17:29 +0530 Subject: [PATCH 1/2] added/swipeRefersh --- .../www/thecodemonks/techbytes/repo/Repo.kt | 3 +- .../techbytes/ui/articles/ArticlesFragment.kt | 85 ++++++++++++++++--- .../ui/viewmodel/ArticleViewModel.kt | 21 ++++- app/src/main/res/layout/fragment_articles.xml | 83 +++++++++--------- 4 files changed, 134 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt b/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt index e86b7ff..892fda9 100644 --- a/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt +++ b/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt @@ -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 @@ -74,7 +75,7 @@ class Repo(private val db: ArticleDatabase) { articles.add(article) } } - + Log.e("test","reCrawlFromNYTimes-completed") return articles } diff --git a/app/src/main/java/www/thecodemonks/techbytes/ui/articles/ArticlesFragment.kt b/app/src/main/java/www/thecodemonks/techbytes/ui/articles/ArticlesFragment.kt index 6642e67..3b3c767 100644 --- a/app/src/main/java/www/thecodemonks/techbytes/ui/articles/ArticlesFragment.kt +++ b/app/src/main/java/www/thecodemonks/techbytes/ui/articles/ArticlesFragment.kt @@ -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()) @@ -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 @@ -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) { @@ -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) + } } + } \ No newline at end of file diff --git a/app/src/main/java/www/thecodemonks/techbytes/ui/viewmodel/ArticleViewModel.kt b/app/src/main/java/www/thecodemonks/techbytes/ui/viewmodel/ArticleViewModel.kt index ccbdde5..ba9a709 100644 --- a/app/src/main/java/www/thecodemonks/techbytes/ui/viewmodel/ArticleViewModel.kt +++ b/app/src/main/java/www/thecodemonks/techbytes/ui/viewmodel/ArticleViewModel.kt @@ -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 @@ -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() } } diff --git a/app/src/main/res/layout/fragment_articles.xml b/app/src/main/res/layout/fragment_articles.xml index ecccf61..3be89ac 100644 --- a/app/src/main/res/layout/fragment_articles.xml +++ b/app/src/main/res/layout/fragment_articles.xml @@ -52,65 +52,64 @@ tools:text="@string/text_no_connectivity" /> - - + android:layout_height="match_parent"> - - - + app:layout_constraintTop_toBottomOf="@+id/container_network_status"> + 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" /> - + - + - + + + - + + \ No newline at end of file From 5dc4b6b819456eb3bd391814ef31cacf4da8e3df Mon Sep 17 00:00:00 2001 From: Chetan Gupta Date: Mon, 5 Oct 2020 11:20:14 +0530 Subject: [PATCH 2/2] removed/logs --- app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt b/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt index 892fda9..fdea129 100644 --- a/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt +++ b/app/src/main/java/www/thecodemonks/techbytes/repo/Repo.kt @@ -75,7 +75,6 @@ class Repo(private val db: ArticleDatabase) { articles.add(article) } } - Log.e("test","reCrawlFromNYTimes-completed") return articles }