-
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
764a7d4
commit 6c273bf
Showing
13 changed files
with
758 additions
and
29 deletions.
There are no files selected for viewing
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
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
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,67 @@ | ||
package ua.kpi.comsys.io8227.jackshen; | ||
|
||
/** An {@link Book} object contains information related to a single book. */ | ||
|
||
class Book { | ||
|
||
/** Title of the book */ | ||
private String mTitle; | ||
|
||
/** Subtitle of the book */ | ||
private String mSubtitle; | ||
|
||
/** ISBN of the book*/ | ||
private String mIsbn; | ||
|
||
/** Retail price of the book */ | ||
private String mPrice; | ||
|
||
/** Cover of the book */ | ||
private String mImageUrl; | ||
|
||
/** | ||
* Create book object | ||
* | ||
* @param title - title of the book | ||
* @param subtitle - subtitle of the book | ||
* @param isbn - isbn number of the book | ||
* @param price - retail price of the book | ||
* @param imageUrl - the URL to find cover of the book | ||
*/ | ||
Book(String title, String subtitle, String isbn, String price, String imageUrl) { | ||
this.mTitle = title; | ||
this.mSubtitle = subtitle; | ||
this.mIsbn = isbn; | ||
this.mPrice = price; | ||
this.mImageUrl = imageUrl; | ||
} | ||
|
||
/** | ||
* Return the title information of the book | ||
* | ||
* @return the title of the book | ||
*/ | ||
String getTitle() { | ||
return mTitle; | ||
} | ||
|
||
/** Return the subtitle of the book */ | ||
String getSubtitle() { | ||
return mSubtitle; | ||
} | ||
|
||
/** Return the ISBN number of the book */ | ||
String getISBN() { | ||
return mIsbn; | ||
} | ||
|
||
/** Return the retail price of the book */ | ||
String getPrice() { | ||
return mPrice; | ||
} | ||
|
||
/** Return the URL to find cover of the book */ | ||
String getImageUrl() { | ||
return mImageUrl; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
app/src/main/java/ua/kpi/comsys/io8227/jackshen/BookActivity.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,123 @@ | ||
package ua.kpi.comsys.io8227.jackshen; | ||
|
||
import android.app.LoaderManager; | ||
import android.content.Context; | ||
import android.content.Loader; | ||
import android.net.ConnectivityManager; | ||
import android.net.NetworkInfo; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.ListView; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.RequiresApi; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
|
||
public class BookActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Book>> { | ||
|
||
/** URL for book data */ | ||
private static final String REQUEST_URL = "https://api.jsonbin.io/b/6023ffbd87173a3d2f5b37e9"; | ||
|
||
/** | ||
* Constant value for the book loader ID. | ||
* We can choose any int, it`s really needed for multiple loaders | ||
*/ | ||
private static final int BOOK_LOADER_ID = 1; | ||
|
||
/** Adapter for the list of books */ | ||
private BookAdapter mAdapter; | ||
|
||
/** TextView that is displayed when the list is empty */ | ||
private TextView mEmptyTextView; | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
// Initialize activity on main thread. | ||
// Bundle holds previous state when re-initialized | ||
super.onCreate(savedInstanceState); | ||
|
||
// Inflate the activity's UI | ||
setContentView(R.layout.activity_book); | ||
|
||
// Find a reference to the {@link ListView} in the layout | ||
ListView bookListView = findViewById(R.id.list); | ||
|
||
// Set empty view when there is no data | ||
mEmptyTextView = findViewById(R.id.empty_view); | ||
bookListView.setEmptyView(mEmptyTextView); | ||
|
||
// Create a new adapter that takes an empty list of books as input | ||
mAdapter = new BookAdapter(this, new ArrayList<Book>()); | ||
|
||
// Set the adapter on the {@link ListView} so the list can be populated in UI | ||
bookListView.setAdapter(mAdapter); | ||
|
||
// Get a reference to the ConnectivityManager to check state of network connectivity | ||
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | ||
|
||
// Get details on the currently active default data network | ||
NetworkInfo networkInfo = Objects.requireNonNull(connMgr).getActiveNetworkInfo(); | ||
|
||
// If there is a network connection -> get data | ||
if (networkInfo != null && networkInfo.isConnected()) { | ||
// Get a reference to the LoaderManager, in order to interact with loaders. | ||
LoaderManager loaderManager = getLoaderManager(); | ||
|
||
// Get the loader initialized. Go through the above specified int ID constant | ||
// and pass the bundle to null. | ||
loaderManager.initLoader(BOOK_LOADER_ID, null, this); | ||
} else { | ||
|
||
// Otherwise, display error and hide loading indicator so error message will be visible | ||
View loadingIndicator = findViewById(R.id.progress_bar); | ||
loadingIndicator.setVisibility(View.GONE); | ||
|
||
// Update empty state with no connection error message | ||
mEmptyTextView.setText("No internet connection."); | ||
} | ||
} | ||
|
||
@Override | ||
public Loader<List<Book>> onCreateLoader(int i, Bundle bundle) { | ||
// Create a new loader for the given URL | ||
Uri baseUri = Uri.parse(REQUEST_URL); | ||
|
||
return new BookLoader(this, baseUri.toString()); | ||
} | ||
|
||
/** | ||
* The loader requests and parses information downloader from the internet on a background | ||
* thread pool, keeping the UI thread unblocked | ||
*/ | ||
@Override | ||
public void onLoadFinished(Loader<List<Book>> loader, List<Book> books) { | ||
|
||
// Hide progress bar | ||
View loadingIndicator = findViewById(R.id.progress_bar); | ||
loadingIndicator.setVisibility(View.GONE); | ||
|
||
// Set empty state text to display "No books to display." | ||
mEmptyTextView.setText("No books to display."); | ||
|
||
// Clear the adapter of previous data | ||
mAdapter.clear(); | ||
|
||
// Add valid list of books to the adapter | ||
if (books != null && !books.isEmpty()) | ||
mAdapter.addAll(books); | ||
} | ||
|
||
@Override | ||
public void onLoaderReset(Loader<List<Book>> loader) { | ||
// Clear existing data on adapter as loader is reset | ||
mAdapter.clear(); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
app/src/main/java/ua/kpi/comsys/io8227/jackshen/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,119 @@ | ||
package ua.kpi.comsys.io8227.jackshen; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.AsyncTask; | ||
import android.os.Build; | ||
import android.util.Log; | ||
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 androidx.annotation.NonNull; | ||
import androidx.annotation.RequiresApi; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
|
||
/** | ||
* Class {@link BookAdapter} is used to create a list item layout for each book | ||
* in the data source (a list of {@link Book} objects). | ||
* | ||
* An adapter view such as ListView will be provided with these | ||
* list item layouts to be presented to the user. | ||
*/ | ||
|
||
public class BookAdapter extends ArrayAdapter<Book> { | ||
|
||
/** | ||
* Constructs a new {@link BookAdapter}. | ||
* | ||
* @param context of the app | ||
* @param books - list of books | ||
*/ | ||
|
||
BookAdapter(Context context, List<Book> books) { | ||
super(context, 0, books); | ||
} | ||
|
||
/** | ||
* Returns the view of the list item that shows information about the book in the book list. | ||
*/ | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
@NonNull | ||
@Override | ||
public View getView(int position, View convertView, @NonNull ViewGroup parent) { | ||
// Check if there is an existing convertView that we can reuse, otherwise, | ||
// if convertView is null, then inflate a new list item layout. | ||
View listItemView = convertView; | ||
if (listItemView == null) { | ||
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.book_list_item, parent, false); | ||
} | ||
|
||
// Find the book at the given position in the list of books | ||
Book currentBook = getItem(position); | ||
|
||
// Find the TextView with view ID titleBook | ||
TextView titleView = listItemView.findViewById(R.id.titleBook); | ||
// Find the TextView with view ID subtitleBook | ||
TextView subtitleView = listItemView.findViewById(R.id.subtitleBook); | ||
// Find the TextView with view ID isbnBook | ||
TextView isbnView = listItemView.findViewById(R.id.isbnBook); | ||
// Find the TextView with view ID priceBook | ||
TextView priceView = listItemView.findViewById(R.id.priceBook); | ||
// Find the ImageView with view ID imageBook | ||
ImageView imageView = listItemView.findViewById(R.id.imageBook); | ||
|
||
// Display the title of the current book in that TextView | ||
titleView.setText(Objects.requireNonNull(currentBook).getTitle()); | ||
// Display the subtitle of the current book in that TextView | ||
subtitleView.setText(currentBook.getSubtitle()); | ||
// Display the ISBN of the current book in that TextView | ||
isbnView.setText(currentBook.getISBN()); | ||
// Display the price of the current book in that TextView | ||
priceView.setText(currentBook.getPrice()); | ||
|
||
// Display image in the ImageView widget | ||
if (currentBook.getImageUrl().equals("")) { | ||
imageView.setImageResource(R.drawable.noimage); | ||
} else { | ||
new DownloadImage(imageView).execute(currentBook.getImageUrl()); | ||
} | ||
|
||
return listItemView; | ||
} | ||
|
||
/** Class to download an image from URL */ | ||
private class DownloadImage extends AsyncTask<String, Void, Bitmap> { | ||
final ImageView bmImage; | ||
|
||
DownloadImage(ImageView bmImage) { | ||
this.bmImage = bmImage; | ||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
protected Bitmap doInBackground(String... urls) { | ||
String urlDisplay = urls[0]; | ||
Bitmap mIcon = null; | ||
try { | ||
InputStream in = new java.net.URL(urlDisplay).openStream(); | ||
mIcon = BitmapFactory.decodeStream(in); | ||
} catch (Exception e) { | ||
Log.e("Error", Objects.requireNonNull(e.getMessage())); | ||
e.printStackTrace(); | ||
} | ||
return mIcon; | ||
} | ||
|
||
protected void onPostExecute(Bitmap result) { | ||
bmImage.setImageBitmap(result); | ||
} | ||
} | ||
} |
Oops, something went wrong.