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

commit 支持通过传递商品规格数据的方式,来矫正商品规格的顺序 #19

Open
wants to merge 1 commit 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 app/src/main/java/com/wuhenzhizao/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import com.wuhenzhizao.databinding.ActivityMainBinding;
import com.wuhenzhizao.databinding.LayoutProductShoppingcartBinding;
import com.wuhenzhizao.sku.bean.Sku;
import com.wuhenzhizao.sku.bean.SkuAttrListBean;
import com.wuhenzhizao.titlebar.utils.AppUtils;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -82,7 +84,7 @@ public void showRequestPermissionRationale(int requestCode, Rationale rationale)
private void showSkuDialog() {
if (dialog == null) {
dialog = new ProductSkuDialog(this);
dialog.setData(Product.get(this), new ProductSkuDialog.Callback() {
dialog.setData(Product.get(this), new ArrayList<SkuAttrListBean>(), new ProductSkuDialog.Callback() {
@Override
public void onAdded(Sku sku, int quantity) {
shoppingCartNum += quantity;
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/wuhenzhizao/ProductSkuDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.wuhenzhizao.databinding.DialogProductSkuBinding;
import com.wuhenzhizao.image.GImageLoader;
import com.wuhenzhizao.sku.bean.Sku;
import com.wuhenzhizao.sku.bean.SkuAttrListBean;
import com.wuhenzhizao.sku.bean.SkuAttribute;
import com.wuhenzhizao.sku.view.OnSkuListener;
import com.wuhenzhizao.titlebar.utils.AppUtils;
import com.wuhenzhizao.titlebar.utils.KeyboardConflictCompat;
import com.wuhenzhizao.utils.NumberUtils;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -187,20 +189,20 @@ public void onClick(View v) {
});
}

public void setData(final Product product, Callback callback) {
public void setData(final Product product, ArrayList<SkuAttrListBean> skuAttrList, Callback callback) {
this.product = product;
this.skuList = product.getSkus();
this.callback = callback;

priceFormat = context.getString(R.string.comm_price_format);
stockQuantityFormat = context.getString(R.string.product_detail_sku_stock);

updateSkuData();
updateSkuData(skuAttrList);
updateQuantityOperator(1);
}

private void updateSkuData() {
binding.scrollSkuList.setSkuList(product.getSkus());
private void updateSkuData(ArrayList<SkuAttrListBean> skuAttrList) {
binding.scrollSkuList.setSkuList(product.getSkus(), skuAttrList);

Sku firstSku = product.getSkus().get(0);
if (firstSku.getStockQuantity() > 0) {
Expand Down
13 changes: 10 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

buildscript {
repositories {
jcenter()
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/public/' }
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}

allprojects {
repositories {
jcenter()
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/public/' }
google()
maven { url 'https://jitpack.io' }
mavenCentral()
}
}

Expand Down
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Tue Aug 29 15:05:30 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-all.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.wuhenzhizao.sku.bean;

import java.util.ArrayList;

/**
* 由于之前不支持排序 这里是用于排序的类 保证顺序的数据从这里写入
*/
public class SkuAttrListBean {

private String specName;
private ArrayList<String> specValueList;

public String getSpecName() {
return specName;
}

public void setSpecName(String specName) {
this.specName = specName;
}

public ArrayList<String> getSpecValueList() {
return specValueList;
}

public void setSpecValueList(ArrayList<String> specValueList) {
this.specValueList = specValueList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import android.widget.LinearLayout;

import com.wuhenzhizao.sku.bean.Sku;
import com.wuhenzhizao.sku.bean.SkuAttrListBean;
import com.wuhenzhizao.sku.bean.SkuAttribute;
import com.wuhenzhizao.sku.utils.ViewUtils;
import com.wuhenzhizao.sku.widget.SkuMaxHeightScrollView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -67,14 +69,39 @@ public void setListener(OnSkuListener listener) {
* 绑定sku数据
*
* @param skuList
* @param skuAttrList sku属性列表 从外部整理好顺序后 直接在这里赋值
*/
public void setSkuList(List<Sku> skuList) {
public void setSkuList(List<Sku> skuList, ArrayList<SkuAttrListBean> skuAttrList) {
this.skuList = skuList;
// 清空sku视图
skuContainerLayout.removeAllViews();

// 获取分组的sku集合
Map<String, List<String>> dataMap = getSkuGroupByName(skuList);
// 这里如果skuAttrList有值 则以skuAttrList为准
// 这里加强验证 因为如果传入的数据和sku的数据不匹配 也会引发问题 (sku属性排序问题最多算是显示问题 如果不匹配则会造成闪退问题)
if(skuAttrList != null && skuAttrList.size() > 0 && skuAttrList.size() == dataMap.size()){
// 在转换前先做一次校验
boolean canUse = true;
for(SkuAttrListBean skuAttr: skuAttrList){ // 外层校验
List<String> values = dataMap.get(skuAttr.getSpecName());
if(values != null && values.size() > 0){
for(String value :dataMap.get(skuAttr.getSpecName())){ // 内层校验
if(!values.contains(value)){
canUse = false;
}
}
}else{
canUse = false;
}
}

if(canUse){
dataMap.clear();
dataMap = getSkuAttrListByData(skuAttrList);
}
}

selectedAttributeList = new LinkedList<>();
int index = 0;
for (Iterator<Map.Entry<String, List<String>>> it = dataMap.entrySet().iterator(); it.hasNext(); ) {
Expand Down Expand Up @@ -131,6 +158,19 @@ private Map<String, List<String>> getSkuGroupByName(List<Sku> list) {
return dataMap;
}

/**
* 通过返回的数据 进行数据转换 (同上 数据源不同)
* @param skuAttrList sku数据
* @return 转换后的map
*/
private Map<String, List<String>> getSkuAttrListByData(ArrayList<SkuAttrListBean> skuAttrList){
Map<String, List<String>> dataMap = new LinkedHashMap<>();
for(SkuAttrListBean skuAttr: skuAttrList){
dataMap.put(skuAttr.getSpecName(), skuAttr.getSpecValueList());
}
return dataMap;
}

/**
* 重置所有属性的选中状态
*/
Expand Down
9 changes: 3 additions & 6 deletions local.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Aug 29 14:58:51 CST 2017
sdk.dir=/Users/liufei/Library/Android/sdk
#Mon Nov 22 11:34:35 CST 2021
sdk.dir=/Users/jieyu/Library/Android/sdk
bintray.user=wuhenzhizao
bintray.apikey=91c6f68324df28268c19d8e34b68f20e7c977f22