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