Skip to content

Commit

Permalink
Merge pull request #34 from physphil/develop
Browse files Browse the repository at this point in the history
merge into master
  • Loading branch information
physphil authored Nov 18, 2016
2 parents 11f47f0 + 063eaf8 commit 6e5b6c0
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 61 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ Unit Converter Ultimate

*A simple, lightweight, Material Design themed unit converter for Android.*

<a href="https://play.google.com/store/apps/details?id=com.physphil.android.unitconverterultimate&utm_source=global_co&utm_medium=prtn r&utm_content=Mar2515&utm_campaign=PartBadge&pcampaignid=MKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1">
<img alt="Get it on Google Play"
src="https://play.google.com/intl/en_us/badges/images/generic/en-play-badge.png"
width="170"/>
<a href="https://play.google.com/store/apps/details?id=com.physphil.android.unitconverterultimate&utm_source=global_co&utm_medium=prtnr&utm_content=Mar2515&utm_campaign=PartBadge&pcampaignid=MKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1">
<img src="https://play.google.com/intl/en_us/badges/images/generic/en_badge_web_generic.png"
alt="Get it on Google Play"
height="80"/>
</a><a href="https://f-droid.org/app/com.physphil.android.unitconverterultimate">
<img src="https://f-droid.org/badge/get-it-on.png"
alt="Get it on F-Droid" height="80">
<img src="https://f-droid.org/badge/get-it-on.png"
alt="Get it on F-Droid"
height="80">
</a>


Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ android {
applicationId "com.physphil.android.unitconverterultimate"
minSdkVersion 14
targetSdkVersion 24
versionCode 50000
versionName '5.0'
versionCode 50001
versionName '5.0.1'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void onDrawerClosed(View drawerView){}
@Override
public void onDrawerStateChanged(int newState){}
});
setupDrawer(conversion);
setupDrawer(getMenuPositionOfConversion(conversion));

if(savedInstanceState == null)
{
Expand Down Expand Up @@ -200,6 +200,45 @@ private int getConversionFromDrawer(int itemId)
return Conversion.AREA;
}

private int getMenuPositionOfConversion(@Conversion.id final int conversion)
{
switch (conversion)
{
case Conversion.AREA:
return 0;
case Conversion.COOKING:
return 1;
case Conversion.CURRENCY:
return 2;
case Conversion.STORAGE:
return 3;
case Conversion.ENERGY:
return 4;
case Conversion.FUEL:
return 5;
case Conversion.LENGTH:
return 6;
case Conversion.MASS:
return 7;
case Conversion.POWER:
return 8;
case Conversion.PRESSURE:
return 9;
case Conversion.SPEED:
return 10;
case Conversion.TEMPERATURE:
return 11;
case Conversion.TIME:
return 12;
case Conversion.TORQUE:
return 13;
case Conversion.VOLUME:
return 14;
default:
return 0;
}
}

private void hideKeyboard()
{
View v = getCurrentFocus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.physphil.android.unitconverterultimate.fragments;

import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -79,6 +78,7 @@ public final class ConversionFragment extends Fragment implements ConversionView
private double mResult;
private Preferences mPrefs;
private ConversionState mState;
private Context mAppContext;

/**
* Create a new ConversionFragment to display
Expand All @@ -96,10 +96,11 @@ public static ConversionFragment newInstance(@Conversion.id int id)
}

@Override
public void onAttach(Activity activity)
public void onAttach(Context context)
{
super.onAttach(activity);
Preferences.getInstance(activity).getPreferences().registerOnSharedPreferenceChangeListener(this);
super.onAttach(context);
Preferences.getInstance(context).getPreferences().registerOnSharedPreferenceChangeListener(this);
mAppContext = context.getApplicationContext();
}

@Override
Expand Down Expand Up @@ -218,6 +219,13 @@ public void onViewStateRestored(@Nullable Bundle savedInstanceState)
mPresenter.onGetUnitsToDisplay(mConversionId);
}

@Override
public void onDestroyView()
{
super.onDestroyView();
mPresenter.onDestroy();
}

/**
* Set radio buttons to their saved state (if any)
*/
Expand Down Expand Up @@ -296,8 +304,8 @@ private void addUnits(Conversion c)
mGrpFrom.removeAllViews();
mGrpTo.removeAllViews();
RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
lp.bottomMargin = getResources().getDimensionPixelSize(R.dimen.margin_view_small);
lp.topMargin = getResources().getDimensionPixelSize(R.dimen.margin_view_small);
lp.bottomMargin = mAppContext.getResources().getDimensionPixelSize(R.dimen.margin_view_small);
lp.topMargin = mAppContext.getResources().getDimensionPixelSize(R.dimen.margin_view_small);

for (int i = 0; i < c.getUnits().size(); i++)
{
Expand Down Expand Up @@ -381,7 +389,7 @@ private DecimalFormat getDecimalFormat()
symbols.setDecimalSeparator(mPrefs.getDecimalSeparator().charAt(0));

String groupSeparator = mPrefs.getGroupSeparator();
boolean isSeparatorUsed = !groupSeparator.equals(getString(R.string.group_separator_none));
boolean isSeparatorUsed = !groupSeparator.equals(mAppContext.getString(R.string.group_separator_none));
formatter.setGroupingUsed(isSeparatorUsed);
if (isSeparatorUsed)
{
Expand Down Expand Up @@ -477,7 +485,7 @@ public void showToastError(int message)
@Override
public Context getContext()
{
return getActivity();
return mAppContext;
}

// Radio Group checked change listener
Expand Down Expand Up @@ -573,9 +581,12 @@ protected ConversionState doInBackground(Void... params)
protected void onPostExecute(ConversionState conversionState)
{
// This is okay as fragment instance is retained across config change
mState = conversionState;
restoreConversionState();
convert();
if(isAdded())
{
mState = conversionState;
restoreConversionState();
convert();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import com.physphil.android.unitconverterultimate.api.FixerApi;
import com.physphil.android.unitconverterultimate.api.models.CurrencyResponse;
import com.physphil.android.unitconverterultimate.models.Conversion;
import com.physphil.android.unitconverterultimate.models.ConversionState;
import com.physphil.android.unitconverterultimate.models.Unit;
import com.physphil.android.unitconverterultimate.util.Conversions;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import retrofit2.Call;
import retrofit2.Callback;
Expand All @@ -37,6 +38,7 @@
*/
public class ConversionPresenter
{
private Set<Call<CurrencyResponse>> mCalls;
private ConversionView mView;

/**
Expand All @@ -47,6 +49,63 @@ public class ConversionPresenter
public ConversionPresenter(ConversionView mView)
{
this.mView = mView;
this.mCalls = new HashSet<>();
}

public void onDestroy()
{
// Cancel any running currency updates
for (Call<CurrencyResponse> call : mCalls)
{
if(call != null && call.isExecuted() && !call.isCanceled())
{
call.cancel();
}
}

mCalls.clear();
}

public void onUpdateCurrencyConversions()
{
final Call<CurrencyResponse> call = FixerApi.getInstance()
.getService()
.getLatestRates();

mCalls.add(call);
call.enqueue(new Callback<CurrencyResponse>()
{
@Override
public void onResponse(Call<CurrencyResponse> call, Response<CurrencyResponse> response)
{
boolean hadCurrency = Conversions.getInstance().hasCurrency();
Preferences.getInstance(mView.getContext()).saveLatestCurrency(response.body());
Conversions.getInstance().updateCurrencyConversions(mView.getContext());
Conversions.getInstance().setCurrencyUpdated(true);
mView.showToast(R.string.toast_currency_updated);
if (hadCurrency)
{
mView.updateCurrencyConversion();
}
else
{
mView.showUnitsList(Conversions.getInstance().getById(Conversion.CURRENCY));
}
}

@Override
public void onFailure(Call<CurrencyResponse> call, Throwable t)
{
if (!Conversions.getInstance().hasCurrency())
{
mView.showLoadingError(R.string.error_loading_currency);
}
else
{
mView.showToastError(R.string.toast_error_updating_currency);
}
}
});
}

public void onGetUnitsToDisplay(@Conversion.id int conversionId)
Expand Down Expand Up @@ -76,46 +135,6 @@ public void onGetUnitsToDisplay(@Conversion.id int conversionId)
}
}

public void onUpdateCurrencyConversions()
{
FixerApi.getInstance()
.getService()
.getLatestRates()
.enqueue(new Callback<CurrencyResponse>()
{
@Override
public void onResponse(Call<CurrencyResponse> call, Response<CurrencyResponse> response)
{
boolean hadCurrency = Conversions.getInstance().hasCurrency();
Preferences.getInstance(mView.getContext()).saveLatestCurrency(response.body());
Conversions.getInstance().updateCurrencyConversions(mView.getContext());
Conversions.getInstance().setCurrencyUpdated(true);
mView.showToast(R.string.toast_currency_updated);
if(hadCurrency)
{
mView.updateCurrencyConversion();
}
else
{
mView.showUnitsList(Conversions.getInstance().getById(Conversion.CURRENCY));
}
}

@Override
public void onFailure(Call<CurrencyResponse> call, Throwable t)
{
if(!Conversions.getInstance().hasCurrency())
{
mView.showLoadingError(R.string.error_loading_currency);
}
else
{
mView.showToastError(R.string.toast_error_updating_currency);
}
}
});
}

/**
* Convert a temperature value from one unit to another
*
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/res/values-ja/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2015 Phil Shadlyn
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>
<string name="default_number_decimals">4</string>
<string name="default_group_separator">,</string>
<string name="default_decimal_separator">.</string>
<integer name="num_digits_from">15</integer>
</resources>
Loading

0 comments on commit 6e5b6c0

Please sign in to comment.