I am going through Guide to app architecture and trying to implement MVVM and LiveData in one of my apps. I am using realm and I am using this to create a RealmLiveData as shown below
class RealmLiveData<T : RealmModel>(private val results: RealmResults<T>) : MutableLiveData<RealmResults<T>>() {
private val listener = RealmChangeListener<RealmResults<T>> { results -> value = results }
override fun onActive() {
results.addChangeListener(listener)
}
override fun onInactive() {
results.removeChangeListener(listener)
}
}
This how I am updating the list to recyclerview
var mList:ArrayList<Notes> = ArrayList()
lateinit var historyViewModel: HistoryViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_history, container, false)
mRCview = view.findViewById(R.id.list)
historyViewModel = ViewModelProviders.of(activity!!).get(HistoryViewModel::class.java)
// this is how I observe
historyViewModel.getList().observe(this, Observer{
(mRCview.adapter as MyHistoryRecyclerViewAdapter).setData(it)
})
with(mRCview) {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(mContext)
mList = ArrayList()
adapter = MyHistoryRecyclerViewAdapter(
mContext as OnListFragmentInteractionListener
)
}
return view
}
This is how I get the data in my repository class
class HistoryRepository {
fun getHistory(): RealmLiveData<Notes> {
val realmInstance = Realm.getDefaultInstance()
val realmResults = realmInstance
.where(Notes::class.java)
.findAll()
.sort("lastUpdatedTimeStamp", Sort.DESCENDING)
return realmResults.asLiveData()
}
fun <T:RealmModel> RealmResults<T>.asLiveData() = RealmLiveData(this)
}
EDIT
Here is the ViewModel
class HistoryViewModel: ViewModel() {
val repository = HistoryRepository()
fun getList(): RealmLiveData<Notes> {
return repository.getHistory()
}
}
The issue is that the observer is not getting triggered for the first time. If I update the realmresult, the live data update gets invoked and updates my list. Please let me know how I can fix the issue.
We need to notify the Observer of the existing data. When the first Observer registers to historyViewModel.getList() you are registering the realm callback. At this point we need to trigger a change just to notify this Observer of the existing data.
Something like
class RealmLiveData<T : RealmModel>(private val results: RealmResults<T>) : MutableLiveData<RealmResults<T>>() {
private val listener = RealmChangeListener<RealmResults<T>> { results -> value = results }
override fun onActive() {
results.addChangeListener(listener)
listener.onChange(results) // notify the added Observer of the existing data.
}
override fun onInactive() {
results.removeChangeListener(listener)
}
}
Related
I'm making a screen similar to the image.
The data set in advance is taken from the Room DB and the data is set for each tab.
Each tab is a fragment and displays the data in a RecyclerView.
Each tab contains different data, so i set Tab to LiveData in ViewModel and observe it.
Therefore, whenever tabs change, the goal is to get the data for each tab from the database and set it in the RecyclerView.
However, even if I import the data, it is not set in RecyclerView.
I think the data comes in well even when I debug it.
This is not an adapter issue.
What am I missing?
WorkoutList
#Entity
data class WorkoutList(
#PrimaryKey(autoGenerate = true)
val id: Long = 0,
val chest: List<String>,
val back: List<String>,
val leg: List<String>,
val shoulder: List<String>,
val biceps: List<String>,
val triceps: List<String>,
val abs: List<String>
)
ViewModel
class WorkoutListViewModel(application: Application) : AndroidViewModel(application){
private var _part :MutableLiveData<BodyPart> = MutableLiveData()
private var result : List<String> = listOf()
private val workoutDao = WorkoutListDatabase.getDatabase(application).workoutListDao()
private val workoutListRepo = WorkoutListRepository(workoutDao)
val part = _part
fun setList(part : BodyPart) : List<String> {
_part.value = part
viewModelScope.launch(Dispatchers.IO){
result = workoutListRepo.getWorkoutList(part)
}
return result
}
}
Repository
class WorkoutListRepository(private val workoutListDao: WorkoutListDao) {
suspend fun getWorkoutList(part: BodyPart) : List<String> {
val partList = workoutListDao.getWorkoutList()
return when(part) {
is BodyPart.Chest -> partList.chest
is BodyPart.Back -> partList.back
is BodyPart.Leg -> partList.leg
is BodyPart.Shoulder -> partList.shoulder
is BodyPart.Biceps -> partList.biceps
is BodyPart.Triceps -> partList.triceps
is BodyPart.Abs -> partList.abs
}
}
}
Fragment
class WorkoutListTabPageFragment : Fragment() {
private var _binding : FragmentWorkoutListTabPageBinding? = null
private val binding get() = _binding!!
private lateinit var adapter: WorkoutListAdapter
private lateinit var part: BodyPart
private val viewModel: WorkoutListViewModel by viewModels()
companion object {
#JvmStatic
fun newInstance(part: BodyPart) =
WorkoutListTabPageFragment().apply {
arguments = Bundle().apply {
putParcelable("part", part)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let { bundle ->
part = bundle.getParcelable("part") ?: throw NullPointerException("No BodyPart Object")
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
_binding = FragmentWorkoutListTabPageBinding.inflate(inflater, container, false)
binding.apply {
adapter = WorkoutListAdapter()
rv.adapter = adapter
}
val result = viewModel.setList(part)
// Set data whenever tab changes
viewModel.part.observe(viewLifecycleOwner) { _ ->
// val result = viewModel.setList(part)
adapter.addItems(result)
}
return binding.root
}
} viewModel.part.observe(viewLifecycleOwner) { _ ->
adapter.addItems(result)
}
return binding.root
}
}
The problem you are seeing is that in setList you start an asynchronous coroutine on the IO thread to get the list, but then you don't actually wait for that coroutine to run but just return the empty list immediately.
One way to fix that would be to observe a LiveData object containing the list, instead of observing the part. Then, when the asynchronous task is complete
you can post the retrieved data to that LiveData. That would look like this in the view model
class WorkoutListViewModel(application: Application) : AndroidViewModel(application) {
private val _list = MutableLiveData<List<String>>()
val list: LiveData<List<String>>
get() = _list
// "part" does not need to be a member of the view model
// based on the code you shared, but if you wanted it
// to be you could do it like this, then
// call "viewModel.part = part" in "onCreateView". It does not need
// to be LiveData if it's only ever set from the Fragment directly.
//var part: BodyPart = BodyPart.Chest
// calling getList STARTS the async process, but the function
// does not return anything
fun getList(part: BodyPart) {
viewModelScope.launch(Dispatchers.IO){
val result = workoutListRepo.getWorkoutList(part)
_list.postValue(result)
}
}
}
Then in the fragment onCreateView you observe the list, and when the values change you add them to the adapter. If the values may change several times you may need to clear the adapter before adding the items inside the observer.
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
//...
// Set data whenever new data is posted
viewModel.list.observe(viewLifecycleOwner) { result ->
adapter.addItems(result)
}
// Start the async process of retrieving the list, when retrieved
// it will be posted to the live data and trigger the observer
viewModel.getList(part)
return binding.root
}
Note: The documentation currently recommends only inflating views in onCreateView and doing all other setup and initialization in onViewCreated - I kept it how you had it in your question for consistency.
StateFlow is emitting new data after change, but ListAdapter is not being updated/notified, but when configuration is changed(i.e device is rotated from Portrait to Landscape mode) update is occurred:
class TutorialListFragment : Fragment() {
private lateinit var binding: FragmentTutorialListBinding
private val viewModel: ITutorialViewModel by viewModels<TutorialViewModelImpl>()
private lateinit var adapter: TutorialAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentTutorialListBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = binding.recyclerView
adapter = TutorialAdapter()
recyclerView.adapter = adapter
loadData()
}
private fun loadData() {
viewModel
.getTutorialList()
val tutorialList: MutableList<TutorialResponse> = mutableListOf()
viewModel
.tutorialListStateFlow
.onEach { list ->
list.forEach {tutorialResponse->
tutorialList.add(tutorialResponse)
Log.e("TUTORIAL_LIST_FRAG", "$tutorialResponse")
}
adapter.submitList(tutorialList)
}.launchIn(viewLifecycleOwner.lifecycleScope)
}
}
View model is:
class TutorialViewModelImpl: ViewModel(), ITutorialViewModel {
private val mTutorialRepository: ITutorialRepository = TutorialRepositoryImpl()
private val _tutorialListStateFlow = MutableStateFlow<List<TutorialResponse>>(mutableListOf())
override val tutorialListStateFlow: StateFlow<List<TutorialResponse>>
get() = _tutorialListStateFlow.asStateFlow()
init {
mTutorialRepository
.getTutorialListSuccessListener {
viewModelScope
.launch {
_tutorialListStateFlow.emit(it)
Log.e("TUTORIAL_GL_VM", "$it")
}
}
}
override fun getTutorialList() {
// Get list
mTutorialRepository.getTutorialList()
}
}
When I look into Logcat I see this line:
Log.e("TUTORIAL_GL_VM", "$it")
prints all the changes, but no update in ListAdapter.
I assume your data from mTutorialRepository is not a flow ,so you must add .toList() if you want to emit list in stateFlow to get notified
mTutorialRepository.getTutorialListSuccessListener {
viewModelScope.launch {
// here add .toList()
_tutorialListStateFlow.emit(it.toList())
}
}
or if it still does not works, try to change your loadData() like this
private fun loadData() {
// idk what are doing with this ??
viewModel.getTutorialList()
lifecycleScope.launch {
viewModel.tutorialListStateFlow.collect { list ->
adapter.submitList(list)
}
}
}
I am an beginner. My experience when i create an apps, usually i using clean archi and mvvm for the architect, and when using room, it will be use live data also.
But when i create a simple apps don't want to use them, i have trouble with the data which is cannot load directly, please help me. Below is my code
WisataDao.kt
#Dao
interface WisataDao {
#Query("SELECT * from wisata")
fun getAll(): List<WisataEntity>
#Query("SELECT * from wisata WHERE id = :id")
fun getById(id: String): Boolean
#Insert(onConflict = REPLACE)
fun insert(wisata: WisataEntity)
#Delete
fun delete(wisata: WisataEntity)
}
WisataDatabase.kt
#Database(entities = [WisataEntity::class], version = 1)
abstract class WisataDatabase : RoomDatabase() {
abstract fun wisataDao(): WisataDao
companion object {
private var INSTANCE: WisataDatabase? = null
fun getInstance(context: Context): WisataDatabase? {
if (INSTANCE == null) {
synchronized(WisataDatabase::class) {
INSTANCE = Room.databaseBuilder(context.applicationContext,
WisataDatabase::class.java, "wisata.db")
.build()
}
}
return INSTANCE
}
fun destroyInstance() {
INSTANCE = null
}
}
}
FavoriteFrament.kt
class FavoriteFragment : Fragment() {
private lateinit var binding: FragmentFavoriteBinding
private lateinit var database: WisataDatabase
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = FragmentFavoriteBinding.inflate(layoutInflater, container, false)
return binding.root
}
#DelicateCoroutinesApi
#SuppressLint("NotifyDataSetChanged")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
database = WisataDatabase.getInstance(requireContext())!!
binding.rvWisata.layoutManager = LinearLayoutManager(context)
val list = ArrayList<WisataEntity>()
var adapter = HomeAdapter(list)
GlobalScope.launch {
coroutineContext.run {
list.addAll(database.wisataDao().getAll())
adapter = HomeAdapter(list)
adapter.notifyDataSetChanged()
}
}
binding.rvWisata.adapter = adapter
adapter.setOnItemClickCallback(object : HomeAdapter.OnItemClickCallback {
override fun onItemClicked(data: WisataEntity) {
val intent = Intent(context, DetailActivity::class.java)
intent.putExtra("data", data)
startActivity(intent)
}
})
}
}
Please help me guys
The problem is you are initializing the adapter twice but only assigning it once.
//Here 1st initialization
var adapter = HomeAdapter(list)
GlobalScope.launch {
coroutineContext.run {
list.addAll(database.wisataDao().getAll())
//Here 2nd initialization
//but no assignment now the RecyclerView has an adapter
//that doesn't exist anymore because you overwrote it
adapter = HomeAdapter(list)
adapter.notifyDataSetChanged()
}
}
//Here 1 assignment
//You assign the 1st initialized adapter
//that has no data.
binding.rvWisata.adapter = adapter
What you want to do is add a function to the Adapter that accepts a List and pass in the new List to the Adapter and notify the changes.
GlobalScope.launch {
coroutineContext.run {
list.addAll(database.wisataDao().getAll())
//Here 2nd initialization
adapter.setList(list)
adapter.notifyDataSetChanged()
}
}
I looked for many articles and tried to understand how Live Data is observe changes when MVVM architecture is used.
I have a Fragment A, ViewModel and Repository class.
ViewModel is initiated in onCreateView() method of the fragment.
Api call is initiated just after that in onCreateView() method of fragment.
Data from the Server is observed in onViewCreated method of the fragment.
For the first, it is running perfectly fine. But When I update the user name from another Fragment B and come back to Fragment A.
Api is called again in onResume() method of Fragment A to update UI. But here my Live Data is not observed again and UI is not updated
I didn't understand what I am doing wrong? Why observer is not triggering second time?
Below is the code
class FragmentA : Fragment(){
private lateinit var dealerHomeViewModel: DealerHomeViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_home_dealers, container, false)
val dealerHomeFactory = DealerHomeFactory(token!!)
dealerHomeViewModel = ViewModelProvider(this,dealerHomeFactory).get(DealerHomeViewModel::class.java)
dealerHomeViewModel.getDealerHomeData()
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dealerHomeViewModel.dealerInfoLiveData.observe(viewLifecycleOwner, androidx.lifecycle.Observer {dealerInfo ->
// Update UI
tvDealerName.text = dealerInfo.name
})
}
override fun onResume() {
super.onResume()
dealerHomeViewModel.getDealerHomeData()
}
}
//=========================== VIEW MODEL ===================================//
class DealerHomeViewModel(val token:String) : ViewModel() {
var dealerInfoLiveData:LiveData<DealerInfo>
init {
dealerInfoLiveData = MutableLiveData()
}
fun getDealerHomeData(){
dealerInfoLiveData = DealerHomeRepo().getDealerHomePageInfo(token)
}
}
//======================== REPOSITORY ================================//
class DealerHomeRepo {
fun getDealerHomePageInfo(token:String):LiveData<DealerInfo>{
val responseLiveData:MutableLiveData<DealerInfo> = MutableLiveData()
val apiCall: ApiCall? = RetrofitInstance.getRetrofit()?.create(ApiCall::class.java)
val dealerInfo: Call<DealerInfo>? = apiCall?.getDealerInfo(Constants.BEARER+" "+token,Constants.XML_HTTP)
dealerInfo?.enqueue(object : Callback<DealerInfo>{
override fun onFailure(call: Call<DealerInfo>, t: Throwable) {
Log.d(Constants.TAG,t.toString())
}
override fun onResponse(call: Call<DealerInfo>, response: Response<DealerInfo>) {
if(response.isSuccessful){
when(response.body()?.status){
Constants.SUCCESS -> {
responseLiveData.value = response.body()
}
Constants.FAIL -> {
}
}
}
}
})
return responseLiveData
}
}
I think your problem is that you are generating a NEW mutableLiveData each time you use your getDealerHomePageInfo(token:String method.
First time you call getDealerHomePageInfo(token:String) you generate a MutableLiveData and after on onViewCreated you observe it, it has a value.
In onResume, you call again getDealerHomePageInfo(token:String) that generates a NEW MutableLiveData so your observer is pointing to the OLD one.
What would solve your problem is to pass the reference of your viewModel to your repository so it updates the MutableLiveData with each new value, not generate a new one each time.
Edited Answer:
I would do something like this for ViewModel:
class DealerHomeViewModel(val token:String) : ViewModel() {
private val _dealerInfoLiveData:MutableLiveData<DealerInfo> = MutableLiveData()
val dealerInfoLiveData:LiveData = _dealerInfoLiveData
fun getDealerHomeData(){
DealerHomeRepo().getDealerHomePageInfo(token, _dealerInfoLiveData)
}
}
And this for the DealerHomeRemo
class DealerHomeRepo{
fun getDealerHomePageInfo(token:String, liveData: MutableLiveData<DealerInfo>){
val apiCall: ApiCall? = RetrofitInstance.getRetrofit()?.create(ApiCall::class.java)
val dealerInfo: Call<DealerInfo>? = apiCall?.getDealerInfo(Constants.BEARER+" "+token,Constants.XML_HTTP)
dealerInfo?.enqueue(object : Callback<DealerInfo>{
override fun onFailure(call: Call<DealerInfo>, t: Throwable) {
Log.d(Constants.TAG,t.toString())
}
override fun onResponse(call: Call<DealerInfo>, response: Response<DealerInfo>) {
if(response.isSuccessful){
when(response.body()?.status){
Constants.SUCCESS -> {
liveData.value = response.body()
}
Constants.FAIL -> {
}
}
}
}
})
}
For Observers, use the LiveData as before:
dealerHomeViewModel.dealerInfoLiveData.observe(viewLifecycleOwner, androidx.lifecycle.Observer {dealerInfo ->
// Update UI
tvDealerName.text = dealerInfo.name
})
I'm having trouble with deleting data from my Firestore collection when the user clicks a delete button in a recyclerview. I can delete it from the recyclerview without any problems, but I'm having trouble to make the connection between the adapter, the viewmodel and the repository that handles Firestore operations.
In my adapter, I remove the item the user clicked on from the recyclerview:
class ArticleAdapter : RecyclerView.Adapter<ArticleAdapter.ViewHolder>() {
var data = mutableListOf<Product>()
set(value) {
field = value
notifyDataSetChanged()
}
override fun getItemCount() = data.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position]
holder.bind(item)
holder.deleteButton.setOnClickListener {
data.removeAt(position)
notifyDataSetChanged()
}
} ...
The recyclerview is populated after a query to the Firestore collection in my viewmodel:
class ArticleViewModel(private val repository: ProductRepository) : ViewModel() {
var savedProducts: MutableLiveData<MutableList<Product>> = MutableLiveData<MutableList<Product>>()
init {
savedProducts = getProducts()
}
fun getProducts(): MutableLiveData<MutableList<Product>> {
repository.getProducts().addSnapshotListener(EventListener<QuerySnapshot> { value, e ->
if (e != null) {
savedProducts.value = null
return#EventListener
}
val savedProductsList: MutableList<Product> = mutableListOf()
for (doc in value!!) {
val item = doc.toObject(Product::class.java)
item.id = doc.id
savedProductsList.add(item)
}
savedProductsList.sortBy { i -> i.productName }
savedProducts.value = savedProductsList
})
return savedProducts
} }
In my Fragment, I'm then observing any changes that might happen to savedProducts:
class ArticleOverviewFragment : Fragment(), KodeinAware {
override val kodein: Kodein by kodein()
private val factory: ArticleViewModelFactory by instance()
private lateinit var viewModel: ArticleViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentArticleOverviewBinding =
DataBindingUtil.inflate(inflater, R.layout.fragment_article_overview, container, false)
viewModel = ViewModelProviders.of(this, factory).get(ArticleViewModel::class.java)
binding.viewModel = viewModel
val adapter = ArticleAdapter()
binding.recyclerViewGoods.adapter = adapter
viewModel.savedProducts.observe(viewLifecycleOwner, Observer {
it?.let {
adapter.data = it
}
})
...
} }
Is there a way that I can observe/save the ID of the deleted item in my adapter and "transfer" that ID from the adapter to the UI where I call a function declared in the viewmodel whenever that field holding the ID is populated? Or should I directly access the viewmodel from the adapter? Somehow, that feels kinda wrong...
Declare one local variable
var removedPosition : Int ? = null
then update this variable into onClick event of deleteButton
holder.deleteButton.setOnClickListener {
data.removeAt(position)
removedPosition = position
notifyDataSetChanged()
}
Please make one method in Adapter (ArticleAdapter)
fun getRemoveItemPosition() : Int {
var position = removedPosition
return position;
}
which return the position of removed Item and call that method in UI(ArticleOverviewFragment) where you will require to get position of removed item from recyclerview
var removedItemPosition = adapter.getRemoveItemPosition()
Now you will get value of remove item Position using variable called removedItemPosition
So You can get Position of removed Item in UI where you can call a function declared in the viewmodel (ArticleViewModel) to delete particular item in firestore collection.