Skip to content
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

Added the Albums functionality #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 26
buildToolsVersion "25.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.codingblocks.restapiretrofitjson"
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -27,12 +27,13 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support:recyclerview-v7:26.+'
compile 'com.android.support:cardview-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.squareup.picasso:picasso:2.5.2'
testCompile 'junit:junit:4.12'
}
repositories {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.TodosActivity"></activity>
<activity android:name=".activities.TodosActivity" />
<activity android:name=".activities.AlbumActivity" />
<activity android:name=".activities.PhotosActivity" />
<activity android:name=".activities.LargePhotoActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.codingblocks.restapiretrofitjson
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.codingblocks.restapiretrofitjson.activities.AlbumActivity
import com.codingblocks.restapiretrofitjson.activities.PostsActivity
import com.codingblocks.restapiretrofitjson.activities.TodosActivity
import com.codingblocks.restapiretrofitjson.activities.UsersActivity
Expand All @@ -26,7 +27,7 @@ class MainActivity : AppCompatActivity() {
btnTodos.setOnClickListener({
startActivity(Intent(this@MainActivity, TodosActivity::class.java))
})

btnAlbums.setOnClickListener { startActivity(Intent(this@MainActivity , AlbumActivity::class.java)) }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.codingblocks.restapiretrofitjson.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.codingblocks.restapiretrofitjson.R;
import com.codingblocks.restapiretrofitjson.adapters.AlbumAdapter;
import com.codingblocks.restapiretrofitjson.api.API;
import com.codingblocks.restapiretrofitjson.api.AlbumsAPI;
import com.codingblocks.restapiretrofitjson.interfaces.OnItemClickListener;
import com.codingblocks.restapiretrofitjson.models.Album;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class AlbumActivity extends AppCompatActivity {

RecyclerView rvAlbumList ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_album);
rvAlbumList = (RecyclerView)findViewById(R.id.rvAlbumList);
rvAlbumList.setLayoutManager(new LinearLayoutManager(this));
final AlbumAdapter adapter = new AlbumAdapter(new ArrayList<Album>() , this) ;
rvAlbumList.setAdapter(adapter);
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int itemId, View view) {
Intent i = new Intent(AlbumActivity.this ,PhotosActivity.class) ;
i.putExtra("id" , itemId) ;
startActivity(i);
}
});


API.getInstance().getAlbumsAPI().getAlbums().enqueue(new Callback<ArrayList<Album>>() {
@Override
public void onResponse(Call<ArrayList<Album>> call, Response<ArrayList<Album>> response) {
adapter.updateAlbumList(response.body());
}

@Override
public void onFailure(Call<ArrayList<Album>> call, Throwable t) {

}
}); ;


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.codingblocks.restapiretrofitjson.activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.codingblocks.restapiretrofitjson.R;
import com.squareup.picasso.Picasso;

public class LargePhotoActivity extends AppCompatActivity {

ImageView ivLargePhoto ;
TextView tvTitle ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_large_photo);
ivLargePhoto = (ImageView)findViewById(R.id.ivPhoto) ;
tvTitle = (TextView)findViewById(R.id.tvPhotoLTitle) ;
String title = getIntent().getStringExtra("title") ;
String url = getIntent().getStringExtra("url") ;
Picasso.with(this).load(url).into(ivLargePhoto);
tvTitle.setText(title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.codingblocks.restapiretrofitjson.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.codingblocks.restapiretrofitjson.R;
import com.codingblocks.restapiretrofitjson.adapters.PhotosAdapter;
import com.codingblocks.restapiretrofitjson.api.API;
import com.codingblocks.restapiretrofitjson.models.Photo;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PhotosActivity extends AppCompatActivity {

RecyclerView rvPhotos ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);
rvPhotos = (RecyclerView)findViewById(R.id.rvPhotoList);
rvPhotos.setLayoutManager(new LinearLayoutManager(this));
final PhotosAdapter adapter = new PhotosAdapter(new ArrayList<Photo>() ,this) ;
rvPhotos.setAdapter(adapter);

adapter.setOpcl(new PhotosAdapter.OnPhotoClickListener() {
@Override
public void onClick(String url, String title) {
Intent i = new Intent(PhotosActivity.this ,LargePhotoActivity.class) ;
i.putExtra("url" , url) ;
i.putExtra("title" , title) ;
startActivity(i);
}
});

int id = getIntent().getIntExtra("id" , 0 ) ;
API.getInstance().getAlbumsAPI().getPhotoOfAlbum(id).enqueue(new Callback<ArrayList<Photo>>() {
@Override
public void onResponse(Call<ArrayList<Photo>> call, Response<ArrayList<Photo>> response) {
adapter.updatePhotoList(response.body());
}

@Override
public void onFailure(Call<ArrayList<Photo>> call, Throwable t) {

}
});


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.codingblocks.restapiretrofitjson.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.codingblocks.restapiretrofitjson.R;
import com.codingblocks.restapiretrofitjson.interfaces.OnItemClickListener;
import com.codingblocks.restapiretrofitjson.models.Album;

import java.util.ArrayList;

/**
* Created by anirudh on 03/07/17.
*/

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.AlbumViewHolder> {

ArrayList<Album> albumArrayList ;
Context context ;

OnItemClickListener oacl ;

public void setOnItemClickListener(OnItemClickListener oacl) {
this.oacl = oacl;
}

public void updateAlbumList(ArrayList<Album> albums){
this.albumArrayList =albums ;
notifyDataSetChanged();
}


public AlbumAdapter(ArrayList<Album> albumArrayList, Context context) {
this.albumArrayList = albumArrayList;
this.context = context;
}

@Override
public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = li.inflate(R.layout.list_item_album ,parent ,false) ;

return new AlbumViewHolder(itemView);
}

@Override
public void onBindViewHolder(final AlbumViewHolder holder, final int position) {
final Album thisAlbum = albumArrayList.get(position) ;
holder.tvAlbumTitle.setText(thisAlbum.getTitle());
holder.thisView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(oacl !=null)
oacl.onItemClick(thisAlbum.getId() , holder.thisView);
}
});
}

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

public class AlbumViewHolder extends RecyclerView.ViewHolder{
TextView tvAlbumTitle ;
View thisView ;

public AlbumViewHolder(View itemView) {
super(itemView);
tvAlbumTitle = (TextView) itemView.findViewById(R.id.tvAlbumTitle);
thisView = itemView ;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.codingblocks.restapiretrofitjson.adapters;

import android.content.Context;
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.codingblocks.restapiretrofitjson.R;
import com.codingblocks.restapiretrofitjson.models.Photo;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
* Created by anirudh on 03/07/17.
*/

public class PhotosAdapter extends RecyclerView.Adapter<PhotosAdapter.PhotoViewHolder> {

private ArrayList<Photo> photoArrayList ;
private Context context ;
private OnPhotoClickListener opcl ;

public void setOpcl(OnPhotoClickListener opcl) {
this.opcl = opcl;
}

public PhotosAdapter(ArrayList<Photo> photoArrayList, Context context) {
this.photoArrayList = photoArrayList;
this.context = context;
}
public interface OnPhotoClickListener{
public void onClick(String url ,String title) ;
}
public void updatePhotoList(ArrayList<Photo> photos){
this.photoArrayList = photos ;
notifyDataSetChanged();
}

@Override
public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = li.inflate(R.layout.list_item_photos ,parent ,false) ;
return new PhotoViewHolder(itemView);
}

@Override
public void onBindViewHolder(PhotoViewHolder holder, int position) {
final Photo thisPhoto = photoArrayList.get(position) ;
Picasso.with(context).load(thisPhoto.getThumbnailUrl()).into(holder.ivThumbnail);
holder.tvPhotoTitle.setText(thisPhoto.getTitle());
holder.thisView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
opcl.onClick(thisPhoto.getUrl() ,thisPhoto.getTitle());
}
});
}

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

public class PhotoViewHolder extends RecyclerView.ViewHolder{
ImageView ivThumbnail ;
TextView tvPhotoTitle ;
View thisView ;
public PhotoViewHolder(View itemView) {
super(itemView);
ivThumbnail = (ImageView)itemView.findViewById(R.id.ivThumbnail) ;
tvPhotoTitle =(TextView)itemView.findViewById(R.id.tvPhotoTitle) ;
thisView = itemView ;
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class TodoAdapter (

fun bindTodo(todo: Todo) {
itemView.tvTodoTitle.text = todo.title;
itemView.checkBoxTodo.setChecked(todo.completed)
}
}
}
Loading