Skip to content

Commit

Permalink
Multiples cambios
Browse files Browse the repository at this point in the history
Eliminado fichero antiguo de ajustes de Android Studio
Adaptado PlayTourFragment para múltiples formas de juego
Añadidas claves ajenas a entidades Close #22
  • Loading branch information
martinlaizg committed May 4, 2019
1 parent 1c3e208 commit 91cce77
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 99 deletions.
Binary file removed android-studio-settings/settings.jar
Binary file not shown.
27 changes: 27 additions & 0 deletions app/src/main/java/com/martinlaizg/geofind/CompletePlace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.martinlaizg.geofind;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

/**
* A simple {@link Fragment} subclass.
*/
public class CompletePlace
extends Fragment {

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_complete_place, container, false);
}

public CompletePlace() {
// Required empty public constructor
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

public class RetrofitInstance {

private static final String BASE_URL = "https://geo-find-martinlaizg.herokuapp.com/api/";
// private static final String BASE_URL = "http://192.168.1.182:8000/api/";
// private static final String BASE_URL = "https://geo-find-martinlaizg.herokuapp.com/api/";
private static final String BASE_URL = "http://192.168.1.34:8000/api/";
private static Retrofit retrofitInstance;

public static RestClient getRestClient() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.martinlaizg.geofind.data.access.database.entities;

import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import com.google.android.gms.maps.model.LatLng;
Expand All @@ -10,7 +12,9 @@
import java.sql.Date;
import java.util.Calendar;

@Entity(tableName = "places")
@Entity(tableName = "places",
foreignKeys = @ForeignKey(entity = Tour.class, childColumns = "tour_id",
parentColumns = "id"), indices = @Index("tour_id"))
public class Place {

@PrimaryKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.martinlaizg.geofind.data.access.database.entities;

import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import com.martinlaizg.geofind.data.enums.PlayLevel;
Expand All @@ -14,7 +16,12 @@
import java.util.Calendar;
import java.util.List;

@Entity(tableName = "tours")
import static androidx.room.ForeignKey.CASCADE;

@Entity(tableName = "tours",
foreignKeys = @ForeignKey(entity = User.class, childColumns = "creator_id",
parentColumns = "id", onDelete = CASCADE),
indices = @Index("creator_id"))
public class Tour {

@PrimaryKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import android.Manifest;
import android.app.AlertDialog;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.navigation.Navigation;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
Expand All @@ -24,7 +20,6 @@
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.material.button.MaterialButton;
import com.martinlaizg.geofind.R;
import com.martinlaizg.geofind.data.access.database.entities.Play;

import org.jetbrains.annotations.NotNull;

Expand All @@ -37,7 +32,6 @@ public class PlayMapFragment

private static final String TAG = PlayMapFragment.class.getSimpleName();

private static final float DISTANCE_TO_COMPLETE = 20;
private final static float MAX_ZOOM = 18.6f;
private static final float DISTANCE_TO_FIX_ZOOM = 100;
private static final int MAP_PADDING = 200;
Expand All @@ -54,11 +48,12 @@ public class PlayMapFragment
MaterialButton map_type_button;
@BindView(R.id.map_view)
MapView map_view;

private GoogleMap googleMap;

@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(
@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_play_tour, container, false);
ButterKnife.bind(this, view);
map_view.onCreate(savedInstanceState);
Expand All @@ -80,13 +75,9 @@ public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
if(requireActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
requireActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_ACCESS_COARSE_AND_FINE_LOCATION);
if(requireActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
requireActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_ACCESS_COARSE_AND_FINE_LOCATION);
return;
}
this.googleMap.setMyLocationEnabled(true);
Expand All @@ -101,61 +92,38 @@ protected String TAG() {
}

protected void updateView() {
Play play = viewModel.getPlay();
if(place != null) {
place_name.setText(place.getName());
place_description.setText(place.getDescription());
int completed = play.getPlaces().size() + 1;
int total = play.getTour().getPlaces().size();
place_complete
.setText(getResources().getString(R.string.tour_completenes, completed, total));

if(googleMap != null && usrLocation != null) {

// Set distance
Location placeLocation = new Location("");
placeLocation.setLatitude(place.getLat());
placeLocation.setLongitude(place.getLon());
Float distance = 0f;
distance = usrLocation.distanceTo(placeLocation);
Log.d(TAG, "updateView: distance=" + distance + "m");
place_distance.setText(
getResources().getString(R.string.place_distance, distance.intValue()));
if(distance < DISTANCE_TO_COMPLETE) {

Log.i(TAG, "updateView: user arrive to the place");
viewModel.completePlace(place.getId()).observe(this, done -> {
if(!done) {
Toast.makeText(requireContext(), viewModel.getError().getMessage(),
Toast.LENGTH_SHORT).show();
return;
}
Log.d(TAG, "updateView: Place done");
Bundle b = new Bundle();
b.putInt(TOUR_ID, viewModel.getTour().getId());
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.navigate(R.id.reload_play_map, b);
});
}

// Move map camera
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(usrLocation.getLatitude(), usrLocation.getLongitude()));
builder.include(place.getPosition());
CameraUpdate cu;
LatLngBounds cameraPosition = builder.build();
if(distance < DISTANCE_TO_FIX_ZOOM) {
cu = CameraUpdateFactory.newLatLngZoom(cameraPosition.getCenter(), MAX_ZOOM);
} else {
cu = CameraUpdateFactory.newLatLngBounds(cameraPosition, MAP_PADDING);
}
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.animateCamera(cu);
Log.d(TAG, "updateView: zoom=" + googleMap.getCameraPosition().zoom);

// Add place marker
googleMap.addMarker(new MarkerOptions().position(place.getPosition()));

if(place != null && googleMap != null && usrLocation != null) {
// Set distance
place_distance.setText(getResources().getString(R.string.place_distance, distance.intValue()));

// Move map camera
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(usrLocation.getLatitude(), usrLocation.getLongitude()));
builder.include(place.getPosition());
CameraUpdate cu;
LatLngBounds cameraPosition = builder.build();
if(distance < DISTANCE_TO_FIX_ZOOM) {
cu = CameraUpdateFactory.newLatLngZoom(cameraPosition.getCenter(), MAX_ZOOM);
} else {
cu = CameraUpdateFactory.newLatLngBounds(cameraPosition, MAP_PADDING);
}
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.animateCamera(cu);
Log.d(TAG, "updateView: zoom=" + googleMap.getCameraPosition().zoom);

// Add place marker
googleMap.addMarker(new MarkerOptions().position(place.getPosition()));
}
}

@Override
void setUpView() {
place_name.setText(place.getName());
place_description.setText(place.getDescription());
int numCompletedPlaces = viewModel.getPlay().getPlaces().size() + 1;
int numPlaces = viewModel.getPlay().getTour().getPlaces().size();
place_complete.setText(getResources().getString(R.string.tour_completenes, numCompletedPlaces, numPlaces));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -31,23 +32,24 @@ abstract class PlayTourFragment

static final int PERMISSION_ACCESS_COARSE_AND_FINE_LOCATION = 1;

private static final float DISTANCE_TO_COMPLETE = 10;
private static final long LOC_TIME_REQ = 500;
private static final float LOC_DIST_REQ = 5;

Place place;
PlayTourViewModel viewModel;
Location usrLocation;
Location placeLocation;
Float distance;

private LocationManager locationManager;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == PERMISSION_ACCESS_COARSE_AND_FINE_LOCATION && permissions.length >= 2) {
if(permissions[0].equals(Manifest.permission.ACCESS_COARSE_LOCATION) &&
grantResults[0] == PackageManager.PERMISSION_GRANTED &&
permissions[1].equals(Manifest.permission.ACCESS_FINE_LOCATION) &&
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
if(permissions[0].equals(Manifest.permission.ACCESS_COARSE_LOCATION) && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
permissions[1].equals(Manifest.permission.ACCESS_FINE_LOCATION) && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG(), "onRequestPermissionsResult: success");
}
Log.d(TAG(), "onRequestPermissionsResult: deny");
Expand All @@ -64,40 +66,34 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
if(b != null) {
tour_id = b.getInt(TOUR_ID);
}
User u = Preferences
.getLoggedUser(PreferenceManager.getDefaultSharedPreferences(requireContext()));
User u = Preferences.getLoggedUser(PreferenceManager.getDefaultSharedPreferences(requireContext()));
viewModel.loadPlay(u.getId(), tour_id).observe(requireActivity(), place -> {
if(place == null) {
Log.i(TAG(), "onViewCreated: tour completed");
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.navigate(R.id.toCompleteTour);
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder).navigate(R.id.toCompleteTour);
return;
}
this.place = place;
updateView();
placeLocation = new Location("");
placeLocation.setLatitude(place.getLat());
placeLocation.setLongitude(place.getLon());
setUpView();
});
}

@Override
public void onResume() {
super.onResume();
Log.i(TAG(), "onResume: check location permissions");
locationManager = (LocationManager) requireActivity()
.getSystemService(Context.LOCATION_SERVICE);
if(requireActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
requireActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_ACCESS_COARSE_AND_FINE_LOCATION);
locationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE);
if(requireActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
requireActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_ACCESS_COARSE_AND_FINE_LOCATION);
return;
}
usrLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(TAG(), "onResume: start request location updates");
locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOC_TIME_REQ, LOC_DIST_REQ,
this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOC_TIME_REQ, LOC_DIST_REQ, this);
updateView();
}

Expand All @@ -110,10 +106,29 @@ public void onPause() {

abstract void updateView();

abstract void setUpView();

@Override
public void onLocationChanged(@NonNull Location location) {
Log.i(TAG(), "onLocationChanged: ");
usrLocation = location;

// Set distance
distance = usrLocation.distanceTo(placeLocation);
Log.d(TAG(), "updateView: distance=" + distance + "m");
if(distance < DISTANCE_TO_COMPLETE) {
Log.i(TAG(), "updateView: user arrive to the place");
viewModel.completePlace(place.getId()).observe(this, done -> {
if(!done) {
Toast.makeText(requireContext(), viewModel.getError().getMessage(), Toast.LENGTH_SHORT).show();
return;
}
Log.d(TAG(), "updateView: Place done");
Bundle b = new Bundle();
b.putInt(TOUR_ID, viewModel.getTour().getId());
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder).navigate(R.id.reload_play_map, b);
});
}
updateView();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ public MutableLiveData<Boolean> completePlace(Integer place_id) {
public Tour getTour() {
return play.getTour();
}

}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/fragment_complete_place.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CompletePlace">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

</FrameLayout>
Loading

0 comments on commit 91cce77

Please sign in to comment.