diff options
Diffstat (limited to 'app/src/main/java/org/marcinzelent')
7 files changed, 197 insertions, 27 deletions
diff --git a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java index a16e833..af4331a 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java @@ -4,6 +4,7 @@ package org.marcinzelent.liberavem; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -48,6 +49,17 @@ public class AllObservationsFragment extends Fragment { @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + final SwipeRefreshLayout sfl = view.findViewById(R.id.swiperefresh); + sfl.setOnRefreshListener( + new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + DataKeeper.getInstance().downloadData(getActivity()); + sfl.setRefreshing(false); + } + } + ); + DataKeeper.getInstance().downloadData(getActivity()); } diff --git a/app/src/main/java/org/marcinzelent/liberavem/BirdsListAdapter.java b/app/src/main/java/org/marcinzelent/liberavem/BirdsListAdapter.java new file mode 100644 index 0000000..68896c0 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/BirdsListAdapter.java @@ -0,0 +1,63 @@ +package org.marcinzelent.liberavem; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import com.squareup.picasso.Picasso; + + +public class BirdsListAdapter extends ArrayAdapter<String> { + + private final Bird[] birds; + private Context mContext; + + public BirdsListAdapter(Context context, Bird[] birds){ + super(context, R.layout.observations_list_item); + this.mContext = context; + this.birds = birds; + } + + @Override + public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + return getView(position, convertView, parent); + } + + @Override + public int getCount() { + return birds.length; + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + ViewHolder mViewHolder = new ViewHolder(); + + if (convertView == null) { + LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = mInflater.inflate(R.layout.birds_list_item, parent, false); + mViewHolder.name = convertView.findViewById(R.id.bli_name); + mViewHolder.photo = convertView.findViewById(R.id.bli_photo); + convertView.setTag(mViewHolder); + } else { + mViewHolder = (ViewHolder) convertView.getTag(); + } + + mViewHolder.name.setText(birds[position].getNameEnglish()); + Picasso.get().load(birds[position].getPhotoUrl()).into(mViewHolder.photo); + + return convertView; + } + + private static class ViewHolder { + TextView name; + ImageView photo; + } + +}
\ No newline at end of file diff --git a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java index 27f9dc4..2000eab 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java +++ b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java @@ -1,9 +1,6 @@ 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; @@ -103,7 +100,7 @@ public class DataKeeper { private void callPopulator(Activity activity) { if (birds != null && observations != null) { - MyObservationsFragment mo = (MyObservationsFragment) ((MainActivity)activity).getAllFragments().get(0); + MyObservationsFragment mo = (MyObservationsFragment) ((MainActivity)activity).getObservationFragments().get(0); List<Observation> myObservationsList = new ArrayList<>(); for (Observation o : observations) if (o.getUserId().equals("Sminem")) myObservationsList.add(o); @@ -111,7 +108,7 @@ public class DataKeeper { Observation[] myObservations = new Observation[myObservationsList.size()]; myObservations = myObservationsList.toArray(myObservations); mo.populateList(myObservations, birds); - AllObservationsFragment aof = (AllObservationsFragment) ((MainActivity)activity).getAllFragments().get(1); + AllObservationsFragment aof = (AllObservationsFragment) ((MainActivity)activity).getObservationFragments().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 7a17e94..4009f8c 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java @@ -9,6 +9,7 @@ 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.support.v4.widget.SwipeRefreshLayout; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; @@ -139,7 +140,7 @@ public class MainActivity extends AppCompatActivity } - public List<Fragment> getAllFragments() { + public List<Fragment> getObservationFragments() { 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 d0ad649..b83d7b4 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java @@ -4,6 +4,7 @@ package org.marcinzelent.liberavem; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -33,6 +34,20 @@ public class MyObservationsFragment extends Fragment { return rootView; } + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + final SwipeRefreshLayout sfl = view.findViewById(R.id.swiperefresh); + sfl.setOnRefreshListener( + new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + DataKeeper.getInstance().downloadData(getActivity()); + sfl.setRefreshing(false); + } + } + ); + } + public void populateList(final Observation[] observations, final Bird[] birds) { final ListView observationsListView = getView().findViewById(R.id.observations_list_view); diff --git a/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java b/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java index cf1cf18..e69eaba 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java +++ b/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java @@ -1,14 +1,22 @@ package org.marcinzelent.liberavem; -import android.app.Activity; +import android.Manifest; +import android.content.Context; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; +import android.provider.ContactsContract; +import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.EditText; +import android.widget.Spinner; import android.widget.Toast; import com.android.volley.AuthFailureError; @@ -25,40 +33,104 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.UnsupportedEncodingException; -import java.util.HashMap; -import java.util.Map; +import java.util.Calendar; public class NewObservationActivity extends AppCompatActivity { + private Bird selectedBird; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_observation); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + Button cancelButton = findViewById(R.id.new_cancel); + cancelButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); - Button button = findViewById(R.id.button); + Button button = findViewById(R.id.new_add); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View view) { postNewObservation(); } }); + + Spinner newBirdSpinner = findViewById(R.id.new_birds); + BirdsListAdapter adapter = new BirdsListAdapter(this, DataKeeper.getInstance().getBirds()); + newBirdSpinner.setAdapter(adapter); + newBirdSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { + selectedBird = DataKeeper.getInstance().getBirds()[i]; + } + + @Override + public void onNothingSelected(AdapterView<?> adapterView) { + + } + }); + + // Acquire a reference to the system Location Manager + LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + + // Define a listener that responds to location updates + LocationListener locationListener = new LocationListener() { + public void onLocationChanged(Location location) { + // Called when a new location is found by the network location provider. + ((EditText)findViewById(R.id.new_longitude)).setText(String.valueOf(location.getLongitude())); + ((EditText)findViewById(R.id.new_latitude)).setText(String.valueOf(location.getLatitude())); + } + + public void onStatusChanged(String provider, int status, Bundle extras) { + } + + public void onProviderEnabled(String provider) { + } + + public void onProviderDisabled(String provider) { + } + }; + + // Register the listener with the Location Manager to receive location updates + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED + && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + return; + } + locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); + } + + @Override + public boolean onSupportNavigateUp(){ + finish(); + return true; } public void postNewObservation() { try { JSONObject jsonBody = new JSONObject(); - jsonBody.put("BirdId", "51"); - jsonBody.put("Comment", "Værsgo, my friend"); - jsonBody.put("Created", "/Date(1524182400000+0000)/"); - jsonBody.put("Id", "162"); - jsonBody.put("Latitude", "30.1187718"); - jsonBody.put("Longitude", "51.381484"); - jsonBody.put("Placename", "Black lodge"); - jsonBody.put("Population", "5"); + jsonBody.put("BirdId", selectedBird.getId()); + jsonBody.put("Comment", ((EditText)findViewById(R.id.new_comment)).getText()); + jsonBody.put("Created", "/Date(" + System.currentTimeMillis() + "+0000)/"); + jsonBody.put("Id", "0"); + jsonBody.put("Latitude", ((EditText)findViewById(R.id.new_latitude)).getText()); + jsonBody.put("Longitude", ((EditText)findViewById(R.id.new_longitude)).getText()); + jsonBody.put("Placename", ((EditText)findViewById(R.id.new_place)).getText()); + jsonBody.put("Population", ((EditText)findViewById(R.id.new_population)).getText()); jsonBody.put("UserId", "Sminem"); - jsonBody.put("NameDanish", "gråkrage"); - jsonBody.put("NameEnglish", "hooded crow"); + jsonBody.put("NameDanish", selectedBird.getNameDanish()); + jsonBody.put("NameEnglish", selectedBird.getNameEnglish()); final String requestBody = jsonBody.toString(); String url = "http://birdobservationservice.azurewebsites.net/Service1.svc/observations"; @@ -66,13 +138,15 @@ public class NewObservationActivity extends AppCompatActivity { new Response.Listener<String>() { @Override public void onResponse(String response) { - Toast.makeText(getBaseContext(), "Couldn't add observation to the database!", Toast.LENGTH_LONG).show(); + Toast.makeText(getBaseContext(), "Successfully added activity to the database!", Toast.LENGTH_LONG).show(); + NewObservationActivity.this.finish(); + DataKeeper.getInstance().downloadData(NewObservationActivity.this); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - Toast.makeText(getBaseContext(), "Successfully added activity to the database!", Toast.LENGTH_LONG).show(); + Toast.makeText(getBaseContext(), "Couldn't add observation to the database!", Toast.LENGTH_LONG).show(); } }) { diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java index 83e9c9a..373fda6 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java @@ -3,6 +3,7 @@ package org.marcinzelent.liberavem; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.Toolbar; import android.widget.TextView; import com.squareup.picasso.Picasso; @@ -13,6 +14,7 @@ public class ObservationDetailsActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_observation_details); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); Intent intent = getIntent(); Observation observation = (Observation) intent.getSerializableExtra("Observation"); @@ -37,4 +39,10 @@ public class ObservationDetailsActivity extends AppCompatActivity { TextView detailsComment = findViewById(R.id.details_comment); if(observation.getComment() != null) detailsComment.setText(observation.getComment()); } + + @Override + public boolean onSupportNavigateUp(){ + finish(); + return true; + } } |