Last active
December 29, 2016 16:43
-
-
Save gideonseven/80b0a85ec2396dca928a2ea9c4d8509d to your computer and use it in GitHub Desktop.
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
package don.ctrl.wallpaperproject.ui.product; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.media.Image; | |
import android.preference.PreferenceManager; | |
import android.support.v4.view.ViewPager; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import don.ctrl.wallpaperproject.R; | |
import don.ctrl.wallpaperproject.adapter.ProductDetailAdapter; | |
import don.ctrl.wallpaperproject.model.ProductDetails; | |
import don.ctrl.wallpaperproject.services.ApiClient; | |
import don.ctrl.wallpaperproject.ui.BaseActivity; | |
import don.ctrl.wallpaperproject.ui.user.CartActivity; | |
import don.ctrl.wallpaperproject.utils.DatabaseHelper; | |
import don.ctrl.wallpaperproject.utils.PrefUtil; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
public class ProductDetailActivity extends BaseActivity implements View.OnClickListener { | |
private static final String TAG = ProductDetailActivity.class.getSimpleName(); | |
ApiClient apiClient; | |
SharedPreferences sharedPreferences; | |
private String getApiKey, getUserKey, id; | |
private TextView tvName, tvPrice, tvDescription, tvTotal; | |
private Button btnBuy; | |
private Button btnShare; | |
private ImageButton btnPlus; | |
private ImageButton btnMinus; | |
DatabaseHelper databaseHelper; | |
private ViewPager viewPager; | |
ProductDetailAdapter adapter; | |
private int counter = 0; | |
private int price; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_product_detail); | |
showBackArrow(); | |
setToolbarTitle("Detail Produk"); | |
/*showProgressDialog(ProductDetailActivity.this, "Mohon Tunggu");*/ | |
//get id from activity product list | |
id = getIntent().getExtras().getString("id"); | |
// initialize database | |
databaseHelper = new DatabaseHelper(this); | |
getSharedPref(); | |
initViews(); | |
initData(); | |
} | |
private void initViews() { | |
tvName = (TextView) findViewById(R.id.tvName); | |
tvPrice = (TextView) findViewById(R.id.tvPrice); | |
tvDescription = (TextView) findViewById(R.id.tvDescription); | |
tvTotal = (TextView) findViewById(R.id.tvJumlah); | |
btnBuy = (Button) findViewById(R.id.btnBuyDetail); | |
btnShare = (Button) findViewById(R.id.btnShare); | |
btnPlus = (ImageButton) findViewById(R.id.btnPlus); | |
btnMinus = (ImageButton) findViewById(R.id.btnMinus); | |
btnBuy.setOnClickListener(this); | |
btnShare.setOnClickListener(this); | |
btnPlus.setOnClickListener(this); | |
btnMinus.setOnClickListener(this); | |
viewPager = (ViewPager) findViewById(R.id.vpDetail); | |
} | |
private void getSharedPref() { | |
//set sharedpref | |
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); | |
getApiKey = sharedPreferences.getString(PrefUtil.PREF_USER_API_KEY, ""); | |
getUserKey = sharedPreferences.getString(PrefUtil.PREF_USER_USER_KEY, ""); | |
} | |
private void initData() { | |
apiClient = new ApiClient(); | |
Call<ProductDetails> call = apiClient.getApiInterface().getProductDetail(getUserKey, getApiKey, id); | |
call.enqueue(new Callback<ProductDetails>() { | |
@Override | |
public void onResponse(Call<ProductDetails> call, Response<ProductDetails> response) { | |
// hideProgressDialog(); | |
ProductDetails products = response.body(); | |
if (products.getStatus()) { | |
ProductDetails.Datum productsData = products.getDatum(); | |
tvName.setText(productsData.getNamaProduk()); | |
tvPrice.setText("Rp. " + productsData.getHarga()); | |
tvDescription.setText(productsData.getKatalog()); | |
ArrayList<String> gambar = productsData.getGambar(); | |
adapter = new ProductDetailAdapter(ProductDetailActivity.this, gambar); | |
viewPager.setAdapter(adapter); | |
//saving data to database | |
databaseHelper.insertData( | |
productsData.getKodeProduk(), | |
productsData.getNamaProduk(), | |
productsData.getHarga(), | |
productsData.getGambar().get(0)); | |
} | |
} | |
@Override | |
public void onFailure(Call<ProductDetails> call, Throwable t) { | |
// Log error here since request failed | |
Log.e(TAG, t.toString()); | |
// hideProgressDialog(); | |
} | |
}); | |
} | |
private void goToCart() { | |
Intent intent = new Intent(ProductDetailActivity.this, CartActivity.class); | |
intent.putExtra("total_item", counter); | |
startActivity(intent); | |
finish(); | |
} | |
private void share() { | |
Intent sendIntent = new Intent(); | |
sendIntent.setAction(Intent.ACTION_SEND); | |
sendIntent.putExtra(Intent.EXTRA_TEXT, "Saya baru saja membeli di Toko-Wallpaperdinding.com"); | |
sendIntent.setType("text/plain"); | |
startActivity(sendIntent); | |
} | |
private void setBtnPlus() { | |
counter = counter + 1; | |
tvTotal.setText(String.valueOf(counter)); | |
} | |
private void setBtnMinus() { | |
if (counter == 0) { | |
tvTotal.setText("0"); | |
} else { | |
counter = counter - 1; | |
tvTotal.setText(String.valueOf(counter)); | |
} | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
finish(); | |
break; | |
} | |
return true; | |
} | |
@Override | |
public void onClick(View view) { | |
switch (view.getId()) { | |
case R.id.btnBuyDetail: | |
goToCart(); | |
break; | |
case R.id.btnShare: | |
share(); | |
break; | |
case R.id.btnPlus: | |
setBtnPlus(); | |
break; | |
case R.id.btnMinus: | |
setBtnMinus(); | |
break; | |
} | |
} | |
} | |
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@color/light_grey" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".ui.product.ProductDetailActivity"> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/vpDetail" | |
android:layout_width="@dimen/margin_266" | |
android:layout_height="@dimen/margin_266" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" /> | |
<!--<com.viewpagerindicator.CirclePageIndicator--> | |
<!--android:id="@+id/indicator"--> | |
<!--android:layout_width="@dimen/margin_266"--> | |
<!--android:layout_height="wrap_content"--> | |
<!--android:layout_alignBottom="@+id/vpDetail"--> | |
<!--android:layout_centerHorizontal="true"--> | |
<!--android:padding="10dip" />--> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/rlProductDetail" | |
android:layout_marginTop="16dp" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/tvName" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="-" | |
android:textSize="@dimen/text_20sp" /> | |
<TextView | |
android:id="@+id/tvDescription" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="18dp" | |
android:text="-" | |
android:textSize="@dimen/text_17sp" /> | |
<LinearLayout | |
android:layout_gravity="left" | |
android:layout_marginTop="@dimen/margin_10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:gravity="center"> | |
<ImageButton | |
android:id="@+id/btnMinus" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@drawable/ic_remove" /> | |
<TextView | |
android:text="1" | |
android:id="@+id/tvJumlah" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="@dimen/margin_10dp" | |
android:layout_marginRight="@dimen/margin_10dp" | |
android:gravity="center" /> | |
<ImageButton | |
android:id="@+id/btnPlus" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@drawable/ic_add" /> | |
</LinearLayout> | |
</LinearLayout> | |
<Button | |
android:id="@+id/btnBuyDetail" | |
style="@style/Button.Default" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_alignParentEnd="true" | |
android:layout_alignParentRight="true" | |
android:text="@string/buy" /> | |
<RelativeLayout | |
android:id="@+id/rlProductDetail" | |
android:layout_width="match_parent" | |
android:layout_height="24dp" | |
android:layout_marginTop="15dp" | |
android:orientation="horizontal" | |
android:layout_below="@+id/vpDetail" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true"> | |
<TextView | |
android:id="@+id/tvPrice" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerVertical="true" | |
android:layout_gravity="start" | |
android:text="-" /> | |
<Button | |
android:id="@+id/btnShare" | |
style="@style/Button.Share" | |
android:layout_width="90dp" | |
android:layout_height="40dp" | |
android:layout_alignParentEnd="true" | |
android:layout_alignParentRight="true" | |
android:layout_gravity="right" | |
android:drawableRight="@drawable/ic_menu_share" | |
android:text="Bagikan" | |
android:textAllCaps="false" | |
android:textSize="@dimen/text_11sp" /> | |
</RelativeLayout> | |
</RelativeLayout> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment