Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Product reviews #38

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23+'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v13:23.1.1'
compile 'de.greenrobot:eventbus:2.4.0'
compile project(':core')
}
58 changes: 49 additions & 9 deletions android/src/main/java/org/robovm/store/StoreAppActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import de.greenrobot.event.EventBus;
import org.robovm.store.api.RoboVMWebService;
import org.robovm.store.api.RoboVMWebService.ActionWrapper;
import org.robovm.store.fragments.*;
import org.robovm.store.model.Product;
import org.robovm.store.util.Action;
import org.robovm.store.util.ImageCache;
import org.robovm.store.util.Images;
import org.robovm.store.util.*;

public class StoreAppActivity extends Activity {
private int baseFragment;
Expand Down Expand Up @@ -63,6 +62,18 @@ public <T> void invoke(Action<T> action, T result) {
}
}

@Override
protected void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}

@Override
protected void onPause() {
super.onPause();
EventBus.getDefault().unregister(this);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down Expand Up @@ -145,12 +156,13 @@ private int getOutAnimationForFragment(Fragment fragment) {
}

public void showProductDetail(Product product, int itemVerticalOffset) {
ProductDetailsFragment productDetails = new ProductDetailsFragment(product, itemVerticalOffset);
productDetails.setAddToBasketListener((order) -> {
RoboVMWebService.getInstance().getBasket().add(order);
setupActionBar();
});
switchScreens(productDetails);
ProductFragment productFragment = new ProductFragment(product, itemVerticalOffset);
// ProductDetailsFragment productDetails = new ProductDetailsFragment(product, itemVerticalOffset);
// productDetails.setAddToBasketListener((order) -> {
// RoboVMWebService.getInstance().getBasket().add(order);
// setupActionBar();
// });
switchScreens(productFragment);
}

public void setupActionBar() {
Expand All @@ -161,6 +173,34 @@ public void setupActionBar(boolean showUp) {
getActionBar().setDisplayHomeAsUpEnabled(showUp);
}

public void onEvent(EventSetupActionBar pEventSetupActionBar){
this.setupActionBar();
}

public void onEvent(EventCreateReview pEventCreateReview){
RoboVMWebService.getInstance().createReview(pEventCreateReview.getReview(), reviewResponse -> {
//ignore response
//refresh reviews
refreshProducts();
});
}

public void onEvent(EventUpdateReview pEventUpdateReview){
RoboVMWebService.getInstance().updateReview(pEventUpdateReview.getReview(), reviewResponse -> {
//ignore response
//refresh reviews
refreshProducts();
});
}

private void refreshProducts() {
RoboVMWebService.getInstance().getProducts(products -> {
if(products != null) {
EventBus.getDefault().post(new EventRefreshReviewsList(products));
}
});
}

public void showBasket() {
BasketFragment basket = new BasketFragment(RoboVMWebService.getInstance().getBasket());
basket.setCheckoutListener(this::showLogin);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package org.robovm.store.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;
import de.greenrobot.event.EventBus;
import org.robovm.store.R;
import org.robovm.store.model.ProductReview;
import org.robovm.store.util.EventCreateReview;
import org.robovm.store.util.EventUpdateReview;

import java.util.Date;

/**
* Created by andrei on 24/11/15.
*/
public class AddEditReviewDialogFragment extends DialogFragment {

public static final String TAG_STARS = "tag_stars";
public static final String TAG_PRODUCT_ID = "tag_product_id";
public static final String TAG_REVIEW_ID = "tag_review_id";
public static final String TAG_FULL_NAME = "tag_full_name";
public static final String TAG_COMMENT = "tag_comment";
public static final String TAG_NEW_REVIEW = "tag_new_review";


private RatingBar mRatingBarView;
private EditText mFullNameEditText;
private EditText mCommentEditText;

private ProductReview mCurrentReview;
private boolean mIsNewReview;

public AddEditReviewDialogFragment(){
this.mCurrentReview = new ProductReview();
}
public static AddEditReviewDialogFragment newInstance(String pProductId) {
return newInstance(pProductId, null);
}
public static AddEditReviewDialogFragment newInstance(String pProductId, ProductReview productReview){
AddEditReviewDialogFragment fragment = new AddEditReviewDialogFragment();
Bundle bundle = new Bundle();
bundle.putString(TAG_PRODUCT_ID, pProductId);
bundle.putBoolean(TAG_NEW_REVIEW, true);
if(productReview != null) {
bundle.putBoolean(TAG_NEW_REVIEW, false);
bundle.putInt(TAG_STARS, productReview.getStars());
bundle.putString(TAG_REVIEW_ID, productReview.getId());
bundle.putString(TAG_FULL_NAME, productReview.getFullName());
bundle.putString(TAG_COMMENT, productReview.getComment());
}
fragment.setArguments(bundle);
return fragment;
}

@Override public Dialog onCreateDialog(Bundle savedInstanceState) {
View contentView;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
if(LoginFragment.isRoboVMAccountEmailValid()){
alertDialogBuilder.setPositiveButton(R.string.dialog_add_review_positive, (dialog, which) -> {
//do nothing here, override onStart to stop the dialog to dismiss when clicking OK
});
contentView = LayoutInflater.from(getActivity()).inflate(R.layout.product_reviews_add,null,false);
this.injectViews(contentView);
this.afterViews();
} else {
contentView = LoginFragment.createInstructions(LayoutInflater.from(getActivity()),null,null);
}
alertDialogBuilder
.setCancelable(false)
.setTitle(R.string.dialog_add_review_title)
.setView(contentView)
.setNegativeButton(R.string.dialog_add_review_negative, (dialog, which) -> {
dismiss();
});
return alertDialogBuilder.create();
}

@Override
public void onStart() {
super.onStart();
final AlertDialog dialog = (AlertDialog) getDialog();
if (dialog != null) {
dialog.setCancelable(false);
if(LoginFragment.isRoboVMAccountEmailValid()) {
Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(v -> {
if (validateForm()) {
mCurrentReview.setFullName(mFullNameEditText.getText().toString());
mCurrentReview.setComment(mCommentEditText.getText().toString());
mCurrentReview.setDate(new Date());
mCurrentReview.setEmail(LoginFragment.ROBOVM_ACCOUNT_EMAIL);
mCurrentReview.setStars((int)mRatingBarView.getRating());
dialog.dismiss();
if(mIsNewReview){
createReview();
} else {
updateReview();
}
}
});
}
}
}

private void createReview(){
EventBus.getDefault().post(new EventCreateReview(mCurrentReview));
}

private void updateReview(){
EventBus.getDefault().post(new EventUpdateReview(mCurrentReview));
}

private void injectViews(View pView){
this.mRatingBarView = ((RatingBar) pView.findViewById(R.id.rating));
this.mFullNameEditText = ((EditText) pView.findViewById(R.id.fullName));
this.mCommentEditText = ((EditText) pView.findViewById(R.id.comment));
}

private void afterViews(){
this.mRatingBarView.setStepSize(1f);
if(getArguments() != null){
this.mRatingBarView.setRating(getArguments().getInt(TAG_STARS, 0));
this.mFullNameEditText.setText(getArguments().getString(TAG_FULL_NAME, ""));
this.mCommentEditText.setText(getArguments().getString(TAG_COMMENT, ""));
this.mCurrentReview.setProductId(getArguments().getString(TAG_PRODUCT_ID, null));
this.mCurrentReview.setId(getArguments().getString(TAG_REVIEW_ID, null));
this.mIsNewReview = getArguments().getBoolean(TAG_NEW_REVIEW, true);
}
}

private boolean validateForm() {
return validateRatingBar(mRatingBarView)
&& validateEditText(mFullNameEditText)
&& validateEditText(mCommentEditText);
}

private boolean validateRatingBar(RatingBar pRatingBarView) {
if(pRatingBarView.getRating() == 0){
toast("Please give your stars rating!");
return false;
}
return true;
}

private boolean validateEditText(EditText pEditText) {
if(pEditText.getText() == null || pEditText.getText().toString().isEmpty()){
toast("Please input your " + pEditText.getHint()+"!");
return false;
}
return true;
}


private void toast(String pMessage){
Toast.makeText(getActivity(), pMessage, Toast.LENGTH_SHORT).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Patterns;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -37,7 +38,7 @@ public class LoginFragment extends Fragment {
// TODO: Enter your RoboVM account email address here
// If you do not have a RoboVM Account please sign up here:
// https://account.robovm.com/#/register
private static final String ROBOVM_ACCOUNT_EMAIL = "";
public static final String ROBOVM_ACCOUNT_EMAIL = "";

private Runnable loginSuccessListener;

Expand All @@ -55,13 +56,19 @@ public void onCreate(Bundle savedInstanceState) {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (ROBOVM_ACCOUNT_EMAIL == null || ROBOVM_ACCOUNT_EMAIL.isEmpty()) {
if (isRoboVMAccountEmailValid()) {
return createInstructions(inflater, container, savedInstanceState);
}
return createLoginView(inflater, container, savedInstanceState);
}

private View createInstructions(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
public static boolean isRoboVMAccountEmailValid(){
return ROBOVM_ACCOUNT_EMAIL != null
&& !ROBOVM_ACCOUNT_EMAIL.isEmpty()
&& Patterns.EMAIL_ADDRESS.matcher(ROBOVM_ACCOUNT_EMAIL).matches();
}

public static View createInstructions(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.prefill_robovm_account_instructions, null);
TextView textView = (TextView) view.findViewById(R.id.codeTextView);
Spanned coloredText = Html.fromHtml(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class ProductDetailsFragment extends Fragment implements ViewTreeObserver
private final Random random = new Random();
private int currentIndex;
private boolean shouldAnimatePop;
private BadgeDrawable basketBadge;
private List<String> images = new ArrayList<>();
private boolean cached;
private int slidingDelta;
Expand Down Expand Up @@ -146,18 +145,6 @@ public void onStop() {
}
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
MenuItem cartItem = menu.findItem(R.id.cart_menu_item);
cartItem.setIcon(basketBadge = new BadgeDrawable(cartItem.getIcon()));

Basket basket = RoboVMWebService.getInstance().getBasket();
basketBadge.setCount(basket.size());
basket.addOnBasketChangeListener(() -> basketBadge.setCountAnimated(basket.size()));
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
if (!enter && shouldAnimatePop) {
Expand Down
Loading