Skip to content

Commit

Permalink
Refactor presenter and synclifecyclecallback classes to 2 presenters
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Mar 21, 2019
1 parent 224e5b5 commit fd596a8
Show file tree
Hide file tree
Showing 18 changed files with 935 additions and 948 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import org.smartregister.p2p.handler.OnActivityRequestPermissionHandler;
import org.smartregister.p2p.handler.OnActivityResultHandler;
import org.smartregister.p2p.handler.OnResumeHandler;
import org.smartregister.p2p.interactor.P2pModeSelectInteractor;
import org.smartregister.p2p.presenter.P2pModeSelectPresenter;
import org.smartregister.p2p.presenter.P2PReceiverPresenter;
import org.smartregister.p2p.presenter.P2PSenderPresenter;
import org.smartregister.p2p.util.Constants;
import org.smartregister.p2p.util.DialogUtils;
import org.smartregister.p2p.util.Permissions;
Expand All @@ -59,8 +59,8 @@ public class P2pModeSelectActivity extends AppCompatActivity implements P2pModeS
private TextView messagesTv;
private EditText messageToSendEt;

private P2pModeSelectContract.Presenter presenter;
private P2pModeSelectContract.Interactor interactor;
private P2pModeSelectContract.SenderPresenter senderBasePresenter;
private P2pModeSelectContract.ReceiverPresenter receiverBasePresenter;

private ArrayList<OnActivityResultHandler> onActivityResultHandlers = new ArrayList<>();
private ArrayList<OnResumeHandler> onResumeHandlers = new ArrayList<>();
Expand All @@ -85,7 +85,11 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClick(View v) {
if (messageToSendEt.getText() != null) {
String messageToSend = messageToSendEt.getText().toString();
interactor.sendMessage(messageToSend);

// This is not ideal but for testing purposes for now
// It will not be in the any final release
receiverBasePresenter.sendTextMessage(messageToSend);
senderBasePresenter.sendTextMessage(messageToSend);
displayMessage("YOU: " + messageToSend);

messageToSendEt.setText("");
Expand All @@ -97,18 +101,18 @@ public void onClick(View v) {
@Override
protected void onStart() {
super.onStart();
initializePresenter();
initializePresenters();

sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presenter.onSendButtonClicked();
senderBasePresenter.onSendButtonClicked();
}
});
receiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presenter.onReceiveButtonClicked();
receiverBasePresenter.onReceiveButtonClicked();
}
});
}
Expand Down Expand Up @@ -408,9 +412,9 @@ public Context getContext() {
}

@Override
public void initializePresenter() {
interactor = new P2pModeSelectInteractor(this);
presenter = new P2pModeSelectPresenter(this, interactor);
public void initializePresenters() {
receiverBasePresenter = new P2PReceiverPresenter(this);
senderBasePresenter = new P2PSenderPresenter(this);
}

@Override
Expand All @@ -434,7 +438,8 @@ protected void onStop() {
sendButton.setOnClickListener(null);
receiveButton.setOnClickListener(null);

presenter.onStop();
receiverBasePresenter.onStop();
senderBasePresenter.onStop();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public abstract class BaseSyncConnectionAuthenticator {

protected P2pModeSelectContract.View view;
protected P2pModeSelectContract.Interactor interactor;
protected P2pModeSelectContract.Presenter presenter;
protected P2pModeSelectContract.BasePresenter basePresenter;

public BaseSyncConnectionAuthenticator(@NonNull P2pModeSelectContract.View view
, @NonNull P2pModeSelectContract.Interactor interactor, @NonNull P2pModeSelectContract.Presenter presenter) {
, @NonNull P2pModeSelectContract.Interactor interactor, @NonNull P2pModeSelectContract.BasePresenter basePresenter) {
this.view = view;
this.interactor = interactor;
this.presenter = presenter;
this.basePresenter = basePresenter;
}

public P2pModeSelectContract.View getView() {
Expand All @@ -30,8 +30,8 @@ public P2pModeSelectContract.Interactor getInteractor() {
return interactor;
}

public P2pModeSelectContract.Presenter getPresenter() {
return presenter;
public P2pModeSelectContract.BasePresenter getBasePresenter() {
return basePresenter;
}

public abstract void authenticate(@NonNull DiscoveredDevice discoveredDevice, @NonNull final AuthenticationCallback authenticationCallback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class ReceiverConnectionAuthenticator extends BaseSyncConnectionAuthentic

public ReceiverConnectionAuthenticator(@NonNull P2pModeSelectContract.View view
, @NonNull P2pModeSelectContract.Interactor interactor
, @NonNull P2pModeSelectContract.Presenter presenter) {
super(view, interactor, presenter);
, @NonNull P2pModeSelectContract.BasePresenter basePresenter) {
super(view, interactor, basePresenter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class SenderConnectionAuthenticator extends BaseSyncConnectionAuthenticat

public SenderConnectionAuthenticator(@NonNull P2pModeSelectContract.View view
, @NonNull P2pModeSelectContract.Interactor interactor
, @NonNull P2pModeSelectContract.Presenter presenter) {
super(view, interactor, presenter);
, @NonNull P2pModeSelectContract.BasePresenter basePresenter) {
super(view, interactor, basePresenter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public interface BaseView extends ActivityResultHandler, ActivityResumeHandler, ActivityRequestPermissionResultHandler {

void initializePresenter();
void initializePresenters();

void runOnUiThread(@NonNull Runnable runnable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,30 @@ interface OnLocationEnabled {
}
}

interface Presenter {
interface BasePresenter {

void onSendButtonClicked();
void onStop();

void sendTextMessage(@NonNull String message);
}

interface ReceiverPresenter extends BasePresenter {

void onReceiveButtonClicked();

void prepareForAdvertising(boolean returningFromRequestingPermissions);

void startAdvertisingMode();

}

interface SenderPresenter extends BasePresenter {

void onSendButtonClicked();

void prepareForDiscovering(boolean returningFromRequestingPermissions);

void startDiscoveringMode();

void onStop();
}

interface Interactor extends BaseInteractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.smartregister.p2p.handler;

import android.support.annotation.NonNull;

import org.smartregister.p2p.contract.P2pModeSelectContract;

/**
Expand All @@ -10,20 +9,21 @@

public class AdvertisingResumeHandler implements OnResumeHandler {

private P2pModeSelectContract.Presenter presenter;
private P2pModeSelectContract.ReceiverPresenter receiverPresenter;
private P2pModeSelectContract.Interactor interactor;


public AdvertisingResumeHandler(@NonNull P2pModeSelectContract.Presenter presenter, P2pModeSelectContract.Interactor interactor) {
this.presenter = presenter;
public AdvertisingResumeHandler(@NonNull P2pModeSelectContract.ReceiverPresenter receiverPresenter
, P2pModeSelectContract.Interactor interactor) {
this.receiverPresenter = receiverPresenter;
this.interactor = interactor;
}


@Override
public void onResume() {
if (interactor.isAdvertising()) {
presenter.prepareForAdvertising(false);
receiverPresenter.prepareForAdvertising(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.smartregister.p2p.presenter;

import android.support.annotation.NonNull;

import org.smartregister.p2p.contract.P2pModeSelectContract;
import org.smartregister.p2p.interactor.P2pModeSelectInteractor;

/**
* Created by Ephraim Kigamba - ekigamba@ona.io on 08/03/2019
*/

public abstract class BaseP2pModeSelectPresenter implements P2pModeSelectContract.BasePresenter {

protected P2pModeSelectContract.View view;
protected P2pModeSelectContract.Interactor interactor;

public BaseP2pModeSelectPresenter(@NonNull P2pModeSelectContract.View view) {
this(view, new P2pModeSelectInteractor(view.getContext()));
}

protected BaseP2pModeSelectPresenter(@NonNull P2pModeSelectContract.View view, @NonNull P2pModeSelectContract.Interactor p2pModeSelectInteractor) {
this.view = view;
this.interactor = p2pModeSelectInteractor;

// This will be added when issue https://github.com/OpenSRP/android-p2p-sync/issues/24
// is being worked on
//view.addOnResumeHandler(new AdvertisingResumeHandler(this, interactor));
}

@Override
public void sendTextMessage(@NonNull String message) {
interactor.sendMessage(message);
}

@Override
public void onStop() {
view.dismissAllDialogs();
view.enableSendReceiveButtons(true);

interactor.stopAdvertising();
interactor.stopDiscovering();
interactor.closeAllEndpoints();

interactor.cleanupResources();
interactor = null;
}

}
Loading

0 comments on commit fd596a8

Please sign in to comment.