Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #1206 - Weather View Visibility #1207

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ private void goToNavDrawerItem(int item) {
startActivity(i);
break;
}
if (mCurrentNavDrawerPosition != NAVDRAWER_ITEM_NEARBY) {
WeatherUtils.toggleWeatherViewVisibility(false,weatherView);
}else{
setWeatherData();
}
invalidateOptionsMenu();
}

Expand Down Expand Up @@ -1997,7 +2002,7 @@ public void onValidRegion(boolean isValid) {
if(isValid){
makeWeatherRequest();
}else{
weatherView.setVisibility(View.GONE);
WeatherUtils.toggleWeatherViewVisibility(false,weatherView);
weatherResponse = null;
}
}
Expand All @@ -2007,6 +2012,8 @@ private void initWeatherView(){
}

private void setWeatherData() {
if(weatherResponse == null || mCurrentNavDrawerPosition != NAVDRAWER_ITEM_NEARBY) return;
WeatherUtils.toggleWeatherViewVisibility(true,weatherView);
TextView tempTxtView = findViewById(R.id.weatherTextView);
ImageView weatherImageView = findViewById(R.id.weatherStateImageView);
String weatherIcon = weatherResponse.getCurrent_forecast().getIcon();
Expand Down Expand Up @@ -2045,7 +2052,6 @@ private void makeWeatherRequest(){
@Override
public void onWeatherResponseReceived(ObaWeatherResponse response) {
if(response != null && response.getCurrent_forecast() != null){
weatherView.setVisibility(View.VISIBLE);
weatherResponse = response;
setWeatherData();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.onebusaway.android.ui.weather;

import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

Expand Down Expand Up @@ -39,6 +40,16 @@ public static void setWeatherTemp(TextView weatherTempTxtView, double temp) {
weatherTempTxtView.setText(temperatureText);
}

public static void toggleWeatherViewVisibility(boolean shouldShow, View weatherView) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend always trying to figure out how to reduce the amount of nesting that occurs from control flow within a method. I would recommend writing this method like this:

  • Check whether weatherView is null or not, and return early if it is.
  • DRY up the call to weatherView.setVisibility() by determining visibility with a ternary expression.
public static void toggleWeatherViewVisibility(boolean shouldShow, View weatherView) {
  (if weatherView == null) {
    return;
  }
  
  weatherView.setVisibility(shouldShow ? View.VISIBLE : View.GONE);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better I will refactor it

if (weatherView != null) {
if (shouldShow) {
weatherView.setVisibility(View.VISIBLE);
} else {
weatherView.setVisibility(View.GONE);
}
}
}


private static int getWeatherDrawableRes(String condition) {
switch (condition) {
Expand Down
Loading