Created
July 6, 2023 06:20
-
-
Save senamit2708/be3ca051101bb9e93cd592b7edee79b1 to your computer and use it in GitHub Desktop.
LiveData learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" | |
| android:layout_height="match_parent"> | |
| <EditText | |
| android:id="@+id/txtName" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="32dp" | |
| android:layout_marginEnd="32dp" | |
| android:ems="10" | |
| android:inputType="text" | |
| android:hint="Name" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintHorizontal_bias="0.0" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" /> | |
| <EditText | |
| android:id="@+id/txtMobNo" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="24dp" | |
| android:layout_marginEnd="32dp" | |
| android:ems="10" | |
| android:inputType="text" | |
| android:hint="Mob No" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/txtName" /> | |
| <Button | |
| android:id="@+id/btnSubmit" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="24dp" | |
| android:layout_marginEnd="32dp" | |
| android:text="Submit" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/txtMobNo" /> | |
| <View | |
| android:id="@+id/divider" | |
| android:layout_width="0dp" | |
| android:layout_height="2dp" | |
| android:layout_marginStart="1dp" | |
| android:layout_marginTop="32dp" | |
| android:layout_marginEnd="1dp" | |
| android:background="?android:attr/listDivider" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/btnSubmit" /> | |
| <TextView | |
| android:id="@+id/txtDisplayName" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="32dp" | |
| android:layout_marginEnd="32dp" | |
| android:text="name" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/divider" /> | |
| <TextView | |
| android:id="@+id/txtDisplayMob" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="32dp" | |
| android:layout_marginEnd="32dp" | |
| android:text="mob no" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/txtDisplayName" /> | |
| <View | |
| android:id="@+id/divider2" | |
| android:layout_width="409dp" | |
| android:layout_height="1dp" | |
| android:layout_marginStart="1dp" | |
| android:layout_marginTop="58dp" | |
| android:layout_marginEnd="1dp" | |
| android:background="?android:attr/listDivider" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintHorizontal_bias="0.0" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/txtTransformText" /> | |
| <TextView | |
| android:id="@+id/txtCount" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="24dp" | |
| android:layout_marginEnd="32dp" | |
| android:text="Count" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/txtDisplayMob" /> | |
| <TextView | |
| android:id="@+id/txtTransformText" | |
| android:layout_width="0dp" | |
| android:layout_height="wrap_content" | |
| android:layout_marginStart="32dp" | |
| android:layout_marginTop="16dp" | |
| android:layout_marginEnd="32dp" | |
| android:text="transform text" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/txtCount" /> | |
| </androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.coroutinelearning.fragments | |
| import android.os.Bundle | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import androidx.fragment.app.Fragment | |
| import androidx.fragment.app.activityViewModels | |
| import androidx.lifecycle.Observer | |
| import com.example.coroutinelearning.databinding.NewUserEntryBinding | |
| import com.example.coroutinelearning.entity.User | |
| import com.example.coroutinelearning.viewModels.UserViewModel | |
| class NewUserEntryFragment : Fragment() { | |
| private val TAG = NewUserEntryFragment::class.simpleName | |
| private lateinit var binding: NewUserEntryBinding | |
| private val userViewModel: UserViewModel by activityViewModels() | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| } | |
| override fun onCreateView( | |
| inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? | |
| ): View? { | |
| binding = NewUserEntryBinding.inflate(inflater, container, false) | |
| return binding.root | |
| } | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| binding.btnSubmit.setOnClickListener { | |
| saveUserDetailToDB() | |
| observeNewUser() | |
| observeNameCount() | |
| observerTransformationMap() | |
| } | |
| } | |
| private fun observerTransformationMap() { | |
| userViewModel.transformLiveDataExample.observe(viewLifecycleOwner) { | |
| binding.txtTransformText.text = it | |
| } | |
| } | |
| private fun observeNameCount() { | |
| // userViewModel.wordCount.value = 10 -> error-> because in viewmodel we created livedata in such way that fragment will | |
| //only be able to observe, but not change it in observer. | |
| userViewModel.wordCount.observe(viewLifecycleOwner) { count -> | |
| binding.txtCount.text = count.toString() | |
| } | |
| } | |
| private fun observeNewUser() { | |
| // val user = User(name = "sohan", mobileNumber = "3729857329") | |
| // userViewModel.newUserData.value = user | |
| userViewModel.newUserData.observe(viewLifecycleOwner, Observer { user -> | |
| binding.txtDisplayName.text = user.name | |
| binding.txtDisplayMob.text = user.mobileNumber | |
| }) | |
| } | |
| private fun saveUserDetailToDB() { | |
| val name = binding.txtName.text.toString() | |
| val mobNo = binding.txtMobNo.text.toString() | |
| val user = User(name = name, mobileNumber = mobNo) | |
| userViewModel.saveUserDetail(user) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.coroutinelearning.viewModels | |
| import android.app.Application | |
| import android.view.animation.Transformation | |
| import androidx.lifecycle.AndroidViewModel | |
| import androidx.lifecycle.LiveData | |
| import androidx.lifecycle.MutableLiveData | |
| import androidx.lifecycle.map | |
| import androidx.lifecycle.viewModelScope | |
| import com.example.coroutinelearning.entity.User | |
| import com.example.coroutinelearning.repositories.UserRepository | |
| import kotlinx.coroutines.launch | |
| class UserViewModel(application: Application) : AndroidViewModel(application) { | |
| private val userRepository: UserRepository | |
| val userListLiveData: MutableLiveData<List<User>> = MutableLiveData<List<User>>() | |
| val newUserData: MutableLiveData<User> = MutableLiveData() | |
| var transformLiveDataExample: LiveData<String> = MutableLiveData() | |
| private val _wordCount = MutableLiveData<Int>() | |
| val wordCount: LiveData<Int> //in this way you are restriction other acitivity/fragment from set method -> which is good. | |
| get() = _wordCount | |
| init { | |
| userRepository = UserRepository(application) | |
| } | |
| fun saveUserDetail(user: User) { | |
| viewModelScope.launch { | |
| userRepository.saveUserDetail(user) | |
| newUserData.value = user | |
| _wordCount.value = user.name.length | |
| //transform liveData feature-> if you like to do some manipulation in livedata | |
| transformLiveDataExample = newUserData.map { user -> | |
| user.name + user.mobileNumber | |
| } | |
| } | |
| } | |
| fun viewUserDetail(): LiveData<List<User>> { | |
| viewModelScope.launch { | |
| val userList = userRepository.viewUserDetail() | |
| userListLiveData.value = userList | |
| } | |
| return userListLiveData | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment