-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a502faa
commit 05353aa
Showing
30 changed files
with
865 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/androidTest/java/com/example/hp/booklistingapp/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.hp.booklistingapp; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getTargetContext(); | ||
|
||
assertEquals("com.example.hp.booklistingapp", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.hp.booklistingapp"> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="Book Listing App" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.hp.booklistingapp; | ||
|
||
import java.util.List; | ||
|
||
public class Book { | ||
private String mTitle; | ||
private List<String> mAuthors; | ||
private String mCoverImagePath; | ||
|
||
public Book(String title, List<String> authors, String coverImagePath) { | ||
this.mTitle = title; | ||
this.mAuthors = authors; | ||
this.mCoverImagePath = coverImagePath; | ||
} | ||
|
||
public String getTitle() { | ||
return mTitle; | ||
} | ||
|
||
public List<String> getAuthors() { | ||
return mAuthors; | ||
} | ||
|
||
public String getCoverImagePath() { | ||
return mCoverImagePath; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/example/hp/booklistingapp/BookAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.example.hp.booklistingapp; | ||
|
||
import android.content.Context; | ||
import android.text.TextUtils; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import com.squareup.picasso.Picasso; | ||
|
||
import java.util.ArrayList; | ||
|
||
import static android.view.KeyCharacterMap.load; | ||
|
||
public class BookAdapter extends ArrayAdapter<Book> { | ||
|
||
private static final String AUTHOR_SEPARATOR = ", "; | ||
public BookAdapter(Context context, ArrayList<Book> books) { | ||
super(context, 0, books); | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
|
||
View listItemView = convertView; | ||
if(listItemView == null) { | ||
listItemView = LayoutInflater.from(getContext()).inflate( | ||
R.layout.item_layout, parent, false); | ||
} | ||
|
||
Book currentBook = getItem(position); | ||
|
||
ImageView coverImageView = (ImageView) listItemView.findViewById(R.id.iv_book_cover); | ||
Picasso.with(getContext()) | ||
.load(currentBook.getCoverImagePath()) | ||
.placeholder(R.drawable.placeholder_book_cover) | ||
.into(coverImageView); | ||
|
||
TextView bookTitleTextView = (TextView) listItemView.findViewById(R.id.tv_title_of_book); | ||
bookTitleTextView.setText(currentBook.getTitle()); | ||
|
||
TextView bookAuthorsTextView = (TextView) listItemView.findViewById(R.id.tv_authors_of_book); | ||
bookAuthorsTextView.setText(TextUtils.join(AUTHOR_SEPARATOR, currentBook.getAuthors())); | ||
|
||
return listItemView; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/example/hp/booklistingapp/BookLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.example.hp.booklistingapp; | ||
|
||
import android.content.AsyncTaskLoader; | ||
import android.content.Context; | ||
|
||
import java.util.List; | ||
|
||
public class BookLoader extends AsyncTaskLoader<List<Book>> { | ||
|
||
private String mSearchTerm; | ||
|
||
public BookLoader(Context context, String searchTerm) { | ||
super(context); | ||
mSearchTerm = searchTerm; | ||
} | ||
|
||
@Override | ||
protected void onStartLoading() { | ||
forceLoad(); | ||
} | ||
|
||
@Override | ||
public List<Book> loadInBackground() { | ||
List<Book> books = null; | ||
if (null != mSearchTerm && !mSearchTerm.isEmpty()) { | ||
books = QueryUtils.fetchBookData(getContext(), mSearchTerm); | ||
} | ||
return books; | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
src/main/java/com/example/hp/booklistingapp/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.example.hp.booklistingapp; | ||
|
||
import android.app.LoaderManager; | ||
import android.content.Context; | ||
import android.content.Loader; | ||
import android.net.ConnectivityManager; | ||
import android.net.NetworkInfo; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.EditText; | ||
import android.widget.ListView; | ||
import android.widget.ProgressBar; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
import butterknife.BindView; | ||
import butterknife.ButterKnife; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Book>> { | ||
|
||
private static final int BOOKS_LOADER_ID = 1; | ||
|
||
private static final String SEARCH_TERM_KEY = "Search Term"; | ||
|
||
@BindView(R.id.et_search_term) | ||
public EditText mSearchTermEditText; | ||
|
||
@BindView(R.id.tv_message_display) | ||
public TextView mMessageDisplayTextView; | ||
|
||
@BindView(R.id.loading_indicator) | ||
public ProgressBar mLoadingIndicator; | ||
|
||
@BindView(R.id.lv_books) | ||
public ListView mBookListView; | ||
private BookAdapter mBookAdapter; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
ButterKnife.bind(this); | ||
mBookAdapter = new BookAdapter(this, new ArrayList<Book>()); | ||
mBookListView.setAdapter(mBookAdapter); | ||
|
||
if (isNetworkAvailable()) { | ||
LoaderManager loaderManager = getLoaderManager(); | ||
loaderManager.initLoader(BOOKS_LOADER_ID, null, this); | ||
} else { | ||
showMessage(getString(R.string.error_no_internet)); | ||
} | ||
} | ||
|
||
private void showMessage(String message) { | ||
mLoadingIndicator.setVisibility(View.GONE); | ||
mBookListView.setVisibility(View.GONE); | ||
|
||
mMessageDisplayTextView.setText(message); | ||
mMessageDisplayTextView.setVisibility(View.VISIBLE); | ||
} | ||
|
||
private void showProgressBar() { | ||
mMessageDisplayTextView.setVisibility(View.GONE); | ||
mBookListView.setVisibility(View.GONE); | ||
|
||
mLoadingIndicator.setVisibility(View.VISIBLE); | ||
} | ||
|
||
private void showBooks(List<Book> books) { | ||
mLoadingIndicator.setVisibility(View.GONE); | ||
mMessageDisplayTextView.setVisibility(View.GONE); | ||
|
||
mBookAdapter.clear(); | ||
mBookAdapter.addAll(books); | ||
mBookListView.setVisibility(View.VISIBLE); | ||
} | ||
|
||
public void onClickSearch(View view) { | ||
String searchTerm = mSearchTermEditText.getText().toString().trim(); | ||
boolean searchTermAvailable = null != searchTerm && !searchTerm.isEmpty(); | ||
boolean networkAvailable = isNetworkAvailable(); | ||
|
||
if (networkAvailable && searchTermAvailable) { | ||
showProgressBar(); | ||
Bundle bundle = new Bundle(); | ||
bundle.putString(SEARCH_TERM_KEY, searchTerm); | ||
LoaderManager loaderManager = getLoaderManager(); | ||
loaderManager.restartLoader(BOOKS_LOADER_ID, bundle, this); | ||
} else if (!networkAvailable) { | ||
showMessage(getString(R.string.error_no_internet)); | ||
} else { | ||
Toast.makeText(this, R.string.error_enter_search_term, Toast.LENGTH_LONG).show(); | ||
} | ||
} | ||
|
||
@Override | ||
public Loader<List<Book>> onCreateLoader(int id, Bundle args) { | ||
String searchTerm = null; | ||
if (null != args) { | ||
searchTerm = args.getString(SEARCH_TERM_KEY, null); | ||
} | ||
return new BookLoader(this, searchTerm); | ||
} | ||
|
||
@Override | ||
public void onLoadFinished(Loader<List<Book>> loader, List<Book> data) { | ||
boolean booksAvailable = (null != data && !data.isEmpty()); | ||
|
||
if (booksAvailable) { | ||
showBooks(data); | ||
} else { | ||
showMessage(getString(R.string.error_no_books)); | ||
} | ||
} | ||
|
||
@Override | ||
public void onLoaderReset(Loader<List<Book>> loader) { | ||
showMessage(getString(R.string.error_no_books)); | ||
} | ||
|
||
private boolean isNetworkAvailable() { | ||
ConnectivityManager connMgr = | ||
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | ||
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); | ||
|
||
return networkInfo != null && networkInfo.isConnected(); | ||
} | ||
} |
Oops, something went wrong.