You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
407 lines
14 KiB
407 lines
14 KiB
package com.community.pocket.ui.main.ui.info;
|
|
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.graphics.Paint;
|
|
import android.graphics.Rect;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.Message;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageView;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.RequiresApi;
|
|
import androidx.appcompat.app.AlertDialog;
|
|
import androidx.lifecycle.Observer;
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
import com.community.pocket.R;
|
|
import com.community.pocket.data.model.CreditScore;
|
|
import com.community.pocket.data.model.LocalToken;
|
|
import com.community.pocket.data.model.MyInfo;
|
|
import com.community.pocket.ui.BaseFragment;
|
|
import com.community.pocket.ui.listener.MyTextChange;
|
|
import com.community.pocket.ui.login.LoginActivity;
|
|
import com.community.pocket.ui.main.ui.share.Response;
|
|
import com.community.pocket.util.AppDatabase;
|
|
import com.community.pocket.util.Param;
|
|
import com.community.pocket.util.PropertiesUtil;
|
|
import com.github.mikephil.charting.charts.LineChart;
|
|
import com.github.mikephil.charting.components.Legend;
|
|
import com.github.mikephil.charting.components.XAxis;
|
|
import com.github.mikephil.charting.components.YAxis;
|
|
import com.github.mikephil.charting.data.Entry;
|
|
import com.github.mikephil.charting.data.LineData;
|
|
import com.github.mikephil.charting.data.LineDataSet;
|
|
import com.github.mikephil.charting.formatter.ValueFormatter;
|
|
import com.github.mikephil.charting.utils.Utils;
|
|
|
|
import org.xutils.view.annotation.ContentView;
|
|
import org.xutils.view.annotation.Event;
|
|
import org.xutils.view.annotation.ViewInject;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* 我的信息框架
|
|
*/
|
|
@ContentView(R.layout.info_fragment)
|
|
public class InfoFragment extends BaseFragment {
|
|
|
|
//昵称
|
|
@ViewInject(R.id.nickname)
|
|
private TextView nickname;
|
|
|
|
//信用分
|
|
@ViewInject(R.id.credit_score)
|
|
private TextView creditScore;
|
|
|
|
//头像
|
|
@ViewInject(R.id.headimg)
|
|
private ImageView headimg;
|
|
|
|
//最近发帖数
|
|
@ViewInject(R.id.recentPosts)
|
|
private TextView recentPosts;
|
|
|
|
//最近访客数
|
|
@ViewInject(R.id.recentVisitors)
|
|
private TextView recentVisitors;
|
|
|
|
//手机号
|
|
@ViewInject(R.id.mobie)
|
|
private TextView mobie;
|
|
|
|
//邮箱
|
|
@ViewInject(R.id.email)
|
|
private TextView email;
|
|
|
|
//信用分活动图表
|
|
@ViewInject(R.id.chart)
|
|
private LineChart lineChart;
|
|
|
|
//原密码
|
|
private EditText oldPwd;
|
|
|
|
//新密码
|
|
private EditText newPwd;
|
|
|
|
//确认新密码
|
|
private EditText confirmNewPwd;
|
|
|
|
//打开修改密码弹窗
|
|
@ViewInject(R.id.open_modify_password)
|
|
private Button openModify;
|
|
|
|
private InfoViewModel viewModel;
|
|
|
|
//注销登录
|
|
@ViewInject(R.id.logout)
|
|
private Button logout;
|
|
|
|
private Handler handler;
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
@Override
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
initChart();
|
|
|
|
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(InfoViewModel.class);
|
|
|
|
viewModel.loadInfo();
|
|
|
|
//监听修改密码表单状态
|
|
viewModel.getModifyFormState().observe(getViewLifecycleOwner(), new Observer<InfoFormState>() {
|
|
@Override
|
|
public void onChanged(InfoFormState infoFormState) {
|
|
if (infoFormState == null) {
|
|
return;
|
|
}
|
|
if (infoFormState.getOldPwdError() != null) {
|
|
oldPwd.setError(getString(infoFormState.getOldPwdError(), PropertiesUtil.getIntValue("password.length")));
|
|
}
|
|
|
|
if (infoFormState.getNewPwdError() != null) {
|
|
if (infoFormState.getNewPwdError() == R.string.invalid_password) {
|
|
newPwd.setError(getString(infoFormState.getNewPwdError(), PropertiesUtil.getIntValue("password.length")));
|
|
} else {
|
|
newPwd.setError(getString(infoFormState.getNewPwdError()));
|
|
}
|
|
}
|
|
|
|
if (infoFormState.getConfirmNewPwdError() != null) {
|
|
if (infoFormState.getConfirmNewPwdError() == R.string.invalid_password) {
|
|
confirmNewPwd.setError(getString(infoFormState.getConfirmNewPwdError(), PropertiesUtil.getIntValue("password.length")));
|
|
} else {
|
|
confirmNewPwd.setError(getString(infoFormState.getConfirmNewPwdError()));
|
|
}
|
|
}
|
|
|
|
Message message = new Message();
|
|
Bundle bundle = new Bundle();
|
|
bundle.putBoolean(Param.enable.name(), infoFormState.isDataValid());
|
|
message.setData(bundle);
|
|
handler.sendMessage(message);
|
|
|
|
}
|
|
});
|
|
|
|
//监听修改密码的请求状态
|
|
viewModel.getModifyResponse().observe(getViewLifecycleOwner(), new Observer<InfoResponse>() {
|
|
@Override
|
|
public void onChanged(InfoResponse infoResponse) {
|
|
if (infoResponse == null) {
|
|
return;
|
|
}
|
|
|
|
infoResponse.toast(getContext());
|
|
|
|
if (infoResponse.getResult() == Response.Result.OK) {
|
|
logout(logout);
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
//监听个人信息请求状态
|
|
viewModel.getInfoResponse().observe(getViewLifecycleOwner(), new Observer<InfoResponse>() {
|
|
@Override
|
|
public void onChanged(InfoResponse myInfoInfoResponse) {
|
|
if (myInfoInfoResponse == null) {
|
|
return;
|
|
}
|
|
|
|
myInfoInfoResponse.toast(getContext());
|
|
|
|
if (myInfoInfoResponse.getResult() == Response.Result.OK) {
|
|
loadInfo(myInfoInfoResponse.getMyInfo());
|
|
}
|
|
}
|
|
});
|
|
|
|
//打开修改密码弹窗
|
|
openModify.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
openModifyPassword();
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
* 加载个人信息
|
|
*
|
|
* @param myInfo 个人信息
|
|
*/
|
|
private void loadInfo(MyInfo myInfo) {
|
|
nickname.setText(getString(R.string.nick_name, myInfo.getId()));
|
|
creditScore.setText(getString(R.string.credit_score, myInfo.getCreditScore()));
|
|
recentVisitors.setText(getString(R.string.recent_visitors, myInfo.getVisitors()));
|
|
recentPosts.setText(getString(R.string.recent_posts, myInfo.getPosts()));
|
|
mobie.setText(myInfo.getMobie());
|
|
email.setText(myInfo.getEmail());
|
|
|
|
if (myInfo.getScoreHistory() != null && myInfo.getScoreHistory().size() >= 2) {
|
|
loadChart(myInfo.getScoreHistory());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 打开修改密码弹窗
|
|
*/
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
private void openModifyPassword() {
|
|
View view = View.inflate(getContext(), R.layout.modify_password, null);
|
|
//原密码
|
|
oldPwd = view.findViewById(R.id.old_password);
|
|
//新密码
|
|
newPwd = view.findViewById(R.id.new_password);
|
|
//确认新密码
|
|
confirmNewPwd = view.findViewById(R.id.new_confirm_password);
|
|
|
|
TextWatcher textWatcher = new MyTextChange() {
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
viewModel.modifyPwdChanged(oldPwd.getText().toString(), newPwd.getText().toString(), confirmNewPwd.getText().toString());
|
|
}
|
|
};
|
|
oldPwd.addTextChangedListener(textWatcher);
|
|
newPwd.addTextChangedListener(textWatcher);
|
|
confirmNewPwd.addTextChangedListener(textWatcher);
|
|
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
|
|
final AlertDialog alertDialog = alert.setTitle(R.string.modify_password).setView(view)
|
|
.setNegativeButton(R.string.modify_password, new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
viewModel.modifyPwd(oldPwd.getText().toString(), newPwd.getText().toString());
|
|
dialog.dismiss();
|
|
}
|
|
})
|
|
.setNeutralButton(R.string.action_close, new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
dialog.dismiss();
|
|
}
|
|
})
|
|
.create();
|
|
handler = new Handler(Looper.getMainLooper()) {
|
|
@Override
|
|
public void handleMessage(@NonNull Message msg) {
|
|
alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(msg.getData().getBoolean(Param.enable.name()));
|
|
}
|
|
};
|
|
|
|
alertDialog.show();
|
|
alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(false);
|
|
}
|
|
|
|
/**
|
|
* 注销登录
|
|
*/
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
@Event(value = R.id.logout)
|
|
private void logout(final View view) {
|
|
new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
AppDatabase.getInstance(getContext()).tokenDao().delete(LocalToken.getTokenInstance());
|
|
LocalToken.logout();
|
|
startActivity(new Intent(view.getContext(), LoginActivity.class));
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
/**
|
|
* 计算文本宽度
|
|
*/
|
|
private float computeTextWidth(String text, float textSize) {
|
|
Paint pain = new Paint();
|
|
pain.setAntiAlias(true);
|
|
pain.setTextSize(textSize);
|
|
Rect rect = new Rect();
|
|
pain.getTextBounds(text, 0, text.length(), rect);
|
|
return Utils.convertPixelsToDp(rect.left + rect.right);
|
|
}
|
|
|
|
//
|
|
private LineDataSet addChart(List<CreditScore> values) {
|
|
List<Entry> entryList = new ArrayList<>();
|
|
|
|
for (int i = 0; i < values.size(); i++) {
|
|
CreditScore score = values.get(i);
|
|
entryList.add(new Entry(i, score.getBeforeScore() + score.getScore()));
|
|
}
|
|
LineDataSet dataSet = new LineDataSet(entryList, "");
|
|
//线宽度
|
|
dataSet.setLineWidth(1.6f);
|
|
//不显示圆点
|
|
dataSet.setDrawCircles(false);
|
|
//线条平滑
|
|
// dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
|
|
dataSet.setValueTextSize(18);
|
|
dataSet.setValueFormatter(new ValueFormatter() {
|
|
@Override
|
|
public String getFormattedValue(float value) {
|
|
return String.valueOf((int) value);
|
|
}
|
|
});
|
|
return dataSet;
|
|
}
|
|
|
|
//设置图表格式
|
|
private void initChart() {
|
|
int textSize = 18;
|
|
|
|
//设置x轴数据格式
|
|
XAxis xAxis = lineChart.getXAxis();
|
|
//轴刻度最小值
|
|
xAxis.setAxisMinimum(1);
|
|
//轴刻度间隔最小值
|
|
xAxis.setGranularity(1);
|
|
//轴文本大小
|
|
xAxis.setTextSize(textSize);
|
|
//轴位置
|
|
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
|
|
//不显示网格线
|
|
xAxis.setDrawGridLines(false);
|
|
|
|
//设置Y轴数据格式
|
|
initYAxis(lineChart.getAxisLeft(), textSize);
|
|
initYAxis(lineChart.getAxisRight(), textSize);
|
|
|
|
//设置x轴0刻度和最后刻度的左右偏移,保证日期显示完整
|
|
float offset = computeTextWidth(getString(R.string.dateformat), textSize) + 3;
|
|
//设置轴左侧偏移
|
|
lineChart.setExtraLeftOffset(offset);
|
|
//设置轴右侧偏移
|
|
lineChart.setExtraRightOffset(offset);
|
|
// 设置底部偏移
|
|
lineChart.setExtraBottomOffset(20);
|
|
//隐藏描述信息
|
|
lineChart.getDescription().setEnabled(false);
|
|
//无数据时显示的文本
|
|
lineChart.setNoDataText(getString(R.string.no_score_history));
|
|
//图例:得到Lengend
|
|
Legend legend = lineChart.getLegend();
|
|
//隐藏Lengend
|
|
legend.setEnabled(false);
|
|
}
|
|
|
|
private void initYAxis(YAxis yAxis, int textSize) {
|
|
//轴细分粒度最小单位长度
|
|
yAxis.setAxisMinimum(1);
|
|
//轴间隔最小单位长度
|
|
yAxis.setGranularity(2);
|
|
//轴文本大小
|
|
yAxis.setTextSize(textSize);
|
|
//
|
|
yAxis.setXOffset(20);
|
|
}
|
|
|
|
//加载图表数据
|
|
private void loadChart(final List<CreditScore> values) {
|
|
LineData lineData;
|
|
|
|
if (lineChart.getData() == null) {
|
|
lineData = new LineData();
|
|
lineChart.setData(lineData);
|
|
} else {
|
|
lineData = lineChart.getData();
|
|
lineData.removeDataSet(0);
|
|
}
|
|
lineData.addDataSet(addChart(values));
|
|
|
|
//设置x轴数据格式
|
|
XAxis xAxis = lineChart.getXAxis();
|
|
//格式化日期数据
|
|
xAxis.setValueFormatter(new ValueFormatter() {
|
|
@Override
|
|
public String getFormattedValue(float value) {
|
|
int index = (int) (value - 1);
|
|
return formatUnix(values.get(index).getTime());
|
|
}
|
|
});
|
|
|
|
//设置标记
|
|
// lineChart.setMarker(new MyChartView(getContext(),R.layout.chart,values));
|
|
|
|
//刷新图表
|
|
lineChart.notifyDataSetChanged();
|
|
lineChart.invalidate();
|
|
}
|
|
}
|
|
|