Commit fdaf2a78b44d07636f2e5beebd022310acfe7625
1 parent
a809e3e571
Exists in
master
图片上传逻辑修复
Showing
1 changed file
with
1 additions
and
1 deletions
Show diff stats
app/src/main/java/com/hjx/parent/dialog/AddHomeworkDialog.java
1 | package com.hjx.parent.dialog; | 1 | package com.hjx.parent.dialog; |
2 | 2 | ||
3 | import android.annotation.SuppressLint; | 3 | import android.annotation.SuppressLint; |
4 | import android.content.Context; | 4 | import android.content.Context; |
5 | import android.util.Pair; | 5 | import android.util.Pair; |
6 | import android.view.View; | 6 | import android.view.View; |
7 | import android.view.WindowManager; | 7 | import android.view.WindowManager; |
8 | import android.widget.AdapterView; | 8 | import android.widget.AdapterView; |
9 | import android.widget.ArrayAdapter; | 9 | import android.widget.ArrayAdapter; |
10 | import android.widget.Toast; | 10 | import android.widget.Toast; |
11 | 11 | ||
12 | import androidx.annotation.NonNull; | 12 | import androidx.annotation.NonNull; |
13 | 13 | ||
14 | import com.bigkoo.pickerview.builder.TimePickerBuilder; | 14 | import com.bigkoo.pickerview.builder.TimePickerBuilder; |
15 | import com.google.gson.Gson; | 15 | import com.google.gson.Gson; |
16 | import com.hjx.parent.R; | 16 | import com.hjx.parent.R; |
17 | import com.hjx.parent.bean.StudentBean; | 17 | import com.hjx.parent.bean.StudentBean; |
18 | import com.hjx.parent.databinding.DialogAddHomeworkBinding; | 18 | import com.hjx.parent.databinding.DialogAddHomeworkBinding; |
19 | import com.hjx.parent.function.Function0; | 19 | import com.hjx.parent.function.Function0; |
20 | import com.hjx.parent.function.Function1; | 20 | import com.hjx.parent.function.Function1; |
21 | import com.hjx.parent.rx.BaseRxActivity; | 21 | import com.hjx.parent.rx.BaseRxActivity; |
22 | import com.prws.common.bean.GradeAndSubject; | 22 | import com.prws.common.bean.GradeAndSubject; |
23 | import com.prws.common.bean.ResponseResult; | 23 | import com.prws.common.bean.ResponseResult; |
24 | import com.prws.common.bean.Student; | 24 | import com.prws.common.bean.Student; |
25 | import com.prws.common.bean.Subject; | 25 | import com.prws.common.bean.Subject; |
26 | import com.prws.common.bean.baidu.BaiduInput; | 26 | import com.prws.common.bean.baidu.BaiduInput; |
27 | import com.prws.common.net.NetWorks; | 27 | import com.prws.common.net.NetWorks; |
28 | import com.prws.common.utils.SharedPreferencesUtil; | 28 | import com.prws.common.utils.SharedPreferencesUtil; |
29 | import com.trello.rxlifecycle2.android.RxLifecycleAndroid; | 29 | import com.trello.rxlifecycle2.android.RxLifecycleAndroid; |
30 | 30 | ||
31 | import java.io.File; | 31 | import java.io.File; |
32 | import java.text.SimpleDateFormat; | 32 | import java.text.SimpleDateFormat; |
33 | import java.util.ArrayList; | 33 | import java.util.ArrayList; |
34 | import java.util.Date; | 34 | import java.util.Date; |
35 | import java.util.HashMap; | 35 | import java.util.HashMap; |
36 | import java.util.List; | 36 | import java.util.List; |
37 | import java.util.Locale; | 37 | import java.util.Locale; |
38 | import java.util.Map; | 38 | import java.util.Map; |
39 | 39 | ||
40 | import io.reactivex.Observable; | 40 | import io.reactivex.Observable; |
41 | import io.reactivex.android.schedulers.AndroidSchedulers; | 41 | import io.reactivex.android.schedulers.AndroidSchedulers; |
42 | import io.reactivex.schedulers.Schedulers; | 42 | import io.reactivex.schedulers.Schedulers; |
43 | 43 | ||
44 | public class AddHomeworkDialog extends BaseDialog<DialogAddHomeworkBinding>{ | 44 | public class AddHomeworkDialog extends BaseDialog<DialogAddHomeworkBinding>{ |
45 | private final BaseRxActivity<?> activity; | 45 | private final BaseRxActivity<?> activity; |
46 | 46 | ||
47 | private Function1<Student> callback; | 47 | private Function1<Student> callback; |
48 | 48 | ||
49 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA); | 49 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA); |
50 | List<Student> studentList; | 50 | List<Student> studentList; |
51 | List<GradeAndSubject> gradeAndSubjectList; | 51 | List<GradeAndSubject> gradeAndSubjectList; |
52 | 52 | ||
53 | List<String> images; | 53 | List<String> images; |
54 | 54 | ||
55 | Student student; | 55 | Student student; |
56 | String grade; | 56 | String grade; |
57 | String term; | 57 | String term; |
58 | String subject; | 58 | String subject; |
59 | Date uploadTime = new Date(); | 59 | Date uploadTime = new Date(); |
60 | 60 | ||
61 | public AddHomeworkDialog(BaseRxActivity<?> context) { | 61 | public AddHomeworkDialog(BaseRxActivity<?> context) { |
62 | super((Context) context); | 62 | super((Context) context); |
63 | activity = context; | 63 | activity = context; |
64 | } | 64 | } |
65 | 65 | ||
66 | public void show(List<String> paths, Function1<Student> callback) { | 66 | public void show(List<String> paths, Function1<Student> callback) { |
67 | images = paths; | 67 | images = paths; |
68 | this.callback = callback; | 68 | this.callback = callback; |
69 | super.show(); | 69 | super.show(); |
70 | } | 70 | } |
71 | 71 | ||
72 | @SuppressLint("SetTextI18n") | 72 | @SuppressLint("SetTextI18n") |
73 | @Override | 73 | @Override |
74 | public void initView() { | 74 | public void initView() { |
75 | getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | 75 | getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); |
76 | binding.btnClose.setOnClickListener(v -> dismiss()); | 76 | binding.btnClose.setOnClickListener(v -> dismiss()); |
77 | binding.spStudent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | 77 | binding.spStudent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { |
78 | @Override | 78 | @Override |
79 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | 79 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
80 | student = studentList.get(position); | 80 | student = studentList.get(position); |
81 | if (gradeAndSubjectList == null) return; | 81 | if (gradeAndSubjectList == null) return; |
82 | int p = 0; | 82 | int p = 0; |
83 | for (GradeAndSubject it: gradeAndSubjectList) { | 83 | for (GradeAndSubject it: gradeAndSubjectList) { |
84 | if (it.getGrade().getGrade().equals(student.grade)) { | 84 | if (it.getGrade().getGrade().equals(student.grade)) { |
85 | p = gradeAndSubjectList.indexOf(it); | 85 | p = gradeAndSubjectList.indexOf(it); |
86 | break; | 86 | break; |
87 | } | 87 | } |
88 | } | 88 | } |
89 | binding.spGrade.setSelection(p); | 89 | binding.spGrade.setSelection(p); |
90 | } | 90 | } |
91 | 91 | ||
92 | @Override | 92 | @Override |
93 | public void onNothingSelected(AdapterView<?> parent) { | 93 | public void onNothingSelected(AdapterView<?> parent) { |
94 | } | 94 | } |
95 | }); | 95 | }); |
96 | binding.spTerm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | 96 | binding.spTerm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { |
97 | @Override | 97 | @Override |
98 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | 98 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
99 | term = getContext().getResources().getStringArray(R.array.grade_array)[position]; | 99 | term = getContext().getResources().getStringArray(R.array.grade_array)[position]; |
100 | } | 100 | } |
101 | @Override | 101 | @Override |
102 | public void onNothingSelected(AdapterView<?> parent) { | 102 | public void onNothingSelected(AdapterView<?> parent) { |
103 | } | 103 | } |
104 | }); | 104 | }); |
105 | binding.spGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | 105 | binding.spGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { |
106 | @Override | 106 | @Override |
107 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | 107 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
108 | grade = gradeAndSubjectList.get(position).getGrade().getGrade(); | 108 | grade = gradeAndSubjectList.get(position).getGrade().getGrade(); |
109 | refreshSubjects(position); | 109 | refreshSubjects(position); |
110 | } | 110 | } |
111 | 111 | ||
112 | @Override | 112 | @Override |
113 | public void onNothingSelected(AdapterView<?> parent) { | 113 | public void onNothingSelected(AdapterView<?> parent) { |
114 | } | 114 | } |
115 | }); | 115 | }); |
116 | 116 | ||
117 | binding.tvDate.setText(dateFormat.format(uploadTime)); | 117 | binding.tvDate.setText(dateFormat.format(uploadTime)); |
118 | binding.tvDate.setOnClickListener(v -> { | 118 | binding.tvDate.setOnClickListener(v -> { |
119 | new TimePickerBuilder(getContext(), (date, v1) -> { | 119 | new TimePickerBuilder(getContext(), (date, v1) -> { |
120 | uploadTime = date; | 120 | uploadTime = date; |
121 | binding.tvDate.setText(dateFormat.format(uploadTime)); | 121 | binding.tvDate.setText(dateFormat.format(uploadTime)); |
122 | binding.etName.setText(dateFormat.format(uploadTime) + subject + "作业"); | 122 | binding.etName.setText(dateFormat.format(uploadTime) + subject + "作业"); |
123 | }).setType(new boolean[]{true, true, true, false, false, false}).isDialog(true).build().show(); | 123 | }).setType(new boolean[]{true, true, true, false, false, false}).isDialog(true).build().show(); |
124 | }); | 124 | }); |
125 | 125 | ||
126 | binding.btnConfirm.setOnClickListener(v -> { | 126 | binding.btnConfirm.setOnClickListener(v -> { |
127 | String name = binding.etName.getText().toString().trim(); | 127 | String name = binding.etName.getText().toString().trim(); |
128 | if (name.isEmpty()) { | 128 | if (name.isEmpty()) { |
129 | Toast.makeText(getContext(), "请输入作业名称", Toast.LENGTH_SHORT).show(); | 129 | Toast.makeText(getContext(), "请输入作业名称", Toast.LENGTH_SHORT).show(); |
130 | return; | 130 | return; |
131 | } | 131 | } |
132 | uploadImage(name); | 132 | uploadImage(name); |
133 | }); | 133 | }); |
134 | 134 | ||
135 | getGrade(); | 135 | getGrade(); |
136 | } | 136 | } |
137 | 137 | ||
138 | private String getCurrentStuId() { | 138 | private String getCurrentStuId() { |
139 | String student = (String) SharedPreferencesUtil.getData("student", ""); | 139 | String student = (String) SharedPreferencesUtil.getData("student", ""); |
140 | StudentBean studentBean = new Gson().fromJson(student, StudentBean.class); | 140 | StudentBean studentBean = new Gson().fromJson(student, StudentBean.class); |
141 | return studentBean.getStuId(); | 141 | return studentBean.getStuId(); |
142 | } | 142 | } |
143 | 143 | ||
144 | @SuppressLint("CheckResult") | 144 | @SuppressLint("CheckResult") |
145 | private void getStudents() { | 145 | private void getStudents() { |
146 | NetWorks.listStudent() | 146 | NetWorks.listStudent() |
147 | .subscribeOn(Schedulers.io()) | 147 | .subscribeOn(Schedulers.io()) |
148 | .observeOn(AndroidSchedulers.mainThread()) | 148 | .observeOn(AndroidSchedulers.mainThread()) |
149 | .compose(RxLifecycleAndroid.bindActivity(activity.getRxLifecycle())) | 149 | .compose(RxLifecycleAndroid.bindActivity(activity.getRxLifecycle())) |
150 | .map(ResponseResult::getData) | 150 | .map(ResponseResult::getData) |
151 | .subscribe((data, th) -> { | 151 | .subscribe((data, th) -> { |
152 | if (th != null) th.printStackTrace(); | 152 | if (th != null) th.printStackTrace(); |
153 | if (data != null && data.size() > 0) { | 153 | if (data != null && data.size() > 0) { |
154 | studentList = data; | 154 | studentList = data; |
155 | int position = 0; | 155 | int position = 0; |
156 | String stuId = getCurrentStuId(); | 156 | String stuId = getCurrentStuId(); |
157 | for (int i = 0; i < data.size(); i++) { | 157 | for (int i = 0; i < data.size(); i++) { |
158 | if (data.get(i).stuId.equals(stuId)) { | 158 | if (data.get(i).stuId.equals(stuId)) { |
159 | position = i; | 159 | position = i; |
160 | break; | 160 | break; |
161 | } | 161 | } |
162 | } | 162 | } |
163 | binding.spStudent.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_spinner, data)); | 163 | binding.spStudent.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_spinner, data)); |
164 | binding.spStudent.setSelection(position); | 164 | binding.spStudent.setSelection(position); |
165 | } | 165 | } |
166 | }); | 166 | }); |
167 | } | 167 | } |
168 | 168 | ||
169 | @SuppressLint("CheckResult") | 169 | @SuppressLint("CheckResult") |
170 | private void getGrade() { | 170 | private void getGrade() { |
171 | NetWorks.service_url.listGradeAndSubject(NetWorks.getHeader()) | 171 | NetWorks.service_url.listGradeAndSubject(NetWorks.getHeader()) |
172 | .subscribeOn(Schedulers.io()) | 172 | .subscribeOn(Schedulers.io()) |
173 | .observeOn(AndroidSchedulers.mainThread()) | 173 | .observeOn(AndroidSchedulers.mainThread()) |
174 | .compose(RxLifecycleAndroid.bindActivity(activity.getRxLifecycle())) | 174 | .compose(RxLifecycleAndroid.bindActivity(activity.getRxLifecycle())) |
175 | .map(ResponseResult::getData) | 175 | .map(ResponseResult::getData) |
176 | .firstOrError() | 176 | .firstOrError() |
177 | .subscribe((data, th) -> { | 177 | .subscribe((data, th) -> { |
178 | if (th != null) th.printStackTrace(); | 178 | if (th != null) th.printStackTrace(); |
179 | if (data != null && data.size() > 0) { | 179 | if (data != null && data.size() > 0) { |
180 | gradeAndSubjectList = data; | 180 | gradeAndSubjectList = data; |
181 | List<String> grades = new ArrayList<>(); | 181 | List<String> grades = new ArrayList<>(); |
182 | for (GradeAndSubject it: data) { | 182 | for (GradeAndSubject it: data) { |
183 | grades.add(it.getGrade().getGrade()); | 183 | grades.add(it.getGrade().getGrade()); |
184 | } | 184 | } |
185 | binding.spGrade.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_spinner, grades)); | 185 | binding.spGrade.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_spinner, grades)); |
186 | } | 186 | } |
187 | getStudents(); | 187 | getStudents(); |
188 | }); | 188 | }); |
189 | } | 189 | } |
190 | 190 | ||
191 | private void refreshSubjects(int gradePosition) { | 191 | private void refreshSubjects(int gradePosition) { |
192 | if (gradeAndSubjectList == null) return; | 192 | if (gradeAndSubjectList == null) return; |
193 | List<Subject> subjectList = gradeAndSubjectList.get(gradePosition).getSubjects(); | 193 | List<Subject> subjectList = gradeAndSubjectList.get(gradePosition).getSubjects(); |
194 | List<String> subjects = new ArrayList<>(); | 194 | List<String> subjects = new ArrayList<>(); |
195 | for (Subject s: subjectList) { | 195 | for (Subject s: subjectList) { |
196 | subjects.add(s.getSubject()); | 196 | subjects.add(s.getSubject()); |
197 | } | 197 | } |
198 | binding.spSubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | 198 | binding.spSubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { |
199 | @SuppressLint("SetTextI18n") | 199 | @SuppressLint("SetTextI18n") |
200 | @Override | 200 | @Override |
201 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | 201 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
202 | subject = subjects.get(position); | 202 | subject = subjects.get(position); |
203 | binding.etName.setText(dateFormat.format(uploadTime) + subject + "作业"); | 203 | binding.etName.setText(dateFormat.format(uploadTime) + subject + "作业"); |
204 | } | 204 | } |
205 | @Override | 205 | @Override |
206 | public void onNothingSelected(AdapterView<?> parent) { | 206 | public void onNothingSelected(AdapterView<?> parent) { |
207 | } | 207 | } |
208 | }); | 208 | }); |
209 | binding.spSubject.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_spinner, subjects)); | 209 | binding.spSubject.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_spinner, subjects)); |
210 | } | 210 | } |
211 | 211 | ||
212 | @SuppressLint("CheckResult") | 212 | @SuppressLint("CheckResult") |
213 | public void uploadImage(String name) { | 213 | public void uploadImage(String name) { |
214 | activity.showLoadingDialog("上传作业中..."); | 214 | activity.showLoadingDialog("上传作业中..."); |
215 | Observable.fromIterable(images) | 215 | Observable.fromIterable(images) |
216 | .map(path -> { | 216 | .map(path -> { |
217 | int index = images.indexOf(path); | 217 | int index = images.indexOf(path); |
218 | return new Pair<>(index, path); | 218 | return new Pair<>(index, path); |
219 | }) | 219 | }) |
220 | .subscribeOn(Schedulers.io()) | 220 | .subscribeOn(Schedulers.io()) |
221 | .flatMap(pair -> { | 221 | .flatMap(pair -> { |
222 | int index = pair.first; | 222 | int index = pair.first; |
223 | String path = pair.second; | 223 | String path = pair.second; |
224 | File file = new File(path); | 224 | File file = new File(path); |
225 | String id = file.getName().substring(0, file.getName().lastIndexOf(".")); | 225 | String id = file.getName().substring(0, file.getName().lastIndexOf(".")); |
226 | return Observable.just(file) | 226 | return Observable.just(file) |
227 | .map(var -> { | 227 | .map(var -> { |
228 | BaiduInput baiduInput = NetWorks.inputImage(path, id).blockingGet(); | 228 | BaiduInput baiduInput = NetWorks.inputImage(path, id).blockingGet(); |
229 | if (baiduInput.getError_code() != null && baiduInput.getBrief() != null) { | 229 | if (baiduInput.getError_code() != null && baiduInput.getBrief() == null) { |
230 | throw new RuntimeException("图片入库失败"); | 230 | throw new RuntimeException("图片入库失败"); |
231 | } else { | 231 | } else { |
232 | return baiduInput; | 232 | return baiduInput; |
233 | } | 233 | } |
234 | }) | 234 | }) |
235 | .map(var -> NetWorks.uploadImage(path, id).blockingGet()) | 235 | .map(var -> NetWorks.uploadImage(path, id).blockingGet()) |
236 | .map(var -> { | 236 | .map(var -> { |
237 | Map<String, Object> map = new HashMap<>(); | 237 | Map<String, Object> map = new HashMap<>(); |
238 | map.put("brief", id); | 238 | map.put("brief", id); |
239 | map.put("sort", index); | 239 | map.put("sort", index); |
240 | return map; | 240 | return map; |
241 | }); | 241 | }); |
242 | }, 2) | 242 | }, 2) |
243 | .toList() | 243 | .toList() |
244 | .map(list -> { | 244 | .map(list -> { |
245 | List<String> stuIds = new ArrayList<>(); | 245 | List<String> stuIds = new ArrayList<>(); |
246 | stuIds.add(student.stuId); | 246 | stuIds.add(student.stuId); |
247 | String userId = NetWorks.getUserId(); | 247 | String userId = NetWorks.getUserId(); |
248 | 248 | ||
249 | Map<String, Object> map = new HashMap<>(); | 249 | Map<String, Object> map = new HashMap<>(); |
250 | map.put("name", name); | 250 | map.put("name", name); |
251 | map.put("grade", grade); | 251 | map.put("grade", grade); |
252 | map.put("term", term); | 252 | map.put("term", term); |
253 | map.put("subject", subject); | 253 | map.put("subject", subject); |
254 | map.put("userId", userId); | 254 | map.put("userId", userId); |
255 | map.put("stuIds", stuIds); | 255 | map.put("stuIds", stuIds); |
256 | map.put("briefList", list); | 256 | map.put("briefList", list); |
257 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); | 257 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
258 | map.put("uploadTime", format.format(uploadTime)); | 258 | map.put("uploadTime", format.format(uploadTime)); |
259 | return map; | 259 | return map; |
260 | }) | 260 | }) |
261 | .flatMap(body -> { | 261 | .flatMap(body -> { |
262 | return NetWorks.service_url.uploadHomework(NetWorks.getHeader(), body); | 262 | return NetWorks.service_url.uploadHomework(NetWorks.getHeader(), body); |
263 | }) | 263 | }) |
264 | .observeOn(AndroidSchedulers.mainThread()) | 264 | .observeOn(AndroidSchedulers.mainThread()) |
265 | .compose(RxLifecycleAndroid.bindActivity(activity.getRxLifecycle())) | 265 | .compose(RxLifecycleAndroid.bindActivity(activity.getRxLifecycle())) |
266 | .subscribe((response, th) -> { | 266 | .subscribe((response, th) -> { |
267 | if (th != null) th.printStackTrace(); | 267 | if (th != null) th.printStackTrace(); |
268 | if (response != null && response.getSuccess()) { | 268 | if (response != null && response.getSuccess()) { |
269 | activity.cancelLoadingDialog(); | 269 | activity.cancelLoadingDialog(); |
270 | dismiss(); | 270 | dismiss(); |
271 | if (callback != null) callback.invoke(student); | 271 | if (callback != null) callback.invoke(student); |
272 | } else { | 272 | } else { |
273 | activity.loadFail("上传作业失败"); | 273 | activity.loadFail("上传作业失败"); |
274 | } | 274 | } |
275 | }) | 275 | }) |
276 | ; | 276 | ; |
277 | } | 277 | } |
278 | 278 | ||
279 | @NonNull | 279 | @NonNull |
280 | @Override | 280 | @Override |
281 | public DialogAddHomeworkBinding getBinding() { | 281 | public DialogAddHomeworkBinding getBinding() { |
282 | return DialogAddHomeworkBinding.inflate(getLayoutInflater()); | 282 | return DialogAddHomeworkBinding.inflate(getLayoutInflater()); |
283 | } | 283 | } |
284 | } | 284 | } |
285 | 285 |