use of com.health.openscale.gui.measurement.MeasurementView in project openScale by oliexdev.
the class WidgetProvider method updateWidget.
private void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
// Make sure we use the correct language
context = MainActivity.createBaseContext(context);
final int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int userId = prefs.getInt(getUserIdPreferenceName(appWidgetId), -1);
String key = prefs.getString(getMeasurementPreferenceName(appWidgetId), "");
Timber.d("Update widget %d (%s) for user %d, min width %ddp", appWidgetId, key, userId, minWidth);
if (measurementViews == null) {
measurementViews = MeasurementView.getMeasurementList(context, MeasurementView.DateTimeOrder.NONE);
}
MeasurementView measurementView = measurementViews.get(0);
for (MeasurementView view : measurementViews) {
if (view.getKey().equals(key)) {
measurementView = view;
break;
}
}
OpenScale openScale = OpenScale.getInstance();
ScaleMeasurement latest = openScale.getLastScaleMeasurement(userId);
if (latest != null) {
ScaleMeasurement previous = openScale.getTupleOfScaleMeasurement(latest.getId())[0];
measurementView.loadFrom(latest, previous);
}
// From https://developer.android.com/guide/practices/ui_guidelines/widget_design
final int twoCellsMinWidth = 110;
final int thirdCellsMinWidth = 180;
final int fourCellsMinWidth = 250;
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
// Add some transparency to make the corners appear rounded
int indicatorColor = measurementView.getIndicatorColor();
indicatorColor = (180 << 24) | (indicatorColor & 0xffffff);
views.setInt(R.id.indicator_view, "setBackgroundColor", indicatorColor);
// Show icon in >= two cell mode
if (minWidth >= twoCellsMinWidth) {
views.setImageViewResource(R.id.widget_icon, measurementView.getIconResource());
views.setViewVisibility(R.id.widget_icon, View.VISIBLE);
views.setViewVisibility(R.id.widget_icon_vertical, View.GONE);
} else {
views.setImageViewResource(R.id.widget_icon_vertical, measurementView.getIconResource());
views.setViewVisibility(R.id.widget_icon_vertical, View.VISIBLE);
views.setViewVisibility(R.id.widget_icon, View.GONE);
}
// Show measurement name in >= four cell mode
if (minWidth >= fourCellsMinWidth) {
views.setTextViewText(R.id.widget_name, measurementView.getName());
views.setTextViewText(R.id.widget_date, latest != null ? DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(latest.getDateTime()) : "");
views.setViewVisibility(R.id.widget_name_date_layout, View.VISIBLE);
} else {
views.setViewVisibility(R.id.widget_name_date_layout, View.GONE);
}
// Always show value and delta, but adjust font size based on widget width
views.setTextViewText(R.id.widget_value, measurementView.getValueAsString(true));
SpannableStringBuilder delta = new SpannableStringBuilder();
measurementView.appendDiffValue(delta, false);
views.setTextViewText(R.id.widget_delta, delta);
int textSize;
if (minWidth >= thirdCellsMinWidth) {
textSize = 18;
} else if (minWidth >= twoCellsMinWidth) {
textSize = 17;
} else {
textSize = 12;
}
views.setTextViewTextSize(R.id.widget_value, TypedValue.COMPLEX_UNIT_DIP, textSize);
views.setTextViewTextSize(R.id.widget_delta, TypedValue.COMPLEX_UNIT_DIP, textSize);
// Start main activity when widget is clicked
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
use of com.health.openscale.gui.measurement.MeasurementView in project openScale by oliexdev.
the class StatisticsFragment method updateStatistics.
private void updateStatistics(List<ScaleMeasurement> scaleMeasurementList) {
radarChartWeek.clear();
radarChartMonth.clear();
Calendar histDate = Calendar.getInstance();
Calendar weekPastDate = Calendar.getInstance();
Calendar monthPastDate = Calendar.getInstance();
weekPastDate.setTime(lastScaleMeasurement.getDateTime());
weekPastDate.add(Calendar.DATE, -7);
monthPastDate.setTime(lastScaleMeasurement.getDateTime());
monthPastDate.add(Calendar.DATE, -30);
ScaleMeasurement averageWeek = new ScaleMeasurement();
ScaleMeasurement averageMonth = new ScaleMeasurement();
ArrayList<RadarEntry> entriesLastMeasurement = new ArrayList<>();
ArrayList<RadarEntry> entriesAvgWeek = new ArrayList<>();
ArrayList<RadarEntry> entriesAvgMonth = new ArrayList<>();
for (ScaleMeasurement measurement : scaleMeasurementList) {
histDate.setTime(measurement.getDateTime());
if (weekPastDate.before(histDate)) {
averageWeek.add(measurement);
}
if (monthPastDate.before(histDate)) {
averageMonth.add(measurement);
}
}
averageWeek.divide(averageWeek.count());
averageMonth.divide(averageMonth.count());
for (MeasurementView view : viewMeasurementsStatistics) {
final FloatMeasurementView measurementView = (FloatMeasurementView) view;
Object[] extraData = new Object[3];
// not needed
extraData[0] = null;
// not needed
extraData[1] = null;
extraData[2] = measurementView;
measurementView.loadFrom(averageMonth, null);
entriesAvgMonth.add(new RadarEntry(measurementView.getValue(), extraData));
measurementView.loadFrom(averageWeek, null);
entriesAvgWeek.add(new RadarEntry(measurementView.getValue(), extraData));
measurementView.loadFrom(lastScaleMeasurement, null);
entriesLastMeasurement.add(new RadarEntry(measurementView.getValue(), extraData));
}
RadarDataSet setLastMeasurement = new RadarDataSet(entriesLastMeasurement, getString(R.string.label_title_last_measurement));
setLastMeasurement.setColor(ColorUtil.COLOR_BLUE);
setLastMeasurement.setFillColor(ColorUtil.COLOR_BLUE);
setLastMeasurement.setDrawFilled(true);
setLastMeasurement.setFillAlpha(180);
setLastMeasurement.setLineWidth(2f);
setLastMeasurement.setDrawHighlightCircleEnabled(true);
setLastMeasurement.setDrawHighlightIndicators(false);
RadarDataSet setAvgWeek = new RadarDataSet(entriesAvgWeek, getString(R.string.label_last_week));
setAvgWeek.setColor(ColorUtil.COLOR_GREEN);
setAvgWeek.setFillColor(ColorUtil.COLOR_GREEN);
setAvgWeek.setDrawFilled(true);
setAvgWeek.setFillAlpha(180);
setAvgWeek.setLineWidth(2f);
setAvgWeek.setDrawHighlightCircleEnabled(true);
setAvgWeek.setDrawHighlightIndicators(false);
RadarDataSet setAvgMonth = new RadarDataSet(entriesAvgMonth, getString(R.string.label_last_month));
setAvgMonth.setColor(ColorUtil.COLOR_GREEN);
setAvgMonth.setFillColor(ColorUtil.COLOR_GREEN);
setAvgMonth.setDrawFilled(true);
setAvgMonth.setFillAlpha(180);
setAvgMonth.setLineWidth(2f);
setAvgMonth.setDrawHighlightCircleEnabled(true);
setAvgMonth.setDrawHighlightIndicators(false);
ArrayList<IRadarDataSet> setsAvgWeek = new ArrayList<>();
setsAvgWeek.add(setAvgWeek);
setsAvgWeek.add(setLastMeasurement);
ArrayList<IRadarDataSet> setsAvgMonth = new ArrayList<>();
setsAvgMonth.add(setAvgMonth);
setsAvgMonth.add(setLastMeasurement);
RadarData dataAvgWeek = new RadarData(setsAvgWeek);
dataAvgWeek.setValueTextSize(8f);
dataAvgWeek.setDrawValues(false);
dataAvgWeek.setValueFormatter(new ValueFormatter() {
@Override
public String getRadarLabel(RadarEntry radarEntry) {
FloatMeasurementView measurementView = (FloatMeasurementView) radarEntry.getData();
return measurementView.getValueAsString(true);
}
});
RadarData dataAvgMonth = new RadarData(setsAvgMonth);
dataAvgMonth.setValueTextSize(8f);
dataAvgMonth.setDrawValues(false);
dataAvgMonth.setValueFormatter(new ValueFormatter() {
@Override
public String getRadarLabel(RadarEntry radarEntry) {
FloatMeasurementView measurementView = (FloatMeasurementView) radarEntry.getData();
return measurementView.getValueAsString(true);
}
});
radarChartWeek.setData(dataAvgWeek);
radarChartMonth.setData(dataAvgMonth);
radarChartWeek.animateXY(1000, 1000);
radarChartMonth.animateXY(1000, 1000);
radarChartWeek.invalidate();
radarChartMonth.invalidate();
}
use of com.health.openscale.gui.measurement.MeasurementView in project openScale by oliexdev.
the class WidgetConfigure method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
setContentView(R.layout.widget_configuration);
OpenScale openScale = OpenScale.getInstance();
// Set up user spinner
final Spinner userSpinner = findViewById(R.id.widget_user_spinner);
List<String> users = new ArrayList<>();
final List<Integer> userIds = new ArrayList<>();
for (ScaleUser scaleUser : openScale.getScaleUserList()) {
users.add(scaleUser.getUserName());
userIds.add(scaleUser.getId());
}
// Hide user selector when there's only one user
if (users.size() == 1) {
TableRow row = (TableRow) userSpinner.getParent();
row.setVisibility(View.GONE);
} else if (users.isEmpty()) {
users.add(getResources().getString(R.string.info_no_selected_user));
userIds.add(-1);
findViewById(R.id.widget_save).setEnabled(false);
}
ArrayAdapter<String> userAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, users);
userAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
userSpinner.setAdapter(userAdapter);
// Set up measurement spinner
final Spinner measurementSpinner = findViewById(R.id.widget_measurement_spinner);
List<String> measurements = new ArrayList<>();
final List<String> measurementKeys = new ArrayList<>();
for (MeasurementView measurementView : MeasurementView.getMeasurementList(this, MeasurementView.DateTimeOrder.NONE)) {
if (measurementView.isVisible()) {
measurements.add(measurementView.getName().toString());
measurementKeys.add(measurementView.getKey());
}
}
ArrayAdapter<String> measurementAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, measurements);
measurementAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
measurementSpinner.setAdapter(measurementAdapter);
findViewById(R.id.widget_save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int userId = userIds.get(userSpinner.getSelectedItemPosition());
String measurementKey = measurementKeys.get(measurementSpinner.getSelectedItemPosition());
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putInt(WidgetProvider.getUserIdPreferenceName(appWidgetId), userId).putString(WidgetProvider.getMeasurementPreferenceName(appWidgetId), measurementKey).apply();
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId });
sendBroadcast(intent);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});
}
use of com.health.openscale.gui.measurement.MeasurementView in project openScale by oliexdev.
the class TableFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
tableView = inflater.inflate(R.layout.fragment_table, container, false);
tableHeaderView = tableView.findViewById(R.id.tableHeaderView);
recyclerView = tableView.findViewById(R.id.tableDataView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), layoutManager.getOrientation()));
adapter = new MeasurementsAdapter();
recyclerView.setAdapter(adapter);
measurementViews = MeasurementView.getMeasurementList(getContext(), MeasurementView.DateTimeOrder.FIRST);
for (MeasurementView measurement : measurementViews) {
measurement.setUpdateViews(false);
}
OpenScale.getInstance().getScaleMeasurementsLiveData().observe(getViewLifecycleOwner(), new Observer<List<ScaleMeasurement>>() {
@Override
public void onChanged(List<ScaleMeasurement> scaleMeasurements) {
updateOnView(scaleMeasurements);
}
});
OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
requireActivity().finish();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), onBackPressedCallback);
return tableView;
}
use of com.health.openscale.gui.measurement.MeasurementView in project openScale by oliexdev.
the class OverviewFragment method updateMesurementViews.
private void updateMesurementViews(ScaleMeasurement selectedMeasurement) {
ScaleMeasurement[] tupleScaleData = OpenScale.getInstance().getTupleOfScaleMeasurement(selectedMeasurement.getId());
ScaleMeasurement prevScaleMeasurement = tupleScaleData[0];
for (MeasurementView measurement : lastMeasurementViews) {
measurement.loadFrom(selectedMeasurement, prevScaleMeasurement);
}
}
Aggregations