Skip to content

Instantly share code, notes, and snippets.

@Manikkumar1988
Last active May 11, 2019 14:43
Show Gist options
  • Save Manikkumar1988/4a498adcd05bd642102a38c4cd71f847 to your computer and use it in GitHub Desktop.
Save Manikkumar1988/4a498adcd05bd642102a38c4cd71f847 to your computer and use it in GitHub Desktop.
private ViewModelProvider.Factory fuelLogViewModelFactory;
private FuelLogViewModel fuelLogViewModel;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fuelLogViewModel = ViewModelProviders.of(getActivity(),fuelLogViewModelFactory)
.get(FuelLogViewModel.class);
observeForAverageConsumption();
}
private void observeForAverageConsumption() {
fuelLogViewModel.getAverageFuelConsumption().observe(this, new Observer<FuelStat>() {
@Override
public void onChanged(FuelStat fuelStat) {
if(Objects.isNull(fuelStat)) {
averageConsumptionValue.setText(getText(R.string.empty_value));
drivingCost.setText(getText(R.string.empty_value));
totalDistance.setText(getText(R.string.empty_value));
totalAmouunt.setText(getText(R.string.empty_value));
totalFuelConsumption.setText(getText(R.string.empty_value));
} else {
averageConsumptionValue.setText(String.valueOf(fuelStat.getAverageConsumption()));
drivingCost.setText(String.valueOf(fuelStat.getDrivingCost()));
totalDistance.setText(String.valueOf(fuelStat.getTotalDistance()));
totalAmouunt.setText(String.valueOf(fuelStat.getTotalAmount()));
totalFuelConsumption.setText(String.valueOf(fuelStat.getTotalFuelConsumption()));
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment