I have two LiveData, aMVoice1, and aMVoice2.
I hope to check if they are equal.
I know I need to use observe to get the value of a LiveData.
so I think isEqual = (mDetailViewModel.aMVoice1.value==mDetailViewMode2.aMVoice1.value ) is wrong.
But I think there are some problems with fun observeVoice(), how can I fix it?
class FragmentDetail : Fragment() {
private lateinit var binding: LayoutDetailBinding
private val mDetailViewModel by lazy {
...
}
var isEqual=false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
...
binding.lifecycleOwner = this.viewLifecycleOwner
binding.aDetailViewModel=mDetailViewModel
isEqual = (mDetailViewModel.aMVoice1.value==mDetailViewMode2.aMVoice1.value ) // I don't think it's correct.
observeVoice()
return binding.root
}
fun observeVoice() {
mDetailViewModel.aMVoice1.observe(viewLifecycleOwner){value1->
isEqual = (value1==mDetailViewModel.aMVoice2.value) // mDetailViewModel.aMVoice2.value maybe null
}
}
}
class DetailViewModel(private val mDBVoiceRepository: DBVoiceRepository, private val voiceId1:Int,private val voiceId2:Int) : ViewModel() {
val aMVoice1=mDBVoiceRepository.getVoiceById(voiceId1)
val aMVoice2=mDBVoiceRepository.getVoiceById(voiceId2)
}
class DBVoiceRepository private constructor(private val mDBVoiceDao: DBVoiceDao){
fun getVoiceById(id:Int)=mDBVoiceDao.getVoiceById(id)
}
#Dao
interface DBVoiceDao{
#Query("SELECT * FROM voice_table where id=:id")
fun getVoiceById(id:Int):LiveData<MVoice>
}
data class MVoice(
#PrimaryKey (autoGenerate = true) #ColumnInfo(name = "id") var id: Int = 0,
var name: String = "",
var path: String = ""
)
Added Content
Is it Ok for the following code?
fun observeVoice() {
mDetailViewModel.aMVoice1.observe(viewLifecycleOwner){value1->
mDetailViewModel.aMVoice2.observe(viewLifecycleOwner){value2->
isEqual = (value1==value2)
}
}
}
According to the official documents, the best way to achieve a solution for such cases is to use MediatorLiveData as a LiveData merger. Using it, you can check the equality of values when a new value is posted on either of LiveDatas:
class DetailViewModel(...) : ViewModel() {
val areMVoicesEqual = MediatorLiveData<Boolean>().apply {
addSource(aMVoice1) { postValue(it == aMVoice2.value) }
addSource(aMVoice2) { postValue(it == aMVoice1.value) }
}
}
Then:
fun observeVoice() {
mDetailViewModel.areMVoicesEqual.observe(viewLifecycleOwner){ equality ->
// do whatever you want with `equality`
}
}
Note that Added Content snippet you mentioned is not correct. In fact, in this case, every time a value is being observed on aMVoice1, a new Observer starts to observe on aMVoice2 which is not right.
Related
I'm using the Epoxy library on Android.
What I'm curious about is why the parameter of the lambda expression doesn't get an error when the type doesn't match.
The listener is a lambda expression that takes an Int type as a parameter.
But listener(addDetailClicked) works normally.
Shouldn't it be listener(Int)? or listener({ i -> addDetailClicked(i) }).
Actually, I don't know why it works even after I write the code.
How is this possible?
Model
#EpoxyModelClass(layout = R.layout.item_routine)
abstract class EpoxyRoutineModel() : EpoxyModelWithHolder<EpoxyRoutineModel.Holder>() {
#EpoxyAttribute
var workout: String = "see"
#EpoxyAttribute
var curPos: Int = 0
#EpoxyAttribute
lateinit var listener: (Int) -> Unit // this
override fun bind(holder: Holder) {
holder.workout.text = workout
holder.add_btn.setOnClickListener {
listener(curPos)
}
}
}
Controller
class RoutineItemController(
private val addDetailClicked: (Int) -> Unit)
: EpoxyController() {
private var routineItem : List<RoutineItem>? = emptyList()
override fun buildModels() {
var i:Int =0
routineItem?.forEach {
when(it) {
is RoutineItem.RoutineModel ->
EpoxyRoutineModel_()
.id(i++)
.curPos(i++)
.workout("d")
.listener(addDetailClicked) // why? listener(Int) or listener({ i -> addDetailClicked(i) })
.addTo(this)
}
}
}
}
Fragment
class WriteRoutineFragment : Fragment() {
private var _binding : FragmentWriteRoutineBinding? = null
private val binding get() = _binding!!
private lateinit var epoxyController : RoutineItemController
private val vm : WriteRoutineViewModel by viewModels { WriteRoutineViewModelFactory() }
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
_binding = FragmentWriteRoutineBinding.inflate(inflater, container, false)
epoxyController = RoutineItemController(::addDetail)
binding.rv.adapter = epoxyController.adapter
binding.rv.itemAnimator = null
return binding.root
}
private fun addDetail(pos: Int) {
vm.addDetail2(pos)
}
}
I believe you missed the fact that EpoxyRoutineModel_ contains setters for data types found in EpoxyRoutineModel. For example, EpoxyRoutineModel.curPos is of type Int, so EpoxyRoutineModel_.curPos() is a function declared as:
fun curPos(Int): EpoxyRoutineModel_
(or similar)
Similarly, EpoxyRoutineModel.listener is of type (Int) -> Unit, so EpoxyRoutineModel_.listener() is declared as:
fun listener((Int) -> Unit): EpoxyRoutineModel_
So listener() is a function that receives another function (which itself receives Int). So we can provide addDetailClicked there.
I am working on a android project with MVVM structure. I want to use LiveData as recommended.
In the samples there are always just simple objecttypes e.g. String.
But I want to put an more complex/nested objecttype into LiveData.
For example an objectstructure like this:
class ClassA {
private var testVarB = ClassB()
fun getTestVarB(): ClassB {
return this.testVarB
}
fun setTestVarB(classB: ClassB) {
this.testVarB = classB
}
fun setTxt(str: String) {
this.testVarB.getTestVarC().setStr(str)
}
}
class ClassB {
private var testVarC = ClassC()
fun getTestVarC(): ClassC {
return this.testVarC
}
fun setTestVarB(classC: ClassC) {
this.testVarC = classC
}
}
class ClassC {
private var str: String = "class C"
fun getStr(): String {
return this.str
}
fun setStr(str: String) {
if (str != this.str) {
this.str = str
}
}
}
and my ViewModel looks like this:
class MyViewModel : ViewModel() {
var classAObj= ClassA()
private var _obj: MutableLiveData<ClassA> = MutableLiveData()
val myLiveData: LiveData<ClassA> = _obj
init {
_obj.value = classAObj
}
}
and the LiveDataObject is observed in the fragment:
class FirstFragment : Fragment() {
private var viewModel = MyViewModel()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.myLiveData.observe(
requireActivity(),
Observer<ClassA>() {
// should get fired!
Log.d("TAG", "update view")
})
}
}
So if the variable str of ClassC changes the callback should get executed.
I am looking for a smart and simple solution.
I just found this similar post:
LiveData update on object field change
This example got a depth of 1. But I am looking for a solution with arbitrarily depth.
The fact that I can not find a sample of the solution for my problem makes me suspicious.
So I guess my approach is kind of wrong or bad practice anyway.
Maybe I should look for a way breaking things down and observe just simple objects.
Has anyone a solution or opinion to this?
Thanks for your help!
Here is the solution i have worked out:
I am using the PropertyAwareMutableLiveData class from here: LiveData update on object field change
class PropertyAwareMutableLiveData<T : BaseObservable> : MutableLiveData<T>() {
private val callback = object : Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
value = value
}
}
override fun setValue(value: T?) {
super.setValue(value)
value?.addOnPropertyChangedCallback(callback)
}
}
Based on this I extended the model with an iterface/abstract class.
abstract class InterfaceObservable : BaseObservable() {
open fun setNewString(s: String) {
notifyPropertyChanged(BR.str)
}
}
class ClassA : InterfaceObservable() {
private var testVarB = ClassB()
fun getTestVarB(): ClassB {
return this.testVarB
}
fun setTestVarB(classB: ClassB) {
this.testVarB = classB
}
override fun setNewString(s: String) {
super.setNewString(s)
this.testVarB.setNewString(s)
}
}
class ClassB {
private var testVarC = ClassC()
fun getTestVarC(): ClassC {
return this.testVarC
}
fun setTestVarB(classC: ClassC) {
this.testVarC = classC
}
fun setNewString(s: String) {
this.testVarC.setStr(s)
}
}
class ClassC : BaseObservable() {
#Bindable
private var str: String = "class C"
fun getStr(): String {
return this.str
}
fun setStr(str: String) {
if (str != this.str) {
this.str = str
}
}
}
In my ViewModel I use the PropertyAwareMutableLiveData class.
class MyViewModel() : ViewModel() {
var classAObj: ClassA = ClassA()
val myLiveData = PropertyAwareMutableLiveData<ClassA>()
init {
myLiveData.value = classAObj
}
}
In the Fragment I can observe the LiveData object. If ClassC.str changes the observer will get notified and can change the UI.
class MyFragment : Fragment() {
private lateinit var viewModel: MyViewModel
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.myLiveData.observe(
viewLifecycleOwner,
Observer<ClassA> {
Log.d("TAG", "change your UI here")
})
}
}
Every property which is relevant in your UI, should only be changeable over the interface given by the class InterfaceObservable.
Thats the reason why this is not a perfect solution.
But maybe it is reasonable in your case.
The issue is from the way you create your ViewModel. You can't directly instantiate it. If you use fragment-ktx artifact you can do like that :
private val model: SharedViewModel by activityViewModels()
The fragment has his own lifecycle. So you should replace requireActivity() by viewLifeCycleOwner
viewModel.myLiveData.observe(
viewLifeCycleOwner,
Observer<ClassA>() {
// should get fired!
Log.d("TAG", "update view")
})
More information here: https://developer.android.com/topic/libraries/architecture/viewmodel#sharing
i have a problem with my code and i can't solve it .
let just a brief of what i'm going to do : i have a address in the ChooseFragment and there is a edit button for editting the address . ok so far good . the edit click is pass new address to the DatabaseRoom and the address text would be changed . but this is happens just for first time . the secound time the address not changed !! . i know that the insert method work and send the new data to the database room but when i want get it with my query (SELECT * FROM ... ) just show the first parameter and not replace with new value . what is wrong with my codes ?
this is my table :
#Entity (tableName = "addresstable")
data class AddressTb(
#PrimaryKey(autoGenerate = true)```
val id : Int? ,
var address: String)```
this is my database :
#Database(entities = [RoomTables::class , AddressTb::class], version = 1, exportSchema = false)
abstract class DataBaseRoom : RoomDatabase() {
abstract fun GetDao(): DaoCart
companion object {
#Volatile
private var instance: DataBaseRoom? = null
private val lock = Any()
operator fun invoke(context: Context) = instance
?: synchronized(lock) {
instance
?: makeDatabase(
context
).also {
instance = it
}
}
private fun makeDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
DataBaseRoom::class.java,
"name"
).build()
}
}```
this is my Dao :
```//address table dao
#Query("SELECT * FROM addresstable")
fun getalladress () : LiveData<AddressTb>
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertaddress (model : AddressTb)
#Query("DELETE FROM addresstable")
suspend fun deleteaddress ()
this is my Repository :
fun getalladdress() = db.GetDao().getalladress()
suspend fun deleteaddress() = db.GetDao().deleteaddress()
suspend fun insertaddress(model : AddressTb) = db.GetDao().insertaddress(model)
this is my Viewmodel :
fun getaddress() = repository.getalladdress()
fun deleteaddres() = CoroutineScope(Dispatchers.Default).launch {
repository.deleteaddress()
}
fun insertaddress(model : AddressTb) = CoroutineScope(Dispatchers.Default).launch {
repository.insertaddress(model)
this is my fragment where i fetch the new insert :
class ChosseAddress : Fragment() {
lateinit var viewModelRoom: ViewModelRoom
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val vl = inflater.inflate(R.layout.choose_address_layout, container, false)
val database = DataBaseRoom(requireContext())
val repository = RepositoryCart(database)
val factoryRoom = FactoryRoom(repository)
viewModelRoom =
ViewModelProvider(ViewModelStoreOwner { ViewModelStore() }, factoryRoom).get(
ViewModelRoom::class.java
)
viewModelRoom.getaddress().observe(viewLifecycleOwner, Observer {
try {
vl.txt_address.text = it.address
} catch (ex: Exception) {
null
}
})
val animsec: Animation =
AnimationUtils.loadAnimation(vl.context, R.anim.anim_for_btn_zoom_out)
vl.button_back_choose_address.setOnClickListener {
it.startAnimation(animsec)
childFragmentManager.beginTransaction()
.replace(R.id.choose_address_container, HamburgerFragment())
.commit()
}
vl.edit_address.setOnClickListener {
val mycustomview =
LayoutInflater.from(context).inflate(R.layout.alertfialog_costume, null)
val dialogtext = LayoutInflater.from(context).inflate(R.layout.edit_alert_txt, null)
val mBuilder = AlertDialog.Builder(context)
.setView(mycustomview)
.setCustomTitle(dialogtext)
val show = mBuilder.show()
mycustomview.edit_manually.setOnClickListener {
show.dismiss()
childFragmentManager.beginTransaction()
.replace(R.id.choose_address_container, ManuallyAddressFragment())
.commit()
}
}
return vl
}
}```
and this is where i insert data to database :
class ManuallyAddressFragment : Fragment() {
lateinit var viewmodel: ViewModelRoom
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val layoutview = inflater.inflate(R.layout.manually_address_fragment, container, false)
val database = DataBaseRoom(layoutview.context)
val repos = RepositoryCart(database)
val factory = FactoryRoom(repos)
viewmodel = ViewModelProvider(ViewModelStoreOwner { viewModelStore },
factory).get(ViewModelRoom::class.java)
val btncancle: Button = layoutview.btn_cancle
val btnsubmit: Button = layoutview.btn_submit_address
btnsubmit.setOnClickListener {
val edittext = layoutview.edit_address_manually
if (edittext.text.toString().isEmpty()) {
Toast.makeText(context, R.string.submit_btn, Toast.LENGTH_SHORT).show()
} else {
val insert = (AddressTb( null , edittext.text.toString()))
viewmodel.insertaddress(insert)
childFragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.anim_fragment_manually,
R.anim.anim_fragment_chooseaddress
)
.replace(R.id.manually_container, ChosseAddress())
.commit()
Toast.makeText(context, "آدرس شما با موفقیت ثبت شد", Toast.LENGTH_SHORT).show()
}
}
btncancle.setOnClickListener {
childFragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.anim_fragment_manually,
R.anim.anim_fragment_chooseaddress
)
.replace(R.id.manually_container, ChosseAddress())
.commit()
}
return layoutview
}
}```
i tried so things also use update metohd but the database just back the first parameter and i want the new insert value ...
To update the value use the same id, as in your code I see you try to insert new object AddressTb(null , edittext.text.toString()) you are passing null value for id, pass the same id from the AddressTb object that you wish to update which you get from getalladdress.
For starters, if you're getting all the addresses, it should look like this:
#Query("SELECT * FROM addresstable")
fun getalladress () : LiveData<List<AddressTb>>
To select a single address, use something like this:
#Query("SELECT * FROM addresstable WHERE id= :id")
fun findAddressById(id: Long) : AddressTb
TLDR: How could I properly implement an MVVM architecture with LiveData?
I have a fragment class that observe a viewModel exposed livedata:
viewModel.loginResultLiveData.observe
class LoginFragment : Fragment() {
private lateinit var binding: FragmentLoginBinding
private val viewModel by fragmentScopedViewModel { injector.loginViewModel }
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentLoginBinding.inflate(inflater, container, false)
val username = binding.loginInputField.toString()
val password = binding.passwordInputField.toString()
binding.loginSignInButton.setOnClickListener { viewModel.login(
username,
password
) }
viewModel.loginResultLiveData.observe(viewLifecycleOwner){
when(it){
is LoginResult.Success -> doSmth()
}
}
return binding.root
}
}
View model class simply ask for a mapped livedata object.
class LoginViewModel #Inject internal constructor(
private val loginUseCase: LoginUseCase
) : ViewModel() {
lateinit var loginResultLiveData: MutableLiveData<LoginResult>
fun login(username: String, password: String) {
loginResultLiveData = loginUseCase.login(username, password)
}
}
Model uses use case, to map a result from the original format and also would map errors:
class LoginUseCase #Inject internal constructor(
private val authRepository: AuthEmailRepository
) {
var loginResultLiveData = MutableLiveData<LoginResult>()
fun login(userName: String, password: String): MutableLiveData<LoginResult> {
authRepository.login(userName, password)
.addOnCompleteListener {
if (it.isSuccessful) {
loginResultLiveData.postValue(LoginResult.Success)
} else {
loginResultLiveData.postValue(LoginResult.Fail(it.exception.toString()))
}
}
return loginResultLiveData
}
}
The problem is that only after loginSignInButtonis clicked, the model creates a liveData object. But I'm starting to observe this object immediately after onClickListener is set. Also each time a button is clicked, this would create a new instance of viewModel.loginResultLiveData, which doesn’t make sense.
binding.loginSignInButton.setOnClickListener { viewModel.login(
username,
password
) }
viewModel.loginResultLiveData.observe(viewLifecycleOwner){
when(it){
is LoginResult.Success -> doSmth()
}
}
How could I properly implement MVVM architecture with LiveData in this case?
I could also move logic I now have in LoginUseCaseto ModelView and then have something like this, which avoids the problem described before. But then I cannot delegate mapping/error handling to use case.
class LoginViewModel #Inject internal constructor(
private val loginUseCase: LoginUseCase
) : ViewModel() {
val loginResult: MutableLiveData<LoginResult> = MutableLiveData()
fun login(username: String, password: String) = loginUseCase.login(username, password)
.addOnCompleteListener {
if (it.isSuccessful) {
loginResult.postValue(LoginResult.Success)
} else {
loginResult.postValue(LoginResult.Fail(it.exception.toString()))
}
}
}
You are trying to observe a mutable LiveData that is only initialized after the onClickListener so you won't get it to work, also you have a lateinit property that is only initialized if you call the login method which will throw an exception.
To solve your problem you can have a MediatorLiveData that will observe your other live data and pass the result back to your fragment observer.
You can try the following:
class LoginViewModel #Inject internal constructor(
private val loginUseCase: LoginUseCase
) : ViewModel() {
private var _loginResultLiveData = MediatorLiveData<LoginResult>()
val loginResultLiveData: LiveData<LoginResult> = _loginResultLiveData
fun login(username: String, password: String) {
val loginUseCaseLiveData = loginUseCase.login(username, password)
_loginResultLiveData.addSource(loginUseCaseLiveData) {
_loginResultLiveData.value = it
}
}
}
I am trying, without success, to solve a problem for days. I would like to update my recyclerView whenever the records of a particular model change in the Database (DB Room). I use ViewModel to handle the model data and the list of records are stored in LiveData.
Database
#Database(entities = arrayOf(Additive::class), version = ElementDatabase.DB_VERSION, exportSchema = false)
abstract class ElementDatabase() : RoomDatabase() {
companion object {
const val DB_NAME : String = "element_db"
const val DB_VERSION : Int = 1
fun get(appContext : Context) : ElementDatabase {
return Room.databaseBuilder(appContext, ElementDatabase::class.java, DB_NAME).build()
}
}
abstract fun additivesModels() : AdditiveDao
}
Model
#Entity
class Additive {
#PrimaryKey #ColumnInfo(name = "id")
var number : String = ""
var dangerousness : Int = 0
var description : String = ""
var names : String = ""
var notes : String = ""
var risks : String = ""
var advice : String = ""
}
Dao
#Dao
interface AdditiveDao {
#Query("SELECT * FROM Additive")
fun getAllAdditives() : LiveData<List<Additive>>
#Query("SELECT * FROM Additive WHERE id = :arg0")
fun getAdditiveById(id : String) : Additive
#Query("DELETE FROM Additive")
fun deleteAll()
#Insert(onConflict = REPLACE)
fun insert(additive: Additive)
#Update
fun update(additive: Additive)
#Delete
fun delete(additive: Additive)
}
ViewModel
class AdditiveViewModel(application: Application) : AndroidViewModel(application) {
private var elementDatabase : ElementDatabase
private val additivesModels : LiveData<List<Additive>>
init {
this.elementDatabase = ElementDatabase.get(appContext = getApplication())
this.additivesModels = this.elementDatabase.additivesModels().getAllAdditives()
}
fun getAdditivesList() : LiveData<List<Additive>> {
return this.additivesModels
}
fun deleteItem(additive : Additive) {
DeleteAsyncTask(this.elementDatabase).execute(additive)
}
private class DeleteAsyncTask internal constructor(private val db: ElementDatabase) : AsyncTask<Additive, Void, Void>() {
override fun doInBackground(vararg params: Additive): Void? {
db.additivesModels().delete(params[0])
return null
}
}
}
Fragment
class AdditivesFragment : LifecycleFragment() {
private var viewModel : AdditiveViewModel? = null
private var adapter : AdditivesAdapter? = null
companion object {
fun newInstance() : AdditivesFragment {
val f = AdditivesFragment()
val args = Bundle()
f.arguments = args
return f
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater?.inflate(R.layout.fragment_additives, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
this.adapter = AdditivesAdapter(ArrayList<Additive>())
this.additives_list.layoutManager = GridLayoutManager(this.context, 2, GridLayoutManager.VERTICAL, false)
this.additives_list.adapter = this.adapter
this.viewModel = ViewModelProviders.of(this).get(AdditiveViewModel::class.java)
this.viewModel?.getAdditivesList()?.observe(this, Observer<List<Additive>> { additivesList ->
if(additivesList != null) {
this.adapter?.addItems(additivesList)
}
})
super.onActivityCreated(savedInstanceState)
}
}
Now, my question is why is the observer called only once (at the start of the fragment) and then is not called back again? How can I keep the observer constantly listening to the changes in the DB (insert, update, delete) so that my recyclerView instantly can be updated? Thanks a lot for any suggestion.
This is where you made a mistake:
this.viewModel = ViewModelProviders.of(this).get(AdditiveViewModel::class.java)
you are passing this while you are inside the fragment which is pretty disturbing for some people cause it is not a syntax error but logical. You have to pass activity!! instead, it will be like this:
this.viewModel = ViewModelProviders.of(activity!!).get(AdditiveViewModel::class.java)
UPDATE:
Pass viewLifecycleOwner while being inside fragment while observing the Data
mainViewModel.data(viewLifecycleOwner, Observer{})
If you're using fragmentKtx, you can init viewModel this way:
private val viewModel by viewModels<MainViewModel>()
If You've viewModelFactory:
private val viewModel by viewModels<MainViewModel>{
viewModelFactory
}
with this approach you don't need to call:
// you can omit this statement completely
viewModel = ViewModelProviders.of(this).get(AdditiveViewModel::class.java)
You can simply just start observing the data..