Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.github.bumptech.glide:glide:3.6.0'
compile 'com.android.support:support-v4:22.2.0'
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.coding.interview.makeitawesome" >

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -16,6 +18,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Picture"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package android.coding.interview.makeitawesome;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

/**
* Created by pinghelram on 27/07/15.
*/
public class Picture extends Activity {
ImageView myImage;
public static final String IMAGEURL = "image_url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
myImage = (ImageView) findViewById(R.id.iv_image);

Intent myIntent = getIntent();
String url = myIntent.getStringExtra(IMAGEURL);

Glide.with(this).load(url).into(myImage);
}




}
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
package android.coding.interview.makeitawesome.adapter;

import android.coding.interview.makeitawesome.R;
import android.coding.interview.makeitawesome.data.ImageData;
import android.content.Context;
import android.media.Image;
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.bumptech.glide.Glide;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* adapter keeping data for list of photos
*/
public class PhotosAdapter extends RecyclerView.Adapter<PhotosAdapter.ViewHolder> {

// TODO this is a dummy data that you have to replace. You probably need List of some objects representing pictures
// rather than just strings
private List<String> mData = Arrays.asList("dummy 1", "dummy 2", "dummy 3", "dummy 4", "etc");
//private List<String> mData = Arrays.asList("dummy 1", "dummy 2", "dummy 3", "dummy 4", "etc");
private List<ImageData> mData;
private Context context;
private ClickListener clickListener;

public PhotosAdapter(ArrayList<ImageData> mData, Context context){
this.mData = mData;
this.context = context;
}

public void setClickListener(ClickListener clickListener){
this.clickListener = clickListener;
}

public void insertmData(ImageData mImage, int position){
mData.add(position, mImage);
notifyItemInserted(position);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
Expand All @@ -27,19 +53,45 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {

@Override
public void onBindViewHolder(ViewHolder holder, int i) {
holder.mText.setText(mData.get(i));
//holder.mText.setText(mData.get(i));
holder.getImageTitle().setText(mData.get(i).getTitle());
//holder.getTv_name_search().setImageResource(R.drawable.abc_ab_share_pack_mtrl_alpha);
Glide.with(context).load(mData.get(i).getThumbnailUrl()).into(holder.getIv_image());
}

@Override
public int getItemCount() {
return mData.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
final TextView mText;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final TextView mTitle;
private final ImageView mImageView;

public ViewHolder(View itemView) {
super(itemView);
mText = (TextView) itemView;
mTitle = (TextView) itemView.findViewById(R.id.tv_image_title);
mImageView = (ImageView) itemView.findViewById(R.id.iv_image);
itemView.setOnClickListener(this);
}

public TextView getImageTitle() {
return mTitle;
}

public ImageView getIv_image() {
return mImageView;
}

@Override
public void onClick(View v) {
if (clickListener!= null){
clickListener.itemClicked(v, getAdapterPosition());
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, specially taking into account that this was the "fast" tests

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, no, no. I misunderstood what he was doing. :(

I thought he created a new interface to notify "events", but he is accesing a clickListener in the Adapter, totally coupling both classes. It's not so nice.

}
}

public interface ClickListener {
void itemClicked(View view, int position);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package android.coding.interview.makeitawesome.data;

/**
* Created by pinghelram on 27/07/15.
*/
public class ImageData {
String title, url, thumbnailUrl;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getThumbnailUrl() {
return thumbnailUrl;
}

public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
package android.coding.interview.makeitawesome.fragment;

import android.coding.interview.makeitawesome.Picture;
import android.coding.interview.makeitawesome.R;
import android.coding.interview.makeitawesome.adapter.PhotosAdapter;
import android.coding.interview.makeitawesome.data.ImageData;
import android.coding.interview.makeitawesome.utils.DividerItemDecoration;
import android.coding.interview.makeitawesome.utils.JSONResponseHandler;
import android.coding.interview.makeitawesome.utils.QueryResult;
import android.content.Intent;
import android.location.Location;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
* This is Your TASK:<br>
* This is a fragment where you need to show list of pictures and details fetched from API<br>
* Most of skeleton for showing UI is implemented. You need to take care of getting the data from server, updating adapter and displaying results
*/
public class PicturesFragment extends Fragment {
public class PicturesFragment extends Fragment implements PhotosAdapter.ClickListener{
private static final String TAG = "PicturesFragment";
private final static String URL = "http://jsonplaceholder.typicode.com/photos/";
private PhotosAdapter mPhotosAdapter;
protected RecyclerView mRecyclerView;
private ArrayList<ImageData> mData = new ArrayList<>();

public static Fragment newInstance() {
return new PicturesFragment();
Expand All @@ -25,13 +48,87 @@ public static Fragment newInstance() {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(R.layout.pictures_list_fragment, container, false);
setupRecyclerView(rv);
return rv;
mPhotosAdapter = new PhotosAdapter(mData, getActivity());

//todo if there's no results show it
//tv_no_results = (TextView) findViewById(R.id.tv_no_results);

new HttpGetTask().execute(); //start background task


mRecyclerView = (RecyclerView) inflater.inflate(R.layout.pictures_list_fragment, container, false);
setupRecyclerView(mRecyclerView);
return mRecyclerView;
}

private void setupRecyclerView(RecyclerView recyclerView) {
mRecyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL_LIST));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He took the time to search and reuse this DividerItemDecoration. Good point, I guess.

mRecyclerView.setAdapter(mPhotosAdapter);
mPhotosAdapter.setClickListener(this);
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new PhotosAdapter());
}

@Override
public void itemClicked(View view, int position) {
Intent intent = new Intent(getActivity(), Picture.class);
String url = mData.get(position).getUrl();

intent.putExtra(Picture.IMAGEURL, url);
startActivity(intent);
}

/**
* download the data
*/
private class HttpGetTask extends AsyncTask<Void, Void, ArrayList<ImageData>> {
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");


@Override
protected ArrayList<ImageData> doInBackground(Void... unused) {
Log.v(TAG, "doInBackground");

HttpGet request = new HttpGet(URL);
JSONResponseHandler searchresponseHandler = new JSONResponseHandler();

try {
return mClient.execute(request, searchresponseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
} catch (IOException ioexception) {
ioexception.printStackTrace();
} finally {
if (mClient != null) mClient.close();
}
return null;
}

@Override
protected void onPostExecute(ArrayList<ImageData> result) {
Log.v(TAG, "onPostExecute");
ArrayList<ImageData> searchResults = new ArrayList<>();

if (mClient != null && result != null) {
Log.v(TAG, "search complete");
/*for (Map.Entry<String, ImageData> mLibraryItem : result.getphotos().entrySet()) {
searchResults.add(mLibraryItem.getValue());
}*/
//for (ImageData data : )
for (ImageData mData : result){
searchResults.add(mData);
}
}
if ((result == null || result.size() == 0)) {
//todo: if there's no results show no results found
//tv_no_results.setVisibility(View.VISIBLE);
} else {
//tv_no_results.setVisibility(View.GONE);
int counter = 0;
for (ImageData imageData: searchResults) {
mPhotosAdapter.insertmData(imageData, counter);
counter++;
}
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asynctask and not using any cool network library makes me think he is not super expert in Android nor in Java. But he doesn't code like a newbie.

}
}
Loading