Skip to content

Commit

Permalink
Merge branch 'hotfix/native_crash_fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
obolsh committed Nov 18, 2019
2 parents ecd7507 + 1114a7b commit 21f4de6
Show file tree
Hide file tree
Showing 39 changed files with 674 additions and 368 deletions.
13 changes: 9 additions & 4 deletions app/src/main/java/gps/map/navigator/model/DataCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class DataCache implements Cache {
@Override
public List<IMapPlace> getHistoryPlaces() {
List<byte[]> arrays = storage.getChunkedData();
if (arrays != null) {
if (arrays != null && !arrays.isEmpty()) {
int cacheSize = arrays.size();
List<IMapPlace> output = new ArrayList<>(cacheSize);
for (int i = 0; i < cacheSize; i++) {
Expand All @@ -69,7 +69,7 @@ private List<IMapPlace> appendMyLocation(@NonNull List<IMapPlace> output) {

@Override
public void setHistoryPlaces(@Nullable List<IMapPlace> historyPlaces) {
if (historyPlaces != null) {
if (historyPlaces != null && !historyPlaces.isEmpty()) {
sortByLastUsedTime(historyPlaces);
List<byte[]> arrays = new ArrayList<>();
for (int i = 0; i < historyPlaces.size(); i++) {
Expand All @@ -80,7 +80,9 @@ public void setHistoryPlaces(@Nullable List<IMapPlace> historyPlaces) {
}

private void sortByLastUsedTime(@NonNull List<IMapPlace> places) {
Collections.sort(places, new DescendingTimeComparator());
if (!places.isEmpty()) {
Collections.sort(places, new DescendingTimeComparator());
}
}

@Nullable
Expand Down Expand Up @@ -117,6 +119,9 @@ private void savePlace(@Nullable IMapPlace place, @Nullable String key) {
}

private boolean placeAlreadyExist(@NonNull List<IMapPlace> historyPlaces, @NonNull IMapPlace place) {
if (historyPlaces.isEmpty()) {
return false;
}
for (int i = 0; i < historyPlaces.size(); i++) {
if (place.getId().equals(historyPlaces.get(i).getId())) {
return true;
Expand Down Expand Up @@ -248,7 +253,7 @@ public void setRawData(@Nullable String key, @Nullable byte[] rawData) {
public void removeHistoryPlace(@Nullable IMapPlace placeToRemove) {
if (placeToRemove != null) {
List<IMapPlace> places = getHistoryPlaces();
if (places != null) {
if (places != null && !places.isEmpty()) {
int position = getPosition(places, placeToRemove);
places.remove(position);
setHistoryPlaces(places);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ public class BuildRouteFragment extends AbstractNaviFragment implements ISwipeRo
@Named(Constants.DestinationClickListener)
View.OnClickListener destinationClickListener;
@Nullable
private IMapPlace originPlace;
@Nullable
private IMapPlace destinationPlace;
@Nullable
private TextView originTitle;
@Nullable
private TextView destinationTitle;
Expand All @@ -89,6 +85,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
setupDestination(view);
setupSwipeOriginAndDestination(view);
setupToolbarNavigation(view);
logger.debug("BuildRouteFragment onCreateView");
return view;
}

Expand Down Expand Up @@ -123,16 +120,17 @@ private IMapPlace getBestOrigin() {
}

private void setOriginPlace(@Nullable IMapPlace place) {
if (place != null) {
if (place != null
&& (presenter.getLastOrigin() == null
|| !placesAreTheSame(place, presenter.getLastDestination()))) {
setOriginTitle(place.getTitle());
originPlace = place;
presenter.setLastOrigin(place);
logger.debug("Set origin as: " + place);
} else {
setOriginTitle(context.getResources().getString(R.string.choose_origin_default));
originPlace = null;
presenter.setLastOrigin(null);
logger.debug("Clean last origin");
}
presenter.setLastOrigin(originPlace);
}

private void setOriginTitle(@NonNull String title) {
Expand Down Expand Up @@ -169,33 +167,47 @@ private void setupToolbarNavigation(@NonNull View view) {

@Override
public void swipeOriginAndDestination() {
IMapPlace lastOrigin = originPlace;
IMapPlace lastDestination = destinationPlace;
logger.debug("BuildRouteFragment swipeOriginAndDestination");
IMapPlace lastOrigin = presenter.getLastOrigin();
IMapPlace lastDestination = presenter.getLastDestination();
setOriginPlace(lastDestination);
setDestinationPlace(lastOrigin);
}

@Override
public void setOnlyOrigin(@NonNull IMapPlace origin) {
presenter.setLastOrigin(origin);
logger.debug("BuildRouteFragment setOnlyOrigin");
if (!placesAreTheSame(origin, presenter.getLastDestination())) {
presenter.setLastOrigin(origin);
}
}

@Override
public void setOnlyDestination(@NonNull IMapPlace destination) {
presenter.setLastDestination(destination);
logger.debug("BuildRouteFragment setOnlyDestination");
if (!placesAreTheSame(destination, presenter.getLastOrigin())) {
presenter.setLastDestination(destination);
}
}

private void setDestinationPlace(@Nullable IMapPlace place) {
if (place != null) {
if (place != null
&& (presenter.getLastDestination() == null
|| !placesAreTheSame(place, presenter.getLastOrigin()))) {
setDestinationTitle(place.getTitle());
destinationPlace = place;
presenter.setLastDestination(place);
logger.debug("Set destination as: " + place);
} else {
setDestinationTitle(context.getResources().getString(R.string.choose_destination_default));
destinationPlace = null;
presenter.setLastDestination(null);
logger.debug("Clean last destination");
}
presenter.setLastDestination(destinationPlace);
}

private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) {
return place != null && comparing != null
&& place.getLongitude() == comparing.getLongitude()
&& place.getLatitude() == comparing.getLatitude();
}

private void setDestinationTitle(@NonNull String title) {
Expand Down Expand Up @@ -232,14 +244,16 @@ public void setHistoryPlaces(@NonNull List<IMapPlace> placeList) {

@Override
public void setNewPickedPlace(@NonNull IMapPlace mapPlace) {
if (originPlace == null) {
logger.debug("BuildRouteFragment setNewPickedPlace");
if (presenter.getLastOrigin() == null && !placesAreTheSame(mapPlace, presenter.getLastDestination())) {
setOriginPlace(mapPlace);
} else if (destinationPlace == null) {
openShowRouteFragmentIfRequied();
} else if (presenter.getLastDestination() == null && !placesAreTheSame(mapPlace, presenter.getLastOrigin())) {
setDestinationPlace(mapPlace);
openShowRouteFragmentIfRequied();
} else {
logger.error("Can't use picked new place");
}
openShowRouteFragmentIfRequied();
}

@Override
Expand All @@ -255,7 +269,7 @@ public void markAsFavouritePlace(@NonNull IMapPlace mapPlace) {

private void setFavouriteState(@NonNull IMapPlace mapPlace, boolean favourite) {
List<IMapPlace> places = presenter.getHistoryPlaces();
if (places != null) {
if (places != null && !places.isEmpty()) {
int position = getPosition(places, mapPlace);

mapPlace.setFavourite(favourite);
Expand Down Expand Up @@ -286,7 +300,7 @@ private int getPosition(@NonNull List<IMapPlace> places, @NonNull IMapPlace item
}

private void openShowRouteFragmentIfRequied() {
if (originPlace != null && destinationPlace != null) {
if (presenter.getLastOrigin() != null && presenter.getLastDestination() != null) {
IRoute route = buildNewRoute();
presenter.setLastRoute(route);
presenter.setLastOrigin(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void markAsFavouritePlace(@NonNull IMapPlace mapPlace) {

private void setFavouriteState(@NonNull IMapPlace mapPlace, boolean favourite) {
List<IMapPlace> places = presenter.getHistoryPlaces();
if (places != null) {
if (places != null && !places.isEmpty()) {
int position = getPosition(places, mapPlace);

mapPlace.setFavourite(favourite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,29 @@ private void changeActiveRoute(@Nullable IMapPlace newOrigin, @Nullable IMapPlac
if (route == null) {
return;
}
if (newOrigin != null) {
if (newOrigin != null && (newDestination != null || !placesAreTheSame(newOrigin, route.getDestination()))) {
route.setOrigin(newOrigin);
}
if (newDestination != null) {
if (newDestination != null && (newOrigin != null || !placesAreTheSame(newDestination, route.getOrigin()))) {
route.setDestination(newDestination);
}

presenter.setLastRoute(route);

if (newOrigin != null) {
if (newOrigin != null && (newDestination != null || !placesAreTheSame(newOrigin, route.getDestination()))) {
setOriginTitle();
}

if (newDestination != null) {
if (newDestination != null && (newOrigin != null || !placesAreTheSame(newDestination, route.getOrigin()))) {
setDestinationTitle();
}

showRouteOnMap(route);
}

private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) {
return place != null && comparing != null
&& place.getLongitude() == comparing.getLongitude()
&& place.getLatitude() == comparing.getLatitude();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@

import javax.inject.Inject;

import gps.map.navigator.model.interfaces.Cache;
import gps.map.navigator.model.interfaces.IMapPlace;
import gps.map.navigator.model.interfaces.PlaceProxyListener;

public class DestinationChangeListener implements PlaceProxyListener {
@Inject
ISwipeRoute swipeRoute;
@Inject
Cache cache;

@Inject
DestinationChangeListener() {
}

@Override
public void onPlaceLocated(@NonNull IMapPlace mapPlace) {
swipeRoute.setOnlyDestination(mapPlace);
if (!placesAreTheSame(mapPlace, cache.getLastOrigin())) {
swipeRoute.setOnlyDestination(mapPlace);
}
}

@Override
public void onPlacesLocated(@NonNull List<IMapPlace> mapPlaces) {

}

private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) {
return place != null && comparing != null
&& place.getLongitude() == comparing.getLongitude()
&& place.getLatitude() == comparing.getLatitude();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@

import javax.inject.Inject;

import gps.map.navigator.model.interfaces.Cache;
import gps.map.navigator.model.interfaces.IMapPlace;
import gps.map.navigator.model.interfaces.PlaceProxyListener;

public class OriginChangeListener implements PlaceProxyListener {
@Inject
ISwipeRoute swipeRoute;
@Inject
Cache cache;

@Inject
OriginChangeListener() {
}

@Override
public void onPlaceLocated(@NonNull IMapPlace mapPlace) {
swipeRoute.setOnlyOrigin(mapPlace);
if (!placesAreTheSame(mapPlace, cache.getLastDestination())) {
swipeRoute.setOnlyOrigin(mapPlace);
}
}

@Override
public void onPlacesLocated(@NonNull List<IMapPlace> mapPlaces) {

}

private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) {
return place != null && comparing != null
&& place.getLongitude() == comparing.getLongitude()
&& place.getLatitude() == comparing.getLatitude();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public class AdapterFilter extends Filter {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (adapter != null) {
adapter.changePlacesList((List<IMapPlace>) results.values);
adapter.notifyDataSetChanged();
if (adapter != null && results != null && results.values instanceof List) {
List<IMapPlace> list = (List<IMapPlace>) results.values;
if (!list.isEmpty()) {
adapter.changePlacesList(list);
adapter.notifyDataSetChanged();
}
}
}

Expand All @@ -45,7 +48,7 @@ protected FilterResults performFiltering(CharSequence constraint) {
@NonNull
private List<IMapPlace> getFilteredResults(@Nullable List<IMapPlace> originalPlacesList, @NonNull String constraint) {
List<IMapPlace> results = new ArrayList<>();
if (originalPlacesList != null) {
if (originalPlacesList != null && !originalPlacesList.isEmpty()) {
for (IMapPlace item : originalPlacesList) {
if (itemMatch(item, constraint)) {
results.add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,19 @@ public void removePlace(int position, @NonNull IMapPlace place) {
@Override
public void updatePlace(@NonNull IMapPlace update) {
if (places != null && originalPlacesList != null) {
int position = getPosition(places, update);
places.set(position, update);
int originalPosition = getPosition(originalPlacesList, update);
originalPlacesList.set(originalPosition, update);
notifyItemChanged(position);
if (places.isEmpty()) {
places.add(update);
} else {
int position = getPosition(places, update);
places.set(position, update);
if (originalPlacesList.isEmpty()) {
originalPlacesList.add(update);
} else {
int originalPosition = getPosition(originalPlacesList, update);
originalPlacesList.set(originalPosition, update);
}
notifyItemChanged(position);
}
}
}

Expand All @@ -126,7 +134,7 @@ public void setInitialPlacesList(@NonNull List<IMapPlace> places) {
public void showFoundedPlacesList(@NonNull List<IMapPlace> foundedPlaces) {
if (places != null) {
places.clear();
} else {
} else {
places = new ArrayList<>();
}
places.addAll(foundedPlaces);
Expand All @@ -135,6 +143,9 @@ public void showFoundedPlacesList(@NonNull List<IMapPlace> foundedPlaces) {
}

private int getPosition(@NonNull List<IMapPlace> places, @NonNull IMapPlace item) {
if (places.isEmpty()) {
return 0;
}
for (int i = 0; i < places.size(); i++) {
if (item.getId().equals(places.get(i).getId())) {
return i;
Expand All @@ -144,6 +155,8 @@ private int getPosition(@NonNull List<IMapPlace> places, @NonNull IMapPlace item
}

private void sortByLastUsedTime(@NonNull List<IMapPlace> places) {
Collections.sort(places, new DescendingTimeComparator());
if (!places.isEmpty()) {
Collections.sort(places, new DescendingTimeComparator());
}
}
}
Loading

0 comments on commit 21f4de6

Please sign in to comment.