Skip to content

Commit

Permalink
Añadida reordenación de sitios
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlaizg committed Aug 14, 2019
1 parent be8dd58 commit cedd085
Show file tree
Hide file tree
Showing 31 changed files with 263 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public interface PlaceDAO {
@Query("SELECT * FROM places WHERE id = :placeId")
Place getPlace(int placeId);

@Query("SELECT * FROM places WHERE tour_id = :tour_id")
@Query("SELECT * FROM places WHERE tour_id = :tour_id ORDER BY `order` ASC")
List<Place> getTourPlaces(Integer tour_id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public interface PlayDAO {
@Query("SELECT * FROM plays WHERE tour_id = :tour_id AND user_id = :user_id")
Play getPlay(int user_id, int tour_id);

@Query("SELECT p.* FROM places p INNER JOIN place_play pp ON p.id=pp.place_id WHERE pp.play_id = :play_id")
@Query("SELECT p.* FROM places p INNER JOIN place_play pp ON p.id=pp.place_id WHERE pp" +
".play_id = :play_id ORDER BY `order` ASC")
List<Place> getPlaces(Integer play_id);

@Query("SELECT * from plays WHERE id = :play_id")
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/martinlaizg/geofind/utils/KeyboardUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.martinlaizg.geofind.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class KeyboardUtils {

/**
* Hide the keyboard
*
* @param activity
* the activity
*/
public static void hideKeyboard(Activity activity) {
InputMethodManager editTextInput = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = activity.getCurrentFocus();
if(currentFocus != null && editTextInput != null) {
editTextInput.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void onBindViewHolder(@NonNull final CreatorPlacesViewHolder holder, int
holder.place_card.setOnClickListener(
v -> Navigation.findNavController(fragmentActivity, R.id.main_fragment_holder)
.navigate(R.id.toCreatePlace, b));

}

@Override
Expand Down Expand Up @@ -100,6 +99,17 @@ public void setPlaces(FragmentActivity fragmentActivity, List<Place> places) {
}
}

public void move(int from, int to) {
Place p = places.remove(from);
places.add(to, p);

for(int i = 0; i < places.size(); i++) {
places.get(i).setOrder(i);
}

notifyItemMoved(from, to);
}

class CreatorPlacesViewHolder
extends RecyclerView.ViewHolder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.martinlaizg.geofind.config.Preferences;
import com.martinlaizg.geofind.data.Secure;
import com.martinlaizg.geofind.data.access.api.entities.Login;
import com.martinlaizg.geofind.data.access.api.service.exceptions.APIException;
import com.martinlaizg.geofind.data.access.api.error.ErrorType;
import com.martinlaizg.geofind.data.access.database.entities.User;
import com.martinlaizg.geofind.views.viewmodel.EditProfileViewModel;
import com.squareup.picasso.Picasso;
Expand Down Expand Up @@ -205,9 +205,10 @@ private void updateUser() {
viewModel.updateUser(login, user).observe(this, newUser -> {
save_button.setEnabled(true);
if(newUser == null) {
APIException error = viewModel.getError();
Log.e(TAG, "updateUser: " + getString(R.string.something_went_wrong), error);
Toast.makeText(requireContext(), "Algo ha ido mal " + error.getType().toString(),
ErrorType error = viewModel.getError();
Log.e(TAG,
"updateUser: " + getString(R.string.something_went_wrong) + error.toString());
Toast.makeText(requireContext(), "Algo ha ido mal " + error.toString(),
Toast.LENGTH_SHORT).show();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class MainFragment

private User user;
private PlayListAdapter adapter;
@SuppressWarnings("FieldCanBeLocal")
private MainFragmentViewModel viewModel;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.martinlaizg.geofind.views.fragment.creator;

import android.app.AlertDialog;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
Expand All @@ -34,6 +32,7 @@
import com.google.android.material.textfield.TextInputLayout;
import com.martinlaizg.geofind.R;
import com.martinlaizg.geofind.data.access.database.entities.Place;
import com.martinlaizg.geofind.utils.KeyboardUtils;
import com.martinlaizg.geofind.views.viewmodel.CreatorViewModel;
import com.squareup.picasso.Picasso;

Expand Down Expand Up @@ -91,14 +90,7 @@ 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) {
Objects.requireNonNull(editTextInput)
.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
KeyboardUtils.hideKeyboard(requireActivity());

if(!storePlace()) return;

Expand All @@ -112,7 +104,6 @@ public void onClick(View v) {
*
* @return true if no has errors else false
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean storePlace() {
// Get the name
String placeName = Objects.requireNonNull(new_place_name.getEditText()).getText().toString()
Expand Down Expand Up @@ -292,14 +283,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
new_place_location_button.setOnClickListener(v -> {
if(!storePlace()) return;

// Hide the keyboard
InputMethodManager editTextInput = (InputMethodManager) requireActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = requireActivity().getCurrentFocus();
if(currentFocus != null) {
Objects.requireNonNull(editTextInput)
.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
KeyboardUtils.hideKeyboard(requireActivity());

Navigation.findNavController(requireActivity(), R.id.main_fragment_holder)
.navigate(R.id.toCreatePlaceLocation);
Expand Down Expand Up @@ -364,14 +348,8 @@ private AlertDialog buildDialog() {
place_image_view.setVisibility(View.VISIBLE);
}

// Hide the keyboard
InputMethodManager editTextInput = (InputMethodManager) requireActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = requireActivity().getCurrentFocus();
if(currentFocus != null) {
Objects.requireNonNull(editTextInput)
.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
KeyboardUtils.hideKeyboard(requireActivity());

}).create();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.martinlaizg.geofind.views.fragment.creator;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
Expand Down Expand Up @@ -56,7 +55,6 @@ public class CreatePlaceLocationFragment
private Location usrLocation;
private Location placeLocation;

@SuppressLint("MissingPermission")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.martinlaizg.geofind.views.fragment.creator;

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;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
Expand All @@ -29,6 +27,7 @@
import com.martinlaizg.geofind.data.access.database.entities.Tour;
import com.martinlaizg.geofind.data.access.database.entities.User;
import com.martinlaizg.geofind.data.enums.PlayLevel;
import com.martinlaizg.geofind.utils.KeyboardUtils;
import com.martinlaizg.geofind.views.viewmodel.CreatorViewModel;
import com.squareup.picasso.Picasso;

Expand Down Expand Up @@ -146,25 +145,13 @@ private AlertDialog buildDialog() {
tour_image_view.setVisibility(View.VISIBLE);
}

// Hide the keyboard
InputMethodManager editTextInput = (InputMethodManager) requireActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = requireActivity().getCurrentFocus();
if(currentFocus != null) {
editTextInput.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
KeyboardUtils.hideKeyboard(requireActivity());
}).create();
}

@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);
}
KeyboardUtils.hideKeyboard(requireActivity());

tour_name_layout.setError("");
String name = Objects.requireNonNull(tour_name_layout.getEditText()).getText().toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

Expand Down Expand Up @@ -62,6 +63,28 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
adapter = new CreatorPlacesAdapter();
places_list.setLayoutManager(new LinearLayoutManager(requireActivity()));
places_list.setAdapter(adapter);

ItemTouchHelper helper = new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder dragged,
@NonNull RecyclerView.ViewHolder target) {
int from = dragged.getAdapterPosition();
int to = target.getAdapterPosition();

adapter.move(from, to);
return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder,
int direction) {

}
});
helper.attachToRecyclerView(places_list);

// Back button callback
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.martinlaizg.geofind.R;
import com.martinlaizg.geofind.config.Preferences;
import com.martinlaizg.geofind.data.access.api.error.ErrorType;
import com.martinlaizg.geofind.data.access.api.service.exceptions.APIException;
import com.martinlaizg.geofind.data.access.database.entities.User;
import com.martinlaizg.geofind.data.enums.UserType;
import com.martinlaizg.geofind.views.adapter.TourListAdapter;
Expand Down Expand Up @@ -68,8 +67,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(requireContext());
User u = Preferences.getLoggedUser(sp);
if(u.getUser_type() != null && u.getUser_type() != UserType.USER) {
create_tour_button
.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.toCreateTour));
create_tour_button.setOnClickListener(
Navigation.createNavigateOnClickListener(R.id.toCreateTour));
} else {
create_tour_button.setOnClickListener(v -> Toast
.makeText(requireContext(), getString(R.string.no_permissions),
Expand All @@ -84,13 +83,13 @@ private void refreshTours() {
if(tours != null) {
adapter.setTours(tours);
} else {
APIException error = viewModel.getError();
if(error.getType() == ErrorType.NETWORK) {
ErrorType error = viewModel.getError();
if(error == ErrorType.NETWORK) {
Toast.makeText(requireContext(),
getResources().getString(R.string.network_error),
Toast.LENGTH_SHORT).show();
} else {
Log.e(TAG, "onCreateView: ", error);
Log.e(TAG, "onCreateView: " + error);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.martinlaizg.geofind.views.fragment.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
Expand All @@ -11,7 +10,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -35,7 +33,8 @@
import com.martinlaizg.geofind.config.Preferences;
import com.martinlaizg.geofind.data.Secure;
import com.martinlaizg.geofind.data.access.api.entities.Login;
import com.martinlaizg.geofind.data.access.api.service.exceptions.APIException;
import com.martinlaizg.geofind.data.access.api.error.ErrorType;
import com.martinlaizg.geofind.utils.KeyboardUtils;
import com.martinlaizg.geofind.views.viewmodel.LoginViewModel;

import java.util.Objects;
Expand Down Expand Up @@ -104,8 +103,8 @@ private void login(Login login) {
email_input.setError("");
password_input.setError("");
if(user == null) {
@SuppressWarnings("ThrowableNotThrown") APIException e = viewModel.getError();
switch(e.getType()) {
ErrorType e = viewModel.getError();
switch(e) {
case TOKEN:
case PROVIDER:
case PROVIDER_LOGIN:
Expand Down Expand Up @@ -146,13 +145,7 @@ private void login(Login login) {
load_layout.setVisibility(View.GONE);
});

// Hide the keyboard
InputMethodManager editTextInput = (InputMethodManager) requireActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = requireActivity().getCurrentFocus();
if(currentFocus != null) {
editTextInput.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
KeyboardUtils.hideKeyboard(requireActivity());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void registry() {
viewModel.registry(l).observe(this, (user) -> {
btn_registry.setEnabled(true);
if(user == null) {
if(viewModel.getError().getType() == ErrorType.EMAIL) {
if(viewModel.getError() == ErrorType.EMAIL) {
email_input.setError(getString(R.string.email_in_use));
} else {
Toast.makeText(requireContext(), getString(R.string.other_error),
Expand Down
Loading

0 comments on commit cedd085

Please sign in to comment.