riku
2025-10-30 e9aa93f381afcf9f9cf0c39f2b9e32375ed49528
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
package cn.flightfeather.thirdappmodule.fragment;
 
 
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import cn.flightfeather.thirdappmodule.CommonApplication;
import cn.flightfeather.thirdappmodule.R;
import cn.flightfeather.thirdappmodule.activity.LoginActivity;
import cn.flightfeather.thirdappmodule.bean.entity.Userinfo;
import cn.flightfeather.thirdappmodule.httpservice.SettingsService;
import cn.flightfeather.thirdappmodule.util.GlobalConfig;
import cn.flightfeather.thirdappmodule.util.push.LoginInitializer;
import cn.flightfeather.thirdappmodule.view.SettingCommItemView;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
 
/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends Fragment implements View.OnClickListener{
    private TextView tv_userName;
    private TextView text_version;
    private CommonApplication application;
    private SettingCommItemView sc_changePassword;
    private SettingCommItemView sc_logOut;
    private SettingsService settingsService;
    private Dialog changePWDDialog;
 
    public SettingFragment() {
        // Required empty public constructor
    }
 
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_setting, container, false);
    }
 
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        application = (CommonApplication) getActivity().getApplication();
        settingsService = application.getRetrofit().create(SettingsService.class);
        tv_userName = (TextView) view.findViewById(R.id.tv_user_name);
        tv_userName.setText(application.getCurrentUser().getRealname());
        text_version = view.findViewById(R.id.text_version);
        text_version.setText(GlobalConfig.getInstance().getVersionName());
        sc_changePassword = (SettingCommItemView) view.findViewById(R.id.sc_edit_password);
        sc_logOut = (SettingCommItemView) view.findViewById(R.id.sc_logout);
 
        sc_logOut.setOnClickListener(this);
        sc_changePassword.setOnClickListener(this);
    }
 
    private void logOut(){
        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
        dialog.setTitle("要退出登录吗?");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                LoginInitializer.INSTANCE.onLoginStatusCheck(getContext(), false, application.getCurrentUser().getAcountname());
 
                onLogOut();
                getActivity().finish();
            }
        });
        dialog.setNegativeButton("取消",null);
        dialog.show();
     }
 
    protected void onLogOut() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }
 
     private void changePassword(){
         final View view2 = View.inflate(getContext(), R.layout.dialog_change_password, null);
 
         AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
         dialog.setTitle("修改密码");
         dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
                 EditText  et_pwd1 = (EditText) view2.findViewById(R.id.et_pwd1);
                 EditText et_pwd2 = (EditText) view2.findViewById(R.id.et_pwd2);
 
                 if (et_pwd1.getText().toString().equals(et_pwd2.getText().toString())){
                     Userinfo user = application.getCurrentUser();
                     user.setPassword(et_pwd1.getText().toString());
                     updateUserInfo(user);
                 }else {
                     Toast.makeText(application, "两次密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
                 }
             }
         });
         dialog.setView(view2);
         dialog.setNegativeButton("取消",null);
         changePWDDialog = dialog.show();
 
     }
     private void updateUserInfo(Userinfo userinfo){
         Call<ResponseBody> updateUserInfo = settingsService.updateUserInfo(userinfo);
         updateUserInfo.enqueue(new Callback<ResponseBody>() {
             @Override
             public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                 if (response.body()!=null){
                     Toast.makeText(application, "修改成功", Toast.LENGTH_SHORT).show();
                     changePWDDialog.dismiss();
                 }else {
                     Toast.makeText(application, "修改失败", Toast.LENGTH_SHORT).show();
                 }
             }
 
             @Override
             public void onFailure(Call<ResponseBody> call, Throwable t) {
                 Toast.makeText(application, "联网失败", Toast.LENGTH_SHORT).show();
             }
         });
     }
 
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.sc_logout) {
            logOut();
//                startActivity(new Intent(getActivity(), DebugTransSiteLatlngActivity.class));
        } else if (id == R.id.sc_edit_password) {
            changePassword();
        }
 
    }
}