Skip to content

Commit

Permalink
Introduced mvp to main activity and extracted some nested classes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomcz committed Apr 23, 2017
1 parent 15dc2b2 commit 062911a
Show file tree
Hide file tree
Showing 62 changed files with 578 additions and 4,537 deletions.
77 changes: 77 additions & 0 deletions app/src/main/java/hrv/band/app/device/ConnectionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 1,77 @@
package hrv.band.app.device;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

import hrv.band.app.device.antplus.AntPlusRRDataDevice;
import hrv.band.app.device.msband.MSBandRRIntervalDevice;

/**
* Copyright (c) 2017
* Created by Thomas Czogalik on 23.04.2017
*/
public class ConnectionManager {
private Activity activity;
private SharedPreferences sharedPreferences;

private static final String SELECTED_DEVICE_ID = "selected_device_id";

public ConnectionManager(Activity activity) {
this.activity = activity;
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity);
}

/**
* Connects to ms band.
*/
public HRVRRIntervalDevice connectToMSBand() {
setDevice(Device.MSBAND);
return connectToIntervalDevice();
}

@NonNull
private HRVRRIntervalDevice connectToIntervalDevice() {
HRVRRIntervalDevice hrvRRIntervalDevice = getDevice();
hrvRRIntervalDevice.connect();
return hrvRRIntervalDevice;
}

/**
* Connects to ant device.
*/
public HRVRRIntervalDevice connectToAnt() {
setDevice(Device.ANT);
return connectToIntervalDevice();
}

/**
* Disconnects connected device.
*/
public HRVRRIntervalDevice disconnectDevices(HRVRRIntervalDevice hrvRRIntervalDevice) {
setDevice(Device.NONE);
if (hrvRRIntervalDevice != null) {
hrvRRIntervalDevice.notifyDeviceStatusChanged(HRVDeviceStatus.DISCONNECTED);
}
return null;
}

public HRVRRIntervalDevice getDevice() {
Device device = Device.values()[sharedPreferences.getInt(SELECTED_DEVICE_ID, 0)];
switch (device) {
case MSBAND:
return new MSBandRRIntervalDevice(activity);
case ANT:
return new AntPlusRRDataDevice(activity);
default:
return null;
}
}

private void setDevice(Device device) {
SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
prefsEditor.putInt(SELECTED_DEVICE_ID, device.ordinal());
prefsEditor.apply();
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/hrv/band/app/device/Device.java
Original file line number Diff line number Diff line change
@@ -0,0 1,12 @@
package hrv.band.app.device;

/**
* Copyright (c) 2017
* Created by Thomas on 23.04.2017.
*
* Possible devices the user can select.
*/

public enum Device {
NONE, MSBAND, ANT
}
Original file line number Diff line number Diff line change
@@ -1,7 1,6 @@
package hrv.band.app.device.antplus;

import android.app.Activity;
import android.content.Context;

import com.dsi.ant.plugins.antplus.pcc.AntPlusHeartRatePcc;
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
Expand All @@ -25,12 24,10 @@ public class AntPlusRRDataDevice
AntPluginPcc.IPluginAccessResultReceiver<AntPlusHeartRatePcc>,
AntPlusHeartRatePcc.ICalculatedRrIntervalReceiver {

private final Context context;
private final Activity activity;
private AntPlusHeartRatePcc wgtplc;

public AntPlusRRDataDevice(Context context, Activity activity) {
this.context = context;
public AntPlusRRDataDevice(Activity activity) {
this.activity = activity;
}

Expand Down Expand Up @@ -107,7 104,7 @@ public void connect() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
AntPlusHeartRatePcc.requestAccess(activity, context, helper, helper);
AntPlusHeartRatePcc.requestAccess(activity, activity.getApplicationContext(), helper, helper);
}
});
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/hrv/band/app/ui/presenter/IMainPresenter.java
Original file line number Diff line number Diff line change
@@ -0,0 1,11 @@
package hrv.band.app.ui.presenter;

/**
* Copyright (c) 2017
* Created by Thomas on 23.04.2017.
*/

public interface IMainPresenter {
boolean agreedToDisclaimer();
void handleNavBar(int id);
}
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
package hrv.band.app.ui.presenter;

import java.util.Date;
import java.util.List;

import hrv.band.app.model.Measurement;

/**
* Copyright (c) 2017
* Created by Thomas Czogalik on 23.04.2017
*/

public interface IMeasuringPresenter {
int getDuration();

Measurement createMeasurement(List<Double> rrInterval, Date time);
}
66 changes: 66 additions & 0 deletions app/src/main/java/hrv/band/app/ui/presenter/MainPresenter.java
Original file line number Diff line number Diff line change
@@ -0,0 1,66 @@
package hrv.band.app.ui.presenter;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import hrv.band.app.R;
import hrv.band.app.ui.view.activity.IMainView;
import hrv.band.app.ui.view.activity.ImprintActivity;
import hrv.band.app.ui.view.activity.IntroActivity;
import hrv.band.app.ui.view.activity.SettingsActivity;
import hrv.band.app.ui.view.activity.WebActivity;
import hrv.band.app.ui.view.fragment.DisclaimerDialogFragment;

import static hrv.band.app.ui.view.activity.web.WebsiteUrls.WEBSITE_PRIVACY_URL;
import static hrv.band.app.ui.view.activity.web.WebsiteUrls.WEBSITE_URL;

/**
* Copyright (c) 2017
* Created by Thomas on 23.04.2017.
*/

public class MainPresenter implements IMainPresenter {
private IMainView mainView;

public MainPresenter(IMainView mainView) {
this.mainView = mainView;
}

@Override
public boolean agreedToDisclaimer() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mainView.getMainActivity());
return sharedPreferences.getBoolean(DisclaimerDialogFragment.DISCLAIMER_AGREEMENT, false);
}

@Override
public void handleNavBar(int id) {
switch (id) {
case R.id.menu_help:
mainView.startActivity(IntroActivity.class);
break;
case R.id.menu_website:
mainView.startActivity(WebActivity.class, WebActivity.WEBSITE_URL_ID, WEBSITE_URL);
break;
case R.id.menu_share:
mainView.openShareIntent();
break;
case R.id.menu_privacy:
mainView.startActivity(WebActivity.class, WebActivity.WEBSITE_URL_ID, WEBSITE_PRIVACY_URL);
break;
case R.id.menu_feedback:
mainView.startFeedbackDialog();
break;
case R.id.menu_imprint:
mainView.startActivity(ImprintActivity.class);
break;
case R.id.menu_rate:
mainView.rateApp();
break;
case R.id.menu_Settings:
mainView.startActivity(SettingsActivity.class);
break;
default:
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 1,49 @@
package hrv.band.app.ui.presenter;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.Date;
import java.util.List;

import common.ArrayUtils;
import hrv.RRData;
import hrv.band.app.model.Measurement;
import hrv.band.app.ui.view.fragment.IMeasuringView;

/**
* Copyright (c) 2017
* Created by Thomas Czogalik on 23.04.2017
*/

public class MeasuringPresenter implements IMeasuringPresenter {
private SharedPreferences sharedPreferences;

private static final String RECORDING_LENGTH_ID = "recording_length";


public MeasuringPresenter(IMeasuringView measuringView) {
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(measuringView.getParentActivity());
}

@Override
public int getDuration() {
//TODO: int statt string
String durationPrefVal = sharedPreferences.getString(RECORDING_LENGTH_ID, "90");
return Integer.parseInt(durationPrefVal) * 1000;
}

@Override
public Measurement createMeasurement(List<Double> rrInterval, Date time) {
//start calculation
double[] rrArray = convertListToDouble(rrInterval);

RRData.createFromRRInterval(rrArray, units.TimeUnit.SECOND);
Measurement.MeasurementBuilder measurementBuilder = Measurement.from(time, rrArray);
return measurementBuilder.build();
}

private double[] convertListToDouble(List<Double> doubles) {
return ArrayUtils.toPrimitiveIgnoreNull(doubles.toArray(new Double[0]));
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/hrv/band/app/ui/view/activity/IMainView.java
Original file line number Diff line number Diff line change
@@ -0,0 1,25 @@
package hrv.band.app.ui.view.activity;

import android.app.Activity;

/**
* Copyright (c) 2017
* Created by Thomas on 23.04.2017.
*/

public interface IMainView {
void startActivity(Class<? extends Activity> activity);

void startActivity(Class<? extends Activity> activity, String extraId, String extra);

void startFeedbackDialog();

Activity getMainActivity();

void openShareIntent();

void rateApp();

void stopMeasuring();

}
Loading

0 comments on commit 062911a

Please sign in to comment.