Verified Commit 3c049a86 authored by HashikD's avatar HashikD
Browse files

Moved SharedPreferences to constants to increase cohesion in presenter

parent 1102b25c
......@@ -16,6 +16,7 @@ import androidx.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.torproject.snowflake.constants.AppPreferenceConstants;
import org.torproject.snowflake.constants.BrokerConstants;
import org.torproject.snowflake.constants.ForegroundServiceConstants;
import org.torproject.snowflake.exceptions.EmptySIDException;
......@@ -80,7 +81,7 @@ public class MyPersistentService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: executed with startId: " + startId);
sharedPreferences = getSharedPreferences(getString(R.string.sharedpreference_file), MODE_PRIVATE);
isServiceStarted = sharedPreferences.getBoolean(getString(R.string.is_service_running_bool_key), false);
isServiceStarted = sharedPreferences.getBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, false);
if (intent != null) {
if (intent.getAction().equals(ForegroundServiceConstants.ACTION_START))
......@@ -132,10 +133,10 @@ public class MyPersistentService extends Service {
if (setState == ForegroundServiceConstants.SERVICE_RUNNING) {
isServiceStarted = true;
editor.putBoolean(getString(R.string.is_service_running_bool_key), true);
editor.putBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, true);
} else {
isServiceStarted = false;
editor.putBoolean(getString(R.string.is_service_running_bool_key), false);
editor.putBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, false);
}
editor.apply();
}
......@@ -146,8 +147,8 @@ public class MyPersistentService extends Service {
private void updateServedCount() {
SharedPreferences sp = GlobalApplication.getAppPreferences();
sp.edit()
.putInt(getString(R.string.users_served_key),
sp.getInt(getString(R.string.users_served_key), 0) + 1)
.putInt(AppPreferenceConstants.USER_SERVED_KEY,
sp.getInt(AppPreferenceConstants.USER_SERVED_KEY, 0) + 1)
.apply();
}
......
package org.torproject.snowflake.constants;
public class AppPreferenceConstants {
public static final String USER_SERVED_KEY = "users_served";
public static final String DATE_KEY = "date";
public static final String INITIAL_RUN_KEY = "initial_run";
public static final String IS_SERVICE_RUNNING_KEY = "is_service_running";
}
......@@ -4,6 +4,7 @@ import android.content.SharedPreferences;
import android.util.Log;
import org.torproject.snowflake.GlobalApplication;
import org.torproject.snowflake.constants.AppPreferenceConstants;
import org.torproject.snowflake.presenters.MainActivityPresenter;
import java.text.ParseException;
......@@ -13,7 +14,6 @@ import java.util.Date;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
/**
......@@ -55,8 +55,8 @@ public class MainActivityModel {
sharedPreferences.edit().putBoolean(key, val).apply();
}
public boolean isServiceRunning(String key) {
return sharedPreferences.getBoolean(key, false);
public boolean isServiceRunning() {
return sharedPreferences.getBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, false);
}
/**
......@@ -71,7 +71,7 @@ public class MainActivityModel {
listener = (prefs, key) -> {
Log.d(TAG, "setListenerForCount: Listener: Key = " + key);
if (key.equals("users_served")) {
if (key.equals(AppPreferenceConstants.USER_SERVED_KEY)) {
servedCount = sharedPreferences.getInt(key, 0);
if (presenter != null)
presenter.updateServedCount(servedCount);
......@@ -81,17 +81,17 @@ public class MainActivityModel {
sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
}
public String getDate(String dateKey) {
return sharedPreferences.getString(dateKey, "");
public String getDate() {
return sharedPreferences.getString(AppPreferenceConstants.DATE_KEY, "");
}
/**
* Setting the users served date and value.
*/
public void setDateAndServed(String dateKey, String valueKey, String date, int value) {
public void setDateAndServed(String date, int value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(dateKey, date);
editor.putInt(valueKey, value);
editor.putString(AppPreferenceConstants.DATE_KEY, date);
editor.putInt(AppPreferenceConstants.USER_SERVED_KEY, value);
editor.apply();
}
......@@ -111,7 +111,7 @@ public class MainActivityModel {
//No value for key. Set the date value to current date and users served to 0.
if (stringRecordedDate.equals("")) {
setDateAndServed("date", "users_served", stringCurrentDate, 0);
setDateAndServed(stringCurrentDate, 0);
} else {
//Check if the current system date is greater than recorded date, if so reset the "served" flag.
Date recordedDate = simpleDateFormat.parse(stringRecordedDate);
......@@ -125,7 +125,7 @@ public class MainActivityModel {
return true;
} else {
//Current date is bigger than recorded date. Reset the values. i.e comparision > 0
setDateAndServed("date", "users_served", simpleDateFormat.format(currentDate), 0);
setDateAndServed(simpleDateFormat.format(currentDate), 0);
}
}
......@@ -146,7 +146,7 @@ public class MainActivityModel {
.observeOn(AndroidSchedulers.mainThread())
.subscribe((status) -> { //Runs on main thread
//By this point the servedCount must be reset or left as is after checking the dates.
presenter.updateServedCount(getServedCount("users_served"));
presenter.updateServedCount(getServedCount(AppPreferenceConstants.USER_SERVED_KEY));
setListenerForCount();
});
}
......
......@@ -4,6 +4,7 @@ import android.util.Log;
import org.torproject.snowflake.MainActivity;
import org.torproject.snowflake.R;
import org.torproject.snowflake.constants.AppPreferenceConstants;
import org.torproject.snowflake.models.MainActivityModel;
/**
......@@ -32,7 +33,7 @@ public class MainActivityPresenter {
public int getServedCount() {
Log.d(TAG, "getServedCount: ");
if (view != null) {
return model.getServedCount(((MainActivity) view).getString(R.string.users_served_key));
return model.getServedCount(AppPreferenceConstants.USER_SERVED_KEY);
}
return 0;
}
......@@ -40,7 +41,7 @@ public class MainActivityPresenter {
public boolean getInitialRunBoolean() {
Log.d(TAG, "getInitialRunBoolean: ");
if (view != null) {
return model.getInitialRunBool(((MainActivity) view).getString(R.string.initial_run_boolean_key));
return model.getInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY);
}
return false;
}
......@@ -53,7 +54,7 @@ public class MainActivityPresenter {
public void setInitialRunBoolean(boolean val) {
Log.d(TAG, "setInitialRunBoolean: ");
if (view != null) {
model.setInitialRunBool(((MainActivity) view).getString(R.string.initial_run_boolean_key), val);
model.setInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY, val);
}
}
......@@ -65,7 +66,7 @@ public class MainActivityPresenter {
public boolean isServiceRunning() {
Log.d(TAG, "isServiceRunning: ");
if (view != null) {
return model.isServiceRunning(((MainActivity) view).getString(R.string.is_service_running_bool_key));
return model.isServiceRunning();
}
return true;
}
......@@ -81,7 +82,7 @@ public class MainActivityPresenter {
* Getting the served date.
*/
public String getDate() {
return model.getDate("date");
return model.getDate();
}
public void checkDate() {
......
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="sharedpreference_file">org.torproject.snowflake.snowflake_preferences</string>
<string name="users_served_key">users_served</string>
<string name="served_date_key">date</string>
<string name="initial_run_boolean_key">initial_run</string>
<string name="is_service_running_bool_key">is_service_running</string>
</resources>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment