Skip to content

Commit

Permalink
Fix bugs and code refactor
Browse files Browse the repository at this point in the history
- refactor destroy binding
- refactor code in glide
- refactor glide functions
- fix animations
- refactor nav args
- refactor animation transition name and all click listeners
- remove unusable resources
  • Loading branch information
stslex committed Jul 30, 2021
1 parent 7c8df05 commit 4adac49
Show file tree
Hide file tree
Showing 20 changed files with 149 additions and 176 deletions.
1 change: 1 addition & 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 @@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import androidx.core.view.doOnPreDraw
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.FragmentNavigatorExtras
Expand Down Expand Up @@ -44,11 +45,15 @@ class AllPhotosFragment : Fragment() {
adapter = AllPhotosAdapter(clickListener)
recyclerView = binding.fragmentAllPhotosRecycler.fragmentAllPhotosRecyclerView
layoutManager = LinearLayoutManager(requireContext())
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
viewModel.allPhotos.observe(viewLifecycleOwner) {
adapter.addItems(it.image)
}
postponeEnterTransition()
recyclerView.doOnPreDraw {
startPostponedEnterTransition()
}
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
}

private fun initScrollListener() {
Expand All @@ -73,17 +78,17 @@ class AllPhotosFragment : Fragment() {
})
}

private val clickListener = ImageClickListener { imageModel, imageView ->
private val clickListener = ImageClickListener { imageView, id ->
val directions = MainFragmentDirections.actionNavHomeToNavSinglePhoto(
imageModel,
imageView.transitionName
imageView.transitionName,
id
)
val extras = FragmentNavigatorExtras(imageView to imageView.transitionName)
findNavController().navigate(directions, extras)
}

override fun onDestroy() {
super.onDestroy()
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class AllPhotosViewHolder(private val binding: ItemRecyclerAllPhotosBinding) :
RecyclerView.ViewHolder(binding.root), View.OnClickListener {

private lateinit var clickListener: ImageClickListener
private lateinit var imageModel: ImageModel
private lateinit var id: String

fun bind(imageModel: ImageModel) {
this.imageModel = imageModel
id = imageModel.id
binding.itemPagerImage.transitionName = imageModel.urls.regular
binding.itemPagerImage.downloadAndSet(imageModel.urls.regular)
binding.itemPagerImagePerson.downloadAndSetSmallRound(imageModel.user?.profile_image!!.medium)
Expand All @@ -30,7 +30,7 @@ class AllPhotosViewHolder(private val binding: ItemRecyclerAllPhotosBinding) :

override fun onClick(p0: View) {
when (p0) {
is ImageView -> clickListener.onClick(imageModel, p0)
is ImageView -> clickListener.onClick(p0, id)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,17 @@ class CollectionsFragment : Fragment() {
})
}

private val clickListener = CollectionClickListener { collection, image ->
private val clickListener = CollectionClickListener { imageView, title ->
val directions = MainFragmentDirections.actionNavHomeToNavSingleCollection(
collection,
image.transitionName
transitionName = imageView.transitionName,
title = title
)
pagesNumCollections = 0
val extras = FragmentNavigatorExtras(image to image.transitionName)
val extras = FragmentNavigatorExtras(imageView to imageView.transitionName)
findNavController().navigate(directions, extras)
}

override fun onDestroy() {
super.onDestroy()
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class CollectionsViewHolder(private val binding: ItemRecyclerCollectionsBinding)
RecyclerView.ViewHolder(binding.root), View.OnClickListener {

private lateinit var clickListener: CollectionClickListener
private lateinit var collection: CollectionModel
private lateinit var title: String

fun bind(collection: CollectionModel) {
this.collection = collection
binding.itemCollectionImage.transitionName = collection.cover_photo?.urls!!.regular
binding.itemCollectionTitle.text = collection.title
binding.itemCollectionImage.downloadAndSet(collection.cover_photo.urls.regular)
title = collection.title
binding.itemCollectionImage.transitionName = collection.id
binding.itemCollectionTitle.text = title
binding.itemCollectionImage.downloadAndSet(collection.cover_photo?.urls!!.regular)
binding.itemCollectionAuthorName.text = collection.user?.name
binding.itemCollectionNumber.text = "${collection.total_photos} photos"
binding.itemCollectionImagePerson.downloadAndSetSmallRound(collection.user?.profile_image!!.small)
Expand All @@ -32,7 +32,7 @@ class CollectionsViewHolder(private val binding: ItemRecyclerCollectionsBinding)

override fun onClick(p0: View?) {
when (p0) {
is ImageView -> clickListener.onClick(collection, p0)
is ImageView -> clickListener.onClick(p0, title)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ class MainFragment : BaseFragment() {
}.attach()
}

override fun onDestroy() {
super.onDestroy()
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.transition.MaterialContainerTransform
import com.stslex.splashgallery.data.model.domain.collection.CollectionModel
import com.stslex.splashgallery.databinding.FragmentSingleCollectionBinding
import com.stslex.splashgallery.ui.all_photos.adapter.AllPhotosAdapter
import com.stslex.splashgallery.utils.Result
Expand All @@ -30,13 +29,12 @@ class SingleCollectionFragment : BaseFragment() {

private val viewModel: SingleCollectionViewModel by viewModels { viewModelFactory.get() }

private lateinit var collection: CollectionModel
private lateinit var id: String
private var pagesImage = MutableLiveData<Int>()
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: AllPhotosAdapter
private lateinit var layoutManager: LinearLayoutManager
private var isScrolling = false
private var pageNum = 1

override fun onAttach(context: Context) {
super.onAttach(context)
Expand Down Expand Up @@ -87,7 +85,7 @@ class SingleCollectionFragment : BaseFragment() {

private fun initRecyclerView() {
pagesImage.observe(viewLifecycleOwner) {
viewModel.getAllPhotos(collection.id, it)
viewModel.getAllPhotos(id, it)
}
recyclerView = binding.fragmentCollectionRecyclerView.fragmentAllPhotosRecyclerView
adapter = AllPhotosAdapter(clickListener)
Expand All @@ -112,29 +110,29 @@ class SingleCollectionFragment : BaseFragment() {
}
}

private val clickListener = ImageClickListener { imageModel, imageView ->
private val clickListener = ImageClickListener { imageView, id ->
val directions =
SingleCollectionFragmentDirections.actionNavSingleCollectionToNavSinglePhoto(
imageModel,
imageView.transitionName
imageView.transitionName,
id
)
val extras = FragmentNavigatorExtras(imageView to imageView.transitionName)
findNavController().navigate(directions, extras)
}

private fun getNavigationArgs() {
val extras: SingleCollectionFragmentArgs by navArgs()
collection = extras.collectionModel
val transitionName = extras.transitionName
binding.fragmentCollectionRecyclerView.fragmentAllPhotosRecyclerView.transitionName =
transitionName
id = extras.transitionName
binding.fragmentCollectionRecyclerView.fragmentAllPhotosRecyclerView.transitionName = id
}

override fun onDestroy() {
super.onDestroy()
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object
companion object {
private var pageNum = 1
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stslex.splashgallery.ui.single_image

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -8,6 +9,7 @@ import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.google.android.material.transition.MaterialContainerTransform
import com.stslex.splashgallery.R
import com.stslex.splashgallery.databinding.FragmentSingleImageBinding
import com.stslex.splashgallery.utils.setImageWithRequest

Expand All @@ -18,7 +20,11 @@ class SingleImageFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialContainerTransform(requireContext(), true)
sharedElementEnterTransition = MaterialContainerTransform().apply {
drawingViewId = R.id.nav_host_fragment
duration = 700.toLong()
scrimColor = Color.TRANSPARENT
}
}

override fun onCreateView(
Expand All @@ -41,10 +47,9 @@ class SingleImageFragment : Fragment() {
private fun getNavigationArgs() {
postponeEnterTransition()
val extras: SingleImageFragmentArgs by navArgs()
val url = extras.url
val transitionName = extras.transitionName
binding.fragmentSingleImageImage.transitionName = transitionName
setImageWithRequest(url, binding.fragmentSingleImageImage)
val url = extras.transitionName
binding.fragmentSingleImageImage.transitionName = url
setImageWithRequest(url, binding.fragmentSingleImageImage, false)
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stslex.splashgallery.ui.single_photo_screen

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -10,7 +11,7 @@ import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.google.android.material.transition.MaterialContainerTransform
import com.stslex.splashgallery.data.model.domain.image.ImageModel
import com.stslex.splashgallery.R
import com.stslex.splashgallery.databinding.FragmentSinglePhotoBinding
import com.stslex.splashgallery.utils.Result
import com.stslex.splashgallery.utils.appComponent
Expand All @@ -25,7 +26,7 @@ class SinglePhotoFragment : BaseFragment(), View.OnClickListener {
private val binding get() = _binding!!
private val viewModel: SinglePhotoViewModel by viewModels { viewModelFactory.get() }

private lateinit var image: ImageModel
private lateinit var id: String

override fun onAttach(context: Context) {
super.onAttach(context)
Expand All @@ -34,7 +35,11 @@ class SinglePhotoFragment : BaseFragment(), View.OnClickListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialContainerTransform(requireContext(), true)
sharedElementEnterTransition = MaterialContainerTransform().apply {
drawingViewId = R.id.nav_host_fragment
duration = 700.toLong()
scrimColor = Color.TRANSPARENT
}
}

override fun onCreateView(
Expand All @@ -49,15 +54,17 @@ class SinglePhotoFragment : BaseFragment(), View.OnClickListener {
super.onViewCreated(view, savedInstanceState)
getNavigationArgs()
setListener()
bindImage()
binding.singlePhotoImage.setOnClickListener(this)
}

private fun setListener() {
viewModel.getCurrentPhoto(image.id)
viewModel.getCurrentPhoto(id)
viewModel.currentPhoto.observe(viewLifecycleOwner) {
when (it) {
is Result.Success -> {
setImageWithRequest(url = it.data.urls.regular, binding.singlePhotoImage, true)
binding.singlePhotoProfileImage.downloadAndSetSmallRound(it.data.user?.profile_image!!.medium)
binding.singlePhotoProfileUsername.text = it.data.user.username
binding.singlePhotoAperture.text = it.data.exif?.aperture
binding.singlePhotoCamera.text = it.data.exif?.model
binding.singlePhotoDimension.text = it.data.exif?.exposure_time
Expand All @@ -73,29 +80,22 @@ class SinglePhotoFragment : BaseFragment(), View.OnClickListener {
}
}

private fun bindImage() {
setImageWithRequest(url = image.urls.regular, binding.singlePhotoImage)
binding.singlePhotoProfileImage.downloadAndSetSmallRound(image.user?.profile_image!!.medium)
binding.singlePhotoProfileUsername.text = image.user!!.username
}

private fun getNavigationArgs() {
postponeEnterTransition()
val extras: SinglePhotoFragmentArgs by navArgs()
image = extras.imageModel
val transitionName = extras.transitionName
binding.singlePhotoImage.transitionName = transitionName
id = extras.id
binding.singlePhotoImage.transitionName = extras.transitionName
}

override fun onDestroy() {
super.onDestroy()
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private val clickListener = ImageClickListener { imageModel, imageView ->
private val clickListener = ImageClickListener { imageView, id ->
val directions = SinglePhotoFragmentDirections.actionNavSinglePhotoToNavSingleImage(
imageModel.urls.regular,
imageView.transitionName
transitionName = imageView.transitionName,
id = id
)
val extras = FragmentNavigatorExtras(imageView to imageView.transitionName)
findNavController().navigate(directions, extras)
Expand All @@ -104,7 +104,7 @@ class SinglePhotoFragment : BaseFragment(), View.OnClickListener {
override fun onClick(p0: View?) {
when (p0) {
binding.singlePhotoImage -> {
clickListener.onClick(image, binding.singlePhotoImage)
clickListener.onClick(binding.singlePhotoImage, id)
}
}
}
Expand Down
Loading

0 comments on commit 4adac49

Please sign in to comment.