use of com.health.openscale.gui.measurement.ChartActionBarView in project openScale by oliexdev.
the class GraphFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
openScale = OpenScale.getInstance();
if (savedInstanceState == null) {
if (!openScale.isScaleMeasurementListEmpty()) {
calYears = openScale.getLastScaleMeasurement().getDateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
;
calLastSelected = openScale.getLastScaleMeasurement().getDateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
} else {
calYears = LocalDate.ofEpochDay(savedInstanceState.getLong(CAL_YEARS_KEY));
calLastSelected = LocalDate.ofEpochDay(savedInstanceState.getLong(CAL_LAST_SELECTED_KEY));
}
graphView = inflater.inflate(R.layout.fragment_graph, container, false);
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
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);
chartTop.setDrawGridBackground(false);
chartTop.getLegend().setEnabled(false);
chartTop.getAxisLeft().setEnabled(false);
chartTop.getAxisRight().setEnabled(false);
chartTop.getDescription().setEnabled(false);
chartTop.setOnChartValueSelectedListener(new chartTopValueTouchListener());
XAxis chartTopxAxis = chartTop.getXAxis();
chartTopxAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
chartTopxAxis.setDrawGridLines(false);
chartTopxAxis.setTextColor(ColorUtil.getTintColor(graphView.getContext()));
chartTopxAxis.setValueFormatter(new ValueFormatter() {
private final SimpleDateFormat mFormat = new SimpleDateFormat("MMM", Locale.getDefault());
@Override
public String getAxisLabel(float value, AxisBase axis) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, (int) value);
return mFormat.format(calendar.getTime());
}
});
txtYear = graphView.findViewById(R.id.txtYear);
txtYear.setText(Integer.toString(calYears.getYear()));
chartActionBarView = graphView.findViewById(R.id.chartActionBar);
chartActionBarView.setOnActionClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
generateGraphs();
}
});
ImageView optionMenu = graphView.findViewById(R.id.optionMenu);
optionMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.show();
}
});
btnLeftYear = graphView.findViewById(R.id.btnLeftYear);
btnLeftYear.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
calYears = calYears.minusYears(1);
txtYear.setText(Integer.toString(calYears.getYear()));
generateGraphs();
}
});
btnRightYear = graphView.findViewById(R.id.btnRightYear);
btnRightYear.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
calYears = calYears.plusYears(1);
txtYear.setText(Integer.toString(calYears.getYear()));
generateGraphs();
}
});
popup = new PopupMenu(getContext(), optionMenu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.enableChartActionBar:
if (item.isChecked()) {
item.setChecked(false);
prefs.edit().putBoolean("enableGraphChartActionBar", false).apply();
chartActionBarView.setVisibility(View.GONE);
} else {
item.setChecked(true);
prefs.edit().putBoolean("enableGraphChartActionBar", true).apply();
chartActionBarView.setVisibility(View.VISIBLE);
}
return true;
case R.id.enableMonth:
if (item.isChecked()) {
item.setChecked(false);
prefs.edit().putBoolean("showMonth", false).apply();
} else {
item.setChecked(true);
prefs.edit().putBoolean("showMonth", true).apply();
}
// TODO HACK to refresh graph; graph.invalidate and notfiydatachange is not enough!?
getActivity().recreate();
generateGraphs();
return true;
case R.id.enableWeek:
if (item.isChecked()) {
item.setChecked(false);
prefs.edit().putBoolean("showWeek", false).apply();
} else {
item.setChecked(true);
prefs.edit().putBoolean("showWeek", true).apply();
}
// TODO HACK to refresh graph; graph.invalidate and notfiydatachange is not enough!?
getActivity().recreate();
generateGraphs();
return true;
default:
return false;
}
}
});
popup.getMenuInflater().inflate(R.menu.graph_menu, popup.getMenu());
MenuItem enableMonth = popup.getMenu().findItem(R.id.enableMonth);
enableMonth.setChecked(prefs.getBoolean("showMonth", true));
MenuItem enableWeek = popup.getMenu().findItem(R.id.enableWeek);
enableWeek.setChecked(prefs.getBoolean("showWeek", false));
MenuItem enableMeasurementBar = popup.getMenu().findItem(R.id.enableChartActionBar);
enableMeasurementBar.setChecked(prefs.getBoolean("enableGraphChartActionBar", true));
if (enableMeasurementBar.isChecked()) {
chartActionBarView.setVisibility(View.VISIBLE);
} else {
chartActionBarView.setVisibility(View.GONE);
}
showMenu = graphView.findViewById(R.id.showMenu);
showMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GraphFragmentDirections.ActionNavGraphToNavDataentry action = GraphFragmentDirections.actionNavGraphToNavDataentry();
action.setMeasurementId(markedMeasurement.getId());
action.setMode(MeasurementEntryFragment.DATA_ENTRY_MODE.VIEW);
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(action);
}
});
editMenu = graphView.findViewById(R.id.editMenu);
editMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GraphFragmentDirections.ActionNavGraphToNavDataentry action = GraphFragmentDirections.actionNavGraphToNavDataentry();
action.setMeasurementId(markedMeasurement.getId());
action.setMode(MeasurementEntryFragment.DATA_ENTRY_MODE.EDIT);
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(action);
}
});
deleteMenu = graphView.findViewById(R.id.deleteMenu);
deleteMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteMeasurement();
}
});
OpenScale.getInstance().getScaleMeasurementsLiveData().observe(getViewLifecycleOwner(), new Observer<List<ScaleMeasurement>>() {
@Override
public void onChanged(List<ScaleMeasurement> scaleMeasurements) {
chartView.updateMeasurementList(scaleMeasurements);
generateGraphs();
}
});
OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
requireActivity().finish();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), onBackPressedCallback);
return graphView;
}
use of com.health.openscale.gui.measurement.ChartActionBarView in project openScale by oliexdev.
the class OverviewFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
overviewView = inflater.inflate(R.layout.fragment_overview, container, false);
prefs = PreferenceManager.getDefaultSharedPreferences(overviewView.getContext());
txtTitleUser = overviewView.findViewById(R.id.txtTitleUser);
chartView = overviewView.findViewById(R.id.chartView);
chartView.setOnChartValueSelectedListener(new onChartSelectedListener());
chartView.setProgressBar(overviewView.findViewById(R.id.progressBar));
chartView.setIsInGraphKey(false);
chartActionBarView = overviewView.findViewById(R.id.chartActionBar);
chartActionBarView.setIsInGraphKey(false);
chartActionBarView.setOnActionClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chartView.refreshMeasurementList();
updateChartView();
}
});
spinUser = overviewView.findViewById(R.id.spinUser);
ImageView optionMenu = overviewView.findViewById(R.id.rangeOptionMenu);
optionMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rangePopupMenu.show();
}
});
rangePopupMenu = new PopupMenu(getContext(), optionMenu);
rangePopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.enableChartActionBar:
if (item.isChecked()) {
item.setChecked(false);
prefs.edit().putBoolean("enableOverviewChartActionBar", false).apply();
chartActionBarView.setVisibility(View.GONE);
} else {
item.setChecked(true);
prefs.edit().putBoolean("enableOverviewChartActionBar", true).apply();
chartActionBarView.setVisibility(View.VISIBLE);
}
return true;
case R.id.menu_range_day:
prefs.edit().putInt("selectRangeMode", ChartMeasurementView.ViewMode.DAY_OF_ALL.ordinal()).commit();
break;
case R.id.menu_range_week:
prefs.edit().putInt("selectRangeMode", ChartMeasurementView.ViewMode.WEEK_OF_ALL.ordinal()).commit();
break;
case R.id.menu_range_month:
prefs.edit().putInt("selectRangeMode", ChartMeasurementView.ViewMode.MONTH_OF_ALL.ordinal()).commit();
break;
case R.id.menu_range_year:
prefs.edit().putInt("selectRangeMode", ChartMeasurementView.ViewMode.YEAR_OF_ALL.ordinal()).commit();
}
item.setChecked(true);
// TODO HACK to refresh graph; graph.invalidate and notfiydatachange is not enough!?
getActivity().recreate();
return true;
}
});
rangePopupMenu.getMenuInflater().inflate(R.menu.overview_menu, rangePopupMenu.getMenu());
ChartMeasurementView.ViewMode selectedRangePos = ChartMeasurementView.ViewMode.values()[prefs.getInt("selectRangeMode", ChartMeasurementView.ViewMode.DAY_OF_ALL.ordinal())];
switch(selectedRangePos) {
case DAY_OF_ALL:
rangePopupMenu.getMenu().findItem(R.id.menu_range_day).setChecked(true);
break;
case WEEK_OF_ALL:
rangePopupMenu.getMenu().findItem(R.id.menu_range_week).setChecked(true);
break;
case MONTH_OF_ALL:
rangePopupMenu.getMenu().findItem(R.id.menu_range_month).setChecked(true);
break;
case YEAR_OF_ALL:
rangePopupMenu.getMenu().findItem(R.id.menu_range_year).setChecked(true);
break;
}
MenuItem enableMeasurementBar = rangePopupMenu.getMenu().findItem(R.id.enableChartActionBar);
enableMeasurementBar.setChecked(prefs.getBoolean("enableOverviewChartActionBar", false));
if (enableMeasurementBar.isChecked()) {
chartActionBarView.setVisibility(View.VISIBLE);
} else {
chartActionBarView.setVisibility(View.GONE);
}
lastMeasurementViews = MeasurementView.getMeasurementList(getContext(), MeasurementView.DateTimeOrder.LAST);
TableLayout tableOverviewLayout = overviewView.findViewById(R.id.tableLayoutMeasurements);
for (MeasurementView measurement : lastMeasurementViews) {
tableOverviewLayout.addView(measurement);
}
spinUserAdapter = new ArrayAdapter<>(overviewView.getContext(), R.layout.spinner_item, new ArrayList<String>());
spinUser.setAdapter(spinUserAdapter);
// Set item select listener after spinner is created because otherwise item listener fires a lot!?!?
spinUser.post(new Runnable() {
public void run() {
spinUser.setOnItemSelectedListener(new spinUserSelectionListener());
updateUserSelection();
}
});
showEntry = overviewView.findViewById(R.id.showEntry);
showEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OverviewFragmentDirections.ActionNavOverviewToNavDataentry action = OverviewFragmentDirections.actionNavOverviewToNavDataentry();
action.setMeasurementId(markedMeasurement.getId());
action.setMode(MeasurementEntryFragment.DATA_ENTRY_MODE.VIEW);
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(action);
}
});
editEntry = overviewView.findViewById(R.id.editEntry);
editEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OverviewFragmentDirections.ActionNavOverviewToNavDataentry action = OverviewFragmentDirections.actionNavOverviewToNavDataentry();
action.setMeasurementId(markedMeasurement.getId());
action.setMode(MeasurementEntryFragment.DATA_ENTRY_MODE.EDIT);
Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(action);
}
});
deleteEntry = overviewView.findViewById(R.id.deleteEntry);
deleteEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteMeasurement();
}
});
showEntry.setEnabled(false);
editEntry.setEnabled(false);
deleteEntry.setEnabled(false);
chartView.animateY(700);
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 overviewView;
}
Aggregations