commit b45dc33f6fc8dcd920ab0cc2cf31006144f7b6b2
parent c1a0eb440cb60e7f14fdedfe19f0b3184f55faeb
Author: William Lindholm <william_lindholm@outlook.com>
Date: Mon, 22 May 2023 18:47:54 +0200
Formatting and comments.
Diffstat:
4 files changed, 142 insertions(+), 81 deletions(-)
diff --git a/app/src/main/java/com/example/project_assignment/MainActivity.java b/app/src/main/java/com/example/project_assignment/MainActivity.java
@@ -17,7 +17,6 @@ import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
-import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
@@ -25,7 +24,6 @@ import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
-import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
@@ -98,6 +96,11 @@ public class MainActivity extends AppCompatActivity implements JsonTask.JsonTask
});
}
+ /**
+ * This function runs after JSON data has been fetched from remote database.
+ *
+ * @param json
+ */
@Override
public void onPostExecute(String json) {
Gson gson = new Gson();
@@ -113,16 +116,19 @@ public class MainActivity extends AppCompatActivity implements JsonTask.JsonTask
}
+ //Initialize recyclerview and inject meteorite data.
RecyclerView recyclerView = findViewById(R.id.recyclerView);
-
adapter = new RecyclerViewAdapter(this, meteorites);
-
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
-
adapter.notifyDataSetChanged();
}
+ /**
+ * Open popupwindow that shows all possible filter options.
+ *
+ * @param view Current view (MainActivity)
+ */
private void showFilterOptions(View view) {
//initialize layoutInflater
@@ -174,7 +180,6 @@ public class MainActivity extends AppCompatActivity implements JsonTask.JsonTask
}
}
-
//remove meteorites that did not match the filter settings.
for (Meteorite m : toRemove) {
meteorites.remove(m);
@@ -254,24 +259,44 @@ public class MainActivity extends AppCompatActivity implements JsonTask.JsonTask
}
+ /**
+ * Runs after location has been pulled.
+ *
+ * @param location the users current location data.
+ */
@Override
public void onLocationChanged(@NonNull Location location) {
+ /* Prevent meteorite locations from being recaculated each time users location changes,
+ * Or the meteorites have not yet been fetched from database. */
if (distanceSet || meteorites.isEmpty()) {
return;
}
+
+ Toast.makeText(this, "Loaded location", Toast.LENGTH_SHORT).show();
+
for (Meteorite m : meteorites) {
m.generateDistanceFrom(location.getLatitude(), location.getLongitude());
}
+
distanceSet = true;
+
+ //apply stored filter settings.
filterMeteorites();
}
+ /**
+ * Used to filter meteorites based on their mass, date and relative location to the user.
+ */
public void filterMeteorites() {
//if there are saved filter settings, load them.
if (preference.contains("minMass")) {
Toast.makeText(MainActivity.this, "Loaded stored filter", Toast.LENGTH_SHORT).show();
ArrayList<Meteorite> toRemove = new ArrayList<>();
+ /*
+ Since the list which is being iterated through cannot be changed during its iterations.
+ Append all meteorites which are to be removed to a new list.
+ */
for (Meteorite m : meteorites) {
boolean matchingFilter = m.matchingFilter(preference.getInt("minMass", 0), preference.getInt("maxMass", 0), preference.getInt("minYear", 0), preference.getInt("maxYear", 0), preference.getInt("distance", 0));
if (!matchingFilter) {
@@ -283,6 +308,7 @@ public class MainActivity extends AppCompatActivity implements JsonTask.JsonTask
meteorites.remove(m);
}
+ //sort the meteories based on the users preferred sorting method.
switch (preference.getString("sort", "")) {
case "location":
Collections.sort(meteorites, new SortByLocation());
diff --git a/app/src/main/java/com/example/project_assignment/Meteorite.java b/app/src/main/java/com/example/project_assignment/Meteorite.java
@@ -6,10 +6,7 @@ import android.os.Parcelable;
import androidx.annotation.NonNull;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
-import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.SerializedName;
public class Meteorite implements Parcelable {
@@ -20,38 +17,59 @@ public class Meteorite implements Parcelable {
@SerializedName(value = "size", alternate = "mass")
protected String mass;
- protected String location;
+ protected String category;
- private String latitude;
+ protected String location;
private String date;
- private String longitude;
-
protected JsonObject auxdata;
private double distance;
- @Override
- public String toString() {
+ public String getDescription() {
return "The meteorite " + name +
" has a mass of " + mass + "kg " +
"and touched the surface of the earth at " +
getDate() + "\n\n" +
"It landed at lat: " + getLatitude() +
" long: " + getLongitude() +
- " Roughly " + Math.round(distance) + "km from your location";
+ " Roughly " + Math.round(distance) + "km from your location" +
+ "\nIt is part of the category: " + category;
}
+ @Override
+ public String toString() {
+ return "Meteorite{" +
+ "name='" + name + '\'' +
+ ", id='" + id + '\'' +
+ ", mass='" + mass + '\'' +
+ ", category='" + category + '\'' +
+ ", location='" + location + '\'' +
+ ", latitude='" + getLatitude() + '\'' +
+ ", date='" + date + '\'' +
+ ", longitude='" + getLongitude() + '\'' +
+ ", auxdata=" + auxdata.toString() +
+ ", distance=" + distance +
+ '}';
+ }
+
+ /**
+ * Used to create parcel for sending Meteorite data as one object throgh intents.
+ */
protected Meteorite(Parcel in) {
name = in.readString();
id = in.readString();
mass = in.readString();
+ category = in.readString();
date = in.readString();
location = in.readString();
distance = in.readDouble();
}
+ /**
+ * When Meteorite is unpacked from parcel, use below function.
+ */
public static final Creator<Meteorite> CREATOR = new Creator<Meteorite>() {
@Override
public Meteorite createFromParcel(Parcel in) {
@@ -74,61 +92,31 @@ public class Meteorite implements Parcelable {
parcel.writeString(name);
parcel.writeString(id);
parcel.writeString(mass);
+ parcel.writeString(category);
parcel.writeString(getDate());
parcel.writeString(location);
parcel.writeDouble(distance);
}
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
+ /**
+ * Return and set distance valeue to the meteorite from coordinate.
+ * Code inspired by:
+ * https://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude
+ * @param extLatitude The external positions latitude.
+ * @param extLongitude The external positions longitude.
+ * @return Return the distance from the meteorite.
+ */
+ public double generateDistanceFrom(double extLatitude, double extLongitude) {
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public double getMass() {
- if (mass == null) {
- return 0;
- }
- double mass_double = Double.parseDouble(mass);
- return mass_double;
- }
-
- //return the distance of the meteorite from any position.
- public double generateDistanceFrom(double userLatitude, double userLongitude) {
-
- String latLong[] = location.split(",");
- latitude = latLong[0];
- longitude = latLong[1];
-
- if (latitude == null) {
- latitude = "0";
- }
- if (longitude == null) {
- longitude = "0";
- }
-
- //1 = user
- //2 = meteorite
-
- double meteoriteLatitude = Double.parseDouble(this.latitude);
- double meteoriteLongitude = Double.parseDouble(this.longitude);
+ double meteoriteLatitude = Double.parseDouble(getLatitude());
+ double meteoriteLongitude = Double.parseDouble(getLongitude());
final int R = 6371; // Radius of the earth
- double latDistance = Math.toRadians(meteoriteLatitude - userLatitude);
- double lonDistance = Math.toRadians(meteoriteLongitude - userLongitude);
+ double latDistance = Math.toRadians(meteoriteLatitude - extLatitude);
+ double lonDistance = Math.toRadians(meteoriteLongitude - extLongitude);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
- + Math.cos(Math.toRadians(userLatitude)) * Math.cos(Math.toRadians(meteoriteLatitude))
+ + Math.cos(Math.toRadians(extLatitude)) * Math.cos(Math.toRadians(meteoriteLatitude))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
@@ -138,14 +126,17 @@ public class Meteorite implements Parcelable {
return this.distance;
}
- public void setDistance(double distance) {
- this.distance = distance;
- }
-
- public double getDistance() {
- return this.distance;
- }
+ /**
+ * Used to filter meteorites using the parameters below, if the current meteorite
+ * does not match the specified criteria.
+ * @param minMass The lowest accepted mass.
+ * @param maxMass The highest accepted mass.
+ * @param minYear The lowest accepted year.
+ * @param maxYear The highest accepted year.
+ * @param maxDistance The furthest accepted distance.
+ * @return true, if all criteria match, else false.
+ */
public boolean matchingFilter(int minMass, int maxMass, int minYear, int maxYear, int maxDistance) {
//filter mass
double tempMass = Double.parseDouble(mass);
@@ -182,10 +173,11 @@ public class Meteorite implements Parcelable {
return true;
}
- public void setMass(String mass) {
- this.mass = mass;
- }
-
+ /**
+ * Since the date is stored in a dateTime format, truncate the String to only return year before
+ * returning.
+ * @return the year the meteorite landed.
+ */
public String getDate() {
if (date == null) {
date = auxdata.get("date").getAsString().substring(0, 4);
@@ -194,21 +186,60 @@ public class Meteorite implements Parcelable {
return date;
}
+ /**
+ * Since both lat and long values are stored in one string, separated by ",",
+ * split them into separate values before returning.
+ * @return latitude.
+ */
public String getLatitude() {
String latLong[] = location.split(",");
return latLong[0];
}
- public void setLatitude(String latitude) {
- this.latitude = latitude;
- }
-
+ /**
+ * Since both lat and long values are stored in one string, separated by ",",
+ * split them into separate values before returning.
+ * @return longitude.
+ */
public String getLongitude() {
String latLong[] = location.split(",");
return latLong[1];
}
- public void setLongitude(String longitude) {
- this.longitude = longitude;
+ /**
+ * To more easily handle calculations and comparisons based on mass,
+ * convert the value to double before returning.
+ * @return Double mass.
+ */
+ public double getMass() {
+ if (mass == null) {
+ return 0;
+ }
+ double mass_double = Double.parseDouble(mass);
+ return mass_double;
+ }
+
+ public void setDistance(double distance) {
+ this.distance = distance;
+ }
+
+ public double getDistance() {
+ return this.distance;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
}
}
diff --git a/app/src/main/java/com/example/project_assignment/MeteoriteActivity.java b/app/src/main/java/com/example/project_assignment/MeteoriteActivity.java
@@ -24,19 +24,24 @@ public class MeteoriteActivity extends AppCompatActivity implements OnMapReadyCa
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meteorite);
+ // fetch all data sent using the intent.
Intent intent = getIntent();
+ // unpack getParcelableExtra
meteorite = (Meteorite) intent.getParcelableExtra("meteorite");
TextView title = findViewById(R.id.textview_meteoriteTitle);
TextView info = findViewById(R.id.textview_meteoriteInfo);
title.setText(meteorite.name);
- info.setText(meteorite.toString());
+ info.setText(meteorite.getDescription());
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
}
+ /**
+ * Initialize a google maps mapview, and set the location and marker to the meteorites location.
+ */
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
Double.parseDouble(meteorite.getLatitude());
diff --git a/app/src/main/java/com/example/project_assignment/comparators/SortByWeight.java b/app/src/main/java/com/example/project_assignment/comparators/SortByWeight.java
@@ -11,8 +11,7 @@ public class SortByWeight implements Comparator<Meteorite> {
//place null values last.
if (m1.getMass() == 0) {
return 1;
- }
- else if (m2.getMass() == 0) {
+ } else if (m2.getMass() == 0) {
return -1;
} else if (m1.getMass() == 0 && m2.getMass() == 0) {
return 0;