Skip to content

Commit

Permalink
ui nav. and toolbar added
Browse files Browse the repository at this point in the history
  • Loading branch information
shahjadsunny committed Sep 2, 2022
1 parent ca56612 commit bb5b608
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,12 +76,4 @@ class TaskDiffCallback(private val oldList: List<Article>, 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
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<androidx.appcompat.widget.Toolbar>(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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<androidx.appcompat.widget.Toolbar>(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())
})
}
Expand All @@ -75,17 +119,15 @@ 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.")
}
}

private fun showErrorMsg() {

viewModel.msg.observe(viewLifecycleOwner) {
Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
Log.i(TAG, "showErrorMsg: $it")
}
}

Expand All @@ -98,14 +140,4 @@ class NewsFragment : Fragment() , ArticleCallback {
private const val TAG = "NewsFragment"
}

override fun updateLike(article: Article) {


}


}

interface ArticleCallback{
fun updateLike(article: Article)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/com/shahjad/fabhotels/ui/login/User.kt

This file was deleted.

8 changes: 7 additions & 1 deletion app/src/main/res/layout/fragment_article_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.articledetail.ArticleDetailFragment">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/imageView_article"
Expand Down Expand Up @@ -50,6 +54,7 @@
android:padding="10dp"
app:layout_constraintTop_toBottomOf="@+id/like_unlike"
android:id="@+id/title_text"
android:textColor="@color/black"
android:layout_width="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -62,6 +67,7 @@
/>
<TextView
android:padding="10dp"
android:textColor="@color/black"
app:layout_constraintTop_toBottomOf="@+id/title_text"
android:id="@+id/description_text"
android:layout_width="0dp"
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/res/layout/fragment_news.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.login.LoginFragment">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/news_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
app:articles="@{viewmodel.articles}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<ProgressBar
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/res/layout/news_article_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:onClick="@{() -> viewmodel.openArticleDetail(article,position)}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand All @@ -46,14 +49,15 @@
app:cardElevation="5dp"
app:cardCornerRadius="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:focusable="true">
<LinearLayout
android:layout_marginHorizontal="5dp"
android:layout_marginVertical="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onClick="@{() -> viewmodel.openArticleDetail(article,position)}">
>

<ImageView
android:id="@+id/complete_checkbox"
Expand All @@ -73,6 +77,7 @@
android:layout_height="40dp"
android:onClick="@{() -> viewmodel.onLike(position)}"/>
<TextView
android:textColor="@color/black"
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Loading

0 comments on commit bb5b608

Please sign in to comment.