Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location of click event in MVVM architecture

I'm trying to use MVVM with databinding .. Where should I write click event (Viewmodel Or Activity)

Examples are welcome...

like image 369
Anshul gour Avatar asked Aug 09 '18 13:08

Anshul gour


1 Answers

You can write your click events on either. Personally, my preference depends on what I want to achieve and what I need to achieve it.

Click event in ViewModel

Define your ViewModel. Communicate with your activity via a Callback.

public class MyViewModel extends ViewModel{

    private MyCustomCallback callback;
    ...
    public void doOnClick(MyCustomCallback mCallback){
        boolean isSuccessful = doMyAction.execute();
        if (isSuccessful){
            mCallback.actionIsSuccessful();
        } else {
            mCallback.actionFailed();
        }
    }
    ...
    public void setCallback(callback){
        this.callback = callback;   
    }
    public MyCustomCallback getCallback(){
        return this.callback;   
    }
    ...

    public interface MyCustomCallback{
        void actionIsSuccessful();
        void actionFailed();
    }
}

Implement this callback in your activity:

public class MyActivity extends AppCompatActivity{
    ...
    private MyCustomCallback callback;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        ...
        callback = new MyViewModel.MyCustomCallback{
            @Override
            public void actionIsSuccessful(){
                //do something when action is successful
            }
            @Override
            public void actionFailed(){
                //do something when action fails
            }
        }
        viewModel.setCallback(callback);
    }
}

Pass your ViewModel as a variable to your XML. Then do this:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button_background"
    android:layout_margin="20dp"
    tools:text="CLICK ME!!!"
    android:textColor="@android:color/white"
    android:onClick="@{() -> ViewModel.doOnClick(ViewModel.callback)}"/>

Click events in Activity

public class MyActivity extends AppCompatActivity{
    ...
    private MyCustomCallback callback;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        //initialize your binding
        ...
        binding.setClickHandler(new MyActivityClickHandler());
    }

    public class MyActivityClickHandler{
        public void onClickMeClicked(View view){
            //do something
        }
    }
}

Then in your XML:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button_background"
    android:layout_margin="20dp"
    android:text="@string/verify"
    android:textColor="@android:color/white"
    android:onClick="@{ClickHandler::onClickMeClicked}"/>

For more information check the official doc here

like image 101
Ayokunle Paul Avatar answered Oct 05 '22 13:10

Ayokunle Paul