Add a review dialog and function

This commit is contained in:
Gilbert Kimutai 2019-03-25 06:32:24 +03:00
parent 53e6f95ab7
commit 0bc203c5db
8 changed files with 439 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import dagger.multibindings.IntoMap;
import me.gilo.wc.utils.ViewModelFactory; import me.gilo.wc.utils.ViewModelFactory;
import me.gilo.wc.viewmodels.CategoryViewModel; import me.gilo.wc.viewmodels.CategoryViewModel;
import me.gilo.wc.viewmodels.ProductViewModel; import me.gilo.wc.viewmodels.ProductViewModel;
import me.gilo.wc.viewmodels.ReviewViewModel;
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
@ -24,6 +25,12 @@ public abstract class ViewModelModule {
@ViewModelKey(CategoryViewModel.class) @ViewModelKey(CategoryViewModel.class)
abstract ViewModel bindCategoryViewModel(CategoryViewModel viewModel); abstract ViewModel bindCategoryViewModel(CategoryViewModel viewModel);
@Binds
@IntoMap
@ViewModelKey(ReviewViewModel.class)
abstract ViewModel bindRevuewViewModel(ReviewViewModel viewModel);
@Binds @Binds
abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory); abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
} }

View File

@ -0,0 +1,21 @@
package me.gilo.wc.events;
import me.gilo.woodroid.models.Product;
import me.gilo.woodroid.models.ProductReview;
public class ReviewEvent {
ProductReview review;
public ReviewEvent(ProductReview review) {
this.review = review;
}
public ProductReview getReview() {
return review;
}
public void setReview(ProductReview review) {
this.review = review;
}
}

View File

@ -0,0 +1,43 @@
package me.gilo.wc.repo;
import me.gilo.wc.common.WooLiveData;
import me.gilo.woodroid.Woocommerce;
import me.gilo.woodroid.models.Product;
import me.gilo.woodroid.models.ProductReview;
import me.gilo.woodroid.models.filters.ProductFilter;
import me.gilo.woodroid.models.filters.ProductReviewFilter;
import javax.inject.Inject;
import java.util.List;
public class ReviewRepository {
@Inject
Woocommerce woocommerce;
@Inject
public ReviewRepository() {
}
public WooLiveData<List<ProductReview>> reviews(int productId) {
final WooLiveData<List<ProductReview>> callBack = new WooLiveData();
ProductReviewFilter filter = new ProductReviewFilter();
int[] products = {productId};
filter.setProduct(products);
woocommerce.ReviewRepository().reviews(filter).enqueue(callBack);
return callBack;
}
public WooLiveData<ProductReview> create(ProductReview review) {
final WooLiveData<ProductReview> callBack = new WooLiveData();
woocommerce.ReviewRepository().create(review).enqueue(callBack);
return callBack;
}
}

View File

@ -0,0 +1,78 @@
package me.gilo.wc.ui.product.section
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.section_add_a_review.*
import me.gilo.wc.R
import me.gilo.wc.events.ReviewEvent
import me.gilo.woodroid.models.ProductReview
import org.greenrobot.eventbus.EventBus
class AddAReviewDialogFragment : DialogFragment() {
var productId = 0
private val ARG_PRODUCT_ID = "productId"
init {
val args = Bundle()
args.putInt(ARG_PRODUCT_ID, productId)
arguments = args
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.section_add_a_review, container)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
productId = arguments!!.getInt(ARG_PRODUCT_ID)
llSave.setOnClickListener{save()}
}
private fun save() {
var productReview = ProductReview()
var review = etReview.text.toString()
var rating = rbRating.rating
var email = etEmail.text.toString()
var name = etName.text.toString()
productReview.product_id = productId
productReview.rating = rating.toInt()
productReview.review = review
productReview.reviewer = name
productReview.reviewer_email = email
productReview.isVerified = true
EventBus.getDefault().post(ReviewEvent(productReview))
dismiss()
}
companion object {
fun newInstance(productId : Int): AddAReviewDialogFragment {
val fragment = AddAReviewDialogFragment()
val args = Bundle()
args.putInt("productId", productId)
fragment.arguments = args
return fragment
}
}
}

View File

@ -12,18 +12,23 @@ import me.gilo.wc.R
import me.gilo.wc.adapter.ProductReviewAdapter import me.gilo.wc.adapter.ProductReviewAdapter
import me.gilo.wc.common.BaseActivity import me.gilo.wc.common.BaseActivity
import me.gilo.wc.common.Status import me.gilo.wc.common.Status
import me.gilo.wc.events.ReviewEvent
import me.gilo.wc.ui.product.ProductActivity import me.gilo.wc.ui.product.ProductActivity
import me.gilo.wc.viewmodels.ProductViewModel import me.gilo.wc.viewmodels.ProductViewModel
import me.gilo.wc.viewmodels.ReviewViewModel
import me.gilo.woodroid.models.ProductReview import me.gilo.woodroid.models.ProductReview
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.* import java.util.*
class ProductReviewsFragment : Fragment() { class ProductReviewsFragment : Fragment() {
lateinit var viewModel: ProductViewModel lateinit var viewModel: ReviewViewModel
val TAG = "ProductReviewsFragment" val TAG = "ProductReviewsFragment"
var productId = 0
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -44,11 +49,13 @@ class ProductReviewsFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewModel = (activity as BaseActivity).getViewModel(ProductViewModel::class.java) viewModel = (activity as BaseActivity).getViewModel(ReviewViewModel::class.java)
var productId = (activity as ProductActivity).intent.getIntExtra("productId", 0) productId = (activity as ProductActivity).intent.getIntExtra("productId", 0)
reviews(productId) reviews(productId)
tvAddAReview.setOnClickListener{addAReviewDialog()}
} }
private fun reviews(productId : Int) { private fun reviews(productId : Int) {
@ -104,4 +111,55 @@ class ProductReviewsFragment : Fragment() {
} }
} }
lateinit var addAReviewFragment: AddAReviewDialogFragment
private fun addAReviewDialog() {
val manager = childFragmentManager
addAReviewFragment = AddAReviewDialogFragment.newInstance(productId)
addAReviewFragment.isCancelable = true
addAReviewFragment.show(manager, "add Review")
}
override fun onStart() {
super.onStart()
EventBus.getDefault().register(this)
}
override fun onStop() {
super.onStop()
EventBus.getDefault().unregister(this)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: ReviewEvent) {
save(event.review)
}
private fun save(review: ProductReview) {
viewModel.create(review).observe(this, android.arch.lifecycle.Observer { response ->
when (response!!.status()) {
Status.LOADING -> {
}
Status.SUCCESS -> {
val reviewsResponse = response.data()
reviews(productId)
}
Status.ERROR -> {
Log.d("Error", response.error().message)
}
Status.EMPTY -> {
}
}
})
}
} }

View File

@ -0,0 +1,36 @@
package me.gilo.wc.viewmodels;
import android.arch.lifecycle.ViewModel;
import android.content.Context;
import me.gilo.wc.common.WooLiveData;
import me.gilo.wc.repo.CartRepository;
import me.gilo.wc.repo.OrderRepository;
import me.gilo.wc.repo.ProductRepository;
import me.gilo.wc.repo.ReviewRepository;
import me.gilo.woodroid.models.LineItem;
import me.gilo.woodroid.models.Product;
import me.gilo.woodroid.models.ProductReview;
import me.gilo.woodroid.models.filters.ProductFilter;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
public final class ReviewViewModel extends ViewModel {
private final ReviewRepository reviewRepository;
@Inject
ReviewViewModel(ReviewRepository reviewRepository) {
this.reviewRepository = reviewRepository;
}
public WooLiveData<List<ProductReview>> reviews(int productId) {
return reviewRepository.reviews(productId);
}
public WooLiveData<ProductReview> create(ProductReview review) {
return reviewRepository.create(review);
}
}

View File

@ -0,0 +1,161 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="360dp"
android:id="@+id/llLoading"
android:gravity="center"
android:background="@drawable/rect_white"
android:orientation="vertical"
tools:ignore="MissingPrefix"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="16dp"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#ffffff"
android:layout_marginRight="16dp"
/>
<TextView
android:id="@+id/tvTitle"
fontPath="@string/font_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Add a review"
android:textColor="#ffffff"
android:textSize="18sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="16dp"
>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rbRating"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#9e9e9e"
android:layout_marginTop="16dp"
android:visibility="visible">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your review"
android:gravity="top"
android:id="@+id/etReview"
android:paddingBottom="16dp"
android:textColor="#5f6368"
android:textColorHint="#9e9e9e"
android:textSize="16sp"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#9e9e9e"
android:id="@+id/tilName"
android:layout_marginTop="16dp"
android:visibility="visible">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/etName"
android:inputType="textPersonName"
android:paddingBottom="16dp"
android:textColor="#5f6368"
android:textColorHint="#9e9e9e"
android:textSize="16sp"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#9e9e9e"
android:layout_marginTop="16dp"
android:visibility="visible">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:id="@+id/etEmail"
android:inputType="textEmailAddress"
android:paddingBottom="16dp"
android:textColor="#5f6368"
android:textColorHint="#9e9e9e"
android:textSize="16sp"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="16dp"
android:id="@+id/llSave"
>
<TextView
fontPath="@string/font_regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Save"
android:textColor="#ffffff"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>

View File

@ -8,18 +8,42 @@
tools:ignore="MissingPrefix" tools:ignore="MissingPrefix"
> >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:gravity="bottom"
>
<TextView <TextView
fontPath="@string/font_medium" fontPath="@string/font_medium"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="left" android:gravity="left"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="Reviews" android:text="Reviews"
android:layout_gravity="center_vertical"
android:textColor="@color/text_black_2" android:textColor="@color/text_black_2"
android:textSize="20sp"/> android:textSize="20sp"/>
<TextView
android:id="@+id/tvAddAReview"
fontPath="@string/font_regular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="left"
android:padding="16dp"
android:text="Add Review"
android:textColor="#1c5c9a"
android:textSize="16sp"/>
</FrameLayout>
<android.support.v7.widget.RecyclerView <android.support.v7.widget.RecyclerView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"