Skip to content

Commit

Permalink
Merge branch 'release/5.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
physphil committed Dec 22, 2017
2 parents 56f3653 + 7c2cf0c commit e113077
Show file tree
Hide file tree
Showing 27 changed files with 312 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local.properties

# Idea non-crucial project files
*.iws
.navigation

# Signing info
app/keystore.properties
Expand Down
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

// Only apply the google-service plugin if the config file exists
if (file('google-services.json').exists()) {
Expand All @@ -23,7 +24,7 @@ if (file('google-services.json').exists()) {

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
buildToolsVersion '25.0.0'

signingConfigs {
release {
Expand All @@ -43,8 +44,8 @@ android {
applicationId "com.physphil.android.unitconverterultimate"
minSdkVersion 14
targetSdkVersion 24
versionCode 50200
versionName '5.2'
versionCode 50300
versionName '5.3'
}


Expand All @@ -69,6 +70,7 @@ android {
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Phil Shadlyn
* Copyright 2017 Phil Shadlyn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,25 +30,35 @@
import com.physphil.android.unitconverterultimate.fragments.ConversionFragment;
import com.physphil.android.unitconverterultimate.fragments.HelpDialogFragment;
import com.physphil.android.unitconverterultimate.models.Conversion;
import com.physphil.android.unitconverterultimate.presenters.MainActivityPresenter;
import com.physphil.android.unitconverterultimate.presenters.MainActivityView;
import com.physphil.android.unitconverterultimate.util.Conversions;
import com.physphil.android.unitconverterultimate.util.IntentFactory;


/**
* Main activity
* Created by Phizz on 15-07-28.
*/
public class MainActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
public class MainActivity extends BaseActivity implements MainActivityView, SharedPreferences.OnSharedPreferenceChangeListener {

private DrawerLayout mDrawerLayout;
private Conversions mConversions;
private MainActivityPresenter mPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
// TODO - replace with Dagger2 injection
mPresenter = new MainActivityPresenter(this, this, Preferences.getInstance(this));

PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
Preferences.getInstance(this).getPreferences().registerOnSharedPreferenceChangeListener(this);
mConversions = Conversions.getInstance();

// setup language
mPresenter.setLanguageToDisplay();

setContentView(R.layout.activity_main);
setupToolbar();
setToolbarHomeNavigation(true);
Expand Down Expand Up @@ -232,6 +242,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
if (key.equals(Preferences.PREFS_THEME)) {
recreate();
}
else if (key.equals(Preferences.PREFS_LANGUAGE)) {
mPresenter.onLanguageChanged();
}
}

@Override
Expand All @@ -245,4 +258,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}

// region MainActivityView implementation
@Override
public void restartApp() {
startActivity(IntentFactory.getRestartAppIntent(this));
}
// endregion
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Phil Shadlyn
* Copyright 2017 Phil Shadlyn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,10 +19,12 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.physphil.android.unitconverterultimate.api.models.CurrencyResponse;
import com.physphil.android.unitconverterultimate.models.Conversion;
import com.physphil.android.unitconverterultimate.models.Language;

/**
* Preferences class
Expand All @@ -40,6 +42,7 @@ public class Preferences {
public static final String PREFS_SHOW_HELP = "show_help";
private static final String PREFS_CONVERSION = "conversion";
private static final String PREFS_CURRENCY = "currency";
public static final String PREFS_LANGUAGE = "language";

private static Preferences mInstance;
private SharedPreferences mPrefs;
Expand Down Expand Up @@ -127,4 +130,14 @@ public CurrencyResponse getLatestCurrency() {
return null;
}
}

public void setLanguage(final Language language) {
mPrefs.edit().putString(PREFS_LANGUAGE, language.getId()).apply();
}

@NonNull
public Language getLanguage() {
final String id = mPrefs.getString(PREFS_LANGUAGE, Language.DEFAULT.getId());
return Language.fromId(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Phil Shadlyn
* Copyright 2017 Phil Shadlyn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import android.content.ActivityNotFoundException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
Expand All @@ -28,8 +29,12 @@
import com.physphil.android.unitconverterultimate.Preferences;
import com.physphil.android.unitconverterultimate.R;
import com.physphil.android.unitconverterultimate.UnitConverterApplication;
import com.physphil.android.unitconverterultimate.models.Language;
import com.physphil.android.unitconverterultimate.util.IntentFactory;

import java.util.Arrays;
import java.util.Comparator;

/**
* Fragment to display preferences screen
* Created by Phizz on 15-08-02.
Expand Down Expand Up @@ -97,6 +102,9 @@ public boolean onPreferenceClick(Preference preference) {
else {
((PreferenceCategory) findPreference("other")).removePreference(donate);
}

final ListPreference language = (ListPreference) findPreference("language");
sortLanguageOptions(language);
}

@Override
Expand Down Expand Up @@ -147,6 +155,38 @@ private void openIssue() {
}
}

private void sortLanguageOptions(final ListPreference preference) {
// Sort language options so they're always alphabetical, no matter what language the user has chosen
final Language[] languages = Language.values();
Arrays.sort(languages, new Comparator<Language>() {
@Override
public int compare(Language lang1, Language lang2) {
// Always put DEFAULT at top of list, then sort the rest alphabetically
if (lang1 == Language.DEFAULT) {
return Integer.MIN_VALUE;
}
else if (lang2 == Language.DEFAULT) {
return Integer.MAX_VALUE;
}
else {
return getString(lang1.getDisplayStringId()).compareTo(getString(lang2.getDisplayStringId()));
}
}
});

// Create CharSequence[] arrays from the sorted list of Languages to supply to ListPreference
final int size = languages.length;
final CharSequence[] entries = new CharSequence[size];
final CharSequence[] entryValues = new CharSequence[size];
for (int i = 0; i < size; i++) {
entries[i] = getString(languages[i].getDisplayStringId());
entryValues[i] = languages[i].getId();
}

preference.setEntries(entries);
preference.setEntryValues(entryValues);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(Preferences.PREFS_THEME)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017 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.
*/

package com.physphil.android.unitconverterultimate.models

import com.physphil.android.unitconverterultimate.R

// ISO-639 Language codes
private const val LANG_DEFAULT = "def"
private const val LANG_CROATIAN = "hr"
private const val LANG_ENGLISH = "en"
private const val LANG_FARSI = "fa"
private const val LANG_FRENCH = "fr"
private const val LANG_GERMAN = "de"
private const val LANG_HUNGARIAN = "hu"
private const val LANG_JAPANESE = "ja"
private const val LANG_PORTUGUESE_BR = "pt_BR"
private const val LANG_RUSSIAN = "ru"
private const val LANG_SPANISH = "es"
private const val LANG_TURKISH = "tr"

/**
* Represents a Language that a user can select to display the app in.
* Copyright (c) 2017 Phil Shadlyn
*/
enum class Language(val id: String) {
DEFAULT(LANG_DEFAULT),
CROATIAN(LANG_CROATIAN),
ENGLISH(LANG_ENGLISH),
FARSI(LANG_FARSI),
FRENCH(LANG_FRENCH),
GERMAN(LANG_GERMAN),
HUNGARIAN(LANG_HUNGARIAN),
JAPANESE(LANG_JAPANESE),
PORTUGUESE_BR(LANG_PORTUGUESE_BR),
RUSSIAN(LANG_RUSSIAN),
SPANISH(LANG_SPANISH),
TURKISH(LANG_TURKISH);

val displayStringId = when (id) {
LANG_CROATIAN -> R.string.language_croatian
LANG_ENGLISH -> R.string.language_english
LANG_FARSI -> R.string.language_farsi
LANG_FRENCH -> R.string.language_french
LANG_GERMAN -> R.string.language_german
LANG_HUNGARIAN -> R.string.language_hungarian
LANG_JAPANESE -> R.string.language_japanese
LANG_PORTUGUESE_BR -> R.string.language_portuguese_brazil
LANG_RUSSIAN -> R.string.language_russian
LANG_SPANISH -> R.string.language_spanish
LANG_TURKISH -> R.string.language_turkish
else -> R.string.language_default
}

companion object {
/**
* Get the Language corresponding to the given id.
* @param id ISO-639 code for the Language value
* @return the Language corresponding to the given id, or DEFAULT if the language id is not supported
*/
@JvmStatic
fun fromId(id: String) = values().firstOrNull { it.id == id } ?: DEFAULT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.physphil.android.unitconverterultimate.util.Conversions;

import java.math.BigDecimal;
import java.math.RoundingMode;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -206,14 +207,14 @@ public void convertFuelValue(double value, Unit from, Unit to) {
{
BigDecimal toBase = new BigDecimal(from.getConversionToBaseUnit());
BigDecimal fromBase = new BigDecimal(to.getConversionFromBaseUnit());
BigDecimal resultBd = toBase.divide(new BigDecimal(value), BigDecimal.ROUND_UP).multiply(fromBase);
BigDecimal resultBd = toBase.divide(new BigDecimal(value), RoundingMode.UP).multiply(fromBase);
result = resultBd.doubleValue();
}
else if (to.getId() == Unit.L_100K) // Litres/100km
{
BigDecimal fromBase = new BigDecimal(to.getConversionFromBaseUnit());
BigDecimal toBase = new BigDecimal(from.getConversionToBaseUnit());
BigDecimal resultBd = fromBase.divide(new BigDecimal(value).multiply(toBase), BigDecimal.ROUND_UP);
BigDecimal resultBd = fromBase.divide(new BigDecimal(value).multiply(toBase), RoundingMode.UP);
result = resultBd.doubleValue();
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2017 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.
*/

package com.physphil.android.unitconverterultimate.presenters

import android.content.Context
import android.os.Build
import com.physphil.android.unitconverterultimate.Preferences
import com.physphil.android.unitconverterultimate.models.Language
import java.util.*

class MainActivityPresenter(private val view: MainActivityView, private val context: Context, private val prefs: Preferences) {

fun onLanguageChanged() {
// The language has already been set when user picks from dialog, restart app to take effect
view.restartApp()
}

fun setLanguageToDisplay() {
val language = prefs.language

// Use default locale if not specified, otherwise use saved locale from preferences
val locale = when (language) {
Language.DEFAULT -> Locale.getDefault()
Language.PORTUGUESE_BR -> Locale("pt", "BR")
else -> Locale(language.id)
}

val resources = context.resources
val configuration = resources.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale)
} else {
configuration.locale = locale
}
resources.updateConfiguration(configuration, resources.displayMetrics)
}
}
Loading

0 comments on commit e113077

Please sign in to comment.