Skip to content

Commit

Permalink
MI | Updating sync functionality #4
Browse files Browse the repository at this point in the history
Signed-off-by: mib-iqbal <[email protected]>
  • Loading branch information
mib-iqbal committed Nov 23, 2017
1 parent 56540c4 commit d82513f
Show file tree
Hide file tree
Showing 22 changed files with 2,589 additions and 29 deletions.
2 changes: 2 additions & 0 deletions opensrp-bidan/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ android {
versionName "2.0"

multiDexEnabled true
buildConfigField "long", "MAX_SERVER_TIME_DIFFERENCE", "1800000l"
buildConfigField "boolean", "TIME_CHECK", "false"

}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ public class CommonFormFields {
public static final String UNIQUE_ID = "unique_id";
}

public static class CloudantSync {
public static final String ACTION_DATABASE_CREATED = "org.smartregister.DATABASE_CREATED_ACTION";
public static final String ACTION_REPLICATION_ERROR = "org.smartregister.REPLICATION_ERROR_ACTION";
public static final String ACTION_REPLICATION_COMPLETED = "org.smartregister.REPLICATION_COMPLETED_ACTION";
public static final String REPLICATION_ERROR = "REPLICATION_ERROR";
public static final String DOCUMENTS_REPLICATED = "DOCUMENTS_REPLICATED";
public static final String BATCHES_REPLICATED = "BATCHES_REPLICATED";
public static final String COUCHDB_PORT = "";
public static final String COUCH_DATABASE_NAME = "";
public static final String COUCH_DATABASE_USER = "";
public static final String COUCH_DATABASE_PASS = "";

}
public class FormNames {
public static final String EC_REGISTRATION = "ec_registration";
public static final String FP_COMPLICATIONS = "fp_complications";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.smartregister.bidan_cloudant.activity;
import android.database.Cursor;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -10,19 +11,22 @@

import com.flurry.android.FlurryAgent;

import org.opensrp.api.domain.Location;
import org.opensrp.api.util.TreeNode;
import org.smartregister.Context;
import org.smartregister.bidan_cloudant.AllConstantsINA;
import org.smartregister.bidan_cloudant.controller.NavigationControllerINA;
import org.smartregister.bidan_cloudant.service.FormSubmissionSyncService;
import org.smartregister.cursoradapter.SmartRegisterQueryBuilder;
import org.smartregister.event.Listener;

import org.smartregister.bidan_cloudant.sync.UpdateActionsTask;
import org.smartregister.bidan_cloudant.R;
import org.smartregister.bidan_cloudant.face.camera.utils.Tools;
import org.smartregister.bidan_cloudant.lib.FlurryFacade;
import org.smartregister.service.PendingFormSubmissionService;
import org.smartregister.sync.SyncAfterFetchListener;
import org.smartregister.sync.SyncProgressIndicator;
import org.smartregister.sync.UpdateActionsTask;
import org.smartregister.view.activity.SecuredActivity;
import org.smartregister.view.contract.HomeContext;
import org.smartregister.view.controller.NativeAfterANMDetailsFetchListener;
Expand Down Expand Up @@ -290,7 +294,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

public void updateFromServer() {
/*public void updateFromServer() {
FlurryFacade.logEvent("clicked_update_from_server");
UpdateActionsTask updateActionsTask = new UpdateActionsTask(
this, context().actionService(), context().formSubmissionSyncService(),
Expand All @@ -304,6 +308,23 @@ this, context().actionService(), context().formSubmissionSyncService(),
// if(LoginActivity.generator.uniqueIdController().needToRefillUniqueId(LoginActivity.generator.UNIQUE_ID_LIMIT)) // unique id part
// LoginActivity.generator.requestUniqueId(); // unique id part
}*/
public void updateFromServer() {
Log.d("Home", "updateFromServer: tombol update");
UpdateActionsTask updateActionsTask = new UpdateActionsTask(
this, context().actionService(), new FormSubmissionSyncService(context().applicationContext()) ,new SyncProgressIndicator(), context().allFormVersionSyncService());
// FlurryFacade.logEvent("click_update_from_server");
updateActionsTask.updateFromServer(new SyncAfterFetchListener());

// if (LoginActivity.generator.uniqueIdController().needToRefillUniqueId(LoginActivity.generator.UNIQUE_ID_LIMIT)) // unique id part
// LoginActivity.generator.requestUniqueId(); // unique id part

String locationjson = context().anmLocationController().get();
LocationTree locationTree = EntityUtils.fromJson(locationjson, LocationTree.class);

Map<String, TreeNode<String, Location>> locationMap =
locationTree.getLocationsHierarchy();

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import com.flurry.android.FlurryAgent;

import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.Context;
import org.smartregister.bidan_cloudant.R;
import org.smartregister.bidan_cloudant.application.BidanApplication;
Expand Down Expand Up @@ -111,17 +113,20 @@ public void onCreate(Bundle savedInstanceState) {
debugApp();

}
public static Context getOpenSRPContext() {
return BidanApplication.getInstance().context();
}

private void debugApp() {
String uname = getResources().getString(R.string.uname);
String pwd = getResources().getString(R.string.pwd);
// String uname = "demo_test";
// String pwd = "Demo@123";
// String uname = getResources().getString(R.string.uname);
// String pwd = getResources().getString(R.string.pwd);
String uname = "demo_ec";
String pwd = "Satu2345";

LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.login, null);

if (context.userService().hasARegisteredUser()) {
if (getOpenSRPContext().userService().hasARegisteredUser()) {
localLogin(view, uname, pwd);
} else {
remoteLogin(view, uname, pwd);
Expand Down Expand Up @@ -207,10 +212,12 @@ private void initializeProgressDialog() {
}

private void localLogin(View view, String userName, String password) {
if (context.userService().isValidLocalLogin(userName, password)) {
if (getOpenSRPContext().userService().isUserInValidGroup(userName, password)) {
localLoginWith(userName, password);
ErrorReportingFacade.setUsername("", userName);
FlurryAgent.setUserId(userName);

// Tracking Error
// ErrorReportingFacade.setUsername("", userName);
// FlurryAgent.setUserId(userName);
} else {
showErrorDialog(getString(org.smartregister.R.string.login_failed_dialog_message));
view.setClickable(true);
Expand Down Expand Up @@ -344,7 +351,9 @@ private void localLoginWith(String userName, String password) {

private void remoteLoginWith(String userName, String password, String userInfo) {
context.userService().remoteLogin(userName, password, userInfo);
LoginActivity.generator = new Generator(context, userName, password);
// LoginActivity.generator = new Generator(context, userName, password);
String locationId = getUserDefaultLocationId(userInfo);
saveDefaultLocationId(userName,locationId);
goToHome();
DrishtiSyncScheduler.startOnlyIfConnectedToNetwork(getApplicationContext());
}
Expand Down Expand Up @@ -428,5 +437,21 @@ public void postExecuteInUIThread(ResponseStatus result) {
}
});
}
public String getUserDefaultLocationId(String userInfo) {
try {
JSONObject userLocationJSON = new JSONObject(userInfo);
return userLocationJSON.getJSONObject("team").getJSONArray("location").getJSONObject(0).getString("name");
} catch (JSONException e) {
android.util.Log.v("Error : ", e.getMessage());
}

return null;
}

public void saveDefaultLocationId(String userName, String locationId) {
if (userName != null) {
context.userService().getAllSharedPreferences().savePreference(userName + "-locationid", locationId);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public void onPageSelected(int position) {
});


if(LoginActivity.generator.uniqueIdController().needToRefillUniqueId(LoginActivity.generator.UNIQUE_ID_LIMIT)) {
/* if(LoginActivity.generator.uniqueIdController().needToRefillUniqueId(LoginActivity.generator.UNIQUE_ID_LIMIT)) {
String toastMessage = "need to refill unique id, its only "+
LoginActivity.generator.uniqueIdController().countRemainingUniqueId()+
" remaining";
Toast.makeText(context().applicationContext(), toastMessage, Toast.LENGTH_LONG).show();
}
}*/

ziggyService = context().ziggyService();

Expand Down Expand Up @@ -183,15 +183,15 @@ public void OnLocationSelected(String locationJSONString) {

try {
JSONObject locationJSON = new JSONObject(locationJSONString);
JSONObject uniqueId = new JSONObject(LoginActivity.generator.uniqueIdController().getUniqueIdJson());
// JSONObject uniqueId = new JSONObject(LoginActivity.generator.uniqueIdController().getUniqueIdJson());

combined = locationJSON;
Iterator<String> iter = uniqueId.keys();
// Iterator<String> iter = uniqueId.keys();

while (iter.hasNext()) {
/* while (iter.hasNext()) {
String key = iter.next();
combined.put(key, uniqueId.get(key));
}
}*/

} catch (JSONException e) {
e.printStackTrace();
Expand All @@ -218,7 +218,7 @@ public void saveFormSubmission(String formSubmission, String id, String formName
context().formSubmissionRouter().handleSubmission(submission, formName);

if(formName.equals("kartu_ibu_registration")){
saveuniqueid();
// saveuniqueid();
}
//switch to forms list fragment
switchToBaseFragment(formSubmission); // Unnecessary!! passing on data
Expand Down Expand Up @@ -276,16 +276,16 @@ public void startFormActivity(String formName, String entityId, String metaData)

}

public void saveuniqueid() {
/* public void saveuniqueid() {
try {
JSONObject uniqueId = new JSONObject(LoginActivity.generator.uniqueIdController().getUniqueIdJson());
String uniq = uniqueId.getString("unique_id");
LoginActivity.generator.uniqueIdController().updateCurrentUniqueId(uniq);
// LoginActivity.generator.uniqueIdController().updateCurrentUniqueId(uniq);
} catch (JSONException e) {
e.printStackTrace();
}
}
}*/

public void switchToBaseFragment(final String data){
final int prevPageIndex = currentPage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public void onCreate() {
cleanUpSyncState();

}
@Override
public Repository getRepository() {
try {
if (repository == null) {
repository = new BidanRepository(getInstance().getApplicationContext(), context());

}
} catch (UnsatisfiedLinkError e) {
logError("Error on getRepository: " + e);

}
return repository;
}

@Override
public void logoutCurrentUser(){
Expand Down Expand Up @@ -159,12 +172,12 @@ public static Repository initializeRepositoryForUniqueId(){
return null;
}

public UniqueIdRepository uniqueIdRepository() {
/* public UniqueIdRepository uniqueIdRepository() {
if (uniqueIdRepository == null) {
uniqueIdRepository = new UniqueIdRepository((BidanRepository) getRepository());
}
return uniqueIdRepository;
}
}*/

public static synchronized BidanApplication getInstance() {
return (BidanApplication) mInstance;
Expand All @@ -174,7 +187,7 @@ public Context context() {
return context;
}

@Override
/* @Override
public Repository getRepository() {
try {
Expand All @@ -188,7 +201,7 @@ public Repository getRepository() {
}
return repository;
}
}*/

public static SettingsRepository getSettingsRepositoryforUniqueId(){
return settingsRepository();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public void encodeBitmapAndSaveToFirebase(Bitmap bitmap) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
String imageEncoded = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
// DatabaseReference ref = FirebaseDatabase.getInstance()
// .getReference(AllConstantsINA.FIREBASE_OPENSRP_INA)
// .getReference(AllConstants.FIREBASE_OPENSRP_INA)
// .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
// .child(mRestaurant.getPushId())
// .child("imageUrl");
Expand Down
Loading

0 comments on commit d82513f

Please sign in to comment.