Verified Commit 6c8056d2 authored by HashikD's avatar HashikD
Browse files

Added contract interface

parent 3c049a86
......@@ -18,12 +18,13 @@ import org.torproject.snowflake.constants.FragmentConstants;
import org.torproject.snowflake.fragments.AppSettingsFragment;
import org.torproject.snowflake.fragments.MainFragment;
import org.torproject.snowflake.interfaces.MainFragmentCallback;
import org.torproject.snowflake.mvp.MainActivityMVP;
import org.torproject.snowflake.presenters.MainActivityPresenter;
/**
* MainActivity is the main UI of the application.
*/
public class MainActivity extends AppCompatActivity implements MainFragmentCallback, MainActivityPresenter.View {
public class MainActivity extends AppCompatActivity implements MainFragmentCallback, MainActivityMVP.View {
private static final String TAG = "MainActivity";
int currentFragment;
MainActivityPresenter presenter;
......
......@@ -5,7 +5,7 @@ import android.util.Log;
import org.torproject.snowflake.GlobalApplication;
import org.torproject.snowflake.constants.AppPreferenceConstants;
import org.torproject.snowflake.presenters.MainActivityPresenter;
import org.torproject.snowflake.mvp.MainActivityMVP;
import java.text.ParseException;
import java.text.SimpleDateFormat;
......@@ -19,22 +19,22 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
/**
* Model for MainActivity to handle network calls, Shared preferences.
*/
public class MainActivityModel {
public class MainActivityModel implements MainActivityMVP.Model {
private static final String TAG = "MainActivityModel";
private static MainActivityModel instance = null;
private SharedPreferences sharedPreferences;
private MainActivityPresenter presenter;
private MainActivityMVP.Presenter presenter;
private int servedCount;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private MainActivityModel(MainActivityPresenter presenter) {
private MainActivityModel(MainActivityMVP.Presenter presenter) {
sharedPreferences = GlobalApplication.getAppPreferences();
this.presenter = presenter;
servedCount = 0;
}
public static MainActivityModel getInstance(MainActivityPresenter presenter) {
public static MainActivityModel getInstance(MainActivityMVP.Presenter presenter) {
if (instance == null) {
synchronized (MainActivityModel.class) {
instance = new MainActivityModel(presenter);
......@@ -43,16 +43,16 @@ public class MainActivityModel {
return instance;
}
public int getServedCount(String key) {
return sharedPreferences.getInt(key, 0);
public int getServedCount() {
return sharedPreferences.getInt(AppPreferenceConstants.USER_SERVED_KEY, 0);
}
public boolean getInitialRunBool(String key) {
return sharedPreferences.getBoolean(key, true);
public boolean getInitialRunBool() {
return sharedPreferences.getBoolean(AppPreferenceConstants.INITIAL_RUN_KEY, true);
}
public void setInitialRunBool(String key, boolean val) {
sharedPreferences.edit().putBoolean(key, val).apply();
public void setInitialRunBool(boolean val) {
sharedPreferences.edit().putBoolean(AppPreferenceConstants.INITIAL_RUN_KEY, val).apply();
}
public boolean isServiceRunning() {
......@@ -107,7 +107,7 @@ public class MainActivityModel {
try {
String stringCurrentDate = simpleDateFormat.format(Calendar.getInstance().getTime());
String stringRecordedDate = presenter.getDate();
String stringRecordedDate = getDate();
//No value for key. Set the date value to current date and users served to 0.
if (stringRecordedDate.equals("")) {
......@@ -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(AppPreferenceConstants.USER_SERVED_KEY));
presenter.updateServedCount(getServedCount());
setListenerForCount();
});
}
......
package org.torproject.snowflake.mvp;
/**
* MVP contract Interface
*/
public interface MainActivityMVP {
interface View {
void updateCountInFragment(int i);
}
interface Model {
boolean getInitialRunBool();
void setInitialRunBool(boolean val);
boolean isServiceRunning();
void checkDateAsync();
int getServedCount();
}
interface Presenter {
int getServedCount();
boolean getInitialRunBoolean();
void setInitialRunBoolean(boolean val);
boolean isServiceRunning();
void updateServedCount(int count);
void checkDate();
}
}
......@@ -2,20 +2,18 @@ package org.torproject.snowflake.presenters;
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;
import org.torproject.snowflake.mvp.MainActivityMVP;
/**
* Presenter for MainActivity.
*/
public class MainActivityPresenter {
public class MainActivityPresenter implements MainActivityMVP.Presenter {
private static final String TAG = "MainActivityPresenter";
View view;
MainActivityModel model;
MainActivityMVP.View view;
MainActivityMVP.Model model;
public MainActivityPresenter(View view) {
public MainActivityPresenter(MainActivityMVP.View view) {
//Attaching
this.view = view;
model = MainActivityModel.getInstance(this);
......@@ -33,7 +31,7 @@ public class MainActivityPresenter {
public int getServedCount() {
Log.d(TAG, "getServedCount: ");
if (view != null) {
return model.getServedCount(AppPreferenceConstants.USER_SERVED_KEY);
return model.getServedCount();
}
return 0;
}
......@@ -41,7 +39,7 @@ public class MainActivityPresenter {
public boolean getInitialRunBoolean() {
Log.d(TAG, "getInitialRunBoolean: ");
if (view != null) {
return model.getInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY);
return model.getInitialRunBool();
}
return false;
}
......@@ -54,7 +52,7 @@ public class MainActivityPresenter {
public void setInitialRunBoolean(boolean val) {
Log.d(TAG, "setInitialRunBoolean: ");
if (view != null) {
model.setInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY, val);
model.setInitialRunBool(val);
}
}
......@@ -78,12 +76,6 @@ public class MainActivityPresenter {
}
}
/**
* Getting the served date.
*/
public String getDate() {
return model.getDate();
}
public void checkDate() {
model.checkDateAsync();
......
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