mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-01 06:20:33 +02:00
load dynamically measurement data to speed up loading time on rolling chart, see issue #431
This commit is contained in:
@@ -31,6 +31,12 @@ import android.provider.OpenableColumns;
|
||||
import android.text.format.DateFormat;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
|
||||
import com.health.openscale.R;
|
||||
import com.health.openscale.core.alarm.AlarmHandler;
|
||||
import com.health.openscale.core.bluetooth.BluetoothCommunication;
|
||||
@@ -69,11 +75,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class OpenScale {
|
||||
@@ -548,6 +549,17 @@ public class OpenScale {
|
||||
return numOfMonth;
|
||||
}
|
||||
|
||||
public List<ScaleMeasurement> getScaleDataOfStartDate(int year, int month, int day) {
|
||||
int selectedUserId = getSelectedScaleUserId();
|
||||
|
||||
Calendar startCalender = Calendar.getInstance();
|
||||
Calendar endCalender = Calendar.getInstance();
|
||||
|
||||
startCalender.set(year, month, day, 0, 0, 0);
|
||||
|
||||
return measurementDAO.getAllInRange(startCalender.getTime(), endCalender.getTime(), selectedUserId);
|
||||
}
|
||||
|
||||
public List<ScaleMeasurement> getScaleDataOfDay(int year, int month, int day) {
|
||||
int selectedUserId = getSelectedScaleUserId();
|
||||
|
||||
|
@@ -114,6 +114,7 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
|
||||
chartView = graphView.findViewById(R.id.chartView);
|
||||
chartView.setOnChartValueSelectedListener(new onChartValueSelectedListener());
|
||||
chartView.setProgressBar(graphView.findViewById(R.id.progressBar));
|
||||
|
||||
chartTop = graphView.findViewById(R.id.chart_top);
|
||||
chartTop.setDoubleTapToZoomEnabled(false);
|
||||
|
@@ -35,6 +35,8 @@ import android.widget.TableLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.highlight.Highlight;
|
||||
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
|
||||
@@ -51,8 +53,6 @@ import com.health.openscale.gui.views.MeasurementView;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
public class OverviewFragment extends Fragment implements FragmentUpdateListener {
|
||||
private View overviewView;
|
||||
|
||||
@@ -89,6 +89,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
|
||||
chartView = overviewView.findViewById(R.id.chartView);
|
||||
chartView.setOnChartValueSelectedListener(new onChartSelectedListener());
|
||||
chartView.setProgressBar(overviewView.findViewById(R.id.progressBar));
|
||||
chartView.setAnimationOn(false);
|
||||
chartView.setIsInGraphKey(false);
|
||||
|
||||
|
@@ -18,10 +18,13 @@ package com.health.openscale.gui.views;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.RectF;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.github.mikephil.charting.charts.LineChart;
|
||||
@@ -34,6 +37,8 @@ import com.github.mikephil.charting.data.LineData;
|
||||
import com.github.mikephil.charting.data.LineDataSet;
|
||||
import com.github.mikephil.charting.formatter.ValueFormatter;
|
||||
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
|
||||
import com.github.mikephil.charting.listener.ChartTouchListener;
|
||||
import com.github.mikephil.charting.listener.OnChartGestureListener;
|
||||
import com.github.mikephil.charting.utils.Utils;
|
||||
import com.health.openscale.R;
|
||||
import com.health.openscale.core.OpenScale;
|
||||
@@ -77,6 +82,9 @@ public class ChartMeasurementView extends LineChart {
|
||||
private ViewMode viewMode;
|
||||
private boolean isAnimationOn;
|
||||
private boolean isInGraphKey;
|
||||
private int scrollHistoryCount;
|
||||
private ProgressBar progressBar;
|
||||
private boolean isRollingChart;
|
||||
|
||||
public ChartMeasurementView(Context context) {
|
||||
super(context);
|
||||
@@ -94,17 +102,49 @@ public class ChartMeasurementView extends LineChart {
|
||||
}
|
||||
|
||||
public void setViewRange(final ViewMode mode, boolean rollingChart) {
|
||||
setMeasurementList(openScale.getScaleMeasurementList());
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
isRollingChart = rollingChart;
|
||||
|
||||
if (isRollingChart) {
|
||||
ScaleMeasurement lastMeasurement = openScale.getLatestScaleMeasurement(openScale.getSelectedScaleUserId());
|
||||
|
||||
if (lastMeasurement != null) {
|
||||
Calendar lastMeasurementCalender = Calendar.getInstance();
|
||||
lastMeasurementCalender.setTime(lastMeasurement.getDateTime());
|
||||
|
||||
switch (mode) {
|
||||
case DAY_OF_ALL:
|
||||
lastMeasurementCalender.add(Calendar.DAY_OF_MONTH, -28 * scrollHistoryCount);
|
||||
break;
|
||||
case WEEK_OF_ALL:
|
||||
lastMeasurementCalender.add(Calendar.WEEK_OF_YEAR, -4 * scrollHistoryCount);
|
||||
break;
|
||||
case MONTH_OF_ALL:
|
||||
lastMeasurementCalender.add(Calendar.MONTH, -4 * scrollHistoryCount);
|
||||
break;
|
||||
case YEAR_OF_ALL:
|
||||
lastMeasurementCalender.add(Calendar.YEAR, -4 * scrollHistoryCount);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("view mode not implemented");
|
||||
}
|
||||
|
||||
setMeasurementList(openScale.getScaleDataOfStartDate(lastMeasurementCalender.get(Calendar.YEAR), lastMeasurementCalender.get(Calendar.MONTH), lastMeasurementCalender.get(Calendar.DAY_OF_MONTH)));
|
||||
}
|
||||
} else {
|
||||
setMeasurementList(openScale.getScaleMeasurementList());
|
||||
}
|
||||
setViewMode(mode);
|
||||
|
||||
refresh();
|
||||
|
||||
if (rollingChart) {
|
||||
if (isRollingChart) {
|
||||
setRollingChartOn(mode);
|
||||
}
|
||||
}
|
||||
|
||||
public void setViewRange(int year, final ViewMode mode) {
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
setMeasurementList(openScale.getScaleDataOfYear(year));
|
||||
setViewMode(mode);
|
||||
|
||||
@@ -112,6 +152,7 @@ public class ChartMeasurementView extends LineChart {
|
||||
}
|
||||
|
||||
public void setViewRange(int year, int month, final ViewMode mode) {
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
setMeasurementList(openScale.getScaleDataOfMonth(year, month));
|
||||
setViewMode(mode);
|
||||
|
||||
@@ -126,6 +167,10 @@ public class ChartMeasurementView extends LineChart {
|
||||
isInGraphKey = status;
|
||||
}
|
||||
|
||||
public void setProgressBar(ProgressBar bar) {
|
||||
progressBar = bar;
|
||||
}
|
||||
|
||||
private void initChart() {
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
openScale = OpenScale.getInstance();
|
||||
@@ -138,6 +183,8 @@ public class ChartMeasurementView extends LineChart {
|
||||
minXValue = 0;
|
||||
isAnimationOn = true;
|
||||
isInGraphKey = true;
|
||||
scrollHistoryCount = 1;
|
||||
progressBar = null;
|
||||
|
||||
setHardwareAccelerationEnabled(true);
|
||||
setMarker(new ChartMarkerView(getContext(), R.layout.chart_markerview));
|
||||
@@ -154,6 +201,62 @@ public class ChartMeasurementView extends LineChart {
|
||||
getAxisRight().setTextColor(ColorUtil.getTextColor(getContext()));
|
||||
getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
|
||||
getXAxis().setTextColor(ColorUtil.getTextColor(getContext()));
|
||||
|
||||
setOnChartGestureListener(new OnChartGestureListener() {
|
||||
@Override
|
||||
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
|
||||
if (isRollingChart) {
|
||||
if (progressBar.getVisibility() == GONE) {
|
||||
if (Math.round(getLowestVisibleX()) == Math.round(getXChartMin())) {
|
||||
scrollHistoryCount++;
|
||||
setViewRange(viewMode, isRollingChart);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChartLongPressed(MotionEvent me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChartDoubleTapped(MotionEvent me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChartSingleTapped(MotionEvent me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChartTranslate(MotionEvent me, float dX, float dY) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
||||
progressBar.setVisibility(GONE);
|
||||
}
|
||||
|
||||
private void setViewMode(final ViewMode mode) {
|
||||
|
@@ -153,4 +153,11 @@
|
||||
android:layout_gravity="bottom|end"
|
||||
app:pressedTranslationZ="12dp"/>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progressBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_anchor="@id/chartView"
|
||||
app:layout_anchorGravity="center"
|
||||
/>
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
|
@@ -102,4 +102,12 @@
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progressBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_anchor="@id/chartView"
|
||||
app:layout_anchorGravity="center"
|
||||
/>
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
|
Reference in New Issue
Block a user