Skip to content

Commit

Permalink
Create UnitAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sudonatalie committed Jul 19, 2020
1 parent bc69bb3 commit 83e7fc6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.natalieperna.cupful.R;
import com.natalieperna.cupful.adapters.UnitAdapter;
import com.natalieperna.cupful.data.IngredientReader;
import com.natalieperna.cupful.data.Units;
import com.natalieperna.cupful.models.DisplayUnit;
import com.natalieperna.cupful.models.Ingredient;
import com.natalieperna.cupful.views.IngredientSpinner;

Expand Down Expand Up @@ -120,8 +119,7 @@ private void setupSpinners() {
ingredientInput.setIngredients(ingredientReader.getAll());

// Show units in spinners
ArrayAdapter<DisplayUnit> unitAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Units.getAll());
unitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
UnitAdapter unitAdapter = new UnitAdapter(this, Units.getAll());
topUnit.setAdapter(unitAdapter);
bottomUnit.setAdapter(unitAdapter);

Expand Down Expand Up @@ -224,8 +222,8 @@ private void convert(boolean forward) {

// Get ingredients and units
Ingredient ingredient = (Ingredient) ingredientInput.getSelectedItem();
Unit<? extends Quantity> fromUnit = ((DisplayUnit<? extends Quantity>) fromUnitSpinner.getSelectedItem()).getUnit();
Unit<? extends Quantity> toUnit = ((DisplayUnit<? extends Quantity>) toUnitSpinner.getSelectedItem()).getUnit();
Unit<? extends Quantity> fromUnit = (Unit<? extends Quantity>) fromUnitSpinner.getSelectedItem();
Unit<? extends Quantity> toUnit = (Unit<? extends Quantity>) toUnitSpinner.getSelectedItem();

// Get from value
String fromString = fromInput.getText().toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.natalieperna.cupful.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.natalieperna.cupful.data.Units;

import java.util.List;

import javax.measure.quantity.Quantity;
import javax.measure.unit.Unit;

public class UnitAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final List<Unit<? extends Quantity>> units;

public UnitAdapter(@NonNull Context context, List<Unit<? extends Quantity>> units) {
super();
this.inflater = LayoutInflater.from(context);
this.units = units;
}

@Override
public int getCount() {
return units.size();
}

@Override
public Object getItem(int position) {
return units.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return constructView(position, convertView, parent, android.R.layout.simple_spinner_item);
}

@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return constructView(position, convertView, parent, android.R.layout.simple_spinner_dropdown_item);
}

private View constructView(int position, @Nullable View convertView, @NonNull ViewGroup parent, @LayoutRes int resource) {
TextView view;
if (convertView == null) {
view = (TextView) inflater.inflate(resource, parent, false);
} else {
view = (TextView) convertView;
}

Unit<? extends Quantity> unit = (Unit<? extends Quantity>) getItem(position);
view.setText(Units.unitToString(unit));

return view;
}
}
62 changes: 43 additions & 19 deletions app/src/main/java/com/natalieperna/cupful/data/Units.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.natalieperna.cupful.data;

import com.natalieperna.cupful.models.DisplayUnit;
import java.util.List;
import java.util.Map;

import javax.measure.quantity.Quantity;
import javax.measure.quantity.Volume;
import javax.measure.quantity.VolumetricDensity;
import javax.measure.unit.NonSI;
Expand All @@ -20,24 +22,46 @@ public class Units {
// Density unit
public static final Unit<VolumetricDensity> G_PER_CUP = SI.GRAM.divide(CUP_US).asType(VolumetricDensity.class);

private static final DisplayUnit[] UNITS = {
new DisplayUnit<>(CUP_US, "cup (US)"),
new DisplayUnit<>(TABLESPOON_US, "tbsp (US)"),
new DisplayUnit<>(TEASPOON_US, "tsp (US)"),
new DisplayUnit<>(SI.GRAM, "g"),
new DisplayUnit<>(SI.KILOGRAM, "kg"),
new DisplayUnit<>(NonSI.OUNCE, "oz"),
new DisplayUnit<>(NonSI.POUND, "lb"),
new DisplayUnit<>(SI.MILLI(NonSI.LITER), "mL"),
new DisplayUnit<>(NonSI.LITER, "L"),
new DisplayUnit<>(NonSI.OUNCE_LIQUID_US, "fl oz (US)"),
new DisplayUnit<>(CUP_UK, "cup (UK)"),
new DisplayUnit<>(TABLESPOON_UK, "tbsp (UK)"),
new DisplayUnit<>(TEASPOON_UK, "tsp (UK)"),
new DisplayUnit<>(NonSI.OUNCE_LIQUID_UK, "fl oz (UK)"),
};

public static DisplayUnit[] getAll() {
/**
* List of all units available for conversion
*/
private static final List<Unit<? extends Quantity>> UNITS = List.of(
CUP_US,
TABLESPOON_US,
TEASPOON_US,
SI.GRAM,
SI.KILOGRAM,
NonSI.OUNCE,
NonSI.POUND,
SI.MILLI(NonSI.LITER),
NonSI.LITER,
NonSI.OUNCE_LIQUID_US,
CUP_UK,
TABLESPOON_UK,
TEASPOON_UK,
NonSI.OUNCE_LIQUID_UK
);

public static List<Unit<? extends Quantity>> getAll() {
return UNITS;
}

/**
* Custom string representation for some {@link #UNITS}, otherwise use {@link Unit#toString()}
*/
private static final Map<Unit<? extends Quantity>, String> UNIT_TO_STRING_OVERRIDE = Map.ofEntries(
Map.entry(CUP_US, "cup (US)"),
Map.entry(TABLESPOON_US, "tbsp (US)"),
Map.entry(TEASPOON_US, "tsp (US)"),
Map.entry(NonSI.OUNCE_LIQUID_US, "fl oz (US)"),
Map.entry(CUP_UK, "cup (UK)"),
Map.entry(TABLESPOON_UK, "tbsp (UK)"),
Map.entry(TEASPOON_UK, "tsp (UK)"),
Map.entry(NonSI.OUNCE_LIQUID_UK, "fl oz (UK)")
);

public static String unitToString(Unit<? extends Quantity> unit) {
String s = UNIT_TO_STRING_OVERRIDE.get(unit);
return s == null ? unit.toString() : s;
}
}
23 changes: 0 additions & 23 deletions app/src/main/java/com/natalieperna/cupful/models/DisplayUnit.java

This file was deleted.

0 comments on commit 83e7fc6

Please sign in to comment.