215 lines
8.1 KiB
Java
215 lines
8.1 KiB
Java
package com.uiuios.aios.activity;
|
|
|
|
import android.content.Intent;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
|
|
import androidx.databinding.DataBindingUtil;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.google.android.material.tabs.TabLayout;
|
|
import com.trello.rxlifecycle4.RxLifecycle;
|
|
import com.trello.rxlifecycle4.android.ActivityEvent;
|
|
import com.uiuios.aios.R;
|
|
import com.uiuios.aios.adapter.GoodsListAdapter;
|
|
import com.uiuios.aios.base.BaseDataBindingActivity;
|
|
import com.uiuios.aios.bean.BaseResponse;
|
|
import com.uiuios.aios.bean.GoodsInfo;
|
|
import com.uiuios.aios.bean.GoodsList;
|
|
import com.uiuios.aios.bean.GoodsType;
|
|
import com.uiuios.aios.databinding.ActivityGoodsBinding;
|
|
import com.uiuios.aios.network.NetInterfaceManager;
|
|
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
import io.reactivex.rxjava3.annotations.NonNull;
|
|
import io.reactivex.rxjava3.core.Observer;
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
|
|
|
public class GoodsActivity extends BaseDataBindingActivity {
|
|
private static final String TAG = GoodsActivity.class.getSimpleName();
|
|
|
|
private ActivityGoodsBinding mBinding;
|
|
|
|
private RecyclerView mRecyclerView;
|
|
private GoodsListAdapter mGoodsAdapter;
|
|
|
|
private HashMap<String, GoodsType> mGoodsTypeMap;
|
|
|
|
|
|
@Override
|
|
public boolean setNightMode() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
protected void initDataBinding() {
|
|
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_goods);
|
|
}
|
|
|
|
/**
|
|
* 初始化视图
|
|
*/
|
|
@Override
|
|
public void initView() {
|
|
mBinding.setListener(new Listener());
|
|
// UltimateBarX.statusBarOnly(this)
|
|
// .transparent()
|
|
// .apply();
|
|
UltimateBarX.addStatusBarTopPadding(mBinding.constraintLayout);
|
|
mRecyclerView = mBinding.recyclerView;
|
|
mGoodsAdapter = new GoodsListAdapter();
|
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
|
mRecyclerView.setAdapter(mGoodsAdapter);
|
|
|
|
mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
|
|
@Override
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
Log.e(TAG, "onTabSelected: " + tab.getText());
|
|
if (tab.getText() != null && !TextUtils.isEmpty(tab.getText())) {
|
|
String text = tab.getText().toString();
|
|
if (mGoodsTypeMap.get(text) != null) {
|
|
getAllGoods(mGoodsTypeMap.get(text).getId());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTabUnselected(TabLayout.Tab tab) {
|
|
Log.e(TAG, "onTabUnselected: " + tab.getText());
|
|
}
|
|
|
|
@Override
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
Log.e(TAG, "onTabReselected: " + tab.getText());
|
|
}
|
|
});
|
|
mBinding.tvOrder.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
startActivity(new Intent(GoodsActivity.this, OrderListActivity.class));
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 初始化数据
|
|
*/
|
|
@Override
|
|
public void initData() {
|
|
getGoodsType();
|
|
}
|
|
|
|
private void getGoodsType() {
|
|
NetInterfaceManager.getInstance()
|
|
.getGoodsTypeListObservable()
|
|
.compose(RxLifecycle.bindUntilEvent(getLifecycleSubject(), ActivityEvent.DESTROY))
|
|
.subscribe(new Observer<BaseResponse<List<GoodsType>>>() {
|
|
@Override
|
|
public void onSubscribe(@NonNull Disposable d) {
|
|
Log.e("getGoodsType", "onSubscribe: ");
|
|
}
|
|
|
|
@Override
|
|
public void onNext(@NonNull BaseResponse<List<GoodsType>> listBaseResponse) {
|
|
Log.e("getGoodsType", "onNext: " + listBaseResponse);
|
|
if (listBaseResponse.code == 200) {
|
|
mBinding.tabLayout.setVisibility(View.VISIBLE);
|
|
List<GoodsType> goodsTypeList = listBaseResponse.data;
|
|
List<String> typeString = goodsTypeList.stream().map(GoodsType::getName).collect(Collectors.toList());
|
|
String[] type = new String[goodsTypeList.size()];
|
|
type = typeString.toArray(type);
|
|
mGoodsTypeMap = new HashMap<>();
|
|
for (int i = 0; i < type.length; i++) {
|
|
mGoodsTypeMap.put(type[i], goodsTypeList.get(i));
|
|
TabLayout.Tab tab = mBinding.tabLayout.newTab();//关键的创建一个Tab,注意这里使用的是已经实例的mTablayout创建的Tab,很容易疏忽使用new Tablayout().new Tab()的方式创建,这个是会报错的.
|
|
tab.setText(type[i]);
|
|
if (i == 0) {
|
|
mBinding.tabLayout.addTab(tab, 0, true);//设置选择的item
|
|
} else {
|
|
mBinding.tabLayout.addTab(tab);
|
|
}
|
|
}
|
|
getAllGoods(goodsTypeList.get(0).getId());
|
|
} else {
|
|
mBinding.tabLayout.setVisibility(View.GONE);
|
|
getAllGoods();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(@NonNull Throwable e) {
|
|
Log.e("getGoodsType", "onError: " + e.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
Log.e("getGoodsType", "onComplete: ");
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
private void getAllGoods(int type) {
|
|
NetInterfaceManager.getInstance()
|
|
.getGoodsListObservable(type)
|
|
.compose(RxLifecycle.bindUntilEvent(getLifecycleSubject(), ActivityEvent.DESTROY))
|
|
.subscribe(getGoodsListObservable());
|
|
}
|
|
|
|
|
|
private void getAllGoods() {
|
|
NetInterfaceManager.getInstance()
|
|
.getGoodsListObservable()
|
|
.compose(RxLifecycle.bindUntilEvent(getLifecycleSubject(), ActivityEvent.DESTROY))
|
|
.subscribe(getGoodsListObservable());
|
|
}
|
|
|
|
private Observer<BaseResponse<GoodsList>> getGoodsListObservable() {
|
|
return new Observer<BaseResponse<GoodsList>>() {
|
|
@Override
|
|
public void onSubscribe(@NonNull Disposable d) {
|
|
Log.e("getAllGoods", "onSubscribe: ");
|
|
}
|
|
|
|
@Override
|
|
public void onNext(@NonNull BaseResponse<GoodsList> listBaseResponse) {
|
|
Log.e("getAllGoods", "onNext: " + listBaseResponse);
|
|
if (listBaseResponse.code == 200) {
|
|
GoodsList goodsList = listBaseResponse.data;
|
|
List<GoodsInfo> goodsInfos = goodsList.getData();
|
|
goodsInfos.sort((o1, o2) -> Integer.compare(o2.getWeight(), o1.getWeight()));
|
|
mGoodsAdapter.setGoodsInfoList(goodsInfos);
|
|
mRecyclerView.setVisibility(View.VISIBLE);
|
|
mBinding.clNodata.setVisibility(View.GONE);
|
|
} else {
|
|
mRecyclerView.setVisibility(View.GONE);
|
|
mBinding.clNodata.setVisibility(View.VISIBLE);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(@NonNull Throwable e) {
|
|
Log.e("getAllGoods", "onError: " + e.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
Log.e("getAllGoods", "onComplete: ");
|
|
}
|
|
};
|
|
}
|
|
|
|
public class Listener {
|
|
public void back(View view) {
|
|
finish();
|
|
}
|
|
}
|
|
}
|