From bb5b608610cd0bed8da01fb36b5edeab21e19bd8 Mon Sep 17 00:00:00 2001 From: shahjad sunny Date: Fri, 2 Sep 2022 12:10:41 +0530 Subject: [PATCH] ui nav. and toolbar added --- .idea/misc.xml | 2 + .../shahjad/fabhotels/adaptors/NewsAdaptor.kt | 9 -- .../ui/articledetail/ArticleDetailFragment.kt | 42 ++++++--- .../articledetail/ArticleDetailViewModel.kt | 4 +- .../ui/articledetail/NewsArticleDetail.kt | 2 - .../fabhotels/ui/dashboard/NewsFragment.kt | 88 +++++++++++++------ .../fabhotels/ui/login/LoginFragment.kt | 3 - .../fabhotels/ui/login/LoginViewModel.kt | 1 - .../com/shahjad/fabhotels/ui/login/User.kt | 4 - .../res/layout/fragment_article_detail.xml | 8 +- app/src/main/res/layout/fragment_news.xml | 10 ++- app/src/main/res/layout/news_article_view.xml | 9 +- app/src/main/res/layout/toolbar.xml | 13 +++ app/src/main/res/menu/main_menu.xml | 10 +++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/themes.xml | 8 ++ 16 files changed, 151 insertions(+), 65 deletions(-) delete mode 100644 app/src/main/java/com/shahjad/fabhotels/ui/articledetail/NewsArticleDetail.kt delete mode 100644 app/src/main/java/com/shahjad/fabhotels/ui/login/User.kt create mode 100644 app/src/main/res/layout/toolbar.xml create mode 100644 app/src/main/res/menu/main_menu.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 175a7e0..124213f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,6 +10,8 @@ + + diff --git a/app/src/main/java/com/shahjad/fabhotels/adaptors/NewsAdaptor.kt b/app/src/main/java/com/shahjad/fabhotels/adaptors/NewsAdaptor.kt index c67dbdc..a03b038 100644 --- a/app/src/main/java/com/shahjad/fabhotels/adaptors/NewsAdaptor.kt +++ b/app/src/main/java/com/shahjad/fabhotels/adaptors/NewsAdaptor.kt @@ -4,7 +4,6 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.lifecycle.viewModelScope import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import com.shahjad.fabhotels.data.models.news.Article import com.shahjad.fabhotels.databinding.NewsArticleViewBinding @@ -77,12 +76,4 @@ class TaskDiffCallback(private val oldList: List
, private val newList: override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { return oldList[oldItemPosition] == newList[newItemPosition] } - -// override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean { -// return oldItem.title == newItem.title -// } -// -// override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean { -// return oldItem == newItem -// } } diff --git a/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailFragment.kt b/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailFragment.kt index b6ba348..47ad686 100644 --- a/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailFragment.kt +++ b/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailFragment.kt @@ -7,15 +7,16 @@ import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.lifecycle.Observer +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import androidx.navigation.fragment.findNavController import com.shahjad.fabhotels.MainActivityViewModel import com.shahjad.fabhotels.R import com.shahjad.fabhotels.databinding.FragmentArticleDetailBinding -import com.shahjad.fabhotels.databinding.FragmentNewsBinding class ArticleDetailFragment : Fragment() { - private lateinit var mainActivityViewModel: MainActivityViewModel + private lateinit var mainActivitySharedViewModel: MainActivityViewModel private lateinit var viewModel: ArticleDetailViewModel private var _binding: FragmentArticleDetailBinding? = null @@ -25,26 +26,45 @@ class ArticleDetailFragment : Fragment() { savedInstanceState: Bundle? ): View? { viewModel = ViewModelProvider(this).get(ArticleDetailViewModel::class.java) - mainActivityViewModel = ViewModelProvider(requireActivity()).get(MainActivityViewModel::class.java) + mainActivitySharedViewModel = ViewModelProvider(requireActivity()).get(MainActivityViewModel::class.java) _binding = FragmentArticleDetailBinding.inflate(inflater, container, false).also { it.lifecycleOwner = this it.viewmodel = viewModel - it.mainActivityViewModel = mainActivityViewModel + it.mainActivityViewModel = mainActivitySharedViewModel } -// + setToolbar() getDetailArticle() return binding.root } + private fun setToolbar() { + binding.toolbar.setBackgroundColor( + ContextCompat.getColor( + requireContext(), + R.color.purple_500 + ) + ) + val toolbar = binding.toolbar.findViewById(R.id.toolbar) + (activity as AppCompatActivity).setSupportActionBar(toolbar) + (activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true) + + toolbar.title = getString(R.string.article_detail) + toolbar.setTitleTextColor( + ContextCompat.getColor( + requireContext(), + R.color.white + ) + ) + toolbar.setNavigationOnClickListener { + findNavController().navigateUp() + } + } private fun getDetailArticle() { - mainActivityViewModel.article.observe(viewLifecycleOwner) { + mainActivitySharedViewModel.article.observe(viewLifecycleOwner) { Log.i(TAG, "getDetailArticle:$it ") -// binding.article = it - viewModel.showArticle(it) -// viewModel.onLike(it,mainActivityViewModel ) - + viewModel.showArticleDetails(it) } } diff --git a/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailViewModel.kt b/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailViewModel.kt index 83cac7c..d92c22d 100644 --- a/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailViewModel.kt +++ b/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/ArticleDetailViewModel.kt @@ -17,14 +17,12 @@ class ArticleDetailViewModel : ViewModel() { private const val TAG = "ArticleDetailViewModel" } fun onLike(article:Article,mainActivityViewModel: MainActivityViewModel){ - Log.i(TAG, "onLike:article:${article} ") article.like = !article.like _article.value = article - Log.i(TAG, "onLike:article update:${article} ") mainActivityViewModel._updateArticle.value = Event(article) } - fun showArticle(it: Article?) { + fun showArticleDetails(it: Article?) { it?.let { _article.value = it } diff --git a/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/NewsArticleDetail.kt b/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/NewsArticleDetail.kt deleted file mode 100644 index c453b0d..0000000 --- a/app/src/main/java/com/shahjad/fabhotels/ui/articledetail/NewsArticleDetail.kt +++ /dev/null @@ -1,2 +0,0 @@ -package com.shahjad.fabhotels.ui.articledetail - diff --git a/app/src/main/java/com/shahjad/fabhotels/ui/dashboard/NewsFragment.kt b/app/src/main/java/com/shahjad/fabhotels/ui/dashboard/NewsFragment.kt index 090e6f1..948b383 100644 --- a/app/src/main/java/com/shahjad/fabhotels/ui/dashboard/NewsFragment.kt +++ b/app/src/main/java/com/shahjad/fabhotels/ui/dashboard/NewsFragment.kt @@ -1,28 +1,29 @@ package com.shahjad.fabhotels.ui.dashboard -import androidx.lifecycle.ViewModelProvider import android.os.Bundle import android.util.Log -import androidx.fragment.app.Fragment -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import androidx.fragment.app.Fragment +import androidx.lifecycle.ViewModelProvider import androidx.navigation.fragment.findNavController import com.shahjad.fabhotels.MainActivityViewModel +import com.shahjad.fabhotels.R import com.shahjad.fabhotels.adaptors.NewsAdaptor import com.shahjad.fabhotels.data.local.AppSharedPreference import com.shahjad.fabhotels.data.models.news.Article import com.shahjad.fabhotels.databinding.FragmentNewsBinding -import com.shahjad.fabhotels.util.Event import com.shahjad.fabhotels.util.EventObserver import dagger.hilt.android.AndroidEntryPoint import javax.inject.Inject + @AndroidEntryPoint -class NewsFragment : Fragment() , ArticleCallback { +class NewsFragment : Fragment() { - private lateinit var mainActivityViewModel: MainActivityViewModel + private lateinit var mainActivitySharedViewModel: MainActivityViewModel private lateinit var viewModel: NewsViewModel @Inject @@ -37,34 +38,77 @@ class NewsFragment : Fragment() , ArticleCallback { savedInstanceState: Bundle? ): View? { viewModel = ViewModelProvider(this).get(NewsViewModel::class.java) - mainActivityViewModel = ViewModelProvider(requireActivity()).get(MainActivityViewModel::class.java) + mainActivitySharedViewModel = + ViewModelProvider(requireActivity()).get(MainActivityViewModel::class.java) _binding = FragmentNewsBinding.inflate(inflater, container, false).also { it.lifecycleOwner = this it.viewmodel = viewModel } + setToolbar() + setAdaptor() updateArticleLike() showErrorMsg() openNewsDetail() - mainActivityViewModel.updateArticle.observe(this,EventObserver{ + updateArticleFromDetailFrag() + return binding.root + } + private fun setToolbar() { + + binding.toolbar.setBackgroundColor( + ContextCompat.getColor( + requireContext(), + R.color.purple_500 + ) + ) + val toolbar = binding.toolbar.findViewById(R.id.toolbar) + (activity as AppCompatActivity).setSupportActionBar(toolbar) + (activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(false) + (activity as AppCompatActivity).supportActionBar?.setHomeButtonEnabled(true) + toolbar.title = getString(R.string.dashboard) + toolbar.setTitleTextColor( + ContextCompat.getColor( + requireContext(), + R.color.white + ) + ) + setHasOptionsMenu(true) + + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when(item.itemId ){ + R.id.logout->{ + appSharedPreference.setUserToken("") + appSharedPreference.setUserName("") + findNavController().navigate(NewsFragmentDirections.actionNewsFragmentToLoginFragment()) + } + } + return true + } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.main_menu, menu) + } + private fun updateArticleFromDetailFrag() { + + mainActivitySharedViewModel.updateArticle.observe(viewLifecycleOwner, EventObserver { Log.i(TAG, "onCreateView: update view:$it") - viewModel.likeUpdate(it,it.position) + viewModel.likeUpdate(it, it.position) }) - return binding.root } private fun updateArticleLike() { - viewModel.position.observe(viewLifecycleOwner,EventObserver{ + viewModel.position.observe(viewLifecycleOwner, EventObserver { newsAdaptor?.notifyItemChanged(it) }) } private fun openNewsDetail() { - viewModel.openArticleEvent.observe(viewLifecycleOwner,EventObserver{ - mainActivityViewModel.article.postValue( it) + viewModel.openArticleEvent.observe(viewLifecycleOwner, EventObserver { + mainActivitySharedViewModel.article.postValue(it) findNavController().navigate(NewsFragmentDirections.actionNewsFragmentToArticleDetailFragment()) }) } @@ -75,9 +119,8 @@ class NewsFragment : Fragment() , ArticleCallback { if (viewModel != null) { newsAdaptor = NewsAdaptor(viewModel) binding.newsListView.adapter = newsAdaptor -// _binding.newsAdaptor = newsAdaptor } else { -// Timber.w("ViewModel not initialized when attempting to set up adapter.") + Log.i(TAG,"ViewModel not initialized when attempting to set up adapter.") } } @@ -85,7 +128,6 @@ class NewsFragment : Fragment() , ArticleCallback { viewModel.msg.observe(viewLifecycleOwner) { Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show() - Log.i(TAG, "showErrorMsg: $it") } } @@ -98,14 +140,4 @@ class NewsFragment : Fragment() , ArticleCallback { private const val TAG = "NewsFragment" } - override fun updateLike(article: Article) { - - - } - - } - -interface ArticleCallback{ - fun updateLike(article: Article) -} \ No newline at end of file diff --git a/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginFragment.kt b/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginFragment.kt index cb03937..3f92896 100644 --- a/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginFragment.kt +++ b/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginFragment.kt @@ -16,9 +16,6 @@ import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class LoginFragment : Fragment() { -// companion object { -// fun newInstance() = LoginFragment() -// } private var _binding: FragmentLoginBinding? = null private lateinit var viewModel: LoginViewModel diff --git a/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginViewModel.kt b/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginViewModel.kt index 05fdbb1..6fd3cbb 100644 --- a/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginViewModel.kt +++ b/app/src/main/java/com/shahjad/fabhotels/ui/login/LoginViewModel.kt @@ -73,7 +73,6 @@ class LoginViewModel @Inject constructor(private val loginRepository: LoginRepos } fun submit(view: View){ - Log.i(TAG, "submit::userName:${email.value}, password:${password.value}") if (isEmailValid(email.value)){ _isEmailValid.value = View.INVISIBLE password.value?.length?.let { diff --git a/app/src/main/java/com/shahjad/fabhotels/ui/login/User.kt b/app/src/main/java/com/shahjad/fabhotels/ui/login/User.kt deleted file mode 100644 index a2a69ee..0000000 --- a/app/src/main/java/com/shahjad/fabhotels/ui/login/User.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.shahjad.fabhotels.ui.login - -data class User(val name: String, val password: String) - diff --git a/app/src/main/res/layout/fragment_article_detail.xml b/app/src/main/res/layout/fragment_article_detail.xml index 560bbba..0a66a92 100644 --- a/app/src/main/res/layout/fragment_article_detail.xml +++ b/app/src/main/res/layout/fragment_article_detail.xml @@ -21,8 +21,12 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.articledetail.ArticleDetailFragment"> + + + android:layout_height="match_parent" + android:focusable="true"> + > + \ No newline at end of file diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml new file mode 100644 index 0000000..93dc2a7 --- /dev/null +++ b/app/src/main/res/menu/main_menu.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2c1aafb..260ee21 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,4 +7,7 @@ Password Sign In Enter Password + Dashboard + Article Detail + Logout \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 00b7fe2..747fe6d 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -11,6 +11,14 @@ @color/black ?attr/colorPrimaryVariant + + + + + + + + \ No newline at end of file