Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created March 4, 2015 22:33
Show Gist options
  • Save udacityandroid/3204b20e7672ba163cf6 to your computer and use it in GitHub Desktop.
Save udacityandroid/3204b20e7672ba163cf6 to your computer and use it in GitHub Desktop.
5.07 Refactor Detail Fragment
<!-- Strings for formatting weather-related data -->
<!-- Temperature format [CHAR LIMIT=5] -->
<string name="format_temperature"><xliff:g id="temp">%1.0f</xliff:g>\u00B0</string>
<!-- Windspeed formats -->
<!-- Wind in mph [CHAR LIMIT=25] -->
<string name="format_wind_mph">
Wind: <xliff:g id="speed">%1$1.0f</xliff:g> mph <xliff:g id="direction">%2$s</xliff:g>
</string>
<!-- Wind in kph [CHAR LIMIT=25] -->
<string name="format_wind_kmh">
Wind: <xliff:g id="speed">%1$1.0f</xliff:g> km/h <xliff:g id="direction">%2$s</xliff:g>
</string>
<!-- Pressure format CHAR LIMIT=25] -->
<string name="format_pressure">Pressure: <xliff:g id="pressure">%1.0f</xliff:g> hPa</string>
<!-- Humidity format CHAR LIMIT=25]-->
<string name="format_humidity">Humidity: <xliff:g id="humidity">%1.0f</xliff:g> %%</string>
public static String getFormattedWind(Context context, float windSpeed, float degrees) {
int windFormat;
if (Utility.isMetric(context)) {
windFormat = R.string.format_wind_kmh;
} else {
windFormat = R.string.format_wind_mph;
windSpeed = .621371192237334f * windSpeed;
}
// From wind direction in degrees, determine compass direction as a string (e.g NW)
// You know what's fun, writing really long if/else statements with tons of possible
// conditions. Seriously, try it!
String direction = "Unknown";
if (degrees >= 337.5 || degrees < 22.5) {
direction = "N";
} else if (degrees >= 22.5 && degrees < 67.5) {
direction = "NE";
} else if (degrees >= 67.5 && degrees < 112.5) {
direction = "E";
} else if (degrees >= 112.5 && degrees < 157.5) {
direction = "SE";
} else if (degrees >= 157.5 && degrees < 202.5) {
direction = "S";
} else if (degrees >= 202.5 && degrees < 247.5) {
direction = "SW";
} else if (degrees >= 247.5 && degrees < 292.5) {
direction = "W";
} else if (degrees >= 292.5 && degrees < 337.5) {
direction = "NW";
}
return String.format(context.getString(windFormat), windSpeed, direction);
}
@AlanCowap
Copy link

The if-statement in the getFormattedWind() method of the Utility.java class could be optimised as below, which also handles values in the invalid ranges <0 and >360
Regards,
Alan Cowap

    String direction = "Unknown";
    if (degrees < 22.5 && degrees >= 0.0) {
        direction = "N";
    } else if (degrees < 67.5) {
        direction = "NE";
    } else if (degrees < 112.5) {
        direction = "E";
    } else if (degrees < 157.5) {
        direction = "SE";
    } else if (degrees < 202.5) {
        direction = "S";
    } else if (degrees < 247.5) {
        direction = "SW";
    } else if (degrees < 292.5) {
        direction = "W";
    } else if (degrees < 337.5) {
        direction = "NW";
    } else if (degrees <=360.0 ){
        direction = "N";
    }

@cminter
Copy link

cminter commented Mar 17, 2015

private static final String[] directionsText = { "N", "NE", "E", "SE", "S", "SW", "W", "NW" };
private static final int DEG_TOTAL = 360;
// ...
int degreeRange= (int) (((degrees + (DEG_TOTAL / directionsText.length / 2)) % DEG_TOTAL)
/ (DEG_TOTAL / directionsText.length));
String direction= directionsText[degreeRange];

@dmytroKarataiev
Copy link

I think, this way is clearer and better:

final String[] directionsText = { "N", "NE", "E", "SE", "S", "SW", "W", "NW" };
final int DEGREES_TOTAL = 360;
final int DIR_TOTAL = 8;

String direction = directionsText[Math.round(degrees / (DEGREES_TOTAL / DIR_TOTAL)) % DIR_TOTAL];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment