Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed Mar 4, 2024
1 parent ecfe0af commit 0e39881
Showing 1 changed file with 53 additions and 31 deletions.
84 changes: 53 additions & 31 deletions app/src/org/commcare/utils/SyncDetailCalculations.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import org.commcare.activities.StandardHomeActivity;
import org.commcare.adapters.HomeCardDisplayData;
import org.commcare.adapters.SquareButtonViewHolder;
import org.commcare.android.logging.ReportingUtils;
import org.commcare.android.database.user.models.FormRecord;
import org.commcare.dalvik.R;
import org.commcare.models.database.SqlStorage;
import org.commcare.android.database.user.models.FormRecord;
import org.commcare.modern.util.Pair;
import org.commcare.preferences.HiddenPreferences;
import org.commcare.util.LogTypes;
Expand All @@ -32,19 +31,23 @@
* @author Phillip Mates ([email protected]).
*/
public class SyncDetailCalculations {
private final static String UNSENT_FORM_NUMBER_KEY = "unsent-number-limit";
private final static String UNSENT_FORM_TIME_KEY = "unsent-time-limit";
private final static String LAST_SYNC_KEY_BASE = "last-succesful-sync-";
private static final String UNSENT_FORM_NUMBER_KEY = "unsent-number-limit";
private static final String UNSENT_FORM_TIME_KEY = "unsent-time-limit";
private static final String LAST_SYNC_KEY_BASE = "last-succesful-sync-";

public static void updateSubText(final StandardHomeActivity activity,
SquareButtonViewHolder squareButtonViewHolder,
HomeCardDisplayData cardDisplayData, String notificationText) {
public static void updateSubText(
final StandardHomeActivity activity,
SquareButtonViewHolder squareButtonViewHolder,
HomeCardDisplayData cardDisplayData,
String notificationText) {

int numUnsentForms = getNumUnsentForms();
Pair<Long, String> lastSyncTimeAndMessage = getLastSyncTimeAndMessage();

Spannable syncIndicator = (activity.localize("home.unsent.forms.indicator",
new String[]{String.valueOf(numUnsentForms)}));
Spannable syncIndicator =
(activity.localize(
"home.unsent.forms.indicator",
new String[] {String.valueOf(numUnsentForms)}));

String syncStatus = "";

Expand All @@ -54,7 +57,6 @@ public static void updateSubText(final StandardHomeActivity activity,
syncStatus = lastSyncTimeAndMessage.second;
}


if (numUnsentForms != 0 || HiddenPreferences.shouldShowUnsentFormsWhenZero()) {
if (!TextUtils.isEmpty(syncStatus)) {
syncStatus += "\n\n";
Expand All @@ -65,13 +67,16 @@ public static void updateSubText(final StandardHomeActivity activity,
squareButtonViewHolder.subTextView.setText(syncStatus);

setSyncSubtextColor(
squareButtonViewHolder.subTextView, numUnsentForms, lastSyncTimeAndMessage.first,
squareButtonViewHolder.subTextView,
numUnsentForms,
lastSyncTimeAndMessage.first,
activity.getResources().getColor(cardDisplayData.subTextColor),
activity.getResources().getColor(R.color.cc_dark_warm_accent_color));
}

public static int getNumUnsentForms() {
SqlStorage<FormRecord> formsStorage = CommCareApplication.instance().getUserStorage(FormRecord.class);
SqlStorage<FormRecord> formsStorage =
CommCareApplication.instance().getUserStorage(FormRecord.class);
try {
return StorageUtils.getUnsentRecordsForCurrentApp(formsStorage).length;
} catch (SessionUnavailableException e) {
Expand All @@ -87,16 +92,24 @@ public static Pair<Long, String> getLastSyncTimeAndMessage() {
if (lastSyncTime == 0) {
syncTimeMessage = Localization.get("home.sync.message.last.never");
} else {
syncTimeMessage = DateUtils.formatSameDayTime(lastSyncTime, new Date().getTime(), DateFormat.DEFAULT, DateFormat.DEFAULT);
syncTimeMessage =
DateUtils.formatSameDayTime(
lastSyncTime,
new Date().getTime(),
DateFormat.DEFAULT,
DateFormat.DEFAULT);
}
return new Pair<>(lastSyncTime, Localization.get("home.sync.message.last", new String[]{syncTimeMessage.toString()}));
return new Pair<>(
lastSyncTime,
Localization.get(
"home.sync.message.last", new String[] {syncTimeMessage.toString()}));
}


/**
* @return The number of days since the user has last synced, as calculated by the difference
* between the current date and the date of the last sync. -1 if the user hasn't ever synced
* or if the last date of sync is unavailable
* Calculates the number of days synce the user last synced
*
* @return the difference between the current date and the date of the last sync. -1 if the user
* hasn't ever synced or if the last date of sync is unavailable
*/
public static int getDaysSinceLastSync() {
try {
Expand All @@ -105,10 +118,11 @@ public static int getDaysSinceLastSync() {
return -1;
}
return getDaysBetweenJavaDatetimes(new Date(lastSync), new Date());
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
Logger.log(LogTypes.SOFT_ASSERT,"Error Generating Days since last sync: " +
e.getMessage());
Logger.log(
LogTypes.SOFT_ASSERT,
"Error Generating Days since last sync: " + e.getMessage());
return -1;
}
}
Expand All @@ -122,16 +136,21 @@ public static long getLastSyncTime() {
}

public static long getLastSyncTime(String username) {
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences();
SharedPreferences prefs =
CommCareApplication.instance().getCurrentApp().getAppPreferences();
return prefs.getLong(getLastSyncKey(username), 0);
}

public static String getLastSyncKey(String username) {
return LAST_SYNC_KEY_BASE + username;
}

private static void setSyncSubtextColor(TextView subtext, int numUnsentForms, long lastSyncTime,
int normalColor, int warningColor) {
private static void setSyncSubtextColor(
TextView subtext,
int numUnsentForms,
long lastSyncTime,
int normalColor,
int warningColor) {
if (isSyncStronglyNeeded(numUnsentForms, lastSyncTime)) {
subtext.setTextColor(warningColor);
} else {
Expand All @@ -140,20 +159,23 @@ private static void setSyncSubtextColor(TextView subtext, int numUnsentForms, lo
}

private static boolean isSyncStronglyNeeded(int numUnsentForms, long lastSyncTime) {
return unsentFormNumberLimitExceeded(numUnsentForms) ||
unsentFormTimeLimitExceeded(lastSyncTime);
return unsentFormNumberLimitExceeded(numUnsentForms)
|| unsentFormTimeLimitExceeded(lastSyncTime);
}

private static boolean unsentFormNumberLimitExceeded(int numUnsentForms) {
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences();
SharedPreferences prefs =
CommCareApplication.instance().getCurrentApp().getAppPreferences();
int unsentFormNumberLimit = Integer.parseInt(prefs.getString(UNSENT_FORM_NUMBER_KEY, "5"));
return numUnsentForms > unsentFormNumberLimit;
}

private static boolean unsentFormTimeLimitExceeded(long lastSyncTime) {
SharedPreferences prefs = CommCareApplication.instance().getCurrentApp().getAppPreferences();
double unsentFormTimeLimitInDays = Double.parseDouble(prefs.getString(UNSENT_FORM_TIME_KEY, "5"));
long unsentFormTimeLimitInMsecs = (int)(unsentFormTimeLimitInDays * 24 * 60 * 60 * 1000);
SharedPreferences prefs =
CommCareApplication.instance().getCurrentApp().getAppPreferences();
double unsentFormTimeLimitInDays =
Double.parseDouble(prefs.getString(UNSENT_FORM_TIME_KEY, "5"));
long unsentFormTimeLimitInMsecs = (int) (unsentFormTimeLimitInDays * 24 * 60 * 60 * 1000);

long now = new Date().getTime();
long msecsSinceLastSync = (now - lastSyncTime);
Expand Down

0 comments on commit 0e39881

Please sign in to comment.