Skip to content

Commit

Permalink
AC-382 Sync can be turned on even if there is no network (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGrzybkowski authored and tmarzeion committed Feb 23, 2017
1 parent e3744af commit 0081b25
Showing 1 changed file with 23 additions and 11 deletions.
Expand Up @@ -15,16 +15,18 @@
package org.openmrs.mobile.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import org.openmrs.mobile.R;
import org.openmrs.mobile.activities.dialog.CustomFragmentDialog;
Expand All @@ -37,7 +39,7 @@
import org.openmrs.mobile.databases.OpenMRSDBOpenHelper;
import org.openmrs.mobile.net.AuthorizationManager;
import org.openmrs.mobile.utilities.ApplicationConstants;
import org.openmrs.mobile.utilities.ToastUtil;
import org.openmrs.mobile.utilities.NetworkUtils;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -57,6 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {
mAuthorizationManager = new AuthorizationManager();
}


@Override
protected void onResume() {
super.onResume();
Expand Down Expand Up @@ -86,8 +89,7 @@ public boolean onPrepareOptionsMenu(final Menu menu) {
logoutMenuItem.setTitle(getString(R.string.action_logout) + " " + mOpenMRS.getUsername());
}
if(mSyncbutton !=null) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(OpenMRS.getInstance());
final Boolean syncState = prefs.getBoolean("sync", true);
final Boolean syncState = NetworkUtils.isOnline();
setSyncButtonState(syncState);
}
return true;
Expand Down Expand Up @@ -120,22 +122,32 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
case R.id.syncbutton:
boolean syncState = OpenMRS.getInstance().getSyncState();
OpenMRS.getInstance().setSyncState(!syncState);
setSyncButtonState(!syncState);
if (syncState) {
ToastUtil.notify("Sync OFF");
}
else {
OpenMRS.getInstance().setSyncState(false);
setSyncButtonState(false);
} else if(NetworkUtils.hasNetwork()){
OpenMRS.getInstance().setSyncState(true);
setSyncButtonState(true);
Intent intent = new Intent("org.openmrs.mobile.intent.action.SYNC_PATIENTS");
getApplicationContext().sendBroadcast(intent);
ToastUtil.notify("Sync ON");
} else {
showNoInternetConnectionSnackbar();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private void showNoInternetConnectionSnackbar() {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
"No internet connection", Snackbar.LENGTH_SHORT);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbar.show();
}

public void logout() {
mOpenMRS.clearUserPreferencesData();
mAuthorizationManager.moveToLoginActivity();
Expand Down

0 comments on commit 0081b25

Please sign in to comment.