diff --git a/app/build.gradle b/app/build.gradle index e1d83a5..3e74c1a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,6 +40,10 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + + buildFeatures { + dataBinding true + } } repositories { diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteDetailActivity.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteDetailActivity.java index c2012bb..cca2986 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteDetailActivity.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteDetailActivity.java @@ -25,6 +25,8 @@ import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; import android.widget.Toast; import androidx.annotation.NonNull; @@ -33,26 +35,87 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import it.danieleverducci.nextcloudmaps.R; +import it.danieleverducci.nextcloudmaps.api.ApiProvider; +import it.danieleverducci.nextcloudmaps.databinding.ActivityGeofavoriteDetailBinding; +import it.danieleverducci.nextcloudmaps.model.Geofavorite; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; public class GeofavoriteDetailActivity extends AppCompatActivity implements LocationListener, ActivityCompat.OnRequestPermissionsResultCallback { + public static final String TAG = "GeofavDetail"; public static final String ARG_GEOFAVORITE_ID = "geofavid"; private static final int PERMISSION_REQUEST_CODE = 9999; + private ActivityGeofavoriteDetailBinding binding; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_geofavorite_detail); + binding = ActivityGeofavoriteDetailBinding.inflate(getLayoutInflater()); + setContentView(binding.root); int id = getIntent().getIntExtra(ARG_GEOFAVORITE_ID, 0); if (id == 0) { - // New geofavorite: precompile location + // New geofavorite + Geofavorite gf = new Geofavorite(); + gf.setDateCreated(System.currentTimeMillis()); + gf.setDateModified(System.currentTimeMillis()); + binding.setGeofavorite(gf); + // Precompile location getLocation(); + } else { + // TODO: Load geofavorite from cache for edit } } + /** + * Called when the submit button is clicked + * @param v The button + */ + public void onSubmit(View v) { + saveGeofavorite(); + } + + /** + * Checks fields and sends updated geofavorite to Nextcloud instance + */ + private void saveGeofavorite() { + Geofavorite gf = binding.getGeofavorite(); + gf.setName(binding.nameEt.getText().toString()); + gf.setComment(binding.descriptionEt.getText().toString()); + gf.setDateModified(System.currentTimeMillis()); + + if (!gf.valid()) { + Toast.makeText(GeofavoriteDetailActivity.this, R.string.incomplete_geofavorite, Toast.LENGTH_SHORT).show(); + return; + } + + Call call; + if (gf.getId() == 0) { + // New geofavorite + call = ApiProvider.getAPI().createGeofavorite(gf); + } else { + // Update existing geofavorite + call = ApiProvider.getAPI().updateGeofavorite(gf.getId(), gf); + } + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + finish(); + } + + @Override + public void onFailure(Call call, Throwable t) { + Toast.makeText(GeofavoriteDetailActivity.this, R.string.error_saving_geofavorite, Toast.LENGTH_SHORT).show(); + Log.e(TAG, "Unable to update geofavorite: " + t.getMessage()); + } + }); + } + /** * Obtains the current location (requesting user's permission, if necessary) * and calls updateLocationField() @@ -73,7 +136,7 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca updateLocationField(lastKnown); // Register for location updates in case the user moves before saving locationManager.requestLocationUpdates( - LocationManager.GPS_PROVIDER, 5000, 10, this + LocationManager.GPS_PROVIDER, 1000, 10, this ); } @@ -82,7 +145,9 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca * @param location to set in the geofavorite */ private void updateLocationField(Location location) { - Log.d("Location", location.toString()); + binding.getGeofavorite().setLat(location.getLatitude()); + binding.getGeofavorite().setLng(location.getLongitude()); + binding.notifyChange(); } diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java index 2e49ca0..1d44eac 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java @@ -21,12 +21,18 @@ package it.danieleverducci.nextcloudmaps.model; import android.net.Uri; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.databinding.BindingAdapter; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.io.Serializable; import java.util.Comparator; +import java.util.Date; public class Geofavorite implements Serializable { /** @@ -48,6 +54,7 @@ public class Geofavorite implements Serializable { @SerializedName("id") private int id; @Expose + @Nullable @SerializedName("name") private String name; @Expose @@ -57,15 +64,16 @@ public class Geofavorite implements Serializable { @SerializedName("date_created") private long dateCreated; @Expose - @SerializedName("lat") private float lat; + @SerializedName("lat") private double lat; @Expose - @SerializedName("lng") private float lng; + @SerializedName("lng") private double lng; @Expose @SerializedName("category") private String category; @Expose + @Nullable @SerializedName("comment") private String comment; public int getId() { @@ -76,6 +84,7 @@ public class Geofavorite implements Serializable { this.id = id; } + @Nullable public String getName() { return name; } @@ -100,19 +109,19 @@ public class Geofavorite implements Serializable { this.dateCreated = dateCreated; } - public float getLat() { + public double getLat() { return lat; } - public void setLat(float lat) { + public void setLat(double lat) { this.lat = lat; } - public float getLng() { + public double getLng() { return lng; } - public void setLng(float lng) { + public void setLng(double lng) { this.lng = lng; } @@ -124,6 +133,7 @@ public class Geofavorite implements Serializable { this.category = category; } + @Nullable public String getComment() { return comment; } @@ -140,4 +150,18 @@ public class Geofavorite implements Serializable { return Uri.parse("geo:" + this.lat + "," + this.lng + "(" + this.name + ")"); } + @BindingAdapter("formatDate") + public static void formatDate(@NonNull TextView textView, long timestamp) { + textView.setText((new Date(timestamp)).toString()); + } + + public boolean valid() { + return getLat() != 0 && getLng() != 0 && getName() != null && getName().length() > 0; + } + + @NonNull + @Override + public String toString() { + return "[" + getName() + " (" + getLat() + "," + getLng() + ")]"; + } } diff --git a/app/src/main/res/layout/activity_geofavorite_detail.xml b/app/src/main/res/layout/activity_geofavorite_detail.xml index 63a580e..381e11f 100644 --- a/app/src/main/res/layout/activity_geofavorite_detail.xml +++ b/app/src/main/res/layout/activity_geofavorite_detail.xml @@ -1,68 +1,90 @@ - + - + + + + android:layout_width="match_parent" + android:layout_height="match_parent"> - + android:layout_height="200dp" + android:padding="50dp" + app:srcCompat="@drawable/ic_app" + android:background="@color/defaultBrand" /> - + android:layout_height="match_parent" + android:orientation="vertical" + android:padding="20dp"> - + - + - + - + + + + + + +