parent
							
								
									a4db733512
								
							
						
					
					
						commit
						56064ea46a
					
				
				 3 changed files with 276 additions and 11 deletions
			
			
		| @ -0,0 +1,265 @@ | ||||
| package com.bingce.controlnetwork.util; | ||||
| 
 | ||||
| import android.app.Activity; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.text.TextUtils; | ||||
| 
 | ||||
| import androidx.activity.result.ActivityResultLauncher; | ||||
| import androidx.activity.result.contract.ActivityResultContracts; | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.WorkerThread; | ||||
| import androidx.fragment.app.Fragment; | ||||
| 
 | ||||
| import com.afollestad.materialdialogs.MaterialDialog; | ||||
| import com.bingce.BaseApp; | ||||
| import com.bingce.controlapphelper.R; | ||||
| import com.bingce.controlapphelper.datasource.database.SurveyorDatabaseFactory; | ||||
| import com.bingce.controlapphelper.datasource.database.point.PointRecord; | ||||
| import com.bingce.controlapphelper.util.CheckStorageUtil; | ||||
| import com.bingce.controlapphelper.util.importpoint.PointLibraryUtil; | ||||
| import com.bingce.surveyor.agentweb.AgentWebActivity; | ||||
| import com.bingce.utils.ThreadPoolUtil; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import blankj.utilcode.util.ToastUtils; | ||||
| import blankj.utilcode.util.Utils; | ||||
| import ru.bartwell.exfilepicker.ExFilePicker; | ||||
| import ru.bartwell.exfilepicker.data.ExFilePickerResult; | ||||
| 
 | ||||
| public class ImportPointsUtilPla { | ||||
|     private static final int THEME = android.R.style.Theme_Material_Dialog_Alert; | ||||
|     //    private static final int THEME = android.R.style.ThemeOverlay_Material_Dialog_Alert;
 | ||||
|     @NonNull | ||||
|     private final Fragment fragment; | ||||
|     @NonNull | ||||
|     private final ActivityResultLauncher<Intent> activityResultLauncher; | ||||
|     private String projectId; | ||||
|     private String pointType; | ||||
|     private Context mContext; | ||||
|     private ImportFileType importFileType; | ||||
| 
 | ||||
|     public ImportPointsUtilPla(@NonNull Fragment fragment) { | ||||
|         this.fragment = fragment; | ||||
|         activityResultLauncher = fragment.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { | ||||
|             if (result != null && Activity.RESULT_OK == result.getResultCode()) { | ||||
|                 Intent data = result.getData(); | ||||
|                 if (data != null) { | ||||
|                     ExFilePickerResult object = ExFilePickerResult.getFromIntent(data); | ||||
|                     if (object != null && object.getCount() > 0) { | ||||
|                         try { | ||||
|                             String path = object.getPath() + object.getNames().get(0); | ||||
|                             List<PointRecord> list = new ArrayList<>(); | ||||
|                             String error = null; | ||||
|                             switch (importFileType) { | ||||
|                                 case EXCLE: | ||||
|                                     error = PointLibraryUtil.importStakingJobPointExcel(new File(path), | ||||
|                                             (name, code, x, y, z, remarks) -> addPointToList(list, projectId, pointType, name, code, x, y, z, remarks, list.size())); | ||||
|                                     break; | ||||
|                                 case TEXT: | ||||
|                                     error = PointLibraryUtil.importStakingJobPointTxt(new File(path), | ||||
|                                             (name, code, x, y, z, remarks) -> addPointToList(list, projectId, pointType, name, code, x, y, z, remarks, list.size())); | ||||
|                                     break; | ||||
|                                 case CASS: | ||||
|                                     error = PointLibraryUtil.importStakingJobPointCassDat(new File(path), isCassXY(), | ||||
|                                             (name, code, x, y, z, remarks) -> addPointToList(list, projectId, pointType, name, code, x, y, z, remarks, list.size())); | ||||
|                                     break; | ||||
|                             } | ||||
|                             if (!TextUtils.isEmpty(error)) { | ||||
|                                 ToastUtils.showShort(error); | ||||
|                             } | ||||
|                             if (!list.isEmpty()) { | ||||
|                                 saveRecord(list); | ||||
|                             } | ||||
|                         } catch (Exception exception) { | ||||
|                             exception.printStackTrace(); | ||||
|                         } | ||||
|                     } else { | ||||
|                         ToastUtils.showShort(R.string.file_is_empty); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 存储数据 | ||||
|      * 查询是否有同名点 有同名提示是否覆盖点坐标信息,没有同名直接保存 | ||||
|      * | ||||
|      * @param list 读取的导入点列表 | ||||
|      */ | ||||
|     private void saveRecord(List<PointRecord> list) { | ||||
|         Object waitObj = new Object(); | ||||
|         ThreadPoolUtil.execute(() -> { | ||||
|             //获取已有的数据点
 | ||||
|             List<PointRecord> pointListAgo = SurveyorDatabaseFactory.instance.getPointDataSource().getListByProjectId(projectId, pointType); | ||||
|             Map<String, PointRecord> pointRecordAgoMap = new HashMap<>(); | ||||
|             for (PointRecord pointRecordAgo : pointListAgo) { | ||||
|                 pointRecordAgoMap.put(pointRecordAgo.getName(), pointRecordAgo); | ||||
|             } | ||||
|             //遍历要导入的数据点
 | ||||
|             for (PointRecord pointRecordImport : list) { | ||||
|                 if (pointRecordAgoMap.containsKey(pointRecordImport.getName())) { | ||||
|                     //存在同名 提示是否覆盖
 | ||||
|                     showOverrideDialog(waitObj, pointRecordAgoMap.get(pointRecordImport.getName()), pointRecordImport); | ||||
|                     synchronized (waitObj) { | ||||
|                         try { | ||||
|                             waitObj.wait(); | ||||
|                         } catch (InterruptedException e) { | ||||
|                             e.printStackTrace(); | ||||
|                         } | ||||
|                     } | ||||
|                 } else { | ||||
|                     savePointRecord(pointRecordImport); | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     @WorkerThread | ||||
|     private void savePointRecord(PointRecord pointRecord) { | ||||
|         SurveyorDatabaseFactory.instance.getPointDataSource().saveSync(pointRecord); | ||||
|     } | ||||
| 
 | ||||
|     private void showOverrideDialog(Object waitObj, PointRecord pointRecordAgo, PointRecord pointImport) { | ||||
|         ThreadPoolUtil.executeInMain(() -> { | ||||
|             new MaterialDialog.Builder(mContext) | ||||
|                     .title("提示") | ||||
|                     .content("点名:" + pointRecordAgo.getName() + "重复,是否覆盖?") | ||||
|                     .positiveText("覆盖") | ||||
|                     .negativeText("不覆盖") | ||||
|                     .neutralText("取消") | ||||
|                     .onPositive((dialog, which) -> { | ||||
|                         ThreadPoolUtil.execute(() -> { | ||||
|                             synchronized (waitObj) { | ||||
|                                 pointRecordAgo.x = pointImport.x; | ||||
|                                 pointRecordAgo.y = pointImport.y; | ||||
|                                 pointRecordAgo.z = pointImport.z; | ||||
|                                 pointRecordAgo.remarks = pointImport.remarks; | ||||
|                                 pointRecordAgo.code = pointImport.code; | ||||
|                                 pointRecordAgo.category = pointImport.category; | ||||
|                                 savePointRecord(pointRecordAgo); | ||||
|                                 waitObj.notify(); | ||||
|                             } | ||||
|                         }); | ||||
|                     }) | ||||
|                     .onNegative((dialog, which) -> { | ||||
|                         synchronized (waitObj) { | ||||
|                             waitObj.notify(); | ||||
|                         } | ||||
|                     }) | ||||
|                     .show(); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     private void addPointToList(List<PointRecord> list, String projectId, String pointType, String name, String code, double x, double y, double z, String remarks, int orderIndex) { | ||||
|         PointRecord point = PointRecord.point( | ||||
|                 projectId, pointType, name, code, | ||||
|                 x, y, z, | ||||
|                 remarks, list.size()); | ||||
|         list.add(point); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 入口 | ||||
|      * | ||||
|      * @param projectId | ||||
|      * @param pointType | ||||
|      */ | ||||
|     public void showImportDialog(String projectId, String pointType) { | ||||
|         this.projectId = projectId; | ||||
|         this.pointType = pointType; | ||||
|         mContext = fragment.requireContext(); | ||||
| 
 | ||||
|         importFileType = null; | ||||
| 
 | ||||
|         new MaterialDialog.Builder(mContext) | ||||
|                 .title(R.string.please_choose_import_format) | ||||
|                 .items(new String[]{mContext.getString(R.string.excel_xls) | ||||
|                         , mContext.getString(R.string.text_txt) | ||||
|                         , mContext.getString(R.string.cass_dat_txt)}) | ||||
|                 .itemsCallback((dialog, itemView, which, text) -> { | ||||
|                     switch (which) { | ||||
|                         case 0: | ||||
|                             importFileType = ImportFileType.EXCLE; | ||||
|                             break; | ||||
|                         case 1: | ||||
|                             importFileType = ImportFileType.TEXT; | ||||
|                             break; | ||||
|                         case 2: | ||||
|                             importFileType = ImportFileType.CASS; | ||||
|                             break; | ||||
|                     } | ||||
| 
 | ||||
|                     if (importFileType != null) { | ||||
|                         showImportAppendOrOverideDialog(); | ||||
|                     } | ||||
| 
 | ||||
|                 }).show(); | ||||
|     } | ||||
| 
 | ||||
|     private void showImportAppendOrOverideDialog() { | ||||
|         String message = ""; | ||||
|         switch (importFileType) { | ||||
|             case EXCLE: | ||||
|                 message = mContext.getString(R.string.import_point_excel_guide); | ||||
|                 break; | ||||
|             case TEXT: | ||||
|                 message = mContext.getString(R.string.import_point_text_guide); | ||||
|                 break; | ||||
|             case CASS: | ||||
|                 if (isCassXY()) { | ||||
|                     message = mContext.getString(R.string.import_point_cass_xy_dat); | ||||
|                 } else { | ||||
|                     message = mContext.getString(R.string.import_point_cass_yx_dat); | ||||
|                 } | ||||
|                 break; | ||||
|         } | ||||
| 
 | ||||
|         new MaterialDialog.Builder(mContext) | ||||
|                 .title(R.string.import_points) | ||||
|                 .content(message) | ||||
|                 .positiveText(R.string.import_points) | ||||
|                 .neutralText(R.string.view_sample) | ||||
|                 .onPositive((dialog, which) -> checkStoragePermission()) | ||||
|                 .onNeutral((dialog, which) -> { | ||||
|                     AgentWebActivity.navigation2(mContext, "android_url_import_point_sample"); | ||||
|                 }) | ||||
|                 .show(); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     private void checkStoragePermission() { | ||||
|         CheckStorageUtil.check(mContext, this::selectFolder); | ||||
|     } | ||||
| 
 | ||||
|     private void selectFolder() { | ||||
|         BaseApp app = (BaseApp) Utils.getApp(); | ||||
|         ExFilePicker exFilePicker = new ExFilePicker(); | ||||
|         exFilePicker.setLandscape(app.isLandscape()); | ||||
|         exFilePicker.setShowOnlyExtensions(importFileType.filters); | ||||
|         activityResultLauncher.launch(exFilePicker.createIntent(fragment.requireContext())); | ||||
|     } | ||||
| 
 | ||||
|     private enum ImportFileType { | ||||
|         EXCLE(new String[]{"xls", "xlsx"}), | ||||
|         TEXT(new String[]{"txt"}), | ||||
|         CASS(new String[]{"dat", "txt"}); | ||||
| 
 | ||||
|         private String[] filters; | ||||
| 
 | ||||
|         ImportFileType(String[] filters) { | ||||
|             this.filters = filters; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private boolean isCassXY() { | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -1 +1 @@ | ||||
| Subproject commit 8afd2f2b66bd232cf6f19e571e5c12efd5ad1b3d | ||||
| Subproject commit edf00a6ca7b5afe3a544292bf320eea526d2b511 | ||||
					Loading…
					
					
				
		Reference in new issue