Refactor CoordinateInputAdapter

This commit is contained in:
Alex Sytnyk 2018-06-11 16:47:55 +03:00
parent 234edd7d7c
commit b87c3a43d5
2 changed files with 32 additions and 39 deletions

View file

@ -47,6 +47,7 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import net.osmand.AndroidUtils;
import net.osmand.Location;
import net.osmand.data.LatLon;
@ -57,10 +58,7 @@ import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities.UpdateLocationViewCache;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.dashboard.DashLocationFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapmarkers.CoordinateInputBottomSheetDialogFragment.CoordinateInputFormatChangeListener;
import net.osmand.plus.mapmarkers.adapters.CoordinateInputAdapter;
@ -185,13 +183,11 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
}
private void registerMainView() {
MapActivity mapActivity = getMapActivity();
if (mapActivity == null) {
final Context ctx = getContext();
if (ctx == null) {
return;
}
final Context ctx = getContext();
if (orientationPortrait) {
View.OnClickListener backspaceOnClickListener = new View.OnClickListener() {
@Override
@ -272,7 +268,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
}
}
});
adapter = new CoordinateInputAdapter(mapActivity, mapMarkers);
adapter = new CoordinateInputAdapter(getMyApplication(), mapMarkers);
RecyclerView recyclerView = (RecyclerView) mainView.findViewById(R.id.markers_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(ctx));
recyclerView.setAdapter(adapter);
@ -1024,10 +1020,6 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
return getMyApplication().getUIUtilities().getIcon(resId, colorResId);
}
private MapActivity getMapActivity() {
return (MapActivity) getActivity();
}
private OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
}

View file

@ -1,51 +1,52 @@
package net.osmand.plus.mapmarkers.adapters;
import java.util.List;
import net.osmand.AndroidUtils;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.UiUtilities.UpdateLocationViewCache;
import net.osmand.plus.activities.MapActivity;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import net.osmand.AndroidUtils;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.UiUtilities.UpdateLocationViewCache;
import java.util.List;
public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemViewHolder> {
private MapActivity mapActivity;
private UiUtilities iconsCache;
private OsmandApplication app;
private List<MapMarker> mapMarkers;
private boolean nightTheme;
private UiUtilities uiUtilities;
private UpdateLocationViewCache updateViewCache;
public CoordinateInputAdapter(MapActivity mapActivity, List<MapMarker> mapMarkers) {
this.mapActivity = mapActivity;
iconsCache = mapActivity.getMyApplication().getUIUtilities();
updateViewCache = iconsCache.getUpdateLocationViewCache();
private boolean nightTheme;
public CoordinateInputAdapter(OsmandApplication app, List<MapMarker> mapMarkers) {
this.app = app;
this.mapMarkers = mapMarkers;
nightTheme = !mapActivity.getMyApplication().getSettings().isLightContent();
uiUtilities = app.getUIUtilities();
updateViewCache = uiUtilities.getUpdateLocationViewCache();
nightTheme = !app.getSettings().isLightContent();
}
@NonNull
@Override
public MapMarkerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public MapMarkerItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.map_marker_item_new, parent, false);
return new MapMarkerItemViewHolder(view);
}
@Override
public void onBindViewHolder(final MapMarkerItemViewHolder holder, int position) {
public void onBindViewHolder(@NonNull final MapMarkerItemViewHolder holder, int position) {
final MapMarker mapMarker = getItem(position);
holder.iconDirection.setVisibility(View.VISIBLE);
@ -79,8 +80,8 @@ public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemVi
holder.bottomShadow.setVisibility(lastItem ? View.VISIBLE : View.GONE);
holder.divider.setVisibility((!singleItem && !lastItem) ? View.VISIBLE : View.GONE);
holder.title.setText(mapMarker.getName(mapActivity));
mapActivity.getMyApplication().getUIUtilities().updateLocationView(updateViewCache,
holder.title.setText(mapMarker.getName(app));
uiUtilities.updateLocationView(updateViewCache,
holder.iconDirection, holder.distance, mapMarker.getLatitude(), mapMarker.getLongitude());
}
@ -101,18 +102,18 @@ public class CoordinateInputAdapter extends RecyclerView.Adapter<MapMarkerItemVi
if (nightTheme) {
return AndroidUtils.createPressedStateListDrawable(
getColoredIcon(R.drawable.marker_circle_background_dark_n_with_inset, R.color.keyboard_item_control_dark_bg),
ContextCompat.getDrawable(mapActivity, R.drawable.marker_circle_background_p_with_inset)
ContextCompat.getDrawable(app, R.drawable.marker_circle_background_p_with_inset)
);
}
return ContextCompat.getDrawable(mapActivity, R.drawable.marker_circle_background_light_with_inset);
return ContextCompat.getDrawable(app, R.drawable.marker_circle_background_light_with_inset);
}
private Drawable getColoredIcon(@DrawableRes int resId, @ColorRes int colorResId) {
return iconsCache.getIcon(resId, colorResId);
return uiUtilities.getIcon(resId, colorResId);
}
@ColorInt
private int getResolvedColor(@ColorRes int colorResId) {
return ContextCompat.getColor(mapActivity, colorResId);
return ContextCompat.getColor(app, colorResId);
}
}