Skip to content

Commit

Permalink
Multiples cambios
Browse files Browse the repository at this point in the history
Cambiado orden de pantallas de creación de un tour #16
Mostrado dialogo de confirmación al salir del creador sin guardar (solo al darle al botón de atrás) #67
  • Loading branch information
martinlaizg committed Aug 8, 2019
1 parent ad4cc6a commit 28c9790
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 24 deletions.
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
//
// AndroidX
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.activity:activity:1.1.0-alpha02'
testImplementation 'androidx.arch.core:core-testing:2.0.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Expand All @@ -60,9 +61,9 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-reactivestreams:2.0.0'
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.0.0'
// Navigation
implementation 'androidx.navigation:navigation-common:2.0.0'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.navigation:navigation-common:2.1.0-beta02'
implementation 'androidx.navigation:navigation-fragment:2.1.0-beta02'
implementation 'androidx.navigation:navigation-ui:2.1.0-beta02'
implementation 'androidx.preference:preference:1.0.0'
// Room
implementation 'androidx.room:room-runtime:2.1.0'
Expand All @@ -78,7 +79,7 @@ dependencies {

//
// Google Material
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha09'

// Android Test
androidTestImplementation 'androidx.test:core:1.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

public class RetrofitInstance {

// private static final String BASE_URL = "https://geofind1.herokuapp.com/api/";
private static final String BASE_URL = "http://192.168.1.36:8000/api/";
private static final String BASE_URL = "https://geofind1.herokuapp.com/api/";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public Place(Integer id, String name, Double lat, Double lon, Integer tour_id,
@Ignore
public Place() {
id = 0;
this.name = "";
this.description = "";
}

public int getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
Expand All @@ -34,6 +35,7 @@
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;
import com.martinlaizg.geofind.R;
import com.martinlaizg.geofind.data.access.database.entities.Place;
Expand Down Expand Up @@ -95,6 +97,14 @@ public class CreatePlaceFragment

@Override
public void onClick(View v) {
// Hide the keyboard
InputMethodManager editTextInput = (InputMethodManager) requireActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = requireActivity().getCurrentFocus();
if(currentFocus != null) {
editTextInput.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}

alert_no_place_text.setVisibility(View.INVISIBLE);
Place place = getPlace();
if(place == null) return;
Expand Down Expand Up @@ -293,6 +303,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
new_place_map_view.onCreate(savedInstanceState);
new_place_map_view.onResume();
new_place_map_view.getMapAsync(this);

// Back button listener
requireActivity().getOnBackPressedDispatcher()
.addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
showExitDialog();
}
});
return view;
}

Expand Down Expand Up @@ -396,4 +415,13 @@ public void onMapLongClick(LatLng latLng) {
marker = m;

}

private void showExitDialog() {
new MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.are_you_sure)
.setMessage(getString(R.string.exit_lose_data_alert))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.popBackStack();
}).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -11,6 +12,7 @@
import android.widget.LinearLayout;
import android.widget.Spinner;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand All @@ -19,6 +21,7 @@
import androidx.preference.PreferenceManager;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;
import com.martinlaizg.geofind.R;
import com.martinlaizg.geofind.config.Preferences;
Expand Down Expand Up @@ -64,17 +67,34 @@ public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup
final View view = inflater.inflate(R.layout.fragment_create_tour, container, false);
ButterKnife.bind(this, view);
viewModel = ViewModelProviders.of(requireActivity()).get(CreatorViewModel.class);
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
showExitDialog();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
return view;
}

private void showExitDialog() {
new MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.are_you_sure)
.setMessage(getString(R.string.exit_lose_data_alert))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.popBackStack();
}).show();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Bundle b = getArguments();
int tour_id = 0;
if(b != null) {
tour_id = b.getInt(TOUR_ID);
}
viewModel.getTour(tour_id).observe(requireActivity(), this::setTour);
Log.i(TAG, "onViewCreated: Get tour with id = " + tour_id);
viewModel.getTour(tour_id).observe(this, this::setTour);

done_button.setOnClickListener(this);
add_image_button.setOnClickListener(v -> {
Expand All @@ -86,6 +106,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
private void setTour(Tour tour) {
if(tour == null) {
ErrorType error = viewModel.getError();
Log.e(TAG, "setTour: Error getting the tour" + error.toString());
if(error == ErrorType.EXIST) {
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.popBackStack();
Expand Down Expand Up @@ -138,6 +159,14 @@ private AlertDialog buildDialog() {

@Override
public void onClick(View v) {
// Hide the keyboard
InputMethodManager editTextInput = (InputMethodManager) requireActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = requireActivity().getCurrentFocus();
if(currentFocus != null) {
editTextInput.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}

tour_name_layout.setError("");
String name = Objects.requireNonNull(tour_name_layout.getEditText()).getText().toString()
.trim();
Expand Down Expand Up @@ -166,6 +195,8 @@ public void onClick(View v) {
User user = Preferences
.getLoggedUser(PreferenceManager.getDefaultSharedPreferences(requireContext()));
viewModel.updateTour(name, description, user.getId(), pl, image_url);
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder).popBackStack();
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.navigate(R.id.toEditTour);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand All @@ -16,6 +17,7 @@
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.martinlaizg.geofind.R;
import com.martinlaizg.geofind.data.access.api.error.ErrorType;
import com.martinlaizg.geofind.data.access.database.entities.Tour;
Expand Down Expand Up @@ -60,9 +62,26 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
adapter = new CreatorPlacesAdapter();
places_list.setLayoutManager(new LinearLayoutManager(requireActivity()));
places_list.setAdapter(adapter);
// Back button callback
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
showExitDialog();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
return view;
}

private void showExitDialog() {
new MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.are_you_sure)
.setMessage(getString(R.string.exit_lose_data_alert))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {
Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.popBackStack();
}).show();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Bundle b = getArguments();
Expand Down Expand Up @@ -105,6 +124,12 @@ private void setTour(Tour tour) {
}
}

@Override
public void onDestroy() {
super.onDestroy();
viewModel.reset();
}

@Override
public void onClick(View v) {
create_tour_button.setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import androidx.navigation.Navigation;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.martinlaizg.geofind.R;
import com.martinlaizg.geofind.config.Preferences;
import com.martinlaizg.geofind.data.access.api.service.exceptions.APIException;
Expand Down Expand Up @@ -174,7 +175,7 @@ public void onLocationChanged(@NonNull Location location) {

// If has question display
if(place != null && place.getQuestion() != null && !place.getQuestion().isEmpty()) {
createDialog(place);
showQuestionDialog(place);
questionDialog.show();
} else {
completePlace();
Expand Down Expand Up @@ -207,16 +208,11 @@ private void completePlace() {
viewModel.completePlace(place.getId()).observe(this, place -> {
if(place == null) {
if(viewModel.tourIsCompleted()) {
AlertDialog.Builder questionDialogBuilder = new AlertDialog.Builder(
requireContext()).setTitle(R.string.tour_completed);
questionDialogBuilder.setPositiveButton(R.string.ok,
(dialog, which) -> Navigation
.findNavController(
requireActivity(),
R.id.main_fragment_holder)
.popBackStack(R.id.navTour,
false));
questionDialogBuilder.show();
new MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.tour_completed) //
.setPositiveButton(R.string.ok, (dialog, which) -> Navigation
.findNavController(requireActivity(), R.id.main_fragment_holder)
.popBackStack(R.id.navTour, false)).show();
return;
}
APIException error = viewModel.getError();
Expand All @@ -243,8 +239,9 @@ PackageManager.PERMISSION_GRANTED && requireActivity()
}

@SuppressLint("MissingPermission")
private void createDialog(Place place) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext());
private void showQuestionDialog(Place place) {
AlertDialog.Builder dialogBuilder = new MaterialAlertDialogBuilder(requireContext());

View dialogView = getLayoutInflater()
.inflate(R.layout.question_layout, new ConstraintLayout(requireContext()), false);
TextView question = dialogView.findViewById(R.id.question);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public MutableLiveData<Tour> getTour(int tour_id) {
if(tour == null || tour.getId() != tour_id) {
return loadTour(tour_id);
}
new Thread(() -> t.postValue(tour));
new Thread(() -> t.postValue(tour)).start();
return t;
}

Expand Down Expand Up @@ -119,4 +119,8 @@ public void setPlace(Place place) {
}
}

public void reset() {
this.tour = null;
this.error = null;
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_create_tour.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@

<Spinner
android:id="@+id/difficulty_spinner"
style="spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/navigation/main_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
app:argType="integer" />
<action
android:id="@+id/toEditTour"
app:destination="@+id/navCreator" />
app:destination="@+id/navCreator"
app:popUpTo="@+id/navCreateTour"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/navCreator"
Expand All @@ -43,7 +45,6 @@
tools:layout="@layout/fragment_creator">
<argument
android:name="TOUR_ID"
android:defaultValue="0"
app:argType="integer" />
<action
android:id="@+id/toCreatePlace"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,6 @@
<string name="add_image_url">Add image URL</string>
<string name="image_url">Image URL</string>
<string name="update">Update</string>
<string name="are_you_sure">Are you sure?</string>
<string name="exit_lose_data_alert">You will lose the unsaved data</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@
<string name="app_version">Version de la aplicación</string>
<string name="add_image_url">Añade URL de imagen</string>
<string name="image_url">URL de la imagen</string>
<string name="are_you_sure">¿Estás seguro?</string>
<string name="exit_lose_data_alert">Perderás todos los datos no guardados</string>
<string name="update">Actualizar</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@
<string name="add_image_url">Add image URL</string>
<string name="image_url">Image URL</string>
<string name="update">Update</string>
<string name="are_you_sure">Are you sure?</string>
<string name="exit_lose_data_alert">You will lose the unsaved data</string>
</resources>

0 comments on commit 28c9790

Please sign in to comment.