1. 阐明
存储池分区是在某个存储池建设的分区,是创立文件的根基之一,因为文件夹的创立是建设在存储池和存储池分区的根底上,能够说存储池和存储池分区时文件夹 / 文件的载体。
解决零碎存储池下的存储池分区之外,咱们都能够对存储池分区进行增删改操作和不肯定能够操作胜利。
同样,增加、编辑和删除存储池须要肯定工夫,所以存储池分区存在增加中、批改中、删除中、增加失败、批改失败和删除失败的状态。
如果增加或者批改失败,咱们能够重试或者勾销增加和批改;
如果删除失败的话,咱们只能进行重试操作。
增加 / 编辑存储池分区时,容量单位只能抉择 MB、GB 和 TB,在单位抉择 MB 时,容量必须时 4 的倍数。
注:因为存储池分区在存储池的子级,所以存储池分区也是只有家庭拥有者能力操作
2. 次要代码实现
2.1 存储池分区实体类
/**
* 硬盘和存储池分区
*/
public class DiskBean implements Serializable {
/**
* id : 4255
* name : 安但风张义
* capacity : 91.2
*/
private String id;
private String name;
private long capacity;
private long use_capacity; // 已用容量
private boolean selected;
/**
* 为空则没有工作状态,
* TaskAddPartition_1 增加存储池分区中,TaskAddPartition_0 增加存储池分区失败,
* TaskUpdatePartition_1 批改存储池分区中,TaskUpdatePartition_0 批改存储池分区失败,
* TaskDelPartition_1 删除存储池分区中,TaskDelPartition_0 删除存储池分区失败
*/
private String status;
private String task_id; // 异步工作 id
...
}
3. 存储池分区列表适配器
/**
* 存储池详情的存储池分区列表
*/
public class StoragePoolDetailAdapter extends BaseQuickAdapter<DiskBean, BaseViewHolder> {public StoragePoolDetailAdapter() {super(R.layout.item_storage_pool_detail);
}
@Override
protected void convert(BaseViewHolder helper, DiskBean item) {helper.addOnClickListener(R.id.tvRetry);
helper.addOnClickListener(R.id.tvCancel);
String capacity = FileUtil.getReadableFileSize(item.getUse_capacity())+"/"+FileUtil.getReadableFileSize(item.getCapacity());
String allCapacity = UiUtil.getString(R.string.mine_all_size)+FileUtil.getReadableFileSizeSaveTow(item.getCapacity());
String availableCapacity = UiUtil.getString(R.string.mine_available_capacity)+FileUtil.getReadableFileSizeSaveTow(item.getCapacity() - item.getUse_capacity());
helper.setText(R.id.tvName, item.getName())
.setText(R.id.tvAllSize, allCapacity)
.setText(R.id.tvAvailable, availableCapacity);
ProgressBar rb = helper.getView(R.id.rb);
int progress = item.getCapacity() == 0 ? 100 : (int) (StringUtil.long2Double(item.getUse_capacity())/StringUtil.long2Double(item.getCapacity())*100);
rb.setProgress(progress);
rb.setVisibility(View.VISIBLE);
ImageView ivDot = helper.getView(R.id.ivDot);
LinearLayout llResult = helper.getView(R.id.llResult);
TextView tvRetry= helper.getView(R.id.tvRetry);
TextView tvCancel = helper.getView(R.id.tvCancel);
TextView tvTips = helper.getView(R.id.tvTips);
ImageView ivStatus = helper.getView(R.id.ivStatus);
llResult.setVisibility(View.GONE);
ivStatus.setVisibility(View.GONE);
tvCancel.setVisibility(View.GONE);
String status = item.getStatus();
ivDot.setVisibility(TextUtils.isEmpty(status) ? View.VISIBLE : View.GONE);
if (!TextUtils.isEmpty(status)){switch (status){
case Constant.PARTITION_ADDING: // 增加中
case Constant.PARTITION_UPDATING: // 批改中
case Constant.PARTITION_DELETING: // 删除中
ivStatus.setVisibility(View.VISIBLE);
llResult.setVisibility(View.GONE);
if (status.equals(Constant.PARTITION_DELETING)) { // 删除中
ivStatus.setImageResource(R.drawable.icon_folder_deleting);
}else if (status.equals(Constant.PARTITION_ADDING)) { // 增加中
ivStatus.setImageResource(R.drawable.icon_adding);
}else if (status.equals(Constant.PARTITION_UPDATING)) { // 批改中
ivStatus.setImageResource(R.drawable.icon_folder_updating);
}
break;
case Constant.PARTITION_ADD_FAIL: // 增加失败
case Constant.PARTITION_UPDATE_FAIL: // 批改失败
case Constant.PARTITION_DELETE_FAIL: // 删除失败
ivStatus.setVisibility(View.GONE);
tvRetry.setVisibility(View.VISIBLE);
llResult.setVisibility(View.VISIBLE);
if (status.equals(Constant.PARTITION_DELETE_FAIL)){ // 删除状态
tvCancel.setVisibility(View.GONE);
tvTips.setText(UiUtil.getString(R.string.mine_partition_del_fail));
}else {tvCancel.setVisibility(View.VISIBLE);
rb.setVisibility(View.GONE);
if (status.equals(Constant.PARTITION_ADD_FAIL)){ // 增加失败
tvCancel.setText(UiUtil.getString(R.string.mine_cancel_add));
tvTips.setText(UiUtil.getString(R.string.mine_partition_add_fail));
}else if (status.equals(Constant.PARTITION_UPDATE_FAIL)){ // 批改失败
tvCancel.setText(UiUtil.getString(R.string.mine_cancel_update));
tvTips.setText(UiUtil.getString(R.string.mine_partition_update_fail));
}
}
break;
}
}
}
}
4. 增加、编辑和存储池分区
/**
* 增加 / 编辑分区
*/
public class AddPartitionActivity extends BaseMVPDBActivity<ActivityAddPartitionBinding, AddPartitionContract.View, AddPartitionPresenter> implements AddPartitionContract.View{
...
/**
* 删除确认弹窗
*/
private void showRemoveDialog(){CenterAlertDialog centerAlertDialog = CenterAlertDialog.getInstance(UiUtil.getString(R.string.mine_remove_confirm), UiUtil.getString(R.string.mine_remove_partition_content),
UiUtil.getString(R.string.mine_remove_tips), R.color.color_ff0000, "", UiUtil.getString(R.string.mine_sure_remove));
centerAlertDialog.setConfirmListener(new CenterAlertDialog.OnConfirmListener() {
@Override
public void onConfirm() {PoolNameRequest poolNameRequest = new PoolNameRequest(poolName);
mPresenter.removePartition(Constant.scope_token, diskBean.getName(), poolNameRequest);
centerAlertDialog.dismiss();}
});
centerAlertDialog.show(this);
}
...
/**
* 点击事件
*/
public class OnClickHandler {public void onClick(View view) {int viewId = view.getId();
if (viewId == R.id.ivBack) { // 返回
finish();}else if (viewId == R.id.tvDel){ // 删除
showRemoveDialog();}else if (viewId == R.id.tvUnit){ // 单位
if (bottomListDialog!=null && !bottomListDialog.isShowing()){bottomListDialog.show(AddPartitionActivity.this);
}
}else if (viewId == R.id.tvSave){ // 保留
capacityStr = unitIsMB() ? binding.tvResult.getText().toString().trim() : binding.etCapacity.getText().toString().trim();
long capacity = Long.parseLong(capacityStr);
if (diskBean == null) { // 增加
AddPartitionRequest addPartitionRequest = new AddPartitionRequest(binding.etName.getText().toString(), capacity, defaultUnit, poolName);
mPresenter.addPartition(Constant.scope_token, addPartitionRequest);
}else { // 更新
ModifyPartitionRequest modifyPartitionRequest = new ModifyPartitionRequest(binding.etName.getText().toString(), capacity, defaultUnit, poolName);
mPresenter.modifyPartition(Constant.scope_token, diskBean.getName(), modifyPartitionRequest);
}
}
}
}
}
/**
* 增加分区
*/
public class AddPartitionPresenter extends BasePresenter<AddPartitionModel, AddPartitionContract.View> implements AddPartitionContract.Presenter {
@Override
public AddPartitionModel createModel() {return new AddPartitionModel();
}
/**
* 增加分区
* @param scopeToken
* @param addPartitionRequest
*/
@Override
public void addPartition(String scopeToken, AddPartitionRequest addPartitionRequest) {executeObservable(mModel.addPartition(scopeToken, addPartitionRequest), new RequestDataCallback<Object>() {
@Override
public void onSuccess(Object response) {super.onSuccess(response);
if (mView!=null){mView.addPartitionSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {super.onFailed(errorCode, errorMessage);
if (mView!=null){mView.addPartitionFail(errorCode, errorMessage);
}
}
});
}
/**
* 编辑 存储池分区
* @param scopeToken
* @param name
* @param modifyPartitionRequest
*/
@Override
public void modifyPartition(String scopeToken, String name, ModifyPartitionRequest modifyPartitionRequest) {executeObservable(mModel.modifyPartition(scopeToken, name, modifyPartitionRequest), new RequestDataCallback<Object>() {
@Override
public void onSuccess(Object response) {super.onSuccess(response);
if (mView!=null){mView.modifyPartitionSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {super.onFailed(errorCode, errorMessage);
if (mView!=null){mView.modifyPartitionFail(errorCode, errorMessage);
}
}
});
}
/**
* 删除 存储池分区
* @param scopeToken
* @param name
*/
@Override
public void removePartition(String scopeToken, String name, PoolNameRequest poolNameRequest) {executeObservable(mModel.removePartition(scopeToken, name, poolNameRequest), new RequestDataCallback<Object>() {
@Override
public void onSuccess(Object response) {super.onSuccess(response);
if (mView!=null){mView.removePartitionSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {super.onFailed(errorCode, errorMessage);
if (mView!=null){mView.removePartitionFail(errorCode, errorMessage);
}
}
});
}
}