Skip to content

Commit

Permalink
JR| Global SyncStatusBroadcastReceiver
Browse files Browse the repository at this point in the history
Add a global instance of SyncStatusBroadcastReceiver for easier
subscibing and unsubscribing by BaseActivity and BaseRegisterActivity

Part of fix for issue #474

Signed-off-by: Jason Rogena <jasonrogena@gmail.com>
  • Loading branch information
jasonrogena committed May 13, 2017
1 parent 99375c0 commit 665689c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.ei.opensrp.path.activity;

import android.annotation.TargetApi;
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;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
Expand Down Expand Up @@ -39,9 +36,7 @@
import org.ei.opensrp.Context;
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;
import org.ei.opensrp.path.sync.PathUpdateActionsTask;
Expand Down Expand Up @@ -81,10 +76,8 @@ public abstract class BaseActivity extends AppCompatActivity
private Menu menu;
private static final int REQUEST_CODE_GET_JSON = 3432;
private PathAfterFetchListener pathAfterFetchListener;
private boolean isSyncing;
private Snackbar syncStatusSnackbar;
private ProgressDialog progressDialog;
private SyncStatusBroadcastReceiver syncStatusBroadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -101,77 +94,64 @@ protected void onCreate(Bundle savedInstanceState) {

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
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();
refreshSyncStatusViews(null);
}

@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();
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterSyncStatusBroadcastReceiver();
refreshSyncStatusViews(fetchStatus);
}

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

private void unregisterSyncStatusBroadcastReceiver() {
if(syncStatusBroadcastReceiver != null) {
unregisterReceiver(syncStatusBroadcastReceiver);
}
SyncStatusBroadcastReceiver.getInstance().removeSyncStatusListener(this);
}

public BaseToolbar getBaseToolbar() {
return toolbar;
}

private void toggleIsSyncing() {
private void refreshSyncStatusViews(FetchStatus fetchStatus) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null && navigationView.getMenu() != null) {
MenuItem syncMenuItem = navigationView.getMenu().findItem(R.id.nav_sync);
if (syncMenuItem != null) {
if (isSyncing) {
if (SyncStatusBroadcastReceiver.getInstance().isSyncing()) {
syncMenuItem.setTitle(R.string.syncing);
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();
} else {
if (fetchStatus != null) {
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();
}
String lastSync = getLastSyncTime();

if (!TextUtils.isEmpty(lastSync)) {
Expand Down Expand Up @@ -209,9 +189,16 @@ private String getLastSyncTime() {
@Override
protected void onResume() {
super.onResume();
registerSyncStatusBroadcastReceiver();
initViews();
}

@Override
protected void onPause() {
super.onPause();
unregisterSyncStatusBroadcastReceiver();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
Expand Down Expand Up @@ -276,6 +263,7 @@ public void onClick(View v) {

TextView nameTV = (TextView) navigationView.findViewById(R.id.name_tv);
nameTV.setText(preferredName);
refreshSyncStatusViews(null);
}

@SuppressWarnings("StatementWithEmptyBody")
Expand Down Expand Up @@ -525,7 +513,6 @@ public BaseActivityToggle(Activity activity, DrawerLayout drawerLayout, Toolbar
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
toggleIsSyncing();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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 @@ -12,7 +10,6 @@
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -47,8 +44,6 @@ public abstract class BaseRegisterActivity extends SecuredNativeSmartRegisterAct
public static final String IS_REMOTE_LOGIN = "is_remote_login";
private PathAfterFetchListener pathAfterFetchListener;
private Snackbar syncStatusSnackbar;
private SyncStatusBroadcastReceiver syncStatusBroadcastReceiver;
private boolean isSyncing;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -75,7 +70,6 @@ public void onDrawerOpened(View drawerView) {

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
toggleIsSyncing();

pathAfterFetchListener = new PathAfterFetchListener();

Expand All @@ -86,59 +80,24 @@ public void onDrawerOpened(View drawerView) {
updateFromServer();
}
}

registerSyncStatusBroadcastReceiver();
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterSyncStatusBroadcastReceiver();
}

@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();
refreshSyncStatusViews(null);
}

@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();
refreshSyncStatusViews(fetchStatus);
}

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

private void unregisterSyncStatusBroadcastReceiver() {
if(syncStatusBroadcastReceiver != null) {
unregisterReceiver(syncStatusBroadcastReceiver);
}
SyncStatusBroadcastReceiver.getInstance().removeSyncStatusListener(this);
}

public void updateFromServer() {
Expand All @@ -161,9 +120,16 @@ public void onBackPressed() {
@Override
protected void onResume() {
super.onResume();
registerSyncStatusBroadcastReceiver();
initViews();
}

@Override
protected void onPause() {
super.onPause();
unregisterSyncStatusBroadcastReceiver();
}

private void initViews() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Button logoutButton = (Button) navigationView.findViewById(R.id.logout_b);
Expand Down Expand Up @@ -205,6 +171,7 @@ public void onClick(View v) {

TextView nameTV = (TextView) navigationView.findViewById(R.id.name_tv);
nameTV.setText(preferredName);
refreshSyncStatusViews(null);
}

@SuppressWarnings("StatementWithEmptyBody")
Expand Down Expand Up @@ -238,14 +205,36 @@ this, context().actionService(),
pathUpdateActionsTask.updateFromServer(pathAfterFetchListener);
}

private void toggleIsSyncing() {
private void refreshSyncStatusViews(FetchStatus fetchStatus) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null && navigationView.getMenu() != null) {
MenuItem syncMenuItem = navigationView.getMenu().findItem(R.id.nav_sync);
if (syncMenuItem != null) {
if (isSyncing) {
if (SyncStatusBroadcastReceiver.getInstance().isSyncing()) {
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();
syncMenuItem.setTitle(R.string.syncing);
} else {
if (fetchStatus != null) {
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();
}
String lastSync = getLastSyncTime();

if (!TextUtils.isEmpty(lastSync)) {
Expand Down Expand Up @@ -293,7 +282,6 @@ public BaseActivityToggle(Activity activity, DrawerLayout drawerLayout, Toolbar
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
toggleIsSyncing();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.ei.opensrp.path.activity.LoginActivity;
import org.ei.opensrp.path.db.VaccineRepo;
import org.ei.opensrp.path.receiver.PathSyncBroadcastReceiver;
import org.ei.opensrp.path.receiver.SyncStatusBroadcastReceiver;
import org.ei.opensrp.path.repository.PathRepository;
import org.ei.opensrp.path.repository.UniqueIdRepository;
import org.ei.opensrp.path.repository.VaccineRepository;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void onCreate() {
context = Context.getInstance();
context.updateApplicationContext(getApplicationContext());
context.updateCommonFtsObject(createCommonFtsObject());
SyncStatusBroadcastReceiver.init(this);

applyUserLanguagePreference();
cleanUpSyncState();
Expand Down Expand Up @@ -89,6 +91,7 @@ private void cleanUpSyncState() {
public void onTerminate() {
logInfo("Application is terminating. Stopping Bidan Sync scheduler and resetting isSyncInProgress setting.");
cleanUpSyncState();
SyncStatusBroadcastReceiver.destroy(this);
super.onTerminate();
}

Expand Down
Loading

0 comments on commit 665689c

Please sign in to comment.