improved code style

This commit is contained in:
paul-loedige 2021-01-08 03:03:01 +01:00
parent ffe2e6dcd6
commit 4d9edac29a

View File

@ -1,17 +1,12 @@
package com.example.aped.ui.plots;
import android.graphics.Color;
import android.graphics.ColorSpace;
import android.net.sip.SipSession;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.aped.MainActivity;
import com.example.aped.R;
import com.example.aped.communication.Communicator;
@ -21,7 +16,6 @@ import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
@ -34,17 +28,17 @@ public class PlotFragment extends Fragment {
private MainActivity mainActivity;
/** List of all the devices with buffers. **/
private List<String> bufferedDevices;
/** Line Chart View that is displayed **/
/** Line Chart View that is displayed. **/
private GraphView graphView;
/** maximum value on X axis. **/
private int maxX = 0;
/**
* paul
* @param inflater
* @param container
* Override method for the on create view event.
* @param inflater the default {@link LayoutInflater}
* @param container the {@link ViewGroup} the fragment is in
* @param savedInstanceState
* @return
* @return the created view
*/
@Override
public View onCreateView(final LayoutInflater inflater,
@ -64,6 +58,11 @@ public class PlotFragment extends Fragment {
return view;
}
/**
* Method to create a list of all the devices with buffers.
* @param xml the {@link IXML} to pull the information about the devices
* @return a list of all the device names
*/
private List<String> getBufferedDevices(final IXML xml) {
List<String> returnList = new ArrayList<>();
for (String deviceName : xml.getDeviceNames()) {
@ -74,10 +73,17 @@ public class PlotFragment extends Fragment {
return returnList;
}
private void getBufferedValues(final Communicator communicator, final IXML xml) {
/**
* Method to request and display all the buffered data.
* @param communicator the {@link Communicator} to request the data
* @param xml the {@link IXML} to get information about the data
*/
private void getBufferedValues(final Communicator communicator,
final IXML xml) {
Random random = new Random();
for (String deviceName : this.bufferedDevices) {
int factor;
//normalize PWM and digital data
if (xml.getPort(deviceName).get("protocol").equals("PWM")) {
factor = 1;
} else {
@ -86,10 +92,13 @@ public class PlotFragment extends Fragment {
communicator.readBuffer(deviceName, response -> {
try {
JSONObject buffer = response.getJSONObject("buffer");
for (String pin : (List<String>) (xml.getPort(deviceName)).get("pins")) {
for (String pin : (List<String>) (xml.getPort(deviceName))
.get("pins")) {
String valueString = buffer.getString(pin);
valueString = valueString.substring(2, valueString.length() - 2);
String[] valueStringArray = valueString.split("', '");
valueString = valueString
.substring(2, valueString.length() - 2);
String[] valueStringArray = valueString
.split("', '");
int[] values = new int[valueStringArray.length];
for (int i = 0; i < valueStringArray.length; i++) {
values[i] = Integer.parseInt(valueStringArray[i]);
@ -98,11 +107,16 @@ public class PlotFragment extends Fragment {
//display data
DataPoint[] dataPoints = new DataPoint[values.length];
for (int i = 0; i < values.length; i++) {
dataPoints[i] = new DataPoint(i, values[i] * factor);
dataPoints[i] = new DataPoint(i,
values[i] * factor);
}
LineGraphSeries<DataPoint> lineGraphSeries = new LineGraphSeries<>(dataPoints);
LineGraphSeries<DataPoint> lineGraphSeries =
new LineGraphSeries<>(dataPoints);
lineGraphSeries.setTitle(deviceName + ":" + pin);
lineGraphSeries.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
lineGraphSeries.setColor(Color.rgb(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256)));
graphView.addSeries(lineGraphSeries);
if (maxX < values.length) {
maxX = values.length;
@ -113,10 +127,14 @@ public class PlotFragment extends Fragment {
graphView.getViewport().setXAxisBoundsManual(true);
graphView.getViewport().setYAxisBoundsManual(true);
graphView.getLegendRenderer().setVisible(true);
graphView.getLegendRenderer().setBackgroundColor(Color.rgb(200,200,200));
graphView.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
graphView.getLegendRenderer().setBackgroundColor(Color.rgb(
200, 200, 200));
graphView.getLegendRenderer()
.setAlign(LegendRenderer.LegendAlign.TOP);
} catch (JSONException e) {
Log.e("PlotFragment", "error while parsing JSON on READ BUFFER: " + e.getMessage());
Log.e("PlotFragment",
"error while parsing JSON on READ BUFFER: "
+ e.getMessage());
}
});
}