Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit cdd3f2c

Browse files
sdspikesLyla
authored andcommitted
3.11 Add fully functional units setting
1 parent 29db009 commit cdd3f2c

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,15 @@ private String getReadableDateString(long time){
145145
/**
146146
* Prepare the weather high/lows for presentation.
147147
*/
148-
private String formatHighLows(double high, double low) {
148+
private String formatHighLows(double high, double low, String unitType) {
149+
150+
if (unitType.equals(getString(R.string.pref_units_imperial))) {
151+
high = (high * 1.8) + 32;
152+
low = (low * 1.8) + 32;
153+
} else if (!unitType.equals(getString(R.string.pref_units_metric))) {
154+
Log.d(LOG_TAG, "Unit type not found: " + unitType);
155+
}
156+
149157
// For presentation, assume the user doesn't care about tenths of a degree.
150158
long roundedHigh = Math.round(high);
151159
long roundedLow = Math.round(low);
@@ -193,6 +201,18 @@ private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays)
193201
dayTime = new Time();
194202

195203
String[] resultStrs = new String[numDays];
204+
205+
// Data is fetched in Celsius by default.
206+
// If user prefers to see in Fahrenheit, convert the values here.
207+
// We do this rather than fetching in Fahrenheit so that the user can
208+
// change this option without us having to re-fetch the data once
209+
// we start storing the values in a database.
210+
SharedPreferences sharedPrefs =
211+
PreferenceManager.getDefaultSharedPreferences(getActivity());
212+
String unitType = sharedPrefs.getString(
213+
getString(R.string.pref_units_key),
214+
getString(R.string.pref_units_metric));
215+
196216
for(int i = 0; i < weatherArray.length(); i++) {
197217
// For now, using the format "Day, description, hi/low"
198218
String day;
@@ -220,7 +240,7 @@ private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays)
220240
double high = temperatureObject.getDouble(OWM_MAX);
221241
double low = temperatureObject.getDouble(OWM_MIN);
222242

223-
highAndLow = formatHighLows(high, low);
243+
highAndLow = formatHighLows(high, low, unitType);
224244
resultStrs[i] = day + " - " + description + " - " + highAndLow;
225245
}
226246
return resultStrs;

app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void onCreate(Bundle savedInstanceState) {
4141
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
4242
// updated when the preference changes.
4343
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
44+
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
4445
}
4546

4647
/**

app/src/main/res/values/arrays.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string-array name="pref_units_options">
5+
<item>@string/pref_units_label_metric</item>
6+
<item>@string/pref_units_label_imperial</item>
7+
</string-array>
8+
9+
<string-array name="pref_units_values">
10+
<item>@string/pref_units_metric</item>
11+
<item>@string/pref_units_imperial</item>
12+
</string-array>
13+
</resources>

app/src/main/res/values/strings.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,22 @@
2828
<!-- Default postal code for location preference [CHAR LIMIT=NONE] -->
2929
<string name="pref_location_default" translatable="false">94043</string>
3030

31+
<!-- Label for the temperature units preference [CHAR LIMIT=30] -->
32+
<string name="pref_units_label">Temperature Units</string>
33+
34+
<!-- Label for metric option in temperature unit preference [CHAR LIMIT=25] -->
35+
<string name="pref_units_label_metric">Metric</string>
36+
37+
<!-- Label for imperial option in temperature unit preference [CHAR LIMIT=25] -->
38+
<string name="pref_units_label_imperial">Imperial</string>
39+
40+
<!-- Key name for temperature unit preference in SharedPreferences [CHAR LIMIT=NONE] -->
41+
<string name="pref_units_key" translatable="false">units</string>
42+
43+
<!-- Value in SharedPreferences for metric temperature unit option [CHAR LIMIT=NONE] -->
44+
<string name="pref_units_metric" translatable="false">metric</string>
45+
46+
<!-- Value in SharedPreferences for imperial temperature unit option [CHAR LIMIT=NONE] -->
47+
<string name="pref_units_imperial" translatable="false">imperial</string>
48+
3149
</resources>

app/src/main/res/xml/pref_general.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
android:inputType="text"
1111
android:singleLine="true" />
1212

13+
<ListPreference
14+
android:title="@string/pref_units_label"
15+
android:key="@string/pref_units_key"
16+
android:defaultValue="@string/pref_units_metric"
17+
android:entryValues="@array/pref_units_values"
18+
android:entries="@array/pref_units_options" />
19+
1320
</PreferenceScreen>

0 commit comments

Comments
 (0)