Skip to content

Instantly share code, notes, and snippets.

Avatar

Shivam Dhuria Shivamdhuria

View GitHub Profile
View Viewmodel.kt
val isSubmitEnabled: Flow<Boolean> = combine(_firstName, _password, _userID) { firstName, password, userId ->
val regexString = "[a-zA-Z]+"
val isNameCorrect = firstName.matches(regexString.toRegex())
val isPasswordCorrect = password.length > 8
val isUserIdCorrect = userId.contains("_")
return@combine isNameCorrect and isPasswordCorrect and isUserIdCorrect
}
View Viewmodel.kt
fun setFirstName(name: String) {
_firstName.value = name
}
fun setPassword(password: String) {
_password.value = password
}
fun setUserId(id: String) {
_userID.value = id
View DogListFragment.kt
override fun itemClickedClicked(view: View, dog: Dog) {
exitTransition = Hold().apply {
duration = resources.getInteger(R.integer.motion_duration_large).toLong()
}
reenterTransition = MaterialElevationScale(true).apply {
duration = resources.getInteger(R.integer.motion_duration_small).toLong()
}
val toDogDetailsFragment = DogListFragmentDirections.actionDogListFragmentToDogDetailFragment(dog.imageUrl.toString(), dog.breed)
View DogListFragment.kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
postponeEnterTransition()
view.doOnPreDraw { startPostponedEnterTransition() }
...
}
View DogDetailFragment.kt
override fun onCreate(savedInstanceState: Bundle?) {
sharedElementEnterTransition = MaterialContainerTransform().apply {
duration = 300L
isElevationShadowEnabled = true
setAllContainerColors(requireContext().themeColor(R.attr.colorSurface))
}
super.onCreate(savedInstanceState)
}
View DogDetailFragment.kt
class DogDetailFragment : Fragment(R.layout.dog_detail_fragment) {
private val args: DogDetailFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val (imageUrl, breed) = args
ImageLoader.loadImage(requireContext(), imageUrl, image_dog_detail)
textview_dog_breed.text = breed
//Set the unique transition name to the "end" view
View dog_detail_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
//End view of the Transition
android:id="@+id/detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
View DogListFragment.kt
class DogListFragment : Fragment(R.layout.dog_list_fragment), RecyclerAdapter.RecyclerViewClickListener {
private val adapter by lazy(NONE) { RecyclerAdapter(this) }
override fun onItemClicked(view: View, dog: Dog) {
val toDogDetailsFragment = DogListFragmentDirections.actionDogListFragmentToDogDetailFragment(dog.imageUrl.toString(), dog.breed)
val extras = FragmentNavigatorExtras(view to dog.imageUrl.toString())
navigate(toDogDetailsFragment, extras)
}
View RecyclerAdapter.kt
class RecyclerAdapter(val callback: RecyclerViewClickListener) : ListAdapter<Dog, RecyclerAdapter.DogViewHolder>(UserDataAdapterListDiff()) {
interface RecyclerViewClickListener {
fun onItemClicked(view: View, dog: Dog)
}
....inner class DogViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {
fun bind(dog: Dog) {
with(containerView) {
breed_name.text = dog.breed?.capitalize()
dog.imageUrl?.let { it1 -> ImageLoader.loadImage(containerView.context, it1, image_thumbnail) }
View item_dog.kt
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:src="?attr/selectableItemBackground"
app:cardCornerRadius="8dp"