diff options
author | Javier <dev.git@javispedro.com> | 2021-03-26 20:09:25 +0100 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2021-03-26 20:09:25 +0100 |
commit | 6b2dd5b290850301472a7097c84f76972b43b507 (patch) | |
tree | 670952beacb900aa15d4b9b3b367805cd18993c5 /app/src/main/java | |
parent | 558f29b84406b0d4b9493503f0ca02b35bfed9cf (diff) | |
download | rempe-6b2dd5b290850301472a7097c84f76972b43b507.tar.gz rempe-6b2dd5b290850301472a7097c84f76972b43b507.zip |
Add new Preferences activity and allow choosing temperature units
Diffstat (limited to 'app/src/main/java')
4 files changed, 60 insertions, 2 deletions
diff --git a/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java b/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java index 2e2a441..4d1c99a 100644 --- a/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java +++ b/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java @@ -1,11 +1,13 @@ package com.javispedro.rempe; import android.content.Context; +import android.content.SharedPreferences; import android.view.View; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; +import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.RecyclerView; import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult; @@ -18,6 +20,8 @@ public class DeviceViewHolder extends RecyclerView.ViewHolder implements Device. private final View mView; private final Context mContext; + private final SharedPreferences mPrefs; + private final TextView mTemperatureView; private final TextView mMinMaxTemperatureLabel; private final TextView mMinTemperatureView; @@ -29,12 +33,17 @@ public class DeviceViewHolder extends RecyclerView.ViewHolder implements Device. private final TextView mSignalLabel; private final ProgressBar mSignalBar; + private final static BigDecimal C2F_MULT = new BigDecimal("1.8"); + private final static BigDecimal C2F_ADD = new BigDecimal("32"); + private final static BigDecimal C2K_ADD = new BigDecimal("273.15"); + private Device mDevice; public DeviceViewHolder(View view) { super(view); mView = view; mContext = view.getContext(); + mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); mNameView = view.findViewById(R.id.nameView); mStatusView = view.findViewById(R.id.statusView); mStatusImageView = view.findViewById(R.id.statusImageView); @@ -82,8 +91,18 @@ public class DeviceViewHolder extends RecyclerView.ViewHolder implements Device. mSignalBar.setProgress(0); } - private String formatTemperature(BigDecimal temp) { - return mContext.getString(R.string.temperature_celsius, temp); + private String formatTemperature(BigDecimal tempC) { + switch (Preferences.getUnits(mPrefs)) { + default: + case "celsius": + return mContext.getString(R.string.temperature_celsius, tempC); + case "fahrenheit": + BigDecimal tempF = (tempC.multiply(C2F_MULT)).add(C2F_ADD); + return mContext.getString(R.string.temperature_fahrenheit, tempF); + case "kelvin": + BigDecimal tempK = (tempC.add(C2K_ADD)); + return mContext.getString(R.string.temperature_kelvin, tempK); + } } @Override diff --git a/app/src/main/java/com/javispedro/rempe/MainActivity.java b/app/src/main/java/com/javispedro/rempe/MainActivity.java index 48fa5fd..77caa96 100644 --- a/app/src/main/java/com/javispedro/rempe/MainActivity.java +++ b/app/src/main/java/com/javispedro/rempe/MainActivity.java @@ -1,5 +1,6 @@ package com.javispedro.rempe; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; @@ -104,6 +105,7 @@ public class MainActivity extends AppCompatActivity { switch (id) { case R.id.action_settings: + startActivity(new Intent(this, SettingsActivity.class)); return true; } diff --git a/app/src/main/java/com/javispedro/rempe/Preferences.java b/app/src/main/java/com/javispedro/rempe/Preferences.java index 41c943c..d82b004 100644 --- a/app/src/main/java/com/javispedro/rempe/Preferences.java +++ b/app/src/main/java/com/javispedro/rempe/Preferences.java @@ -40,4 +40,8 @@ class Preferences { editor.putString(PREFS_DEVICES, pref); editor.apply(); } + + public static String getUnits(SharedPreferences prefs) { + return prefs.getString("units", "celsius"); + } } diff --git a/app/src/main/java/com/javispedro/rempe/SettingsActivity.java b/app/src/main/java/com/javispedro/rempe/SettingsActivity.java new file mode 100644 index 0000000..f660c70 --- /dev/null +++ b/app/src/main/java/com/javispedro/rempe/SettingsActivity.java @@ -0,0 +1,33 @@ +package com.javispedro.rempe; + +import android.os.Bundle; + +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.preference.PreferenceFragmentCompat; + +public class SettingsActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.settings_activity); + if (savedInstanceState == null) { + getSupportFragmentManager() + .beginTransaction() + .replace(R.id.settings, new SettingsFragment()) + .commit(); + } + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + } + } + + public static class SettingsFragment extends PreferenceFragmentCompat { + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.root_preferences, rootKey); + } + } +}
\ No newline at end of file |