aboutsummaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
Diffstat (limited to 'app/src')
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java50
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java118
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/MainActivity.java16
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java15
4 files changed, 141 insertions, 58 deletions
diff --git a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java
index e41b29e..a16e833 100644
--- a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java
+++ b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java
@@ -33,8 +33,6 @@ import java.util.TimeZone;
* A simple {@link Fragment} subclass.
*/
public class AllObservationsFragment extends Fragment {
- Observation[] observations;
- Bird[] birds;
public AllObservationsFragment() {
// Required empty public constructor
@@ -50,7 +48,11 @@ public class AllObservationsFragment extends Fragment {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- final ListView observationsListView = view.findViewById(R.id.observations_list_view);
+ DataKeeper.getInstance().downloadData(getActivity());
+ }
+
+ public void populateList(final Observation[] observations, final Bird[] birds) {
+ final ListView observationsListView = getView().findViewById(R.id.observations_list_view);
observationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
@@ -66,45 +68,7 @@ public class AllObservationsFragment extends Fragment {
startActivity(detailsIntent);
}
});
-
- String birdsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/birds";
- final StringRequest birdsRequest = new StringRequest(Request.Method.GET, birdsUrl, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Gson gson = new GsonBuilder().create();
- birds = gson.fromJson(response, Bird[].class);
- }
- }, new com.android.volley.Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(getActivity(), "Couldn't connect to the database!", Toast.LENGTH_LONG).show();
- }
- });
-
- String observationsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/observations";
- final StringRequest observationsRequest = new StringRequest(Request.Method.GET, observationsUrl, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Gson gson = new GsonBuilder().create();
- observations = gson.fromJson(response, Observation[].class);
- for (Observation o : observations) {
- int p = o.getCreated().indexOf('+');
- long epoch = Long.parseLong(o.getCreated().substring(6, p));
- Date date = new Date(epoch);
- String formatted = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.ENGLISH).format(date);
- o.setCreated(formatted);
- }
- final ObservationsListAdapter adapter = new ObservationsListAdapter(getActivity(), observations, birds);
- observationsListView.setAdapter(adapter);
- }
- }, new com.android.volley.Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(getActivity(), "Couldn't connect to the database!", Toast.LENGTH_LONG).show();
- }
- });
-
- Volley.newRequestQueue(getActivity()).add(birdsRequest);
- Volley.newRequestQueue(getActivity()).add(observationsRequest);
+ final ObservationsListAdapter adapter = new ObservationsListAdapter(getActivity(), observations, birds);
+ observationsListView.setAdapter(adapter);
}
}
diff --git a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java
new file mode 100644
index 0000000..67eba43
--- /dev/null
+++ b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java
@@ -0,0 +1,118 @@
+package org.marcinzelent.liberavem;
+
+import android.app.Activity;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.view.View;
+import android.widget.Toast;
+
+import com.android.volley.Request;
+import com.android.volley.Response;
+import com.android.volley.VolleyError;
+import com.android.volley.toolbox.StringRequest;
+import com.android.volley.toolbox.Volley;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+
+public class DataKeeper {
+
+ private static final DataKeeper instance = new DataKeeper();
+
+ public static DataKeeper getInstance() {
+ return instance;
+ }
+
+ private DataKeeper() {
+ }
+
+ private Bird[] birds;
+ private Observation[] observations;
+
+ public Bird[] getBirds() {
+ return birds;
+ }
+
+ public void setBirds(Bird[] birds) {
+ this.birds = birds;
+ }
+
+ public Observation[] getObservations() {
+ return observations;
+ }
+
+ public void setObservations(Observation[] observations) {
+ this.observations = observations;
+ }
+
+ public void downloadData(final Activity activity) {
+ downloadBirds(activity);
+ downloadObservations(activity);
+ }
+
+ private void downloadBirds(final Activity activity) {
+ String birdsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/birds";
+ final StringRequest birdsRequest = new StringRequest(Request.Method.GET, birdsUrl, new Response.Listener<String>() {
+ @Override
+ public void onResponse(String response) {
+ Gson gson = new GsonBuilder().create();
+ birds = gson.fromJson(response, Bird[].class);
+
+ callPopulator(activity);
+ }
+ }, new com.android.volley.Response.ErrorListener() {
+ @Override
+ public void onErrorResponse(VolleyError error) {
+ Toast.makeText(activity, "Couldn't connect to the database!", Toast.LENGTH_LONG).show();
+ }
+ });
+
+ Volley.newRequestQueue(activity).add(birdsRequest);
+ }
+
+ private void downloadObservations(final Activity activity) {
+ String observationsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/observations";
+ final StringRequest observationsRequest = new StringRequest(Request.Method.GET, observationsUrl, new Response.Listener<String>() {
+ @Override
+ public void onResponse(String response) {
+ Gson gson = new GsonBuilder().create();
+ observations = gson.fromJson(response, Observation[].class);
+ for (Observation o : observations) {
+ int p = o.getCreated().indexOf('+');
+ long epoch = Long.parseLong(o.getCreated().substring(6, p));
+ Date date = new Date(epoch);
+ String formatted = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.ENGLISH).format(date);
+ o.setCreated(formatted);
+ }
+ callPopulator(activity);
+ }
+ }, new com.android.volley.Response.ErrorListener() {
+ @Override
+ public void onErrorResponse(VolleyError error) {
+ Toast.makeText(activity, "Couldn't connect to the database!", Toast.LENGTH_LONG).show();
+ }
+ });
+
+ Volley.newRequestQueue(activity).add(observationsRequest);
+ }
+
+ private void callPopulator(Activity activity) {
+ if (birds != null && observations != null) {
+ MyObservationsFragment mo = (MyObservationsFragment) ((MainActivity)activity).getAllFragments().get(0);
+ List<Observation> myObservationsList = new ArrayList<>();
+ for (Observation o : observations)
+ if (o.getUserId().equals("Nancy")) myObservationsList.add(o);
+
+ Observation[] myObservations = new Observation[myObservationsList.size()];
+ myObservations = myObservationsList.toArray(myObservations);
+ mo.populateList(myObservations, birds);
+ AllObservationsFragment aof = (AllObservationsFragment) ((MainActivity)activity).getAllFragments().get(1);
+ aof.populateList(observations, birds);
+ }
+ }
+}
diff --git a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java
index b1c73b4..7a17e94 100644
--- a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java
+++ b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java
@@ -8,6 +8,7 @@ import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
@@ -17,6 +18,8 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
+import android.widget.AdapterView;
+import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Request;
@@ -26,11 +29,15 @@ import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import java.util.List;
+
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
ObservationsFragment.OnFragmentInteractionListener,
AtlasFragment.OnFragmentInteractionListener {
+ Fragment observationsFragment = null;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -39,17 +46,16 @@ public class MainActivity extends AppCompatActivity
setSupportActionBar(toolbar);
if (savedInstanceState ==null) {
- Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = ObservationsFragment.class;
try {
- fragment = (Fragment) fragmentClass.newInstance();
+ observationsFragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
- fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
+ fragmentManager.beginTransaction().replace(R.id.flContent, observationsFragment).commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
@@ -132,4 +138,8 @@ public class MainActivity extends AppCompatActivity
public void onFragmentInteraction(Uri uri) {
}
+
+ public List<Fragment> getAllFragments() {
+ return observationsFragment.getChildFragmentManager().getFragments();
+ }
}
diff --git a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java
index 1d27e99..d0ad649 100644
--- a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java
+++ b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java
@@ -20,8 +20,6 @@ import java.util.ArrayList;
* A simple {@link Fragment} subclass.
*/
public class MyObservationsFragment extends Fragment {
- Observation[] observations;
- Bird[] birds;
public MyObservationsFragment() {
// Required empty public constructor
@@ -35,14 +33,8 @@ public class MyObservationsFragment extends Fragment {
return rootView;
}
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- String[] list = new String[] {
- "String 1",
- "String 2",
- "String 3"
- };
- ListView observationsListView = view.findViewById(R.id.observations_list_view);
+ public void populateList(final Observation[] observations, final Bird[] birds) {
+ final ListView observationsListView = getView().findViewById(R.id.observations_list_view);
observationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
@@ -58,8 +50,7 @@ public class MyObservationsFragment extends Fragment {
startActivity(detailsIntent);
}
});
-
- ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, list);
+ final ObservationsListAdapter adapter = new ObservationsListAdapter(getActivity(), observations, birds);
observationsListView.setAdapter(adapter);
}
}