Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODIFIED:1- binding all the data to xml using dataBinding #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

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

1 change: 1 addition & 0 deletions .idea/.name

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

6 changes: 6 additions & 0 deletions .idea/compiler.xml

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

19 changes: 19 additions & 0 deletions .idea/gradle.xml

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

35 changes: 35 additions & 0 deletions .idea/jarRepositories.xml

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

9 changes: 9 additions & 0 deletions .idea/misc.xml

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

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ android {
}
buildFeatures {
viewBinding true
dataBinding true
}
androidExtensions {
features = ["parcelize"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.wajahatkarim3.imagine.adapters

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.load
import com.wajahatkarim3.imagine.R

@BindingAdapter("loadUrl")
fun loadImage(view: ImageView, url: String?) {
url?.let {
view.load(it){
placeholder(R.color.color_box_background)
crossfade(true)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package com.wajahatkarim3.imagine.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.wajahatkarim3.imagine.R
import com.wajahatkarim3.imagine.databinding.PhotoItemLayoutBinding
import com.wajahatkarim3.imagine.model.PhotoModel

Expand All @@ -28,12 +26,13 @@ class PhotosAdapter(val onPhotoSelected: (photo: PhotoModel, position: Int) -> U
private val photoItems: ArrayList<PhotoModel> = arrayListOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
var binding = PhotoItemLayoutBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
return PhotoViewHolder(
PhotoItemLayoutBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
return PhotoViewHolder(binding)
}

override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
Expand All @@ -48,15 +47,11 @@ class PhotosAdapter(val onPhotoSelected: (photo: PhotoModel, position: Int) -> U
notifyDataSetChanged()
}

inner class PhotoViewHolder(val itemBinding: PhotoItemLayoutBinding) : RecyclerView.ViewHolder(itemBinding.root) {
inner class PhotoViewHolder(private val itemBinding: PhotoItemLayoutBinding) : RecyclerView.ViewHolder(itemBinding.root) {

fun bind(photoModel: PhotoModel, position: Int) {
itemBinding.apply {
imgPhoto.load(photoModel.urls.thumb) {
placeholder(R.color.color_box_background)
crossfade(true)
}

item = photoModel
cardPhoto.setOnClickListener {
onPhotoSelected(photoModel, position)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package com.wajahatkarim3.imagine.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.wajahatkarim3.imagine.R
import com.wajahatkarim3.imagine.databinding.TagItemLayoutBinding
import com.wajahatkarim3.imagine.model.TagModel

Expand All @@ -28,12 +26,7 @@ class TagsAdapter(val onTagSelected: (tag: TagModel, position: Int) -> Unit) : R
private val tagItems: ArrayList<TagModel> = arrayListOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TagViewHolder {
var binding = TagItemLayoutBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return TagViewHolder(binding)
return TagViewHolder(TagItemLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false))
}

override fun onBindViewHolder(holder: TagViewHolder, position: Int) {
Expand All @@ -48,16 +41,11 @@ class TagsAdapter(val onTagSelected: (tag: TagModel, position: Int) -> Unit) : R
notifyDataSetChanged()
}

inner class TagViewHolder(val itemBinding: TagItemLayoutBinding) : RecyclerView.ViewHolder(itemBinding.root) {
inner class TagViewHolder(private val itemBinding: TagItemLayoutBinding) : RecyclerView.ViewHolder(itemBinding.root) {

fun bind(tagModel: TagModel, position: Int) {
itemBinding.apply {
txtTagName.text = tagModel.tagName
imgTag.load(tagModel.imageUrl) {
placeholder(R.color.color_box_background)
crossfade(true)
}

item = tagModel
cardTag.setOnClickListener {
onTagSelected(tagModel, position)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,30 @@ class HomeFragment : BaseFragment<HomeFragmentBinding>() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

bi.vm = viewModel
bi.lifecycleOwner = this

setupViews()
initTags()
initObservations()
}

fun setupViews() {
private fun setupViews() {
context?.let { ctx ->
// Tags RecyclerView
tagsAdapter = TagsAdapter { tag, _ ->
performSearch(tag.tagName)
}
val flexboxLayoutManager = FlexboxLayoutManager(ctx).apply {
bi.recyclerTags.layoutManager = FlexboxLayoutManager(ctx).apply {
flexWrap = FlexWrap.WRAP
flexDirection = FlexDirection.ROW
alignItems = AlignItems.STRETCH
}
bi.recyclerTags.layoutManager = flexboxLayoutManager
bi.recyclerTags.adapter = tagsAdapter

// Photos RecyclerView
photosAdapter = PhotosAdapter() { photo, _ ->
var bundle = bundleOf("photo" to photo)
findNavController().navigate(R.id.action_homeFragment_to_photoDetailsFragment, bundle)
photosAdapter = PhotosAdapter { photo, _ ->
findNavController().navigate(R.id.action_homeFragment_to_photoDetailsFragment, bundleOf("photo" to photo))
}
photosAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
bi.recyclerPopularPhotos.adapter = photosAdapter
Expand All @@ -95,7 +96,7 @@ class HomeFragment : BaseFragment<HomeFragmentBinding>() {
// Input Text Search
bi.inputSearchPhotos.setEndIconOnClickListener {
bi.txtSearchPhotos.setText("")
bi.lblPopular.setText(getString(R.string.label_popular_text_str))
bi.lblPopular.text = (getString(R.string.label_popular_text_str))
viewModel.fetchPhotos(1)
}

Expand All @@ -111,12 +112,10 @@ class HomeFragment : BaseFragment<HomeFragmentBinding>() {
}

private fun performSearch(query: String) {
bi.txtSearchPhotos.setText(query)
bi.lblPopular.setText(getString(R.string.message_search_results_for_str, query))
viewModel.searchPhotos(query)
}

fun initObservations() {
private fun initObservations() {
viewModel.uiStateLiveData.observe(viewLifecycleOwner) { state ->
when (state) {
is LoadingState -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,31 @@ class HomeViewModel @Inject constructor(
var photosListLiveData: LiveData<List<PhotoModel>> = _photosList

private var pageNumber = 1
private var searchQuery: String = ""
var searchQuery: MutableLiveData<String> = MutableLiveData()

init {
fetchPhotos(pageNumber)
}

fun loadMorePhotos() {
pageNumber++
if (searchQuery == "")
if (searchQuery.value == "")
fetchPhotos(pageNumber)
else
searchPhotos(searchQuery, pageNumber)
}

fun retry() {
if (searchQuery == "")
if (searchQuery.value == "")
fetchPhotos(pageNumber)
else
searchPhotos(searchQuery, pageNumber)
}

fun searchPhotos(query: String) {
searchQuery = query
searchQuery.value = query
pageNumber = 1
searchPhotos(query, pageNumber)
searchPhotos(searchQuery, pageNumber)
}

fun fetchPhotos(page: Int) {
Expand Down Expand Up @@ -100,31 +100,33 @@ class HomeViewModel @Inject constructor(
}
}

private fun searchPhotos(query: String, page: Int) {
private fun searchPhotos(query: MutableLiveData<String>, page: Int) {
_uiState.postValue(if (page == 1) LoadingState else LoadingNextPageState)
viewModelScope.launch {
searchPhotosUsecase(query, page).collect { dataState ->
when (dataState) {
is DataState.Success -> {
if (page == 1) {
// First page
_uiState.postValue(ContentState)
_photosList.postValue(dataState.data)
} else {
// Any other page
_uiState.postValue(ContentNextPageState)
var currentList = arrayListOf<PhotoModel>()
_photosList.value?.let { currentList.addAll(it) }
currentList.addAll(dataState.data)
_photosList.postValue(currentList)
query.value?.let {
searchPhotosUsecase(it, page).collect { dataState ->
when (dataState) {
is DataState.Success -> {
if (page == 1) {
// First page
_uiState.postValue(ContentState)
_photosList.postValue(dataState.data)
} else {
// Any other page
_uiState.postValue(ContentNextPageState)
var currentList = arrayListOf<PhotoModel>()
_photosList.value?.let { currentList.addAll(it) }
currentList.addAll(dataState.data)
_photosList.postValue(currentList)
}
}
}

is DataState.Error -> {
if (page == 1) {
_uiState.postValue(ErrorState(dataState.message))
} else {
_uiState.postValue(ErrorNextPageState(dataState.message))
is DataState.Error -> {
if (page == 1) {
_uiState.postValue(ErrorState(dataState.message))
} else {
_uiState.postValue(ErrorNextPageState(dataState.message))
}
}
}
}
Expand Down
Loading