TeacherFragment.java
10.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
package com.hjx.parent.fragment;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.hjx.parent.AccountActivity;
import com.hjx.parent.ErrorDetailActivity;
import com.hjx.parent.LoginActivity;
import com.hjx.parent.R;
import com.hjx.parent.TeacherChooseActivity;
import com.hjx.parent.bean.StudentBean;
import com.hjx.parent.databinding.FragmentTeacherBinding;
import com.prws.common.bean.Record;
import com.prws.common.net.NetWorks;
import com.prws.common.utils.CommonUtil;
import com.prws.common.utils.LogUtil;
import com.prws.common.utils.ScreenUtils;
import com.prws.common.utils.SharedPreferencesUtil;
import com.prws.common.utils.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.SimpleFormatter;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
public class TeacherFragment extends BaseBindingFragment<FragmentTeacherBinding> {
ArrayList<Record> alist = new ArrayList<>();
@Override
protected FragmentTeacherBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
return FragmentTeacherBinding.inflate(inflater, parent, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String photo = (String) SharedPreferencesUtil.getData("photo", "");
if (!TextUtils.isEmpty(photo)) {
RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45));
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(getContext()).load(photo).apply(options).into(getBinding().ivTeacher);
}
String name = (String) SharedPreferencesUtil.getData("name", "");
getBinding().tvTeacher.setText(name);
getBinding().tvAnswerName.setText("亲爱的" + name + "老师");
getBinding().logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logout();
}
});
NetWorks.listRecord(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String str = responseBody.string().toString();
LogUtil.e("TAG", "----" + str);
JSONObject jo = new JSONObject(str);
boolean isSucceed = jo.getBoolean("success");
if (isSucceed) {
JSONArray jarr = jo.getJSONArray("data");
if (jarr.length() > 0) {
getBinding().tvNum.setText("您有" + jarr.length() + "个题目需要答疑");
alist.clear();
for (int i = 0; i < jarr.length(); i++) {
JSONObject jo2 = jarr.getJSONObject(i);
Gson gson = new Gson();
Record sb = gson.fromJson(jo2.toString(), Record.class);
alist.add(sb);
}
loadList();
} else {
getBinding().tvNum.setText("您暂时没有题目需要答疑");
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
private void loadList() {
BaseQuickAdapter adapter = new BaseQuickAdapter(R.layout.item_record, alist) {
@Override
protected void convert(@NonNull BaseViewHolder item, Object o) {
Record record = alist.get(item.getAdapterPosition());
item.setText(R.id.tv_name, record.getStuName());
item.setText(R.id.tv_date, "提问时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(record.getAskTime()));
if (TextUtils.isEmpty(record.getPhoto())) {
item.setImageResource(R.id.iv_student, record.getGender() == 0 ? R.mipmap.ic_avatar_male : R.mipmap.ic_avatar_female);
} else {
RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45));
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(getContext()).load(record.getPhoto()).apply(options).into((ImageView) item.getView(R.id.iv_student));
}
if (TextUtils.isEmpty(record.getContent())) {
item.setGone(R.id.iv_topic, true);
item.setGone(R.id.webview, false);
int maxWidth = CommonUtil.getScreenWidth(getContext()) - CommonUtil.dpToPx(getContext(), 60);
Glide.with(getContext()).load(record.getQuestionUrl()).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
int width = resource.getIntrinsicWidth();
int height = resource.getIntrinsicHeight();
int newHeight = maxWidth * height / width;
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) item.getView(R.id.iv_topic).getLayoutParams();
layoutParams.width = maxWidth;
layoutParams.height = newHeight;
item.getView(R.id.iv_topic).setLayoutParams(layoutParams);
((ImageView) item.getView(R.id.iv_topic)).setImageDrawable(resource);
}
});
} else {
item.setGone(R.id.iv_topic, false);
item.setGone(R.id.webview, true);
WebView webView = item.getView(R.id.webview);
String linkCss = "<link rel=\"stylesheet\" href=\"file:///android_asset/style.css\" type=\"text/css\">";
StringBuilder sb = new StringBuilder(4096);
if (!TextUtils.isEmpty(record.getLabel())) {
sb.append(record.getLabel());
}
sb.append(record.getContent());
if (record.getOptions() != null && record.getOptionsList().size() > 0) {
sb.append("<div class='pt1'>");
sb.append(StringUtils.OptionsHtml(record.getOptionsList(), null));
sb.append("</div>");
}
String body = "<html><header>" + linkCss + "</header>" + sb.toString() + "</body></html>";
WebSettings settings = webView.getSettings();
settings.setUseWideViewPort(true);//设定支持 viewport
settings.setLoadWithOverviewMode(true); //自适应屏幕
webView.loadDataWithBaseURL(linkCss, body, "text/html", "UTF-8", null);
webView.setVisibility(View.VISIBLE);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
};
getBinding().recycleError.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
getBinding().recycleError.setAdapter(adapter);
}
public void logout() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), android.R.style.Theme_DeviceDefault_Light_Dialog_Alert);
builder.setTitle("温馨提示");
builder.setMessage("是否退出登录?");
builder.setCancelable(false);
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
SharedPreferencesUtil.clear(getContext());
restartApp();
}
});
builder.show();
}
private void restartApp() {
//重启app,这一步一定要加上,如果不重启app,可能打开新的页面显示的语言会不正确
Intent intent = new Intent(getContext(), LoginActivity.class);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}