Change icon pack based on user preference
This commit is contained in:
@@ -108,7 +108,7 @@ public class ForecastAdapter extends CursorAdapter {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Glide.with(mContext)
|
Glide.with(mContext)
|
||||||
.load(Utility.getArtUrlForWeatherCondition(mContext, weatherId))
|
.load(Utility.getArtUrlForWeatherCondition(mContext, weatherId))
|
||||||
.error(fallbackIconId)
|
.error(fallbackIconId)
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ public class SettingsActivity extends PreferenceActivity
|
|||||||
// updated when the preference changes.
|
// updated when the preference changes.
|
||||||
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
|
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
|
||||||
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
|
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
|
||||||
|
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_art_pack_key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registers a shared preference change listener that gets notified when preferences change
|
// Registers a shared preference change listener that gets notified when preferences change
|
||||||
@@ -142,6 +143,9 @@ public class SettingsActivity extends PreferenceActivity
|
|||||||
// our location status has changed. Update the summary accordingly
|
// our location status has changed. Update the summary accordingly
|
||||||
Preference locationPreference = findPreference(getString(R.string.pref_location_key));
|
Preference locationPreference = findPreference(getString(R.string.pref_location_key));
|
||||||
bindPreferenceSummaryToValue(locationPreference);
|
bindPreferenceSummaryToValue(locationPreference);
|
||||||
|
} else if ( key.equals(getString(R.string.pref_art_pack_key)) ) {
|
||||||
|
// art pack have changed. update lists of weather entries accordingly
|
||||||
|
getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
|
|||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
public class Utility {
|
public class Utility {
|
||||||
public static String getPreferredLocation(Context context) {
|
public static String getPreferredLocation(Context context) {
|
||||||
@@ -225,30 +226,34 @@ public class Utility {
|
|||||||
* @return url for the corresponding weather artwork. null if no relation is found.
|
* @return url for the corresponding weather artwork. null if no relation is found.
|
||||||
*/
|
*/
|
||||||
public static String getArtUrlForWeatherCondition(Context context, int weatherId) {
|
public static String getArtUrlForWeatherCondition(Context context, int weatherId) {
|
||||||
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
String formatArtUrl = prefs.getString(context.getString(R.string.pref_art_pack_key),
|
||||||
|
context.getString(R.string.pref_art_pack_sunshine));
|
||||||
|
|
||||||
// Based on weather code data found at:
|
// Based on weather code data found at:
|
||||||
// http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
|
// http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
|
||||||
if (weatherId >= 200 && weatherId <= 232) {
|
if (weatherId >= 200 && weatherId <= 232) {
|
||||||
return context.getString(R.string.format_art_url, "storm");
|
return String.format(Locale.US, formatArtUrl, "storm");
|
||||||
} else if (weatherId >= 300 && weatherId <= 321) {
|
} else if (weatherId >= 300 && weatherId <= 321) {
|
||||||
return context.getString(R.string.format_art_url, "light_rain");
|
return String.format(Locale.US, formatArtUrl, "light_rain");
|
||||||
} else if (weatherId >= 500 && weatherId <= 504) {
|
} else if (weatherId >= 500 && weatherId <= 504) {
|
||||||
return context.getString(R.string.format_art_url, "rain");
|
return String.format(Locale.US, formatArtUrl, "rain");
|
||||||
} else if (weatherId == 511) {
|
} else if (weatherId == 511) {
|
||||||
return context.getString(R.string.format_art_url, "snow");
|
return String.format(Locale.US, formatArtUrl, "snow");
|
||||||
} else if (weatherId >= 520 && weatherId <= 531) {
|
} else if (weatherId >= 520 && weatherId <= 531) {
|
||||||
return context.getString(R.string.format_art_url, "rain");
|
return String.format(Locale.US, formatArtUrl, "rain");
|
||||||
} else if (weatherId >= 600 && weatherId <= 622) {
|
} else if (weatherId >= 600 && weatherId <= 622) {
|
||||||
return context.getString(R.string.format_art_url, "snow");
|
return String.format(Locale.US, formatArtUrl, "snow");
|
||||||
} else if (weatherId >= 701 && weatherId <= 761) {
|
} else if (weatherId >= 701 && weatherId <= 761) {
|
||||||
return context.getString(R.string.format_art_url, "fog");
|
return String.format(Locale.US, formatArtUrl, "fog");
|
||||||
} else if (weatherId == 761 || weatherId == 781) {
|
} else if (weatherId == 761 || weatherId == 781) {
|
||||||
return context.getString(R.string.format_art_url, "storm");
|
return String.format(Locale.US, formatArtUrl, "storm");
|
||||||
} else if (weatherId == 800) {
|
} else if (weatherId == 800) {
|
||||||
return context.getString(R.string.format_art_url, "clear");
|
return String.format(Locale.US, formatArtUrl, "clear");
|
||||||
} else if (weatherId == 801) {
|
} else if (weatherId == 801) {
|
||||||
return context.getString(R.string.format_art_url, "light_clouds");
|
return String.format(Locale.US, formatArtUrl, "light_clouds");
|
||||||
} else if (weatherId >= 802 && weatherId <= 804) {
|
} else if (weatherId >= 802 && weatherId <= 804) {
|
||||||
return context.getString(R.string.format_art_url, "clouds");
|
return String.format(Locale.US, formatArtUrl, "clouds");
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,4 +25,16 @@
|
|||||||
<item>@string/pref_units_metric</item>
|
<item>@string/pref_units_metric</item>
|
||||||
<item>@string/pref_units_imperial</item>
|
<item>@string/pref_units_imperial</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
|
<!-- Names of the art packs available -->
|
||||||
|
<string-array name="pref_art_pack_options">
|
||||||
|
<item>@string/pref_art_pack_label_sunshine</item>
|
||||||
|
<item>@string/pref_art_pack_label_cute_dogs</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
|
<!-- Format for retrieving art from an external source [CHAR LIMIT=NONE] -->
|
||||||
|
<string-array name="pref_art_pack_values">
|
||||||
|
<item>@string/pref_art_pack_sunshine</item>
|
||||||
|
<item>@string/pref_art_pack_cute_dogs</item>
|
||||||
|
</string-array>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -80,9 +80,23 @@
|
|||||||
<!-- Value in SharedPreferences for imperial temperature unit option [CHAR LIMIT=NONE] -->
|
<!-- Value in SharedPreferences for imperial temperature unit option [CHAR LIMIT=NONE] -->
|
||||||
<string name="pref_units_imperial" translatable="false">imperial</string>
|
<string name="pref_units_imperial" translatable="false">imperial</string>
|
||||||
|
|
||||||
|
<!-- Label for the art pack preference [CHAR LIMIT=30] -->
|
||||||
|
<string name="pref_art_pack_label">Icon Pack</string>
|
||||||
|
|
||||||
<!-- Format for retrieving art from an external source [CHAR LIMIT=NONE] -->
|
<!-- Label for Sunshine option in art pack preference [CHAR LIMIT=25] -->
|
||||||
<string name="format_art_url" translatable="false">https://raw.githubusercontent.com/udacity/Sunshine-Version-2/sunshine_master/app/src/main/res/drawable-xxhdpi/art_<xliff:g id="description">%s</xliff:g>.png</string>
|
<string name="pref_art_pack_label_sunshine">Sunshine</string>
|
||||||
|
|
||||||
|
<!-- Label for cute dogs option in art pack preference [CHAR LIMIT=25] -->
|
||||||
|
<string name="pref_art_pack_label_cute_dogs">Cute dogs</string>
|
||||||
|
|
||||||
|
<!-- Key name for art pack unit preference in SharedPreferences [CHAR LIMIT=NONE] -->
|
||||||
|
<string name="pref_art_pack_key" translatable="false">art_pack</string>
|
||||||
|
|
||||||
|
<!-- Value in SharedPreferences for Sunshine art pack option [CHAR LIMIT=NONE] -->
|
||||||
|
<string name="pref_art_pack_sunshine" translatable="false">https://raw.githubusercontent.com/udacity/Sunshine-Version-2/sunshine_master/app/src/main/res/drawable-xxhdpi/art_%s.png</string>
|
||||||
|
|
||||||
|
<!-- Value in SharedPreferences for cute dogs art pack option [CHAR LIMIT=NONE] -->
|
||||||
|
<string name="pref_art_pack_cute_dogs" translatable="false">https://raw.githubusercontent.com/udacity/Sunshine-Version-2/sunshine_master/app/src/main/res/drawable-mdpi/art_%s.png</string>
|
||||||
|
|
||||||
<!-- Language-specific constants -->
|
<!-- Language-specific constants -->
|
||||||
<string name="today">Today</string>
|
<string name="today">Today</string>
|
||||||
|
|||||||
@@ -35,6 +35,13 @@
|
|||||||
android:entryValues="@array/pref_units_values"
|
android:entryValues="@array/pref_units_values"
|
||||||
android:entries="@array/pref_units_options" />
|
android:entries="@array/pref_units_options" />
|
||||||
|
|
||||||
|
<ListPreference
|
||||||
|
android:title="@string/pref_art_pack_label"
|
||||||
|
android:key="@string/pref_art_pack_key"
|
||||||
|
android:defaultValue="@string/pref_art_pack_sunshine"
|
||||||
|
android:entryValues="@array/pref_art_pack_values"
|
||||||
|
android:entries="@array/pref_art_pack_options" />
|
||||||
|
|
||||||
<CheckBoxPreference
|
<CheckBoxPreference
|
||||||
android:title="@string/pref_enable_notifications_label"
|
android:title="@string/pref_enable_notifications_label"
|
||||||
android:key="@string/pref_enable_notifications_key"
|
android:key="@string/pref_enable_notifications_key"
|
||||||
|
|||||||
Reference in New Issue
Block a user