android mvvm and livedata: list not observed in activity - mvvm

I am developing an android app that has an activity with a recyclerview and an add button. When add button is clicked, a FragmentDialog is launched. the user enters the name of the book which will be stored in a greendao database. The list of books is displayed in the recyclerview.
I am using mvvm and livedata . the problem is that after adding the book in DialogFragment, the list is not updated, although the list is wrapped in livedata which is observed in activity.
DialogFragment:
// repository
val repository = Injection.provideRepository()
// viewmodel
val factory = CreateBookViewModelFactory(repository)
val viewModel =
ViewModelProvider(this, factory).get(BookViewModel::class.java)
builder.setView(binding.root)
// Add action buttons
.setPositiveButton(R.string.add,
DialogInterface.OnClickListener { dialog, id ->
// create book
book = Book(null, binding.name)
viewModel.onAddBook(book)
})
.setNegativeButton(R.string.cancel,
DialogInterface.OnClickListener { dialog, id ->
getDialog()?.cancel()
})
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
viewModel:
class BookViewModel(val repository: Repository) : ViewModel() {
// Create a LiveData
val _books = MutableLiveData<List<Book>>()
val books = repository.books
fun init() :MutableLiveData<List<Book>> {
_books.postValue(books)
return _books
}
}
activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_books)
binding.setLifecycleOwner(this)
binding.recyclerview.adapter = adapter
binding.recyclerview.setHasFixedSize(false)
// repository
val repository = Injection.provideRepository()
val factory = CreateBookViewModelFactory(repository)
val viewModel =
ViewModelProvider(this, factory).get(BookViewModel::class.java)
viewModel.init().observe(this, Observer {
it?.let {
adapter.submitList(Mapper.mapToBookListDTO(it))
}
})
binding.addReturn.setOnClickListener { view ->
val createFragment: DialogFragment = BookDialog()
createFragment.show(supportFragmentManager, "new Book")
}
}
When I add a new book and click the add button, the list is not updated. But when I reopen the activity the list of books get updated, it means the list is not observed for changes.
I am using the same viewModel for the activity and the DialogFragment. I also tried using seperate viewmodels for each, but same result.
Thanks a lot for your help!

Related

How to add paramaters to new livedata builder in the view model

I'm trying to convert my view models to use the new livedata builder from live data2.0
In all the examples i've seen when you use this new builder pattern they omit how to set parameters take this view model for example, userId is not defined
class UserViewModel : ViewModel() {
private val repository = UserRepository()
val user: LiveData<Response<User>> = liveData {
val data = repository.getUser(userId) // loadUser is a suspend function.
emit(data)
}
}
looks pretty clean on concise, but where do I set the userId its not a function.
before i would expose a function that took a paramater then would update the livedata property.
I was thinking of something like this
class UserViewModel : ViewModel() {
private val repository = UserRepository()
var userId : String? = null
val user: LiveData<Response<User>> = liveData {
val data = repository.getUser(userId) // loadUser is a suspend function.
emit(data)
}
}
and the fragment can set the id, but what if I change the id and want to make another network call?

How to update activity views from a fragment recyclerView

I have MainActivity which has AppBar (with a "cart" TextView) and FrameLayout (with Fragment which contain a RecyclerView). This RecyclerView has a Button to increase Cart Value count. And I want that count to update Cart value in MainActivity.
I found this one but without full explanation "Updating views of Activity or Fragment from a RecyclerView adapter".
No better solution than this ViewModel
First of all add this dependency in build.gradle : implementation 'android.arch.lifecycle:extensions:1.1.1'
Your ViewModel class will look like this :
class MyViewModel: ViewModel() {
val mCartInfo: MutableLiveData<Int> = MutableLiveData()
private var cnt = 0
fun addToCartCounter() {
//TODO Apply your logic here
mCartInfo.value = cnt++
}
}
What is LiveData? See here
After that you've to initialize your ViewModel and observe the changed data, all these things are going to be done in your AppBar's Activity :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myViewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
//These will observe any changes happen in ViewModel's MutableLiveData(it'll directly jump inside Observer)
//Observer is main important thing
myViewModel.mCartInfo.observe(this, Observer {
//tvCartCounter is Toolbar's TextView
tvCartCounter.text = it.toString()
})
}
}
Get ViewModel in your Fragment and your button click which calls a ViewModel's method:
class BlankFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_blank, container, false)
//Get your Activity's ViewModel
val myViewModel = ViewModelProviders.of(requireActivity()).get(MyViewModel::class.java)
//Button click
rootView.btnAddToCart.setOnClickListener {
//It'll call ViewModel's method
myViewModel.addToCartCounter()
}
return rootView
}
}
Whenever addToCartCounter() calls on your button click it'll add value in MutableLiveData soon after it'll triggered your observer.

Native Dialog for location setting on Flutter

Is there a way to implement a Dialog for Location setting like the image below which gets triggered when app requires GPS location and doesn't find. Hitting OK will right away turn on the system GPS. This seems more convenient for users instead of taking them to location and manually turn on.
Is it possible to implement such thing in Flutter?
Expanded View of dialog:
Credits to Rajesh, as answered here. The plugin lets you add this native dialog for a quick location setting.
The implementation is quite simple as this:
import 'package:location/location.dart';
var location = Location();
Future _checkGps() async {
if(!await location.serviceEnabled()){
location.requestService();
}
}
#override
void initState() {
super.initState();
_checkGps();
}
In Kotlin, try this code:
class HomeActivity : AppCompatActivity() {
private val requestLocation = 199
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableLoc()
}
private fun enableLoc() {
val mLocationRequest = LocationRequest.create()
mLocationRequest.interval = 10000
mLocationRequest.fastestInterval = 5000
// mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest)
val client = LocationServices.getSettingsClient(this)
val task =
client.checkLocationSettings(builder.build())
task.addOnSuccessListener(this) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
}
task.addOnFailureListener(this) { e ->
if (e is ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
e.startResolutionForResult(
this,
requestLocation
)
} catch (sendEx: SendIntentException) {
// Ignore the error.
}
}
}
}
}

How to signal/notify super-controller of change in sub-controller?

In JavaFX, how do you model the following:
I show a List of Customers in a Scene. On the left side there is a table on the right side (contentPane) the currently select customer's details are shown.
(Relevant part of) Main-Controller:
#jfxf.FXML
protected def newButtonPressed(): Unit =
{
contentPane.getChildren.clear
contentPane.getChildren.add(FXMLLoader.load(GUILoader.load("customers/form.fxml")))
}
There is a Button to add a new Customer. Upon clicking this button instead of opening a Popup, I replace the "details"-part of the scene and add a form there.
Now for this form (designed - like the rest of the GUI - in the SceneBuilder and saved as .fxml) I use another controller.
class Form extends Main with jfxf.Initializable
{
#jfxf.FXML
private var foreNameTextField: jfxsc.TextField = _
#jfxf.FXML
private var lastNameTextField: jfxsc.TextField = _
#jfxf.FXML
private var ageTextField: jfxsc.TextField = _
override def initialize(url: URL, resourceBundle: ResourceBundle): Unit =
{
}
#jfxf.FXML
protected def ok(): Unit =
{
// TODO validation
val newPerson = new Person(-1, foreNameTextField.getText, lastNameTextField.getText, ageTextField.getText.toInt)
// Save to DB
// Close whole form
}
}
When I'm done with filling in a new customer's detail I click on another button (that calls ok()) and save it to a database.
What I want to do now is close the form and replace it with the detail-form.
Something like calling a protected method in the main-controller like:
protected def clearDetails(): Unit =
{
contentPane.getChildren.clear
contentPane.getChildren.add(savedOldDetails)
}
won't work of course. (Will throw a runtime-exception because there is no contentpane in the sub/form-controller (even if I make it protected)
In Qt (C++) I'd use signals/slots and connect them accordingly.
Seems like in JavaFX there is nothing the like. How am I supposed to share such information?
Do I need to create a "super-controller" for the contentPane?
(I don't know Scala, so I'll write this in Java, hope that is ok).
You can define an observable property in the Form controller, and observe it when you load the form from the main controller:
public class Form implements Initializable {
private final ObjectProperty<Person> person = new SimpleObjectProperty<>(null);
public ObjectProperty<Person> personProperty() {
return person ;
}
public final Person getPerson() {
return personProperty().get();
}
public final void setPerson(Person person) {
personProperty().set(person);
}
// ... code you had before...
#FXML
protected void ok() {
Person person = new Person(-1, foreNameTextField.getText(), lastNameTextField.getText(), ageTextField.getText());
// save to DB...
personProperty().set(person);
}
}
Now in your main controller, load the form as follows:
#FXML
protected void newButtonPressed() {
contentPane.getChildren().clear();
FXMLLoader loader = new FXMLLoader(getClass().getResource("customers/form.fxml"));
Parent form = loader.load();
Form formController = loader.getController();
formController.personProperty().addListener((obs, oldPerson, newPerson) {
if (newPerson != null) {
// clear form, etc, e.g.
clearDetails();
}
});
contentPane.getChildren().add(form);
}

How to avoid View specific code in my ViewModel

My application has a menu option to allow the creation of a new account. The menu option's command is bound to a command (NewAccountCommand) in my ViewModel. When the user clicks the option to create a new account, the app displays a "New Account" dialog where the user can enter such data as Name, Address, etc... and then clicks "Ok" to close the dialog and create the new account.
I know my code in the ViewModel is not correct because it creates the "New Account" dialog and calls ShowDialog(). Here is a snippet from the VM:
var modelResult = newAccountDialog.ShowDialog();
if (modelResult == true)
{
//Create the new account
}
how do i avoid creating and showing the dialog from within my VM so I can unit test the VM?
I like the approach explained in this codeproject article:
http://www.codeproject.com/KB/WPF/XAMLDialog.aspx
It basically creates a WPF Dialog control that can be embedded in the visual tree of another window or usercontrol.
It then uses a style trigger that causes the dialog to open up whenever there is content in the dialog.
so in you xaml all you have to do is this(where DialogViewModel is a property in you ViewModel):
<MyControls:Dialog Content = {Binding DialogViewModel}/>
and in you ViewModel you just have to do the following:
DialogViewModel = new MyDialogViewModel();
so in unit testing all you have to do is:
MyViewModel model = new MyViewModel();
model.DialogViewModel = new MyDialogViewModel();
model.DialogViewModel.InputProperty = "Here's my input";
//Assert whatever you want...
I personally create a ICommand property in my ViewModel that sets the DialogViewModel property, so that the user can push a button to get the dialog to open up.
So my ViewModel never calls a dialog it just instantiates a property. The view interprets that and display a dialog box. The beauty behind this is that if you decide to change your view at all and maybe not display a dialog, your ViewModel does not have to change one bit. It pushes all the User interaction code where it should be...in the view. And creating a wpf control allows me to re-use it whenever I need to...
There are many ways to do this, this is one I found to be good for me. :)
In scenarios like this, I typically use events. The model can raise an event to ask for information and anybody can respond to it. The view would listen for the event and display the dialog.
public class MyModel
{
public void DoSomething()
{
var e = new SomeQuestionEventArgs();
OnSomeQuestion(e);
if (e.Handled)
mTheAnswer = e.TheAnswer;
}
private string mTheAnswer;
public string TheAnswer
{
get { return mTheAnswer; }
}
public delegate void SomeQuestionHandler(object sender, SomeQuestionEventArgs e);
public event SomeQuestionHandler SomeQuestion;
protected virtual void OnSomeQuestion(SomeQuestionEventArgs e)
{
if (SomeQuestion == null) return;
SomeQuestion(this, e);
}
}
public class SomeQuestionEventArgs
: EventArgs
{
private bool mHandled = false;
public bool Handled
{
get { return mHandled; }
set { mHandled = value; }
}
private string mTheAnswer;
public string TheAnswer
{
get { return mTheAnswer; }
set { mTheAnswer = value; }
}
}
public class MyView
{
private MyModel mModel;
public MyModel Model
{
get { return mModel; }
set
{
if (mModel != null)
mModel.SomeQuestion -= new MyModel.SomeQuestionHandler(mModel_SomeQuestion);
mModel = value;
if (mModel != null)
mModel.SomeQuestion += new MyModel.SomeQuestionHandler(mModel_SomeQuestion);
}
}
void mModel_SomeQuestion(object sender, SomeQuestionEventArgs e)
{
var dlg = new MyDlg();
if (dlg.ShowDialog() != DialogResult.OK) return;
e.Handled = true;
e.TheAnswer = dlg.TheAnswer;
}
}
The WPF Application Framework (WAF) shows a concrete example how to accomplish this.
The ViewModel sample application shows an Email Client in which you can open the “Email Account Settings” dialog. It uses dependency injection (MEF) and so you are still able to unit test the ViewModel.
Hope this helps.
jbe
There are different approaches to this. One common approach is to use some form of dependency injection to inject a dialog service, and use the service.
This allows any implementation of that service (ie: a different view) to be plugged in at runtime, and does give you some decoupling from the ViewModel to View.