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.squareup.retrofit:retrofit:1.9.0'
compile 'com.github.bumptech.glide:glide:3.6.0'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
</activity>
</application>

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

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PhotosAdapter extends RecyclerView.Adapter<PhotosAdapter.ViewHolder

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.picture_list_item, parent, false);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.album_list_item, parent, false);
return new ViewHolder(view);
}

Expand Down
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;
Expand All @@ -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>
Expand All @@ -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();
Copy link
Copy Markdown

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.

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();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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("/")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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();
}



}
9 changes: 9 additions & 0 deletions app/src/main/res/layout/album_list_fragment.xml
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>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/album_list_item.xml
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>
6 changes: 4 additions & 2 deletions app/src/main/res/layout/picture_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView


<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
Expand Down