纯蓝牙通信,个人中心相关。
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
package com.ckkj.griddle.mine;
|
||||
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.DefaultItemAnimator;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
|
||||
import com.ckkj.griddle.R;
|
||||
import com.ckkj.griddle.base.BaseActivity;
|
||||
import com.ckkj.griddle.base.BaseFragment;
|
||||
import com.ckkj.griddle.databinding.ShippingAddressLayoutBinding;
|
||||
import com.ckkj.griddle.mine.adapter.ShippingAddressAdapter;
|
||||
import com.ckkj.griddle.mine.entity.ShippingAddressEntity;
|
||||
import com.ckkj.griddle.mine.mvp.MineContract;
|
||||
import com.ckkj.griddle.mine.mvp.MineImpl;
|
||||
import com.ckkj.griddle.mine.mvp.MinePresenter;
|
||||
import com.ckkj.griddle.network.BaseBean;
|
||||
import com.gyf.immersionbar.ImmersionBar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
|
||||
import butterknife.OnClick;
|
||||
|
||||
/** 收货地址管理;本轮使用页面内演示数据,不写入账号资料或服务器。 */
|
||||
public class ShippingAddressActivity
|
||||
extends BaseActivity<ShippingAddressLayoutBinding, MinePresenter>
|
||||
implements MineContract.MineView {
|
||||
|
||||
private static final String STATE_ADDRESSES = "shipping_addresses";
|
||||
private ArrayList<ShippingAddressEntity> addresses = new ArrayList<>();
|
||||
private ShippingAddressAdapter adapter;
|
||||
|
||||
/** 新增和编辑共用返回回调,以稳定 ID 更新对应条目。 */
|
||||
private final ActivityResultLauncher<android.content.Intent> editLauncher =
|
||||
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
|
||||
if (result.getResultCode() != RESULT_OK || result.getData() == null) {
|
||||
return;
|
||||
}
|
||||
ShippingAddressEntity address = (ShippingAddressEntity) result.getData()
|
||||
.getSerializableExtra(AddressEditActivity.EXTRA_ADDRESS);
|
||||
if (address != null) {
|
||||
applyAddress(address);
|
||||
}
|
||||
});
|
||||
|
||||
@Override
|
||||
public MinePresenter createPresenter() {
|
||||
return new MinePresenter(new MineImpl());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLayoutResId(Bundle savedInstanceState) {
|
||||
return R.layout.shipping_address_layout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initData(Bundle savedInstanceState) {
|
||||
super.initData(savedInstanceState);
|
||||
ImmersionBar.with(this).statusBarColor(R.color.personal_info_background)
|
||||
.statusBarDarkFont(true).init();
|
||||
viewBinding.includeTitle.tvTitle.setTypeface(
|
||||
viewBinding.includeTitle.tvTitle.getTypeface(), Typeface.BOLD);
|
||||
if (savedInstanceState == null) {
|
||||
initPreviewAddresses();
|
||||
} else {
|
||||
ArrayList<ShippingAddressEntity> restored = (ArrayList<ShippingAddressEntity>)
|
||||
savedInstanceState.getSerializable(STATE_ADDRESSES);
|
||||
if (restored != null) {
|
||||
addresses = restored;
|
||||
}
|
||||
}
|
||||
adapter = new ShippingAddressAdapter();
|
||||
viewBinding.rvAddresses.setLayoutManager(new LinearLayoutManager(this));
|
||||
// 开启移除和位移动画,删除地址时其余卡片平滑补位。
|
||||
viewBinding.rvAddresses.setItemAnimator(new DefaultItemAnimator());
|
||||
viewBinding.rvAddresses.setAdapter(adapter);
|
||||
adapter.setOnItemChildClickListener((baseAdapter, view, position) -> {
|
||||
ShippingAddressEntity address = adapter.getItem(position);
|
||||
if (address == null) {
|
||||
return;
|
||||
}
|
||||
switch (view.getId()) {
|
||||
case R.id.tv_address_edit:
|
||||
openEditor(address);
|
||||
break;
|
||||
|
||||
case R.id.tv_address_delete:
|
||||
confirmDelete(address);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
renderAddresses();
|
||||
}
|
||||
|
||||
/** 仅用于还原设计稿的演示地址;默认标记始终只保留一个。 */
|
||||
private void initPreviewAddresses() {
|
||||
ShippingAddressEntity first = new ShippingAddressEntity();
|
||||
first.recipient = "陈晓";
|
||||
first.mobile = "13800138821";
|
||||
first.region = "上海市 浦东新区";
|
||||
first.detail = "张江路88号3号楼902室";
|
||||
first.defaultAddress = true;
|
||||
addresses.add(first);
|
||||
ShippingAddressEntity second = new ShippingAddressEntity();
|
||||
second.recipient = "陈晓";
|
||||
second.mobile = "13000009897";
|
||||
second.region = "上海市 徐汇区";
|
||||
second.detail = "漕溪北路88号2号楼601室";
|
||||
addresses.add(second);
|
||||
}
|
||||
|
||||
@OnClick({R.id.img_back, R.id.tv_address_add})
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()) {
|
||||
case R.id.img_back:
|
||||
finish();
|
||||
break;
|
||||
|
||||
case R.id.tv_address_add:
|
||||
openEditor(null);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void openEditor(ShippingAddressEntity address) {
|
||||
Bundle bundle = new Bundle();
|
||||
if (address != null) {
|
||||
bundle.putSerializable(AddressEditActivity.EXTRA_ADDRESS, address.copy());
|
||||
}
|
||||
goTo(mContext, AddressEditActivity.class, bundle, editLauncher);
|
||||
}
|
||||
|
||||
/** 编辑保留原 ID,新增追加;选为默认后取消其他地址的默认标记。 */
|
||||
private void applyAddress(ShippingAddressEntity address) {
|
||||
int existing = -1;
|
||||
for (int index = 0; index < addresses.size(); index++) {
|
||||
ShippingAddressEntity item = addresses.get(index);
|
||||
if (item.id.equals(address.id)) {
|
||||
existing = index;
|
||||
}
|
||||
if (address.defaultAddress) {
|
||||
item.defaultAddress = false;
|
||||
}
|
||||
}
|
||||
if (existing >= 0) {
|
||||
addresses.set(existing, address);
|
||||
} else {
|
||||
addresses.add(address);
|
||||
}
|
||||
renderAddresses();
|
||||
}
|
||||
|
||||
private void confirmDelete(ShippingAddressEntity address) {
|
||||
if (dialog != null && dialog.isShowing()) {
|
||||
return;
|
||||
}
|
||||
// 复用权限说明使用的通用弹窗,仅替换标题、内容和按钮文案。
|
||||
showDialogtext(null, false, false, false,
|
||||
getString(R.string.address_delete), getString(R.string.address_delete_confirm),
|
||||
getString(android.R.string.cancel), getString(R.string.address_delete),
|
||||
new BaseFragment.OnDialogClick() {
|
||||
@Override
|
||||
public void onCancelClick() {
|
||||
// 取消时保留原地址。
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfirmClick() {
|
||||
removeAddress(address.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** 确认时按 ID 重新定位,避免弹窗期间列表变化导致误删其他条目。 */
|
||||
private void removeAddress(String addressId) {
|
||||
for (int index = 0; index < adapter.getData().size(); index++) {
|
||||
ShippingAddressEntity item = adapter.getItem(index);
|
||||
if (item != null && item.id.equals(addressId)) {
|
||||
addresses.remove(item);
|
||||
// 仅移除对应卡片,不全量刷新,保留 RecyclerView 的删除动画。
|
||||
adapter.getData().remove(index);
|
||||
adapter.notifyItemRemoved(index);
|
||||
updateAddressSummary();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void renderAddresses() {
|
||||
addresses.sort(Comparator.comparing(address -> !address.defaultAddress));
|
||||
adapter.setNewData(new ArrayList<>(addresses));
|
||||
updateAddressSummary();
|
||||
}
|
||||
|
||||
/** 独立更新数量和空状态,避免删除后重新提交整份列表打断动画。 */
|
||||
private void updateAddressSummary() {
|
||||
viewBinding.tvAddressSummary.setText(getString(R.string.address_summary, addresses.size()));
|
||||
viewBinding.tvAddressEmpty.setVisibility(addresses.isEmpty() ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
outState.putSerializable(STATE_ADDRESSES, addresses);
|
||||
super.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (dialog != null) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
viewBinding.rvAddresses.setAdapter(null);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showError(String message) {
|
||||
// 当前为页面内交互,尚未发起地址网络请求。
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logOut(BaseBean bean) {
|
||||
// 登录失效沿用父类统一处理。
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user