-
Notifications
You must be signed in to change notification settings - Fork 1
Interview pieter improved #2
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,37 +1,149 @@ | ||
| package android.coding.interview.makeitawesome.fragment; | ||
|
|
||
| import android.app.ProgressDialog; | ||
| 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.content.Intent; | ||
| 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 org.apache.http.client.ClientProtocolException; | ||
| import org.apache.http.client.methods.HttpGet; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
|
|
||
| /** | ||
| * 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<>(); | ||
| private AsyncTask<Void, Void, ArrayList<ImageData>> httpGetTask; | ||
| private ProgressDialog dialog; | ||
| public static Fragment newInstance() { | ||
| return new PicturesFragment(); | ||
| } | ||
|
|
||
| @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()); | ||
|
|
||
| httpGetTask = new HttpGetTask().execute(); //start background task | ||
|
|
||
| mRecyclerView = (RecyclerView) inflater.inflate(R.layout.pictures_list_fragment, container, false); | ||
| setupRecyclerView(mRecyclerView); | ||
| return mRecyclerView; | ||
| } | ||
|
|
||
| @Override | ||
| public void onPause() { | ||
| super.onPause(); | ||
| if (dialog != null){ | ||
| dialog.dismiss(); | ||
| } | ||
| httpGetTask.cancel(true); | ||
| } | ||
|
|
||
| private void setupRecyclerView(RecyclerView recyclerView) { | ||
| mRecyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL_LIST)); | ||
| 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(""); | ||
|
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. I expected him to improve this part in the "long" test |
||
|
|
||
|
|
||
| @Override | ||
| protected void onPreExecute() { | ||
| super.onPreExecute(); | ||
| dialog = new ProgressDialog(getActivity()); | ||
| dialog.setTitle(getString(R.string.loading)); | ||
| dialog.setCancelable(false); | ||
| dialog.show(); | ||
| } | ||
|
|
||
| @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"); | ||
| if (!isCancelled()){ | ||
| dialog.dismiss(); | ||
|
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. Dialog should be dimissed always. No much to add to the improved version. He only included the progressdialog, basically. |
||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@DeCafeina what is the possible implications of having non-static (i.e. inner local)
ViewHolderclass ?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.
:(
I don't know. But I'd say he removes the static because it disturbs him in order to access an outer class element (the clickListener). That's not so good...