Permalink
Browse files

Show the User Images

  • Loading branch information...
dragosholban committed Mar 18, 2018
1 parent 2ab2d0e commit 591d14a9fe58d86d485a9c8eb90d616466e320d8
@@ -27,4 +27,6 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:recyclerview-v7:26.1.+'
implementation 'com.squareup.picasso:picasso:2.71828'
}
@@ -0,0 +1,171 @@
package dragosholban.com.bestphotos;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class ImageRecyclerViewAdapter extends RecyclerView.Adapter {
private static final int TYPE_SQUARE = 0;
private static final int TYPE_VERTICAL = 1;
public static class SquareImageViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView likesTextView;
public SquareImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.gridImageview);
likesTextView = itemView.findViewById(R.id.likesCount);
}
}
public static class VerticalImagesViewHolder extends RecyclerView.ViewHolder {
ImageView imageView1;
ImageView imageView2;
TextView likesTextView1;
TextView likesTextView2;
public VerticalImagesViewHolder(View itemView) {
super(itemView);
imageView1 = itemView.findViewById(R.id.gridImageview1);
likesTextView1 = itemView.findViewById(R.id.likesCount1);
imageView2 = itemView.findViewById(R.id.gridImageview2);
likesTextView2 = itemView.findViewById(R.id.likesCount2);
}
}
private static class MyImage {
FacebookImage image;
int span = 1;
public MyImage(FacebookImage image, int span) {
this.image = image;
this.span = span;
}
}
private Context mContext;
private ArrayList<ArrayList<MyImage>> images = new ArrayList<>();
public ImageRecyclerViewAdapter(Context context, ArrayList<FacebookImage> images) {
this.mContext = context;
distributeImages(images);
}
private void distributeImages(ArrayList<FacebookImage> images) {
ArrayList<MyImage> arrayOfImages = new ArrayList<>();
int i = 0;
int rightPos = 0;
boolean left = true;
int span;
for (FacebookImage image : images) {
i++;
span = 1;
if (left) {
if (i % 6 == 1) {
span = 2;
rightPos = i + 8;
}
arrayOfImages.add(new MyImage(image, span));
if (i % 6 == 2) {
left = false;
continue;
}
} else {
if (i % rightPos == 0) {
span = 2;
left = true;
}
arrayOfImages.add(new MyImage(image, span));
if (i % rightPos == rightPos - 2) {
continue;
}
}
this.images.add(arrayOfImages);
arrayOfImages = new ArrayList<>();
}
if(arrayOfImages.size() > 0) {
this.images.add(arrayOfImages);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
// Create a custom SpanSizeLookup where the first item spans both columns
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return images.get(position).get(0).span;
}
});
recyclerView.setLayoutManager(layoutManager);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_VERTICAL) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.vertical_square_images, parent, false);
VerticalImagesViewHolder holder = new VerticalImagesViewHolder(view);
return holder;
} else {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.square_image, parent, false);
SquareImageViewHolder holder = new SquareImageViewHolder(view);
return holder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ArrayList<MyImage> imgs = images.get(position);
if (imgs.size() > 1) {
VerticalImagesViewHolder myHolder = (VerticalImagesViewHolder) holder;
Picasso.get().load(imgs.get(0).image.url).into(myHolder.imageView1);
myHolder.likesTextView1.setText(String.valueOf(imgs.get(0).image.reactions));
myHolder.imageView1.setTag(imgs.get(0).image.link);
Picasso.get().load(imgs.get(1).image.url).into(myHolder.imageView2);
myHolder.likesTextView2.setText(String.valueOf(imgs.get(1).image.reactions));
myHolder.imageView2.setTag(imgs.get(1).image.link);
} else {
SquareImageViewHolder myHolder = (SquareImageViewHolder) holder;
Picasso.get().load(imgs.get(0).image.url).into(myHolder.imageView);
myHolder.likesTextView.setText(String.valueOf(imgs.get(0).image.reactions));
myHolder.imageView.setTag(imgs.get(0).image.link);
}
}
@Override
public int getItemViewType(int position) {
if (images.get(position).size() > 1) {
return TYPE_VERTICAL;
}
return TYPE_SQUARE;
}
@Override
public int getItemCount() {
return images.size();
}
}
@@ -1,8 +1,12 @@
package dragosholban.com.bestphotos;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.facebook.AccessToken;
import com.facebook.GraphRequest;
@@ -21,12 +25,16 @@
private static final String TAG = ImagesActivity.class.getName();
private ArrayList<FacebookImage> images = new ArrayList<>();
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
recyclerView = this.findViewById(R.id.recyclerView);
final ImagesActivity activity = this;
GraphRequest.Callback callback = new GraphRequest.Callback() {
@Override
@@ -81,11 +89,23 @@ public void onCompleted(GraphResponse response) {
if (fbPhotos.paging != null && fbPhotos.paging.cursors.after != null) {
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500&after=" + fbPhotos.paging.cursors.after, null, HttpMethod.GET, this);
request.executeAsync();
} else {
recyclerView.setAdapter(new ImageRecyclerViewAdapter(activity, images));
}
}
};
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500", null, HttpMethod.GET, callback);
request.executeAsync();
}
public void onImageClick(View view) {
String link = (String) view.getTag();
if (link != null) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
}
@@ -6,4 +6,11 @@
android:layout_height="match_parent"
tools:context="dragosholban.com.bestphotos.ImagesActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true" />
</android.support.constraint.ConstraintLayout>
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<item>
<shape android:shape="rectangle">
<solid android:color="#3b5998"/>
<corners android:radius="20dp" />
<padding
android:top="2dp"
android:bottom="2dp"
android:left="4dp"
android:right="4dp" />
</shape>
</item>
</selector>
@@ -0,0 +1,46 @@
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintDimensionRatio="H,1:1"
android:gravity="center"
android:layout_margin="1dp"
>
<ImageView
android:id="@+id/gridImageview"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="onImageClick"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/likesCount"
android:background="@layout/rounded_border_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:text="0"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="normal"
android:textAlignment="center"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:shadowColor="#000000"
app:layout_constraintBottom_toBottomOf="@id/gridImageview"
app:layout_constraintEnd_toEndOf="@id/gridImageview"
/>
</android.support.constraint.ConstraintLayout>
@@ -0,0 +1,82 @@
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="1dp"
>
<ImageView
android:id="@+id/gridImageview1"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="onImageClick"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/likesCount1"
android:background="@layout/rounded_border_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:text="0"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="normal"
android:textAlignment="center"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:shadowColor="#000000"
app:layout_constraintBottom_toBottomOf="@id/gridImageview1"
app:layout_constraintEnd_toEndOf="@id/gridImageview1"
/>
<ImageView
android:id="@+id/gridImageview2"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="onImageClick"
android:scaleType="centerCrop"
android:layout_marginTop="2dp"
app:layout_constraintTop_toBottomOf="@id/gridImageview1"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/likesCount2"
android:background="@layout/rounded_border_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:text="0"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="normal"
android:textAlignment="center"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:shadowColor="#000000"
app:layout_constraintBottom_toBottomOf="@id/gridImageview2"
app:layout_constraintEnd_toEndOf="@id/gridImageview2"
/>
</android.support.constraint.ConstraintLayout>

0 comments on commit 591d14a

Please sign in to comment.