Skip to content

Commit

Permalink
Make sure there is only one loading dialog at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Jan 8, 2016
1 parent af611a6 commit 770be44
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions app/src/main/java/cz/martykan/forecastie/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class MainActivity extends AppCompatActivity {

View appView;

ProgressDialog progressDialog;
int loading = 0;

private List<Weather> longTermWeather;
private List<Weather> longTermTodayWeather;
private List<Weather> longTermTomorrowWeather;
Expand All @@ -71,6 +74,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_scrolling);
appView = findViewById(R.id.viewApp);

progressDialog = new ProgressDialog(MainActivity.this);

// Load toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Expand Down Expand Up @@ -438,12 +443,14 @@ public boolean onOptionsItemSelected(MenuItem item) {

public class GetWeatherTask extends AsyncTask<String, String, Void> {
String result = "";
private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

protected void onPreExecute() {
progressDialog.setMessage("Downloading your data...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
loading = 1;
if(!progressDialog.isShowing()) {
progressDialog.setMessage("Downloading your data...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
}

@Override
Expand Down Expand Up @@ -476,19 +483,24 @@ protected Void doInBackground(String... params) {

protected void onPostExecute(Void v) {
//parse JSON data
this.progressDialog.dismiss();
if(loading == 1) {
progressDialog.dismiss();
}
loading -= 1;
parseTodayJson(result);
}
}

class GetLongTermWeatherTask extends AsyncTask<String, String, Void> {
String result = "";
private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

protected void onPreExecute() {
progressDialog.setMessage("Downloading your data...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
loading += 1;
if(!progressDialog.isShowing()) {
progressDialog.setMessage("Downloading your data...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
}

@Override
Expand Down Expand Up @@ -521,7 +533,10 @@ protected Void doInBackground(String... params) {

protected void onPostExecute(Void v) {
//parse JSON data
this.progressDialog.dismiss();
if(loading == 1) {
progressDialog.dismiss();
}
loading -= 1;
parseLongTermJson(result);
}
}
Expand Down

0 comments on commit 770be44

Please sign in to comment.