-
Notifications
You must be signed in to change notification settings - Fork 1
Interview pieter 2h30min #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
| @@ -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(); | ||
|
|
@@ -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)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++; | ||
| } | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
| } | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.