From 62e15aaf6bf9bbac6e943f9157d0f9147a6a386d Mon Sep 17 00:00:00 2001 From: Emiliano Iacopini Date: Sat, 21 May 2022 20:25:44 +0200 Subject: [PATCH] Fixing repo --- .../emidev/moviesincisco/MapsActivity.java | 96 +++++++++++++++++-- .../moviesincisco/MovieInfoViewAdapter.java | 49 ++++------ .../moviesincisco/MoviesClusterRenderer.java | 9 +- .../res/layout/cluster_info_window_layout.xml | 2 +- 4 files changed, 112 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/emidev/moviesincisco/MapsActivity.java b/app/src/main/java/com/emidev/moviesincisco/MapsActivity.java index 8c61b18..950380d 100644 --- a/app/src/main/java/com/emidev/moviesincisco/MapsActivity.java +++ b/app/src/main/java/com/emidev/moviesincisco/MapsActivity.java @@ -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; @@ -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; @@ -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(); } @@ -233,7 +243,7 @@ public void run() { @Override public void run() { //Get the movies from the database - movies = new ArrayList(query.getAll()); + movies = new ArrayList<>(query.getAll()); runOnUiThread(new Runnable() { @Override public void run() { @@ -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() { + @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() { @@ -306,11 +342,11 @@ public boolean onClusterClick(final Cluster cluster) { new Thread(new Runnable() { @Override public void run() { + isDismissed = false; //Declaring variables ArrayList moviesInCluster; LayoutInflater inflater; View popupView; - PopupWindow popupWindow; GridLayout grid; //Checks if a movie is multiple times in the cluster boolean alreadyAdded = false; @@ -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 @@ -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; @@ -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 { @@ -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; } diff --git a/app/src/main/java/com/emidev/moviesincisco/MovieInfoViewAdapter.java b/app/src/main/java/com/emidev/moviesincisco/MovieInfoViewAdapter.java index 28b7d92..0356240 100644 --- a/app/src/main/java/com/emidev/moviesincisco/MovieInfoViewAdapter.java +++ b/app/src/main/java/com/emidev/moviesincisco/MovieInfoViewAdapter.java @@ -2,7 +2,11 @@ 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; @@ -10,6 +14,8 @@ 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; @@ -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 clusterManager; - private MovieLocation clickedClusterItem; + Context context; + MoviesClusterRenderer clusterRenderer; - public MovieInfoViewAdapter(LayoutInflater inflater, ClusterManager 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() { - @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 req = new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -78,6 +59,8 @@ public boolean onResourceReady(Drawable resource, Object model, Target 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) @@ -94,7 +77,7 @@ public boolean onResourceReady(Drawable resource, Object model, Target .transition(withCrossFade()) .into(poster); } - ((TextView) popup.findViewById(R.id.title)).setText(clickedClusterItem.getTitle()); + ((TextView) popup.findViewById(R.id.title)).setText(item.getTitle()); return popup; } @@ -102,4 +85,4 @@ public boolean onResourceReady(Drawable resource, Object model, Target public View getInfoContents(@NonNull Marker marker) { return null; } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/emidev/moviesincisco/MoviesClusterRenderer.java b/app/src/main/java/com/emidev/moviesincisco/MoviesClusterRenderer.java index 85088d5..8ca2eba 100644 --- a/app/src/main/java/com/emidev/moviesincisco/MoviesClusterRenderer.java +++ b/app/src/main/java/com/emidev/moviesincisco/MoviesClusterRenderer.java @@ -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; @@ -40,6 +41,12 @@ private BitmapDescriptor BitmapFromVector(int vectorResId) { return BitmapDescriptorFactory.fromBitmap(bitmap); } + @Override + protected boolean shouldRenderAsCluster(Cluster 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)); @@ -47,4 +54,4 @@ protected void onBeforeClusterItemRendered(MovieLocation item, MarkerOptions mar markerOptions.title(item.getTitle()); super.onBeforeClusterItemRendered(item, markerOptions); } -} +} \ No newline at end of file diff --git a/app/src/main/res/layout/cluster_info_window_layout.xml b/app/src/main/res/layout/cluster_info_window_layout.xml index 1fe449f..5f6702d 100644 --- a/app/src/main/res/layout/cluster_info_window_layout.xml +++ b/app/src/main/res/layout/cluster_info_window_layout.xml @@ -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">