| | |
| | | package com.dayu.pipirrapp.tool; |
| | | |
| | | import static com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread; |
| | | |
| | | import android.content.Context; |
| | | import android.text.TextUtils; |
| | | |
| | | import com.dayu.pipirrapp.adapter.AddPictureAdapter; |
| | | import com.dayu.pipirrapp.bean.net.UplodFileState; |
| | | import com.dayu.pipirrapp.net.ApiManager; |
| | | import com.dayu.pipirrapp.net.BaseResponse; |
| | | import com.dayu.pipirrapp.net.upload.UploadFileListener; |
| | | import com.luck.picture.lib.entity.LocalMedia; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import retrofit2.Call; |
| | |
| | | |
| | | /** |
| | | * 当关闭activity时关闭所有请求 |
| | | * |
| | | * @param uplodFileStates |
| | | */ |
| | | public static void cancelAllCall(Map<String, UplodFileState> uplodFileStates){ |
| | |
| | | |
| | | } |
| | | |
| | | // 更新UI |
| | | public static void updateUI(ArrayList<LocalMedia> result, AddPictureAdapter mAdapter) { |
| | | runOnUiThread(() -> { |
| | | boolean isMaxSize = result.size() == mAdapter.getSelectMax(); |
| | | int oldSize = mAdapter.getData().size(); |
| | | mAdapter.notifyItemRangeRemoved(0, isMaxSize ? oldSize + 1 : oldSize); |
| | | mAdapter.getData().clear(); |
| | | mAdapter.getData().addAll(result); |
| | | mAdapter.notifyItemRangeInserted(0, result.size()); |
| | | }); |
| | | } |
| | | |
| | | public static void cancelRemovedUploads(ArrayList<LocalMedia> result, Map<String, UplodFileState> uplodFileStates) { |
| | | List<String> pathsToRemove = new ArrayList<>(); |
| | | for (UplodFileState uplodFileState : uplodFileStates.values()) { |
| | | String filePath = uplodFileState.getFilePath(); |
| | | // 检查 result 中是否包含该文件路径 |
| | | boolean isInResult = false; |
| | | for (LocalMedia media : result) { |
| | | String compressPath = media.getCompressPath(); |
| | | //判断是否有这个路径,没有的话上传该图片并添加uplodFileStates中 |
| | | if (TextUtils.isEmpty(compressPath)) { |
| | | compressPath = media.getRealPath(); |
| | | } |
| | | if (compressPath.equals(filePath)) { |
| | | isInResult = true; |
| | | break; |
| | | } |
| | | } |
| | | // 如果 result 中没有该路径,则将其加入待移除列表 |
| | | if (!isInResult) { |
| | | try { |
| | | //没有该文件以后取消上传 |
| | | uplodFileState.getThisCall().cancel(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | pathsToRemove.add(filePath); |
| | | } |
| | | } |
| | | for (String removeFile : pathsToRemove) { |
| | | uplodFileStates.remove(removeFile); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param context |
| | | * @param media |
| | | * @param uplodFileStates |
| | | * @param mAdapter |
| | | */ |
| | | public static void creatAndUploadFile(Context context, LocalMedia media, Map<String, UplodFileState> uplodFileStates, AddPictureAdapter mAdapter) { |
| | | String compressPath = media.getCompressPath(); |
| | | //判断是否有这个路径,没有的话上传该图片并添加uplodFileStates中 |
| | | if (TextUtils.isEmpty(compressPath)) { |
| | | compressPath = media.getRealPath(); |
| | | } |
| | | if (!uplodFileStates.containsKey(compressPath)) { |
| | | UplodFileState uplodFileState = new UplodFileState(); |
| | | uplodFileState.setFilePath(compressPath); |
| | | if (compressPath.toLowerCase().endsWith(".mp4") || compressPath.toLowerCase().endsWith(".avi") || compressPath.toLowerCase().endsWith(".mkv") || compressPath.toLowerCase().endsWith(".mov")) { |
| | | // 这是视频文件 |
| | | uplodFileState.setUploadType(UplodFileState.VIDEO_TYPE); |
| | | } else { |
| | | uplodFileState.setUploadType(UplodFileState.IMG_TYPE); |
| | | } |
| | | uplodFileState.setExtName(media.getMimeType()); |
| | | uplodFileStates.put(compressPath, uplodFileState); |
| | | // 执行上传图片的操作 |
| | | FileUploadUtils.uploadFile(context, uplodFileState, uplodFileStates, mAdapter); |
| | | } |
| | | } |
| | | |
| | | } |