Skip to content

Commit

Permalink
Fixing repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiliano Iacopini committed May 21, 2022
1 parent a2895ac commit 62e15aa
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 44 deletions.
96 changes: 87 additions & 9 deletions app/src/main/java/com/emidev/moviesincisco/MapsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
Expand All @@ -41,6 +41,7 @@
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.Marker;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterManager;

Expand Down Expand Up @@ -79,13 +80,22 @@ public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
//false = light
private boolean mapStyle = false;

private MoviesClusterRenderer renderer;

private String path;

private boolean isDismissed = false;

PopupWindow popupWindow;

//Set up map clusterer
private void setUpClusterer() {
clusterManager = new MovieClusterManager<>(this, mMap);
clusterManager.setAnimation(false);
//Point the map's listeners at the listeners implemented by the cluster manager.
mMap.setOnCameraIdleListener(clusterManager);
clusterManager.setRenderer(new MoviesClusterRenderer(this, mMap, clusterManager));
renderer = new MoviesClusterRenderer(this, mMap, clusterManager);
clusterManager.setRenderer(renderer);

addItems();
}
Expand Down Expand Up @@ -233,7 +243,7 @@ public void run() {
@Override
public void run() {
//Get the movies from the database
movies = new ArrayList<MovieLocation>(query.getAll());
movies = new ArrayList<>(query.getAll());
runOnUiThread(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -291,13 +301,39 @@ public void onMapLoaded() {
setUpClusterer();

//ClickListener to handle the click of a marker on the map
mMap.setOnMarkerClickListener(clusterManager);
//mMap.setOnMarkerClickListener(clusterManager);

//Setting a custom adapter to show a window when a marker is clicked for every marker
clusterManager.getMarkerCollection().setInfoWindowAdapter(new MovieInfoViewAdapter(LayoutInflater.from(this), clusterManager));
clusterManager.getMarkerCollection().setInfoWindowAdapter(new MovieInfoViewAdapter(LayoutInflater.from(this), this, renderer));

//Setting the custom adapter to the map as well
mMap.setInfoWindowAdapter(clusterManager.getMarkerManager());
//mMap.setInfoWindowAdapter(clusterManager.getMarkerManager());

clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MovieLocation>() {
@Override
public boolean onClusterItemClick(MovieLocation item) {
Marker marker = renderer.getMarker(item);
new Thread(new Runnable() {
@Override
public void run() {
if (item.getPoster() == null) {
path = locator.getTMDBfile(item);
item.setPoster(path);
} else {
path = item.getPoster();
}

runOnUiThread(new Runnable() {
@Override
public void run() {
marker.showInfoWindow();
}
});
}
}).start();
return true;
}
});

//When a cluster (not a marker) is clicked, show the info window of the cluster
clusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MovieLocation>() {
Expand All @@ -306,11 +342,11 @@ public boolean onClusterClick(final Cluster<MovieLocation> cluster) {
new Thread(new Runnable() {
@Override
public void run() {
isDismissed = false;
//Declaring variables
ArrayList<MovieLocation> moviesInCluster;
LayoutInflater inflater;
View popupView;
PopupWindow popupWindow;
GridLayout grid;
//Checks if a movie is multiple times in the cluster
boolean alreadyAdded = false;
Expand All @@ -325,6 +361,14 @@ public void run() {
popupView = inflater.inflate(R.layout.cluster_info_window_layout, null);
//Create the popup window
popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, 375 * 2, true);

popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
isDismissed = true;
}
});

//Getting the grid layout from the popup window layout
grid = popupView.findViewById(R.id.cluster_info_window_layout);
//Column count to 4
Expand Down Expand Up @@ -427,6 +471,11 @@ public void run() {
//Adding rule to align the number inside the container
movieContainerParams.addRule(RelativeLayout.ALIGN_TOP, times.getId());
//Adding the number of instances to the container
if(isDismissed) {
popupWindow.dismiss();
popupWindow = null;
return;
}
movieContainer.addView(times);
multiple = false;
count = 1;
Expand All @@ -436,10 +485,25 @@ public void run() {
//Adding the container to the layout
grid.addView(movieContainer);
//Showing the popup window on the center of the screen
popupWindow.showAtLocation(new View(getApplicationContext()), Gravity.CENTER, 0, 0);
if(isDismissed){
popupWindow.dismiss();
popupWindow = null;
return;
}
if(popupWindow != null) {
popupWindow.showAtLocation(new View(getApplicationContext()), Gravity.CENTER, 0, 0);
}
}
});
}
if(isDismissed) {
if(popupWindow != null) {
popupWindow.dismiss();
popupWindow = null;
}
isDismissed = false;
return;
}
alreadyAdded = false;
}
} else {
Expand Down Expand Up @@ -489,11 +553,25 @@ public void onCameraMove() {
public void onClick(View v) {
if(!mapStyle) {
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getApplicationContext(), R.raw.dark_style));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(0);
}
RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);
rotate.setFillAfter(true);
switchMap.startAnimation(rotate);
switchMap.setColorFilter(getResources().getColor(R.color.holo_red_dark));
mapStyle = true;
}
else {
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getApplicationContext(), R.raw.light_style));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
RotateAnimation rotate = new RotateAnimation(0, -360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);
rotate.setFillAfter(true);
switchMap.startAnimation(rotate);
switchMap.setColorFilter(getResources().getColor(R.color.holo_red_light));
mapStyle = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.room.Room;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
Expand All @@ -24,45 +30,20 @@ public class MovieInfoViewAdapter implements GoogleMap.InfoWindowAdapter {
private final LayoutInflater mInflater;
private final Locator locator = new Locator();
private String path = null;
private final ClusterManager<MovieLocation> clusterManager;
private MovieLocation clickedClusterItem;
Context context;
MoviesClusterRenderer clusterRenderer;

public MovieInfoViewAdapter(LayoutInflater inflater, ClusterManager<MovieLocation> click) {
public MovieInfoViewAdapter(LayoutInflater inflater, Context context, MoviesClusterRenderer renderer) {
this.mInflater = inflater;
this.clusterManager = click;
this.context = context;
this.clusterRenderer = renderer;

clusterManager
.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MovieLocation>() {
@Override
public boolean onClusterItemClick(MovieLocation item) {
clickedClusterItem = item;
return false;
}
});
}

@Override
public View getInfoWindow(@NonNull Marker marker) {
final View popup = mInflater.inflate(R.layout.info_window_layout, null);
ImageView poster = popup.findViewById(R.id.info_window_image);
if(clickedClusterItem.getPoster() == null) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
path = locator.getTMDBfile(clickedClusterItem);
clickedClusterItem.setPoster(path);
}
});

t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
path = clickedClusterItem.getPoster();
}
MovieLocation item = clusterRenderer.getClusterItem(marker);
RequestListener<Drawable> req = new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Expand All @@ -78,6 +59,8 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
return false;
}
};
ImageView poster = popup.findViewById(R.id.info_window_image);
path = item.getPoster();
if(path != null) {
Glide.with(popup)
.load("https://image.tmdb.org/t/p/original" + path)
Expand All @@ -94,12 +77,12 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
.transition(withCrossFade())
.into(poster);
}
((TextView) popup.findViewById(R.id.title)).setText(clickedClusterItem.getTitle());
((TextView) popup.findViewById(R.id.title)).setText(item.getTitle());
return popup;
}

@Override
public View getInfoContents(@NonNull Marker marker) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;

Expand Down Expand Up @@ -40,11 +41,17 @@ private BitmapDescriptor BitmapFromVector(int vectorResId) {
return BitmapDescriptorFactory.fromBitmap(bitmap);
}

@Override
protected boolean shouldRenderAsCluster(Cluster<MovieLocation> cluster) {
//start clustering if at least 2 items overlap
return cluster.getSize() > 1;
}

@Override
protected void onBeforeClusterItemRendered(MovieLocation item, MarkerOptions markerOptions) {
markerOptions.icon(BitmapFromVector(R.drawable.ic_baseline_local_movies_24));
markerOptions.snippet(item.getSnippet());
markerOptions.title(item.getTitle());
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/cluster_info_window_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_height="wrap_content"
android:background="@drawable/background_rounded"
android:gravity="center">

Expand Down

0 comments on commit 62e15aa

Please sign in to comment.