-
Notifications
You must be signed in to change notification settings - Fork 1
test of antonio #4
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 |
|---|---|---|
|
|
@@ -18,4 +18,6 @@ | |
| </activity> | ||
| </application> | ||
|
|
||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
|
|
||
| </manifest> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| package android.coding.interview.makeitawesome.fragment; | ||
|
|
||
| import android.app.Activity; | ||
| import android.coding.interview.makeitawesome.R; | ||
| import android.coding.interview.makeitawesome.adapter.PhotosAdapter; | ||
| import android.coding.interview.makeitawesome.model.Album; | ||
| import android.coding.interview.makeitawesome.networking.RestClient; | ||
| import android.content.Context; | ||
| import android.graphics.Bitmap; | ||
| import android.os.AsyncTask; | ||
| import android.os.Bundle; | ||
| import android.support.annotation.Nullable; | ||
| import android.support.v4.app.Fragment; | ||
|
|
@@ -10,6 +16,20 @@ | |
| import android.view.LayoutInflater; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.widget.ArrayAdapter; | ||
| import android.widget.ImageView; | ||
| import android.widget.ListView; | ||
| import android.widget.TextView; | ||
|
|
||
| import com.bumptech.glide.Glide; | ||
| import com.bumptech.glide.request.animation.GlideAnimation; | ||
| import com.bumptech.glide.request.target.SimpleTarget; | ||
|
|
||
| import java.net.URL; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import retrofit.RestAdapter; | ||
|
|
||
| /** | ||
| * This is Your TASK:<br> | ||
|
|
@@ -18,20 +38,78 @@ | |
| */ | ||
| public class PicturesFragment extends Fragment { | ||
|
|
||
| ListView rv; | ||
| 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); | ||
| /* | ||
| RecyclerView rv = (RecyclerView) inflater.inflate(R.layout.album_list_fragment, container, false); | ||
| setupRecyclerView(rv); | ||
| */ | ||
| rv = (ListView) inflater.inflate(R.layout.album_list_fragment, container, false); | ||
| DownloadFilesTask d = new DownloadFilesTask(); | ||
| d.execute(); | ||
| return rv; | ||
| } | ||
|
|
||
| private void drawList(){ | ||
|
|
||
| } | ||
| private void setupRecyclerView(RecyclerView recyclerView) { | ||
| recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext())); | ||
| recyclerView.setAdapter(new PhotosAdapter()); | ||
| } | ||
|
|
||
| private class DownloadFilesTask extends AsyncTask<URL, Integer, List<Album>> { | ||
| protected List<Album> doInBackground(URL... urls) { | ||
| RestClient restClient = RestClient.getInstance(); | ||
| restClient.getPhotos(); | ||
| List<Album> l = restClient.getPhotos(); | ||
|
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've been a while wondering wtf was that "one" 1. I've just realized it is a lowercase L. |
||
| return l; | ||
| } | ||
|
|
||
| protected void onProgressUpdate(Integer... progress) { | ||
|
|
||
| } | ||
|
|
||
| protected void onPostExecute(List<Album> l) { | ||
| // update the adapter | ||
| rv.setAdapter(new AlbumAdapter(getActivity(),l)); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| class AlbumAdapter extends ArrayAdapter<Album> { | ||
|
|
||
| Activity activity; | ||
| List<Album> data; | ||
|
|
||
| AlbumAdapter(Activity activity, List<Album> data) { | ||
| super(activity.getApplicationContext(), R.layout.album_list_item, data); | ||
| this.activity = activity; | ||
| this.data = data; | ||
| } | ||
|
|
||
| public View getView(int position, View convertView, ViewGroup parent) { | ||
| LayoutInflater inflater = LayoutInflater.from(activity); | ||
|
|
||
| View item = inflater.inflate(R.layout.album_list_item, null); | ||
|
|
||
| TextView txt = (TextView) item.findViewById(R.id.name); | ||
| txt.setText(data.get(position).getTitle()); | ||
|
|
||
| final ImageView img = (ImageView) item.findViewById(R.id.album); | ||
| Glide.with(getActivity()).load(data.get(position).getThumbnailUrl()).asBitmap().into(new SimpleTarget<Bitmap>(100, 100) { | ||
| @Override | ||
| public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { | ||
| img.setImageBitmap(resource); | ||
| } | ||
| }); | ||
| return (item); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package android.coding.interview.makeitawesome.model; | ||
|
|
||
| /** | ||
| * Created by antonio on 24/07/15. | ||
| */ | ||
| public class Album { | ||
|
|
||
| private int albumId; | ||
| private int id; | ||
| private String title; | ||
| private String url; | ||
| private String thumbnailUrl; | ||
|
|
||
| public Album() { | ||
| this.albumId = -1; | ||
| this.id = -1; | ||
| this.title = ""; | ||
| this.url = ""; | ||
| this.thumbnailUrl = ""; | ||
| } | ||
|
|
||
| public Album(int albumId, int id, String title, String url, String thumbnailUrl) { | ||
| this.albumId = albumId; | ||
| this.id = id; | ||
| this.title = title; | ||
| this.url = url; | ||
| this.thumbnailUrl = thumbnailUrl; | ||
| } | ||
|
|
||
| public int getAlbumId() { | ||
| return albumId; | ||
| } | ||
|
|
||
| public void setAlbumId(int albumId) { | ||
| this.albumId = albumId; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(int id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package android.coding.interview.makeitawesome.networking; | ||
|
|
||
| import android.coding.interview.makeitawesome.model.Album; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import retrofit.http.GET; | ||
|
|
||
| /** | ||
| * Created by antonio on 24/07/15. | ||
| */ | ||
| public interface ApiPhoto { | ||
|
|
||
| @GET("/") | ||
|
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. It should be @get("/photo"), and the URL = "http://jsonplaceholder.typicode.com/ It's not super important, but it's something you won't very likely do like he did if you are used to work with REST APIs. |
||
| List<Album> listPhotos(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package android.coding.interview.makeitawesome.networking; | ||
|
|
||
| import android.coding.interview.makeitawesome.model.Album; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import retrofit.RestAdapter; | ||
| import retrofit.android.AndroidLog; | ||
| import retrofit.http.GET; | ||
|
|
||
| /** | ||
| * Created by antonio on 24/07/15. | ||
| */ | ||
| public class RestClient { | ||
|
|
||
| private ApiPhoto api; | ||
|
|
||
|
|
||
| public static RestClient restClient= null; | ||
| private static final String URL = "http://jsonplaceholder.typicode.com/photos/"; | ||
|
|
||
| protected RestClient(){ | ||
| RestAdapter restAdapter = new RestAdapter.Builder() | ||
| .setEndpoint(URL) | ||
| .setLogLevel(RestAdapter.LogLevel.FULL) | ||
| .setLog(new AndroidLog("RETROFIT")) | ||
| .build(); | ||
| api = restAdapter.create(ApiPhoto.class); | ||
| } | ||
|
|
||
| public static synchronized RestClient getInstance(){ | ||
| if(restClient==null){ | ||
| restClient = new RestClient(); | ||
| } | ||
| return restClient; | ||
| } | ||
|
|
||
| public List<Album> getPhotos(){ | ||
| return api.listPhotos(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <ListView xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | ||
| android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" | ||
| android:paddingRight="@dimen/activity_horizontal_margin" | ||
| android:paddingTop="@dimen/activity_vertical_margin" | ||
| android:paddingBottom="@dimen/activity_vertical_margin" | ||
| tools:context=".ListActivity$PlaceholderFragment"> | ||
|
|
||
| </ListView> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:orientation="vertical" android:layout_width="match_parent" | ||
| android:layout_height="match_parent"> | ||
|
|
||
| <ImageView | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:id="@+id/album" | ||
| android:src="@android:drawable/ic_menu_gallery"/> | ||
|
|
||
| <TextView | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:textAppearance="?android:attr/textAppearanceMedium" | ||
| android:text="Medium Text" | ||
| android:id="@+id/name" | ||
| android:layout_gravity="center_horizontal"/> | ||
| </LinearLayout> |
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.
For me, this is by far the worst part. He solves everything with an async task called during onCreateView, what is a quite bad idea. What about Android lifecycle, no clear layers... he just puts everything together. It probably works, but if the problem would have been slightly bigger it would probably grow into a big mess.