Skip to content

Commit 7fb1060

Browse files
committed
Initial Commit with working NYTimes API request and populating the adapter
1 parent 00abde5 commit 7fb1060

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1009
-0
lines changed

app/build.gradle

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 29
5+
buildToolsVersion "29.0.3"
6+
7+
defaultConfig {
8+
applicationId "com.codepath.uiandgitlab"
9+
minSdkVersion 19
10+
targetSdkVersion 29
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15+
}
16+
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
24+
}
25+
26+
dependencies {
27+
implementation fileTree(dir: 'libs', include: ['*.jar'])
28+
29+
implementation 'androidx.appcompat:appcompat:1.1.0'
30+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
31+
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
32+
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
33+
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
34+
implementation 'androidx.recyclerview:recyclerview:1.1.0'
35+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.codepath.bestsellerlistapp">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity android:name="com.codepath.bestsellerlistapp.MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.codepath.bestsellerlistapp;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
6+
import androidx.core.widget.ContentLoadingProgressBar;
7+
import androidx.fragment.app.Fragment;
8+
import androidx.recyclerview.widget.GridLayoutManager;
9+
import androidx.recyclerview.widget.LinearLayoutManager;
10+
import androidx.recyclerview.widget.RecyclerView;
11+
12+
import android.util.Log;
13+
import android.view.LayoutInflater;
14+
import android.view.View;
15+
import android.view.ViewGroup;
16+
import android.widget.Toast;
17+
18+
import com.codepath.bestsellerlistapp.models.BestSellerBook;
19+
import com.codepath.bestsellerlistapp.networking.CallbackResponse;
20+
import com.codepath.bestsellerlistapp.networking.NYTimesApiClient;
21+
22+
import java.util.List;
23+
24+
/**
25+
* A fragment representing a list of Items.
26+
*/
27+
public class BestSellerBooksFragment extends Fragment implements OnListFragmentInteractionListener{
28+
29+
/**
30+
* Mandatory empty constructor for the fragment manager to instantiate the
31+
* fragment (e.g. upon screen orientation changes).
32+
*/
33+
public BestSellerBooksFragment() {
34+
}
35+
36+
@SuppressWarnings("unused")
37+
public static BestSellerBooksFragment newInstance(int columnCount) {
38+
BestSellerBooksFragment fragment = new BestSellerBooksFragment();
39+
Bundle args = new Bundle();
40+
fragment.setArguments(args);
41+
return fragment;
42+
}
43+
44+
@Override
45+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
46+
Bundle savedInstanceState) {
47+
View view = inflater.inflate(R.layout.fragment_best_seller_books_list, container, false);
48+
ContentLoadingProgressBar progressBar = (ContentLoadingProgressBar) view.findViewById(R.id.progress);
49+
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
50+
51+
// Set the adapter
52+
Context context = view.getContext();
53+
recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
54+
updateAdapter(progressBar, recyclerView);
55+
return view;
56+
}
57+
58+
59+
private void updateAdapter(final ContentLoadingProgressBar progressBar, final RecyclerView recyclerView) {
60+
progressBar.show();
61+
NYTimesApiClient nyTimesApiClient = new NYTimesApiClient();
62+
nyTimesApiClient.getBestSellersList(new CallbackResponse<List<BestSellerBook>>() {
63+
@Override
64+
public void onSuccess(List<BestSellerBook> models) {
65+
progressBar.hide();
66+
recyclerView.setAdapter(new BestSellerBooksRecyclerViewAdapter(models, BestSellerBooksFragment.this));
67+
Log.d("BestSellerBooksFragment", "response successful");
68+
}
69+
70+
@Override
71+
public void onFailure(Throwable error) {
72+
progressBar.hide();
73+
Log.e("BestSellerBooksFragment", error.getMessage());
74+
}
75+
});
76+
77+
}
78+
79+
80+
@Override
81+
public void onAttach(Context context) {
82+
super.onAttach(context);
83+
}
84+
85+
@Override
86+
public void onDetach() {
87+
super.onDetach();
88+
}
89+
90+
@Override
91+
public void onItemClick(BestSellerBook item) {
92+
Toast.makeText(getContext(), "test", Toast.LENGTH_LONG).show();
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.codepath.bestsellerlistapp;
2+
3+
import android.view.LayoutInflater;
4+
import android.view.View;
5+
import android.view.ViewGroup;
6+
import android.widget.TextView;
7+
8+
import androidx.recyclerview.widget.RecyclerView;
9+
10+
import com.codepath.bestsellerlistapp.models.BestSellerBook;
11+
12+
import java.util.List;
13+
14+
/**
15+
* {@link RecyclerView.Adapter} that can display a {@link BestSellerBook} and makes a call to the
16+
* specified {@link OnListFragmentInteractionListener}.
17+
*/
18+
public class BestSellerBooksRecyclerViewAdapter extends RecyclerView.Adapter<BestSellerBooksRecyclerViewAdapter.BookViewHolder> {
19+
20+
private final List<BestSellerBook> books;
21+
private final OnListFragmentInteractionListener mListener;
22+
23+
public BestSellerBooksRecyclerViewAdapter(List<BestSellerBook> items, OnListFragmentInteractionListener listener) {
24+
books = items;
25+
mListener = listener;
26+
}
27+
28+
@Override
29+
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
30+
View view = LayoutInflater.from(parent.getContext())
31+
.inflate(R.layout.fragment_best_seller_books, parent, false);
32+
return new BookViewHolder(view);
33+
}
34+
35+
@Override
36+
public void onBindViewHolder(final BookViewHolder holder, int position) {
37+
holder.mItem = books.get(position);
38+
holder.mBookTitle.setText(books.get(position).title);
39+
holder.mBookAuthor.setText(books.get(position).author);
40+
41+
holder.mView.setOnClickListener(new View.OnClickListener() {
42+
@Override
43+
public void onClick(View v) {
44+
if (null != mListener) {
45+
// Notify the active callbacks interface (the activity, if the
46+
// fragment is attached to one) that an item has been selected.
47+
mListener.onItemClick(holder.mItem);
48+
}
49+
}
50+
});
51+
}
52+
53+
@Override
54+
public int getItemCount() {
55+
return books.size();
56+
}
57+
58+
public class BookViewHolder extends RecyclerView.ViewHolder {
59+
public final View mView;
60+
public final TextView mBookTitle;
61+
public final TextView mBookAuthor;
62+
public BestSellerBook mItem;
63+
64+
public BookViewHolder(View view) {
65+
super(view);
66+
mView = view;
67+
mBookTitle = (TextView) view.findViewById(R.id.book_title);
68+
mBookAuthor = (TextView) view.findViewById(R.id.book_author);
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return mBookTitle.toString() + " '" + mBookAuthor.getText() + "'";
74+
}
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codepath.bestsellerlistapp;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
import androidx.fragment.app.FragmentManager;
5+
import androidx.fragment.app.FragmentTransaction;
6+
7+
import android.os.Bundle;
8+
9+
public class MainActivity extends AppCompatActivity {
10+
11+
@Override
12+
protected void onCreate(Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
setContentView(R.layout.activity_main);
15+
FragmentManager supportFragmentManager = getSupportFragmentManager();
16+
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
17+
fragmentTransaction.replace(R.id.content, new BestSellerBooksFragment(), null).commit();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codepath.bestsellerlistapp;
2+
3+
import com.codepath.bestsellerlistapp.models.BestSellerBook;
4+
5+
public interface OnListFragmentInteractionListener {
6+
7+
void onItemClick(BestSellerBook item);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codepath.bestsellerlistapp.models;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class BestSellerBook {
6+
7+
@SerializedName("rank")
8+
public int rank;
9+
10+
@SerializedName("title")
11+
public String title;
12+
13+
@SerializedName("author")
14+
public String author;
15+
16+
@SerializedName("book_image")
17+
public String bookImageUrl;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codepath.bestsellerlistapp.models;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.List;
6+
7+
public class BestSellerResults {
8+
9+
@SerializedName("list_name")
10+
public String listName;
11+
12+
@SerializedName("books")
13+
public List<BestSellerBook> books;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codepath.bestsellerlistapp.models;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class NYTimesAPIResponse {
6+
7+
@SerializedName("status")
8+
public String status;
9+
10+
@SerializedName("results")
11+
public BestSellerResults results;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codepath.bestsellerlistapp.networking;
2+
3+
public interface CallbackResponse<T> {
4+
void onSuccess(T model);
5+
6+
void onFailure(Throwable error);
7+
}

0 commit comments

Comments
 (0)