packageorg.marcinzelent.liberavem;importandroid.app.Activity;importandroid.app.Fragment;importandroid.widget.Toast;importcom.android.volley.Request;importcom.android.volley.Response;importcom.android.volley.VolleyError;importcom.android.volley.toolbox.StringRequest;importcom.android.volley.toolbox.Volley;importcom.google.firebase.auth.FirebaseAuth;importcom.google.gson.Gson;importcom.google.gson.GsonBuilder;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.Locale;publicclassDataKeeper{privatestaticfinalDataKeeperinstance=newDataKeeper();publicstaticDataKeepergetInstance(){returninstance;}privateDataKeeper(){}privateList<Object>fragments=newArrayList<Object>();privateBird[]birds;privateObservation[]observations;publicBird[]getBirds(){returnbirds;}publicvoidsetBirds(Bird[]birds){this.birds=birds;}publicObservation[]getObservations(){returnobservations;}publicvoidsetObservations(Observation[]observations){this.observations=observations;}publicvoiddownloadData(finalActivityactivity){if(birds==null)downloadBirds(activity);downloadObservations(activity);}privatevoiddownloadBirds(finalActivityactivity){StringbirdsUrl="http://birdobservationservice.azurewebsites.net/Service1.svc/birds";finalStringRequestbirdsRequest=newStringRequest(Request.Method.GET,birdsUrl,newResponse.Listener<String>(){@OverridepublicvoidonResponse(Stringresponse){Gsongson=newGsonBuilder().create();birds=gson.fromJson(response,Bird[].class);callPopulator(activity);}},newcom.android.volley.Response.ErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){Toast.makeText(activity,"Couldn't connect to the database!",Toast.LENGTH_LONG).show();}});Volley.newRequestQueue(activity).add(birdsRequest);}privatevoiddownloadObservations(finalActivityactivity){StringobservationsUrl="http://birdobservationservice.azurewebsites.net/Service1.svc/observations";finalStringRequestobservationsRequest=newStringRequest(Request.Method.GET,observationsUrl,newResponse.Listener<String>(){@OverridepublicvoidonResponse(Stringresponse){Gsongson=newGsonBuilder().create();observations=gson.fromJson(response,Observation[].class);for(Observationo:observations){intp=o.getCreated().indexOf('+');longepoch=Long.parseLong(o.getCreated().substring(6,p));Datedate=newDate(epoch);Stringformatted=newSimpleDateFormat("dd.MM.yyyy HH:mm",Locale.ENGLISH).format(date);o.setCreated(formatted);}callPopulator(activity);}},newcom.android.volley.Response.ErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){Toast.makeText(activity,"Couldn't connect to the database!",Toast.LENGTH_LONG).show();}});Volley.newRequestQueue(activity).add(observationsRequest);}publicvoidaddFragment(Objectfragment){fragments.add(fragment);}publicvoidclearFragments(){fragments.clear();}privatevoidcallPopulator(Activityactivity){if(birds!=null&&observations!=null){for(Objectfragment:fragments){if(fragment.getClass()==AllObservationsFragment.class)((AllObservationsFragment)fragment).populateList(observations,birds);else{List<Observation>myObservationsList=newArrayList<>();for(Observationo:observations){Stringuid=FirebaseAuth.getInstance().getCurrentUser().getUid();if(o.getUserId().equals(uid))myObservationsList.add(o);}Observation[]myObservations=newObservation[myObservationsList.size()];myObservations=myObservationsList.toArray(myObservations);((MyObservationsFragment)fragment).populateList(myObservations,birds);}}}}}