samedi 9 novembre 2019

How to start Refactoring a Android Activity Server and UI Coupling using Some Design Pattern?

I am making an app where there are two modules. 1. UI Module (Register the listener) 2. Server Module (Make the listener)

These two modules communicates with the callbacks.

For Eg. : When I click a button in UI, I call the server module's API and then when it successfully do the work, server sends the Callback that UI was listening to. UI's code : server.updatePhoneNumber(newNumber) // I disable the button here to avoid more than one clicks UI's Callback code :

@override

onUpdatePhoneNumber(int result) {

       // using runOnUiThread()

       // do or update UI accordingly, generally I enable the button here again

}

I have done this thing but all the code is in my MainActivity.java, I need to code all UI module according to MVVM or some other pattern. But I am not able to do this.

REGISTER THE LISTENER

ServerImpl.getInstance(this).registerEventListener(this);

CALL TO SERVER

@Override
    public boolean onPreferenceClick(Preference preference) {
     ServerImpl.getInstance(this).setDeviceServiceMode(pref.getDeviceId(), isChecked);
// DISABLE BUTTON
}

THIS IS A CALLBACK

@Override
    public void onSetDeviceServiceMode(String deviceId, boolean result, MdecInterface.Reason errorCode, boolean currServiceMode) {
        MdecLogger.d(LOG_TAG, "onSetDeviceServiceMode: deviceId = " + deviceId + " result = " + result + " errorCode = " + errorCode + " currServiceMode = " + currServiceMode);
        int switchState = 0;
        if (currServiceMode)
            switchState = 1;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (result) {

                    // ENABLE BUTTON
                } else {
                    Toast.makeText(mContext, R.string.server_connect_failed_msg, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

Aucun commentaire:

Enregistrer un commentaire