Skip to content

Commit

Permalink
JR| SyncStatus Broadcast Receiver
Browse files Browse the repository at this point in the history
Use a BroadcastReceiver to tell BaseActivity and BaseRegisterActivity if
sync is currently running

Part of fix for issue #474

Signed-off-by: Jason Rogena <jasonrogena@gmail.com>
  • Loading branch information
jasonrogena committed May 12, 2017
1 parent 8c18f26 commit 1eba121
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.ei.opensrp.domain;

public enum FetchStatus implements Displayable {
fetched("Update successful."), nothingFetched("Already up to date."), fetchedFailed("Update failed. Please try again.");
import java.io.Serializable;

public enum FetchStatus implements Displayable, Serializable{
fetchStarted("Update started"),
fetched("Update successful."),
nothingFetched("Already up to date."),
fetchedFailed("Update failed. Please try again.");
private String displayValue;

private FetchStatus(String displayValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -39,6 +40,7 @@
import org.ei.opensrp.domain.FetchStatus;
import org.ei.opensrp.path.R;
import org.ei.opensrp.path.application.VaccinatorApplication;
import org.ei.opensrp.path.receiver.SyncStatusBroadcastReceiver;
import org.ei.opensrp.path.repository.UniqueIdRepository;
import org.ei.opensrp.path.sync.ECSyncUpdater;
import org.ei.opensrp.path.sync.PathAfterFetchListener;
Expand Down Expand Up @@ -73,7 +75,7 @@
* Created by Jason Rogena - jrogena@ona.io on 16/02/2017.
*/
public abstract class BaseActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
implements NavigationView.OnNavigationItemSelectedListener, SyncStatusBroadcastReceiver.SyncStatusListener {
private static final String TAG = "BaseActivity";
private BaseToolbar toolbar;
private Menu menu;
Expand All @@ -100,29 +102,49 @@ protected void onCreate(Bundle savedInstanceState) {
navigationView.setNavigationItemSelectedListener(this);
toggleIsSyncing();

pathAfterFetchListener = new PathAfterFetchListener() {
@Override
public void afterFetch(FetchStatus fetchStatus) {
isSyncing = false;
if(syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
if (fetchStatus.equals(FetchStatus.fetchedFailed)) {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_failed, Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.setAction(R.string.retry, new View.OnClickListener() {
@Override
public void onClick(View v) {
startSync();
}
});
} else {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_complete, Snackbar.LENGTH_LONG);
}
syncStatusSnackbar.show();
toggleIsSyncing();
}
};
pathAfterFetchListener = new PathAfterFetchListener();

initializeProgressDialog();
registerSyncStatusBroadcastReceiver();
}

@Override
public void onSyncStart() {
isSyncing = true;
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
if (syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
syncStatusSnackbar = Snackbar.make(rootView, R.string.syncing,
Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.show();
toggleIsSyncing();
}

@Override
public void onSyncComplete(FetchStatus fetchStatus) {
isSyncing = false;
if (syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
if (fetchStatus.equals(FetchStatus.fetchedFailed)) {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_failed, Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.setAction(R.string.retry, new View.OnClickListener() {
@Override
public void onClick(View v) {
startSync();
}
});
} else if (fetchStatus.equals(FetchStatus.fetched)
|| fetchStatus.equals(FetchStatus.nothingFetched)) {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_complete, Snackbar.LENGTH_LONG);
}
syncStatusSnackbar.show();
toggleIsSyncing();
}

private void registerSyncStatusBroadcastReceiver() {
SyncStatusBroadcastReceiver syncStatusBroadcastReceiver = new SyncStatusBroadcastReceiver();
syncStatusBroadcastReceiver.addSyncStatusListener(this);
registerReceiver(syncStatusBroadcastReceiver,
new IntentFilter(SyncStatusBroadcastReceiver.ACTION_SYNC_STATUS));
}

public BaseToolbar getBaseToolbar() {
Expand Down Expand Up @@ -266,13 +288,6 @@ public boolean onNavigationItemSelected(MenuItem item) {
}

private void startSync() {
isSyncing = true;
if(syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
syncStatusSnackbar = Snackbar.make(
(ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0), R.string.syncing,
Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.show();
toggleIsSyncing();
PathUpdateActionsTask pathUpdateActionsTask = new PathUpdateActionsTask(
this, getOpenSRPContext().actionService(),
getOpenSRPContext().formSubmissionSyncService(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.ei.opensrp.path.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.design.widget.NavigationView;
Expand All @@ -20,6 +22,7 @@

import org.ei.opensrp.domain.FetchStatus;
import org.ei.opensrp.path.R;
import org.ei.opensrp.path.receiver.SyncStatusBroadcastReceiver;
import org.ei.opensrp.path.sync.ECSyncUpdater;
import org.ei.opensrp.path.sync.PathAfterFetchListener;
import org.ei.opensrp.path.sync.PathUpdateActionsTask;
Expand All @@ -39,7 +42,7 @@
* Created by keyman.
*/
public abstract class BaseRegisterActivity extends SecuredNativeSmartRegisterActivity
implements NavigationView.OnNavigationItemSelectedListener {
implements NavigationView.OnNavigationItemSelectedListener, SyncStatusBroadcastReceiver.SyncStatusListener {

public static final String IS_REMOTE_LOGIN = "is_remote_login";
private PathAfterFetchListener pathAfterFetchListener;
Expand Down Expand Up @@ -73,37 +76,56 @@ public void onDrawerOpened(View drawerView) {
navigationView.setNavigationItemSelectedListener(this);
toggleIsSyncing();

pathAfterFetchListener = new PathAfterFetchListener() {
@Override
public void afterFetch(FetchStatus fetchStatus) {
isSyncing = false;
if(syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
if (fetchStatus.equals(FetchStatus.fetchedFailed)) {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_failed, Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.setAction(R.string.retry, new View.OnClickListener() {
@Override
public void onClick(View v) {
startSync();
}
});
} else {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_complete, Snackbar.LENGTH_LONG);
}
syncStatusSnackbar.show();
toggleIsSyncing();
}
};
pathAfterFetchListener = new PathAfterFetchListener();

Bundle extras = this.getIntent().getExtras();
if (extras != null) {
boolean isRemote = extras.getBoolean(IS_REMOTE_LOGIN);
if (isRemote) {
updateFromServer();
isSyncing = true;
toggleIsSyncing();
}
}

registerSyncStatusBroadcastReceiver();
}

@Override
public void onSyncStart() {
isSyncing = true;
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
if(syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
syncStatusSnackbar = Snackbar.make(rootView, R.string.syncing,
Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.show();
toggleIsSyncing();
}

@Override
public void onSyncComplete(FetchStatus fetchStatus) {
isSyncing = false;
if(syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
if (fetchStatus.equals(FetchStatus.fetchedFailed)) {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_failed, Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.setAction(R.string.retry, new View.OnClickListener() {
@Override
public void onClick(View v) {
startSync();
}
});
} else if (fetchStatus.equals(FetchStatus.fetched)
|| fetchStatus.equals(FetchStatus.nothingFetched)) {
syncStatusSnackbar = Snackbar.make(rootView, R.string.sync_complete, Snackbar.LENGTH_LONG);
}
syncStatusSnackbar.show();
toggleIsSyncing();
}

private void registerSyncStatusBroadcastReceiver() {
SyncStatusBroadcastReceiver syncStatusBroadcastReceiver = new SyncStatusBroadcastReceiver();
syncStatusBroadcastReceiver.addSyncStatusListener(this);
registerReceiver(syncStatusBroadcastReceiver,
new IntentFilter(SyncStatusBroadcastReceiver.ACTION_SYNC_STATUS));
}

public void updateFromServer() {
Expand Down Expand Up @@ -195,13 +217,6 @@ public boolean onNavigationItemSelected(MenuItem item) {
}

private void startSync() {
isSyncing = true;
if(syncStatusSnackbar != null) syncStatusSnackbar.dismiss();
syncStatusSnackbar = Snackbar.make(
(ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0), R.string.syncing,
Snackbar.LENGTH_INDEFINITE);
syncStatusSnackbar.show();
toggleIsSyncing();
PathUpdateActionsTask pathUpdateActionsTask = new PathUpdateActionsTask(
this, context().actionService(),
context().formSubmissionSyncService(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.ei.opensrp.path.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import org.ei.opensrp.domain.FetchStatus;

import java.util.ArrayList;

/**
* Created by Jason Rogena - jrogena@ona.io on 12/05/2017.
*/

public class SyncStatusBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_SYNC_STATUS = "sync_status";
public static final String EXTRA_FETCH_STATUS = "fetch_status";

private final ArrayList<SyncStatusListener> syncStatusListeners;

public SyncStatusBroadcastReceiver() {
syncStatusListeners = new ArrayList<>();
}

public void addSyncStatusListener(SyncStatusListener syncStatusListener) {
syncStatusListeners.add(syncStatusListener);
}

public void removeSyncStatusListener(SyncStatusListener syncStatusListener) {
if (syncStatusListeners.contains(syncStatusListener)) {
syncStatusListeners.remove(syncStatusListener);
}
}

public interface SyncStatusListener {
void onSyncStart();

void onSyncComplete(FetchStatus fetchStatus);
}


@Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
if (data != null) {
FetchStatus fetchStatus = (FetchStatus) data.getSerializable(EXTRA_FETCH_STATUS);
if (fetchStatus.equals(FetchStatus.fetchStarted)) {
for (SyncStatusListener syncStatusListener : syncStatusListeners) {
syncStatusListener.onSyncStart();
}
} else {
for (SyncStatusListener syncStatusListener : syncStatusListeners) {
syncStatusListener.onSyncComplete(fetchStatus);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.ei.opensrp.domain.FetchStatus;
import org.ei.opensrp.domain.Response;
import org.ei.opensrp.path.application.VaccinatorApplication;
import org.ei.opensrp.path.receiver.SyncStatusBroadcastReceiver;
import org.ei.opensrp.path.repository.PathRepository;
import org.ei.opensrp.path.service.intent.PathReplicationIntentService;
import org.ei.opensrp.path.service.intent.PullUniqueIdsIntentService;
Expand Down Expand Up @@ -72,6 +73,8 @@ public void setAdditionalSyncService(AdditionalSyncService additionalSyncService

public void updateFromServer(final PathAfterFetchListener pathAfterFetchListener) {
this.pathAfterFetchListener = pathAfterFetchListener;

sendSyncStatusBroadcastMessage(context, FetchStatus.fetchStarted);
if (org.ei.opensrp.Context.getInstance().IsUserLoggedOut()) {
logInfo("Not updating from server as user is not logged in.");
return;
Expand Down Expand Up @@ -122,6 +125,7 @@ public void postExecuteInUIThread(FetchStatus result) {
Toast.makeText(context, result.displayValue(), Toast.LENGTH_SHORT).show();
}
pathAfterFetchListener.afterFetch(result);
sendSyncStatusBroadcastMessage(context, result);
}
});
}
Expand Down Expand Up @@ -238,4 +242,11 @@ private void startVaccineIntentService(Context context) {
Intent intent = new Intent(context, VaccineIntentService.class);
context.startService(intent);
}

private void sendSyncStatusBroadcastMessage(Context context, FetchStatus fetchStatus) {
Intent intent = new Intent();
intent.setAction(SyncStatusBroadcastReceiver.ACTION_SYNC_STATUS);
intent.putExtra(SyncStatusBroadcastReceiver.EXTRA_FETCH_STATUS, fetchStatus);
context.sendBroadcast(intent);
}
}

0 comments on commit 1eba121

Please sign in to comment.