1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-28 11:35:48 +01:00

Extract inner classes from AbstractChartFragment

No code changes, will simplify future refactor and reuse.
This commit is contained in:
José Rebelo 2023-05-25 23:39:58 +01:00
parent b091521155
commit 79e3df4844
7 changed files with 180 additions and 92 deletions

View File

@ -44,6 +44,7 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractChartFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsData;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.DefaultChartsData;
import nodomain.freeyourgadget.gadgetbridge.database.DBAccess;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;

View File

@ -52,7 +52,8 @@ import java.util.List;
import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractChartFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.DefaultChartsData;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.TimestampTranslation;
import nodomain.freeyourgadget.gadgetbridge.database.DBAccess;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
@ -94,7 +95,7 @@ public class BatteryInfoChartFragment extends AbstractGBFragment {
}
private DefaultBatteryChartsData fill_dcd(List<? extends BatteryLevel> samples) {
AbstractChartFragment.TimestampTranslation tsTranslation = new AbstractChartFragment.TimestampTranslation();
TimestampTranslation tsTranslation = new TimestampTranslation();
List<Entry> entries = new ArrayList<Entry>();
int firstTs = 0;
@ -192,11 +193,11 @@ public class BatteryInfoChartFragment extends AbstractGBFragment {
}
protected static class customFormatter extends ValueFormatter {
private final AbstractChartFragment.TimestampTranslation tsTranslation;
private final TimestampTranslation tsTranslation;
SimpleDateFormat annotationDateFormat = new SimpleDateFormat("dd.MM HH:mm");
Calendar cal = GregorianCalendar.getInstance();
public customFormatter(AbstractChartFragment.TimestampTranslation tsTranslation) {
public customFormatter(TimestampTranslation tsTranslation) {
this.tsTranslation = tsTranslation;
}
@ -239,7 +240,7 @@ public class BatteryInfoChartFragment extends AbstractGBFragment {
}
}
private class DefaultBatteryChartsData extends AbstractChartFragment.DefaultChartsData {
private class DefaultBatteryChartsData extends DefaultChartsData {
public int firstTs;
public DefaultBatteryChartsData(ChartData data, ValueFormatter xValueFormatter, int ts) {
@ -259,7 +260,7 @@ public class BatteryInfoChartFragment extends AbstractGBFragment {
public batteryValuesAndDateMarker(Context context, int layoutResource, int ts) {
super(context, layoutResource);
AbstractChartFragment.TimestampTranslation tsTranslation = new AbstractChartFragment.TimestampTranslation();
TimestampTranslation tsTranslation = new TimestampTranslation();
formatter = new customFormatter(tsTranslation);
top_text = (TextView) findViewById(R.id.chart_marker_item_top);
bottom_text = (TextView) findViewById(R.id.chart_marker_item_bottom);

View File

@ -35,7 +35,6 @@ import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
@ -45,7 +44,6 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@ -826,88 +824,4 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
private int toTimestamp(Date date) {
return (int) ((date.getTime() / 1000));
}
public static class DefaultChartsData<T extends ChartData<?>> extends ChartsData {
private final T data;
private ValueFormatter xValueFormatter;
public DefaultChartsData(T data, ValueFormatter xValueFormatter) {
this.xValueFormatter = xValueFormatter;
this.data = data;
}
public ValueFormatter getXValueFormatter() {
return xValueFormatter;
}
public T getData() {
return data;
}
}
protected static class SampleXLabelFormatter extends ValueFormatter {
private final TimestampTranslation tsTranslation;
SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm");
// SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Calendar cal = GregorianCalendar.getInstance();
public SampleXLabelFormatter(TimestampTranslation tsTranslation) {
this.tsTranslation = tsTranslation;
}
// TODO: this does not work. Cannot use precomputed labels
@Override
public String getFormattedValue(float value) {
cal.clear();
int ts = (int) value;
cal.setTimeInMillis(tsTranslation.toOriginalValue(ts) * 1000L);
Date date = cal.getTime();
String dateString = annotationDateFormat.format(date);
return dateString;
}
}
protected static class PreformattedXIndexLabelFormatter extends ValueFormatter {
private ArrayList<String> xLabels;
public PreformattedXIndexLabelFormatter(ArrayList<String> xLabels) {
this.xLabels = xLabels;
}
@Override
public String getFormattedValue(float value) {
int index = (int) value;
if (xLabels == null || index >= xLabels.size()) {
return String.valueOf(value);
}
return xLabels.get(index);
}
}
/**
* Awkward class that helps in translating long timestamp
* values to float (sic!) values. It basically rebases all
* timestamps to a base (the very first) timestamp value.
*
* It does this so that the large timestamp values can be used
* floating point values, where the mantissa is just 24 bits.
*/
public static class TimestampTranslation {
private int tsOffset = -1;
public int shorten(int timestamp) {
if (tsOffset == -1) {
tsOffset = timestamp;
return 0;
}
return timestamp - tsOffset;
}
public int toOriginalValue(int timestamp) {
if (tsOffset == -1) {
return timestamp;
}
return timestamp + tsOffset;
}
}
}

View File

@ -0,0 +1,39 @@
/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer,
Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.formatter.ValueFormatter;
public class DefaultChartsData<T extends ChartData<?>> extends ChartsData {
private final T data;
private final ValueFormatter xValueFormatter;
public DefaultChartsData(final T data, final ValueFormatter xValueFormatter) {
this.xValueFormatter = xValueFormatter;
this.data = data;
}
public ValueFormatter getXValueFormatter() {
return xValueFormatter;
}
public T getData() {
return data;
}
}

View File

@ -0,0 +1,39 @@
/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer,
Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import com.github.mikephil.charting.formatter.ValueFormatter;
import java.util.ArrayList;
class PreformattedXIndexLabelFormatter extends ValueFormatter {
private final ArrayList<String> xLabels;
public PreformattedXIndexLabelFormatter(ArrayList<String> xLabels) {
this.xLabels = xLabels;
}
@Override
public String getFormattedValue(float value) {
int index = (int) value;
if (xLabels == null || index >= xLabels.size()) {
return String.valueOf(value);
}
return xLabels.get(index);
}
}

View File

@ -0,0 +1,49 @@
/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer,
Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.annotation.SuppressLint;
import com.github.mikephil.charting.formatter.ValueFormatter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
class SampleXLabelFormatter extends ValueFormatter {
private final TimestampTranslation tsTranslation;
@SuppressLint("SimpleDateFormat")
private final SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm");
// SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
private final Calendar cal = GregorianCalendar.getInstance();
public SampleXLabelFormatter(final TimestampTranslation tsTranslation) {
this.tsTranslation = tsTranslation;
}
// TODO: this does not work. Cannot use precomputed labels
@Override
public String getFormattedValue(final float value) {
cal.clear();
final int ts = (int) value;
cal.setTimeInMillis(tsTranslation.toOriginalValue(ts) * 1000L);
final Date date = cal.getTime();
return annotationDateFormat.format(date);
}
}

View File

@ -0,0 +1,45 @@
/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer,
Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
/**
* Awkward class that helps in translating long timestamp
* values to float (sic!) values. It basically rebases all
* timestamps to a base (the very first) timestamp value.
* <p>
* It does this so that the large timestamp values can be used
* floating point values, where the mantissa is just 24 bits.
*/
public class TimestampTranslation {
private int tsOffset = -1;
public int shorten(int timestamp) {
if (tsOffset == -1) {
tsOffset = timestamp;
return 0;
}
return timestamp - tsOffset;
}
public int toOriginalValue(int timestamp) {
if (tsOffset == -1) {
return timestamp;
}
return timestamp + tsOffset;
}
}