Skip to content

Commit

Permalink
closed #43 - removed Context where not necessary % MainActivity renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondrej Oravcok committed Jul 20, 2017
1 parent 21e135f commit 9820133
Show file tree
Hide file tree
Showing 23 changed files with 93 additions and 91 deletions.
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".screens.VehicleListActivity"
android:name=".screens.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar">
Expand All @@ -24,11 +24,11 @@
android:name=".screens.VehicleTabbedDetailActivity"
android:label="Detail"
android:launchMode="singleTop"
android:parentActivityName=".screens.VehicleListActivity"
android:parentActivityName=".screens.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="sk.piskula.fuelup.screens.VehicleListActivity" />
android:value="sk.piskula.fuelup.screens.MainActivity" />
</activity>
<activity
android:name=".screens.VehicleStatisticsActivity"
Expand All @@ -47,7 +47,7 @@
<activity
android:name=".screens.edit.AddVehicleActivity"
android:label="Add Vehicle"
android:parentActivityName=".screens.VehicleListActivity">
android:parentActivityName=".screens.MainActivity">
</activity>
<activity
android:name=".screens.edit.EditVehicleActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class ListExpensesAdapter extends RecyclerView.Adapter<ListExpensesAdapte

private static final String TAG = "ListExpensesAdapter";

private Context context;
private List<Expense> items;
private Callback callback;

Expand All @@ -41,8 +40,7 @@ public interface Callback {

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
View view = LayoutInflater.from(this.context).inflate(R.layout.list_item_expense, parent, false);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_expense, parent, false);
return new ViewHolder(view);
}

Expand All @@ -51,7 +49,7 @@ public void onBindViewHolder(final ViewHolder holder, final int position) {
Expense currentItem = items.get(position);
if (currentItem != null) {
holder.txtInfo.setText(currentItem.getInfo());
holder.txtPrice.setText(CurrencyUtil.getPrice(currentItem.getVehicle().getCurrency(), currentItem.getPrice(), context));
holder.txtPrice.setText(CurrencyUtil.getPrice(currentItem.getVehicle().getCurrency(), currentItem.getPrice()));
holder.txtDate.setText(DateUtil.getDateLocalized(currentItem.getDate()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import sk.piskula.fuelup.entity.enums.DistanceUnit;
import sk.piskula.fuelup.entity.util.CurrencyUtil;
import sk.piskula.fuelup.entity.util.DateUtil;
import sk.piskula.fuelup.screens.MainActivity;

/**
* Created by Martin Styk on 19.06.2017.
Expand All @@ -25,7 +26,6 @@ public class ListFillUpsAdapter extends RecyclerView.Adapter<ListFillUpsAdapter.

private List<FillUp> items;
private Callback callback;
private Context context;

public ListFillUpsAdapter(Callback callback) {
super();
Expand All @@ -39,8 +39,7 @@ public interface Callback {

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
View view = LayoutInflater.from(this.context).inflate(R.layout.list_item_fillup, parent, false);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_fillup, parent, false);
return new ViewHolder(view);
}

Expand All @@ -51,15 +50,14 @@ public void onBindViewHolder(final ViewHolder holder, final int position) {
//set views
holder.txtDistanceFromLastFillUp.setText(currentItem.getDistanceFromLastFillUp().toString());
holder.txtDistanceUnit.setText(currentItem.getVehicle().getDistanceUnit().toString());
holder.txtConsumptionUnit.setText(currentItem.getVehicle().getConsumptionUnit(context));
holder.txtConsumptionUnit.setText(currentItem.getVehicle().getConsumptionUnit());
holder.txtDate.setText(DateUtil.getDateLocalized(currentItem.getDate()));
holder.imgFullnessFillUpSymbol.setImageResource(context.getResources().getIdentifier(
currentItem.isFullFillUp() ? "ic_gasolinedrop_full" : "ic_gasolinedrop_empty", "drawable", context.getPackageName()));
holder.imgFullnessFillUpSymbol.setImageResource(getImageResourceId(currentItem.isFullFillUp()));
holder.txtPriceTotal.setText(CurrencyUtil.getPrice(
currentItem.getVehicle().getCurrency(), currentItem.getFuelPriceTotal(), context));
currentItem.getVehicle().getCurrency(), currentItem.getFuelPriceTotal()));
holder.txtPricePerLitre.setText(CurrencyUtil.getPricePerLitre(
currentItem.getVehicle().getCurrency(), currentItem.getFuelPricePerLitre(), context));
holder.txtPricePerLitreSymbol.setText("/" + context.getString(R.string.litre));
currentItem.getVehicle().getCurrency(), currentItem.getFuelPricePerLitre()));
holder.txtPricePerLitreSymbol.setText("/" + MainActivity.getInstance().getString(R.string.litre));

DecimalFormat bddf = new DecimalFormat();
bddf.setGroupingUsed(false);
Expand Down Expand Up @@ -87,6 +85,12 @@ public void onClick(View v) {
});
}

private int getImageResourceId(boolean isFullFillup) {
String fileName = isFullFillup ? "ic_gasolinedrop_full" : "ic_gasolinedrop_empty";
return MainActivity.getInstance().getResources()
.getIdentifier(fileName, "drawable", MainActivity.getInstance().getPackageName());
}

@Override
public int getItemCount() {
return items.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import sk.piskula.fuelup.R;
import sk.piskula.fuelup.entity.Vehicle;
import sk.piskula.fuelup.entity.VehicleType;
import sk.piskula.fuelup.screens.MainActivity;

/**
* @author Ondrej Oravcok
Expand All @@ -23,7 +25,6 @@ public class ListVehiclesAdapter extends RecyclerView.Adapter<ListVehiclesAdapte
private static final String TAG = ListVehiclesAdapter.class.getSimpleName();

private List<Vehicle> mItems;
private Context context;
private Callback callback;

public ListVehiclesAdapter(Callback callback) {
Expand All @@ -42,8 +43,7 @@ public List<Vehicle> getItems() {

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
View itemView = LayoutInflater.from(this.context).inflate(R.layout.list_item_vehicle, parent, false);
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_vehicle, parent, false);
return new ViewHolder(itemView);
}

Expand All @@ -54,9 +54,7 @@ public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.title.setText(vehicle.getName());
holder.count.setText(vehicle.getVehicleMaker());
holder.thumbnail.setImageBitmap(vehicle.getPicture());
holder.overflow.setImageResource(context.getResources()
.getIdentifier("ic_type_" + vehicle.getType().getName().toLowerCase(),
"drawable", context.getPackageName()));
holder.overflow.setImageResource(getImageResourceId(vehicle.getType()));

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -66,6 +64,12 @@ public void onClick(View v) {
});
}

private int getImageResourceId(VehicleType type) {
Context cxt = MainActivity.getInstance();
return cxt.getResources().getIdentifier(
"ic_type_" + type.getName().toLowerCase(), "drawable", cxt.getPackageName());
}

@Override
public int getItemCount() {
return mItems.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import sk.piskula.fuelup.R;
import sk.piskula.fuelup.entity.Vehicle;
import sk.piskula.fuelup.screens.MainActivity;
import sk.piskula.fuelup.screens.statisticfragments.StatisticsChartConsumptionFragment;
import sk.piskula.fuelup.screens.statisticfragments.StatisticsChartConsumptionPreviewFragment;

Expand All @@ -16,12 +17,10 @@
public class PagerStatisticsAdapter extends FragmentStatePagerAdapter {

private Vehicle vehicle;
private Context context;

public PagerStatisticsAdapter(FragmentManager fm, Vehicle vehicle, Context context) {
public PagerStatisticsAdapter(FragmentManager fm, Vehicle vehicle) {
super(fm);
this.vehicle = vehicle;
this.context = context;
}

@Override
Expand All @@ -45,9 +44,9 @@ public int getCount() {
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return context.getString(R.string.statistics_fuel_consumption);
return MainActivity.getInstance().getString(R.string.statistics_fuel_consumption);
case 1:
return context.getString(R.string.statistics_fuel_consumption);
return MainActivity.getInstance().getString(R.string.statistics_fuel_consumption);
default:
return "Fragment" + position;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SpinnerCurrencyAdapter extends BaseAdapter implements SpinnerAdapte

public SpinnerCurrencyAdapter(Activity activity) {
this.activity = activity;
this.currencies = CurrencyUtil.getSupportedCurrencies(activity);
this.currencies = CurrencyUtil.getSupportedCurrencies();
}

@Override
Expand Down Expand Up @@ -62,7 +62,7 @@ public View getView(int position, View view, ViewGroup viewGroup) {

//TODO getDisplayName() API level 19
currencyName.setText(currencies.get(position).getCurrencyCode());
currencySymbol.setText(CurrencyUtil.getCurrencySymbol(currencies.get(position), activity.getApplicationContext()));
currencySymbol.setText(CurrencyUtil.getCurrencySymbol(currencies.get(position)));

return spinView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ public class ExpenseService {

private static final String TAG = ExpenseService.class.getSimpleName();

private Context context;
private Dao<Expense, Long> expensesDao;

public ExpenseService(Context context) {
this.context = context;
this.expensesDao = DatabaseProvider.get(context).getExpenseDao();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ public class FillUpService {

private static final String TAG = FillUpService.class.getSimpleName();

private Context context;
private Dao<FillUp, Long> fillUpDao;

public FillUpService(Context context) {
this.context = context;
this.fillUpDao = DatabaseProvider.get(context).getFillUpDao();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ public class VehicleService {

private static final String TAG = VehicleService.class.getSimpleName();

private Context context;
private Dao<Vehicle, Long> vehicleDao;
private VehicleTypeService vehicleTypeService;

public VehicleService(Context context) {
this.context = context;
this.vehicleDao = DatabaseProvider.get(context).getVehicleDao();
vehicleTypeService = new VehicleTypeService(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public class VehicleTypeService {

private static final String TAG = VehicleTypeService.class.getSimpleName();

private Context context;
private Dao<VehicleType, Long> vehicleTypeDao;

public VehicleTypeService(Context context) {
this.context = context;
this.vehicleTypeDao = DatabaseProvider.get(context).getVehicleTypeDao();
}

Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/sk/piskula/fuelup/entity/Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sk.piskula.fuelup.entity.enums.DistanceUnit;
import sk.piskula.fuelup.entity.enums.VolumeUnit;
import sk.piskula.fuelup.entity.util.CurrencyUtil;
import sk.piskula.fuelup.screens.MainActivity;

/**
* @author Ondrej Oravcok
Expand Down Expand Up @@ -167,8 +168,8 @@ public void setPathToPicture(String pathToPicture) {
this.pathToPicture = pathToPicture;
}

public String getCurrencySymbol(Context context) {
return CurrencyUtil.getCurrencySymbol(this.getCurrency(), context);
public String getCurrencySymbol() {
return CurrencyUtil.getCurrencySymbol(this.getCurrency());
}

public Vehicle() {
Expand Down Expand Up @@ -218,11 +219,11 @@ public Vehicle[] newArray(int size) {
}
};

public String getConsumptionUnit(Context context) {
public String getConsumptionUnit() {
if (this.getDistanceUnit() == DistanceUnit.mi) {
return context.getString(R.string.units_mpg);
return MainActivity.getInstance().getString(R.string.units_mpg);
} else {
return context.getString(R.string.units_litreper100km);
return MainActivity.getInstance().getString(R.string.units_litreper100km);
}
}
}
34 changes: 18 additions & 16 deletions app/src/main/java/sk/piskula/fuelup/entity/util/CurrencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;
import java.util.Properties;

import sk.piskula.fuelup.screens.MainActivity;

/**
* @author Ondrej Oravcok
* @version 28.6.2017
Expand All @@ -35,31 +37,31 @@ public class CurrencyUtil {

private static Properties properties = new Properties();;

public static String getCurrencySymbol(Currency currency, Context context) {
checkPropertiesAreLoaded(context);
return getCurrencySymbol(currency);
public static String getCurrencySymbol(Currency currency) {
checkPropertiesAreLoaded();
return getCurrencySymbolFromProperties(currency);
}

private static void checkPropertiesAreLoaded(Context context) {
private static void checkPropertiesAreLoaded() {
if (properties.isEmpty()) {
try {
properties.load(context.getAssets().open(PROPERTY_FILE));
properties.load(MainActivity.getInstance().getAssets().open(PROPERTY_FILE));
} catch (IOException e) {
Log.e(TAG, "Cannot load currencies from " + PROPERTY_FILE, e);
}
}
}

private static String getCurrencySymbol(Currency currency) {
private static String getCurrencySymbolFromProperties(Currency currency) {
if (properties.containsKey(currency.getCurrencyCode())) {
return properties.getProperty(currency.getCurrencyCode()).split(DELIMETER)[CODE];
} else {
return currency.getSymbol();
}
}

public static List<Currency> getSupportedCurrencies(Context context) {
checkPropertiesAreLoaded(context);
public static List<Currency> getSupportedCurrencies() {
checkPropertiesAreLoaded();

List<Currency> currencies = new ArrayList<>();
for (String currencyString : properties.stringPropertyNames())
Expand All @@ -81,8 +83,8 @@ private static String getPriceFormatted(double value, int coefficient, boolean i
}
}

public static String getPrice(Currency currency, double value, Context context) {
checkPropertiesAreLoaded(context);
public static String getPrice(Currency currency, double value) {
checkPropertiesAreLoaded();
if (properties.containsKey(currency.getCurrencyCode())) {
String[] currencyStrings = properties.getProperty(currency.getCurrencyCode()).split(DELIMETER);

Expand All @@ -96,8 +98,8 @@ public static String getPrice(Currency currency, double value, Context context)
}
}

public static String getPricePerLitre(Currency currency, double value, Context context) {
checkPropertiesAreLoaded(context);
public static String getPricePerLitre(Currency currency, double value) {
checkPropertiesAreLoaded();
if (properties.containsKey(currency.getCurrencyCode())) {
String[] currencyStrings = properties.getProperty(currency.getCurrencyCode()).split(DELIMETER);

Expand All @@ -112,12 +114,12 @@ public static String getPricePerLitre(Currency currency, double value, Context c
}
}

public static String getPrice(Currency currency, BigDecimal value, Context context) {
return getPrice(currency, value.doubleValue(), context);
public static String getPrice(Currency currency, BigDecimal value) {
return getPrice(currency, value.doubleValue());
}

public static String getPricePerLitre(Currency currency, BigDecimal value, Context context) {
return getPricePerLitre(currency, value.doubleValue(), context);
public static String getPricePerLitre(Currency currency, BigDecimal value) {
return getPricePerLitre(currency, value.doubleValue());
}

}
Loading

0 comments on commit 9820133

Please sign in to comment.