Created
December 4, 2013 03:04
-
-
Save benjamintanweihao/7781672 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.benjamintanweihao.bookeesg.product; | |
| import com.benjamintanweihao.bookeesg.R; | |
| import android.graphics.Bitmap; | |
| import android.os.AsyncTask; | |
| import android.os.Bundle; | |
| import android.support.v4.app.Fragment; | |
| import android.support.v4.app.FragmentActivity; | |
| import android.support.v4.app.FragmentManager; | |
| import android.support.v4.app.FragmentPagerAdapter; | |
| import android.support.v4.view.ViewPager; | |
| import android.text.method.ScrollingMovementMethod; | |
| import android.util.Log; | |
| import android.view.ViewGroup; | |
| import android.widget.ImageView; | |
| import android.widget.ListView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import com.benjamintanweihao.bookeesg.home.HomeActivity; | |
| import com.nlbservice.bookeesg.ArrayOfItem; | |
| import com.nlbservice.bookeesg.CatalogueService; | |
| import com.nlbservice.bookeesg.Credientials; | |
| import com.nlbservice.bookeesg.GetAvailabilityInfoResponse; | |
| import com.nlbservice.bookeesg.GetTitleDetailsResponse; | |
| import com.nlbservice.bookeesg.Item; | |
| import com.nlbservice.bookeesg.Modifiers; | |
| import com.viewpagerindicator.TabPageIndicator; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| public class ProductDetailsActivity extends FragmentActivity { | |
| private static final String[] CONTENT = new String[]{ "Availability", "Details", "Reviews" }; | |
| ImageView productImageView; | |
| TextView titleTextView; | |
| ViewPager mViewPager; | |
| TabPageIndicator indicator; | |
| ProductDetailsFragmentAdapter mProductDetailsFragmentAdapter; | |
| CatalogueService catalogueService = new CatalogueService(); | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.product_details_tabs); | |
| mProductDetailsFragmentAdapter = new ProductDetailsFragmentAdapter(getSupportFragmentManager()); | |
| mViewPager = (ViewPager) findViewById(R.id.pager); | |
| mViewPager.setAdapter(mProductDetailsFragmentAdapter); | |
| indicator = (TabPageIndicator) findViewById(R.id.indicator); | |
| indicator.setViewPager(mViewPager); | |
| productImageView = (ImageView) findViewById(R.id.productImageView); | |
| titleTextView = (TextView) findViewById(R.id.title_text_view); | |
| String isbn = getIntent().getStringExtra(HomeActivity.EXTRA_SCAN_RESULT); | |
| new GetTitleDetailsTask().execute(isbn); | |
| new SetCoverImageTask().execute(isbn); | |
| new GetAvailabilityInfoAsyncTask().execute(isbn); | |
| } | |
| class ProductDetailsFragmentAdapter extends FragmentPagerAdapter { | |
| private HashMap<Integer, Fragment> mPageReferenceMap = new HashMap<Integer, Fragment>(); | |
| public ProductDetailsFragmentAdapter(FragmentManager fm) { | |
| super(fm); | |
| } | |
| @Override | |
| public Fragment getItem(int position) { | |
| if (position == 0) { | |
| Fragment f1 = ProductAvailabilityListFragment.newInstance(); | |
| mPageReferenceMap.put(position, f1); | |
| return f1; | |
| } else if (position == 1) { | |
| Fragment f2 = ProductDetailsFragment.newInstance(); | |
| mPageReferenceMap.put(position, f2); | |
| return f2; | |
| } else if (position == 2) { | |
| Fragment f3 = ProductAvailabilityListFragment.newInstance(); | |
| mPageReferenceMap.put(position, f3); | |
| return f3; | |
| } else { | |
| // TODO: This needs to be retired eventually | |
| return TestFragment.newInstance(CONTENT[position % CONTENT.length]); | |
| } | |
| } | |
| @Override | |
| public void destroyItem(ViewGroup container, int position, Object object) { | |
| mPageReferenceMap.remove(position); | |
| super.destroyItem(container, position, object); | |
| } | |
| public Fragment getFragmentByPosition(int position) { | |
| return mPageReferenceMap.get(position); | |
| } | |
| @Override | |
| public CharSequence getPageTitle(int position) { | |
| return CONTENT[position % CONTENT.length].toUpperCase(); | |
| } | |
| @Override | |
| public int getCount() { | |
| return CONTENT.length; | |
| } | |
| } | |
| private class SetCoverImageTask extends AsyncTask<String, Void, Bitmap> { | |
| @Override | |
| protected Bitmap doInBackground(String... isbns) { | |
| String isbn = isbns[0].toString(); | |
| String url = "http://covers.openlibrary.org/b/isbn/" + isbn + "-M.jpg"; | |
| return Utility.getBitmapFromURL(url); | |
| } | |
| @Override | |
| protected void onPostExecute(Bitmap bitmap) { | |
| productImageView.setImageBitmap(bitmap); | |
| } | |
| } | |
| private class GetTitleDetailsTask extends AsyncTask<String, Void, GetTitleDetailsResponse> { | |
| @Override | |
| protected GetTitleDetailsResponse doInBackground(String... isbns) { | |
| String isbn = isbns[0].toString(); | |
| try { | |
| GetTitleDetailsResponse response = catalogueService.GetTitleDetails(Credientials.APIKEY, isbn); | |
| return response; | |
| } catch (Exception ex) { | |
| } | |
| return null; | |
| } | |
| @Override | |
| protected void onPostExecute(GetTitleDetailsResponse response) { | |
| if (response != null && response.TitleDetail != null) { | |
| if (response.TitleDetail.TitleName != null) { | |
| titleTextView.setText(response.TitleDetail.TitleName); | |
| } else { | |
| titleTextView.setText("N.A"); | |
| } | |
| // Populate the [Details] tab | |
| Fragment detailsFragment = mProductDetailsFragmentAdapter.getFragmentByPosition(1); | |
| // FIXME: If rotate, everything goes to hell. | |
| TextView publisherTextView = (TextView) detailsFragment.getView().findViewById(R.id.product_details_publisher_text_view); | |
| TextView physicalDescriptionTextView = (TextView) detailsFragment.getView().findViewById(R.id.product_details_physical_description_text_view); | |
| TextView isbnTextView = (TextView) detailsFragment.getView().findViewById(R.id.product_details_isbn_text_view); | |
| TextView summaryTextView = (TextView)detailsFragment.getView().findViewById(R.id.product_details_summary_text_view); | |
| if (response.TitleDetail.Publisher != null) { | |
| publisherTextView.setText(response.TitleDetail.Publisher); | |
| } else { | |
| publisherTextView.setText("N.A"); | |
| } | |
| if (response.TitleDetail.PhysicalDesc != null) { | |
| physicalDescriptionTextView.setText(response.TitleDetail.PhysicalDesc); | |
| } else { | |
| physicalDescriptionTextView.setText("N.A"); | |
| } | |
| if (response.TitleDetail.ISBN != null) { | |
| isbnTextView.setText(response.TitleDetail.ISBN); | |
| } else { | |
| isbnTextView.setText("N.A"); | |
| } | |
| if (response.TitleDetail.Summary != null) { | |
| summaryTextView.setText(response.TitleDetail.Summary); | |
| summaryTextView.setMovementMethod(ScrollingMovementMethod.getInstance()); | |
| } else { | |
| summaryTextView.setText(""); | |
| } | |
| } else { | |
| // TODO: No result! Go back. | |
| Toast.makeText(getApplicationContext(), "No result!", Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } | |
| private class GetAvailabilityInfoAsyncTask extends AsyncTask<String, Void, GetAvailabilityInfoResponse> { | |
| @Override | |
| protected GetAvailabilityInfoResponse doInBackground(String... isbns) { | |
| String isbn = isbns[0].toString(); | |
| Modifiers modifiers = new Modifiers(); | |
| try { | |
| GetAvailabilityInfoResponse response = catalogueService.GetAvailabilityInfo(Credientials.APIKEY, isbn, modifiers); | |
| return response; | |
| } catch (Exception ex) { | |
| } | |
| return null; | |
| } | |
| @Override | |
| protected void onPostExecute(GetAvailabilityInfoResponse response) { | |
| if (response != null) { | |
| ArrayOfItem items = response.Items; | |
| ArrayList<AvailiabilityInfo> availiabilityInfoList = new ArrayList<AvailiabilityInfo>(); | |
| for(Item i : items) { | |
| AvailiabilityInfo aInfo = new AvailiabilityInfo(i.BranchName, i.LocationDesc, i.StatusDesc, i.StatusDate, i.CallNumber); | |
| availiabilityInfoList.add(aInfo); | |
| } | |
| // Populate the [Availability] tab | |
| AvailabilityInfoItemAdapter adapter = new AvailabilityInfoItemAdapter(ProductDetailsActivity.this.getBaseContext(), R.layout.availability_list_item, availiabilityInfoList); | |
| ProductAvailabilityListFragment listFragment = (ProductAvailabilityListFragment)mProductDetailsFragmentAdapter.getFragmentByPosition(0); | |
| ListView listView = listFragment.getListView(); | |
| listView.setAdapter(adapter); | |
| adapter.notifyDataSetChanged(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment