Skip to content

Commit

Permalink
Fixed NPE on entering insulin or blood pressure
Browse files Browse the repository at this point in the history
  • Loading branch information
Faltenreich committed Apr 7, 2019
1 parent 822de2e commit 6279f26
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Diaguard/Diaguard.iml
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check-libraries" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check-manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/checkBetaDebugClasspath" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/checkStoreReleaseClasspath" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/compatible_screen_manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant_run_merged_manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javac" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/linked_res_for_bundle" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/module_bundle" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/prebuild" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/processed_res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard-rules" />
Expand All @@ -150,6 +153,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 28 Platform" jdkType="Android SDK" />
Expand Down
4 changes: 2 additions & 2 deletions Diaguard/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 35
versionName "3.2.0"
versionCode 36
versionName "3.2.1"
vectorDrawables.useSupportLibrary = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.faltenreich.diaguard.data.entity.Food;
import com.faltenreich.diaguard.data.entity.Meal;
import com.faltenreich.diaguard.data.entity.Measurement;

import java.lang.reflect.Constructor;

import butterknife.ButterKnife;

/**
Expand Down Expand Up @@ -46,6 +49,18 @@ public MeasurementAbstractView(Context context, Food food) {
init();
}

public MeasurementAbstractView(Context context, Measurement.Category category) {
super(context);
try {
Class<T> clazz = category.toClass();
Constructor<T> constructor = clazz.getConstructor();
measurement = constructor.newInstance();
} catch (Exception exception) {
Log.e(TAG, String.format("Could not get newInstance for %s", category.toClass().getSimpleName()));
}
init();
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MeasurementGenericView(Context context, T measurement) {
}

public MeasurementGenericView(Context context, Measurement.Category category) {
super(context, (T) category.createMeasurement());
super(context, category);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MeasurementInsulinView extends MeasurementAbstractView<Insulin> {
@BindView(R.id.insulin_basal) StickyHintInput inputBasal;

public MeasurementInsulinView(Context context) {
super(context, new Insulin());
super(context, Measurement.Category.INSULIN);
}

public MeasurementInsulinView(Context context, Insulin insulin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

import com.faltenreich.diaguard.data.entity.Food;
import com.faltenreich.diaguard.data.entity.Measurement;
import com.faltenreich.diaguard.ui.activity.EntryActivity;

import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -105,16 +102,10 @@ public void addMeasurement(Food food) {
}
}

// TODO: Improved performance
public void addMeasurements(List<Measurement> measurements) {
long start = DateTime.now().getMillis();

for (Measurement measurement : measurements) {
addMeasurement(categories.size(), measurement);
}

long end = DateTime.now().getMillis();
Log.d(EntryActivity.class.getSimpleName(), String.format("Took millis: %d", end - start));
}

public void removeMeasurement(Measurement.Category category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class MeasurementMealView extends MeasurementAbstractView<Meal> {
@BindView(R.id.list_item_measurement_meal_food_list) FoodInputView foodInputView;

public MeasurementMealView(Context context) {
super(context, new Meal());
super(context, Measurement.Category.MEAL);
}

public MeasurementMealView(Context context, Meal meal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MeasurementPressureView extends MeasurementAbstractView<Pressure> {
StickyHintInput diastolic;

public MeasurementPressureView(Context context) {
super(context, new Pressure());
super(context, Measurement.Category.PRESSURE);
}

public MeasurementPressureView(Context context, Pressure pressure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,27 @@ private void initData() {
textViewCategory.setText(categoryName);
buttonRemove.setContentDescription(String.format(getContext().getString(R.string.remove_placeholder), categoryName));

boolean isUpdating = measurement != null;
switch (category) {
case INSULIN:
content.addView(new MeasurementInsulinView(getContext(), measurement != null ? (Insulin) measurement : null));
content.addView(isUpdating ?
new MeasurementInsulinView(getContext(), (Insulin) measurement) :
new MeasurementInsulinView(getContext()));
break;
case MEAL:
content.addView(measurement != null ? new MeasurementMealView(getContext(), (Meal) measurement) : new MeasurementMealView(getContext(), food));
content.addView(isUpdating ?
new MeasurementMealView(getContext(), (Meal) measurement) :
new MeasurementMealView(getContext(), food));
break;
case PRESSURE:
content.addView(new MeasurementPressureView(getContext(), measurement != null ? (Pressure) measurement : null));
content.addView(isUpdating ?
new MeasurementPressureView(getContext(), (Pressure) measurement) :
new MeasurementPressureView(getContext()));
break;
default:
content.addView(measurement != null ? new MeasurementGenericView<>(getContext(), measurement) : new MeasurementGenericView<>(getContext(), category));
content.addView(isUpdating ?
new MeasurementGenericView<>(getContext(), measurement) :
new MeasurementGenericView<>(getContext(), category));
}
}

Expand Down

0 comments on commit 6279f26

Please sign in to comment.