Skip to content

Commit

Permalink
Merge pull request #1341 from dimagi/copy_of_fcm_background_sync
Browse files Browse the repository at this point in the history
Duplicate of fcm_background_sync
  • Loading branch information
avazirna authored Oct 3, 2023
2 parents ffd5e12 + 4734d6d commit 8850102
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.javarosa.core.model.condition.EvaluationContext;

import java.util.concurrent.locks.ReentrantLock;

/**
* Interface defining all functionality to be implemented by any class that will receive and
* process status codes from a SessionNavigator
Expand All @@ -19,4 +21,5 @@ public interface SessionNavigationResponder {
// Provide a hook to the current evaluation context that the SessionNavigator will use
EvaluationContext getEvalContextForNavigator();

ReentrantLock getBackgroundSyncLock();
}
7 changes: 7 additions & 0 deletions src/main/java/org/commcare/session/SessionNavigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class SessionNavigator {
public static final int START_SYNC_REQUEST = 7;
public static final int PROCESS_QUERY_REQUEST = 8;
public static final int REPORT_CASE_AUTOSELECT = 9;
public static final int FORM_ENTRY_ATTEMPT_DURING_SYNC = 10;

private final SessionNavigationResponder responder;
private CommCareSession currentSession;
Expand Down Expand Up @@ -107,6 +108,12 @@ private void readyToProceed() {
else if (currentSession.getForm() == null) {
sendResponse(NO_CURRENT_FORM);
} else {
// The current state indicate that a form needs to be instantiated but a background sync is ongoing
if (responder.getBackgroundSyncLock().isLocked()) {
sendResponse(FORM_ENTRY_ATTEMPT_DURING_SYNC);
return;
}

sendResponse(START_FORM_ENTRY);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.commcare.session.SessionNavigationResponder;
import org.javarosa.core.model.condition.EvaluationContext;

import java.util.concurrent.locks.ReentrantLock;

/**
* A mock implementer of the SessionNavigationResponder interface, for testing purposes
*
Expand All @@ -14,9 +16,11 @@ public class MockSessionNavigationResponder implements SessionNavigationResponde

private final SessionWrapper sessionWrapper;
private int lastReceivedResultCode;
private ReentrantLock backgroungSyncLock;

public MockSessionNavigationResponder(SessionWrapper sessionWrapper) {
this.sessionWrapper = sessionWrapper;
this.backgroungSyncLock = new ReentrantLock();
}

public int getLastResultCode() {
Expand All @@ -38,4 +42,9 @@ public EvaluationContext getEvalContextForNavigator() {
return sessionWrapper.getEvaluationContext();
}

@Override
public ReentrantLock getBackgroundSyncLock() {
return backgroungSyncLock;
}

}

0 comments on commit 8850102

Please sign in to comment.