From 4e6fc4fcdbcc3c75be13b3f8b80c24e4648fe354 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Thu, 19 Apr 2018 23:03:13 +0200 Subject: Added details view, improved observations list --- .idea/caches/build_file_checksums.ser | Bin 508 -> 535 bytes .idea/kotlinc.xml | 7 - .idea/misc.xml | 14 +- .idea/modules.xml | 2 +- .idea/vcs.xml | 7 - app/build.gradle | 13 +- app/src/main/AndroidManifest.xml | 5 +- .../liberavem/AllObservationsFragment.java | 73 ++++++- .../main/java/org/marcinzelent/liberavem/Bird.java | 52 +++-- .../org/marcinzelent/liberavem/MainActivity.java | 9 + .../liberavem/MyObservationsFragment.java | 21 +- .../org/marcinzelent/liberavem/Observation.java | 93 +++++++++ .../liberavem/ObservationDetailsActivity.java | 36 ++++ .../liberavem/ObservationsFragment.java | 2 - .../liberavem/ObservationsListAdapter.java | 88 +++++++++ .../marcinzelent/liberavem/SquareImageView.java | 28 +++ .../res/layout/activity_observation_details.xml | 216 +++++++++++++++++++++ app/src/main/res/layout/observations_list_item.xml | 41 ++++ .../main/res/values/attrs_square_image_view.xml | 8 + app/src/main/res/values/strings.xml | 7 + build.gradle | 4 +- 21 files changed, 675 insertions(+), 51 deletions(-) delete mode 100644 .idea/kotlinc.xml delete mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/org/marcinzelent/liberavem/Observation.java create mode 100644 app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java create mode 100644 app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java create mode 100644 app/src/main/java/org/marcinzelent/liberavem/SquareImageView.java create mode 100644 app/src/main/res/layout/activity_observation_details.xml create mode 100644 app/src/main/res/layout/observations_list_item.xml create mode 100644 app/src/main/res/values/attrs_square_image_view.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index d0a563d..0f5c9d1 100644 Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml deleted file mode 100644 index 5806fb3..0000000 --- a/.idea/kotlinc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index d1817bf..99202cc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,11 +5,12 @@ @@ -24,5 +25,10 @@ - + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index f6f9cee..33f5b4a 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 8306744..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 21419c7..fe925d9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,11 +26,14 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" - implementation 'com.android.support:appcompat-v7:27.1.0' - implementation 'com.android.support:support-v4:27.1.0' - implementation 'com.android.support:design:27.1.0' - implementation 'com.android.support.constraint:constraint-layout:1.0.2' - implementation 'com.android.support:support-vector-drawable:27.1.0' + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support:support-v4:27.1.1' + implementation 'com.android.support:design:27.1.1' + implementation 'com.android.support.constraint:constraint-layout:1.1.0' + implementation 'com.android.support:support-vector-drawable:27.1.1' + implementation 'com.android.volley:volley:1.1.0' + implementation 'com.google.code.gson:gson:2.8.2' + implementation 'com.squareup.retrofit2:retrofit:2.4.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c5206f3..edc0b5d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ xmlns:tools="http://schemas.android.com/tools" package="org.marcinzelent.liberavem"> + + + android:label="@string/title_activity_settings" /> + \ No newline at end of file diff --git a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java index ee07682..3f3ad7b 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java @@ -1,18 +1,40 @@ package org.marcinzelent.liberavem; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Toast; + +import com.android.volley.Request; +import com.android.volley.RequestQueue; +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 org.jetbrains.annotations.Nullable; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; /** * A simple {@link Fragment} subclass. */ public class AllObservationsFragment extends Fragment { - + Observation[] observations; + Bird[] birds; public AllObservationsFragment() { // Required empty public constructor @@ -26,4 +48,51 @@ public class AllObservationsFragment extends Fragment { return rootView; } + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + final ListView observationsListView = view.findViewById(R.id.observations_list_view); + + observationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) { + Intent detailsIntent = new Intent(getActivity(), ObservationDetailsActivity.class); + detailsIntent.putExtra("Observation", observations[position]); + + //String photoUrl = ""; + //for (Bird bird : birds) + // if (bird.getId() == observations[position].getBirdId()) + // photoUrl = bird.getPhotoUrl(); + //detailsIntent.putExtra("Photo", photoUrl); + startActivity(detailsIntent); + } + }); + + //final ObservationsListAdapter adapter = new ObservationsListAdapter(getActivity(), observations, birds); + //observationsListView.setAdapter(adapter); + + String observationsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/observations"; + final StringRequest observationRequest = new StringRequest(Request.Method.GET, observationsUrl, new Response.Listener() { + @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(), "Database connection error :( ", Toast.LENGTH_LONG).show(); + } + }); + + Volley.newRequestQueue(getActivity()).add(observationRequest); + } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/Bird.java b/app/src/main/java/org/marcinzelent/liberavem/Bird.java index 668059c..a94faf3 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/Bird.java +++ b/app/src/main/java/org/marcinzelent/liberavem/Bird.java @@ -1,37 +1,51 @@ package org.marcinzelent.liberavem; -public class Bird { - private int Id; - private long Created; - private String NameEnglish; - private String NameDanish; - private String PhotoUrl; - - public Bird(int id, long created, String nameEnglish, String nameDanish, String photoUrl) { - Id = id; - Created = created; - NameEnglish = nameEnglish; - NameDanish = nameDanish; - PhotoUrl = photoUrl; +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; + +public class Bird implements Serializable { + @SerializedName("Id") + private int id; + @SerializedName("Created") + private String created; + @SerializedName("NameEnglish") + private String nameEnglish; + @SerializedName("NameDanish") + private String nameDanish; + @SerializedName("PhotoUrl") + private String photoUrl; + + public Bird(int id, String created, String nameEnglish, String nameDanish, String photoUrl) { + this.id = id; + this.created = created; + this.nameEnglish = nameEnglish; + this.nameDanish = nameDanish; + this.photoUrl = photoUrl; } public int getId() { - return Id; + return id; } - public long getCreated() { - return Created; + public String getCreated() { + return created; } public String getNameEnglish() { - return NameEnglish; + return nameEnglish; } public String getNameDanish() { - return NameDanish; + return nameDanish; } public String getPhotoUrl() { - return PhotoUrl; + return photoUrl; + } + + @Override + public String toString() { + return getNameEnglish(); } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java index dc261c5..b1c73b4 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java @@ -17,6 +17,14 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; +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.google.gson.Gson; +import com.google.gson.GsonBuilder; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, @@ -51,6 +59,7 @@ public class MainActivity extends AppCompatActivity toggle.syncState(); NavigationView navigationView = findViewById(R.id.nav_view); + navigationView.getMenu().getItem(0).setChecked(true); navigationView.setNavigationItemSelectedListener(this); } diff --git a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java index bcc9067..1d27e99 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java @@ -1,11 +1,13 @@ package org.marcinzelent.liberavem; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; @@ -18,7 +20,8 @@ import java.util.ArrayList; * A simple {@link Fragment} subclass. */ public class MyObservationsFragment extends Fragment { - + Observation[] observations; + Bird[] birds; public MyObservationsFragment() { // Required empty public constructor @@ -40,6 +43,22 @@ public class MyObservationsFragment extends Fragment { "String 3" }; ListView observationsListView = view.findViewById(R.id.observations_list_view); + + observationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) { + Intent detailsIntent = new Intent(getActivity(), ObservationDetailsActivity.class); + detailsIntent.putExtra("Observation", observations[position]); + + String photoUrl = ""; + for (Bird bird : birds) + if (bird.getId() == observations[position].getBirdId()) + photoUrl = bird.getPhotoUrl(); + detailsIntent.putExtra("Photo", photoUrl); + startActivity(detailsIntent); + } + }); + ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, list); observationsListView.setAdapter(adapter); } diff --git a/app/src/main/java/org/marcinzelent/liberavem/Observation.java b/app/src/main/java/org/marcinzelent/liberavem/Observation.java new file mode 100644 index 0000000..be02812 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/Observation.java @@ -0,0 +1,93 @@ +package org.marcinzelent.liberavem; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; + +public class Observation implements Serializable { + @SerializedName("Id") + private int id; + @SerializedName("Created") + private String created; + @SerializedName("Latitude") + private double latitude; + @SerializedName("Longitude") + private double longitude; + @SerializedName("Placename") + private String placeName; + @SerializedName("Comment") + private String comment; + @SerializedName("UserId") + private String userId; + @SerializedName("BirdId") + private int birdId; + @SerializedName("NameEnglish") + private String nameEnglish; + @SerializedName("NameDanish") + private String nameDanish; + @SerializedName("Population") + private int population; + + public Observation(int id, String created, double latitude, double longitude, String placeName, + String comment, String userId, int birdId, String nameEnglish, + String nameDanish, int population) { + this.id = id; + this.created = created; + this.latitude = latitude; + this.longitude = longitude; + this.placeName = placeName; + this.comment = comment; + this.userId = userId; + this.birdId = birdId; + this.nameEnglish = nameEnglish; + this.nameDanish = nameDanish; + this.population = population; + } + + public int getId() { + return id; + } + + public String getCreated() { + return created; + } + public void setCreated(String created) { + this.created = created; + } + + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + + public String getPlaceName() { + return placeName; + } + + public String getComment() { + return comment; + } + + public String getUserId() { + return userId; + } + + public int getBirdId() { + return birdId; + } + + public String getNameEnglish() { + return nameEnglish; + } + + public String getNameDanish() { + return nameDanish; + } + + public int getPopulation() { + return population; + } +} diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java new file mode 100644 index 0000000..8373145 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java @@ -0,0 +1,36 @@ +package org.marcinzelent.liberavem; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.widget.TextView; + +public class ObservationDetailsActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_observation_details); + + Intent intent = getIntent(); + Observation observation = (Observation) intent.getSerializableExtra("Observation"); + //String photoUrl = (String) intent.getSerializableExtra("Photo"); + + TextView detailsName = findViewById(R.id.details_name); + detailsName.setText(observation.getNameEnglish()); + TextView detailsCreated = findViewById(R.id.details_created); + detailsCreated.setText(observation.getCreated()); + TextView detailsUser = findViewById(R.id.details_user); + detailsUser.setText(observation.getUserId()); + TextView detailsPopulation = findViewById(R.id.details_population); + detailsPopulation.setText(String.valueOf(observation.getPopulation())); + TextView detailsPlacename = findViewById(R.id.details_placename); + detailsPlacename.setText(observation.getPlaceName()); + TextView detailsLongitude = findViewById(R.id.details_longitude); + detailsLongitude.setText(String.valueOf(observation.getLongitude())); + TextView detailsLatitude = findViewById(R.id.details_latitude); + detailsLatitude.setText(String.valueOf(observation.getLatitude())); + TextView detailsComment = findViewById(R.id.details_comment); + if(observation.getComment() != null) detailsComment.setText(observation.getComment()); + } +} diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java index aefaaf4..b093461 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java @@ -8,12 +8,10 @@ import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ListView; /** diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java new file mode 100644 index 0000000..b73f103 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java @@ -0,0 +1,88 @@ +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.BaseAdapter; +import android.widget.ImageView; +import android.widget.TextView; + + +public class ObservationsListAdapter extends BaseAdapter { + + private Context context; + private final Observation[] observations; + private final Bird[] birds; + + public ObservationsListAdapter(Context context, Observation[] observations, Bird[] birds){ + //super(context, R.layout.single_list_app_item, utilsArrayList); + this.context = context; + this.observations = observations; + this.birds = birds; + } + + @Override + public int getCount() { + return observations.length; + } + + @Override + public Object getItem(int i) { + return i; + } + + @Override + public long getItemId(int i) { + return i; + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + ViewHolder viewHolder; + final View result; + + if (convertView == null) { + + viewHolder = new ViewHolder(); + LayoutInflater inflater = LayoutInflater.from(context); + convertView = inflater.inflate(R.layout.observations_list_item, parent, false); + viewHolder.name = convertView.findViewById(R.id.oli_name); + viewHolder.date = convertView.findViewById(R.id.oli_date); + viewHolder.photo = convertView.findViewById(R.id.oli_photo); + + result = convertView; + + convertView.setTag(viewHolder); + } else { + viewHolder = (ViewHolder) convertView.getTag(); + result = convertView; + } + + viewHolder.name.setText(observations[position].getNameEnglish()); + viewHolder.date.setText(observations[position].getCreated()); + + String photoUrl = ""; + //for (Bird bird : birds) + // if (bird.getId() == observations[position].getBirdId()) + // photoUrl = bird.getPhotoUrl(); + + //viewHolder.photo.setImageResource(photoUrl); + + return convertView; + } + + public void add(int position, Observation observation, Bird bird) { + + } + + private static class ViewHolder { + TextView name; + TextView date; + ImageView photo; + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/marcinzelent/liberavem/SquareImageView.java b/app/src/main/java/org/marcinzelent/liberavem/SquareImageView.java new file mode 100644 index 0000000..ed5dbb7 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/SquareImageView.java @@ -0,0 +1,28 @@ +package org.marcinzelent.liberavem; + +import android.content.Context; +import android.util.AttributeSet; + +public class SquareImageView extends android.support.v7.widget.AppCompatImageView { + + public SquareImageView(Context context) { + super(context); + } + + public SquareImageView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SquareImageView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + int width = getMeasuredWidth(); + setMeasuredDimension(width, width); + } + +} diff --git a/app/src/main/res/layout/activity_observation_details.xml b/app/src/main/res/layout/activity_observation_details.xml new file mode 100644 index 0000000..938fe91 --- /dev/null +++ b/app/src/main/res/layout/activity_observation_details.xml @@ -0,0 +1,216 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/observations_list_item.xml b/app/src/main/res/layout/observations_list_item.xml new file mode 100644 index 0000000..3aea796 --- /dev/null +++ b/app/src/main/res/layout/observations_list_item.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs_square_image_view.xml b/app/src/main/res/values/attrs_square_image_view.xml new file mode 100644 index 0000000..13e655a --- /dev/null +++ b/app/src/main/res/values/attrs_square_image_view.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1f045bd..1561e86 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -88,4 +88,11 @@ Hello blank fragment My observations All observations + Created: + Author: + Population: + Place: + Latitude: + Longitude: + Comment: diff --git a/build.gradle b/build.gradle index 683db77..eb8c588 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.1.51' + ext.kotlin_version = '1.2.30' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.0' + classpath 'com.android.tools.build:gradle:3.1.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong -- cgit v1.2.3