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.
299 lines
10 KiB
299 lines
10 KiB
package com.example.drink_order_system;
|
|
|
|
import android.app.AlertDialog;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.app.Fragment;
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.Button;
|
|
import android.widget.CheckBox;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageButton;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import java.io.IOException;
|
|
|
|
import okhttp3.Call;
|
|
import okhttp3.Callback;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
|
|
public class MineFragment extends Fragment {
|
|
private TextView user;
|
|
private TextView tvuser;
|
|
private TextView tvsex;
|
|
private TextView tvtel;
|
|
private TextView tvadd;
|
|
private Button btset;
|
|
private Button btexit;
|
|
static String tel=null;
|
|
static String add=null;
|
|
private LinearLayoutManager llM;
|
|
|
|
private LayoutInflater layoutInflater;
|
|
private Context mContext;
|
|
private AlertDialog buyDialog = null;
|
|
private AlertDialog.Builder builder = null;
|
|
private View view_buy;
|
|
private View view;
|
|
private String userName;
|
|
public MineFragment() {
|
|
// Required empty public constructor
|
|
}
|
|
public static MineFragment newInstance(String userName) {
|
|
MineFragment myFragment = new MineFragment();
|
|
Bundle args = new Bundle();
|
|
args.putString("userName", userName);
|
|
myFragment.setArguments(args);
|
|
return myFragment;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
if (getArguments() != null) {
|
|
userName = getArguments().getString("userName");
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
mContext = getActivity();
|
|
layoutInflater = inflater;
|
|
view = inflater.inflate(R.layout.fragment_mine, container, false);
|
|
|
|
llM = new LinearLayoutManager(this.getActivity());
|
|
|
|
tvuser = view.findViewById(R.id.user2);
|
|
tvsex = view.findViewById(R.id.sex2);
|
|
tvtel = view.findViewById(R.id.tel2);
|
|
tvadd = view.findViewById(R.id.add2);
|
|
|
|
builder = new AlertDialog.Builder(this.getActivity());
|
|
view_buy = inflater.inflate(R.layout.dialogue_buy, null, false);
|
|
builder.setView(view_buy);
|
|
builder.setCancelable(false);
|
|
buyDialog = builder.create();
|
|
view_buy.findViewById(R.id.button_quit).setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
buyDialog.dismiss();
|
|
}
|
|
});
|
|
view_buy.findViewById(R.id.button_bought).setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Account temp = new Account(userName, mContext);
|
|
String takeAway = "0";
|
|
Ordered_drinks.clearOrdered_array();
|
|
refresh();
|
|
buyDialog.dismiss();
|
|
}
|
|
});
|
|
return view;
|
|
}
|
|
|
|
@Override
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
// 退出登录按钮
|
|
btexit = getActivity().findViewById(R.id.exit);
|
|
btexit.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
showLogoutConfirmationDialog();
|
|
}
|
|
});
|
|
// 修改信息按钮
|
|
btset = getActivity().findViewById(R.id.set);
|
|
btset.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
showEditInfoDialog();
|
|
}
|
|
});
|
|
|
|
// 初始化用户信息
|
|
fetchUserInfo();
|
|
}
|
|
|
|
private void showEditInfoDialog() {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_edit_info, null);
|
|
builder.setView(dialogView);
|
|
|
|
final TextView user = dialogView.findViewById(R.id.User);
|
|
final EditText etPwd = dialogView.findViewById(R.id.etPwd);
|
|
final EditText etSex = dialogView.findViewById(R.id.etSex);
|
|
final EditText etTel = dialogView.findViewById(R.id.etTel);
|
|
final EditText etAdd = dialogView.findViewById(R.id.etAdd);
|
|
|
|
Button btnConfirm = dialogView.findViewById(R.id.btnConfirm);
|
|
Button btnCancel = dialogView.findViewById(R.id.btnCancel);
|
|
final AlertDialog dialog = builder.create();
|
|
|
|
// 获取现有用户信息并填充到对话框
|
|
user.setText(tvuser.getText().toString());
|
|
etSex.setText(tvsex.getText().toString());
|
|
etTel.setText(tvtel.getText().toString());
|
|
etAdd.setText(tvadd.getText().toString());
|
|
tel=tvtel.getText().toString();
|
|
add=tvadd.getText().toString();
|
|
|
|
btnConfirm.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
// 获取用户输入的信息
|
|
String newSex = etSex.getText().toString();
|
|
String newTel = etTel.getText().toString();
|
|
String newAdd = etAdd.getText().toString();
|
|
String newPwd = etPwd.getText().toString();
|
|
//if(newPwd.equals("")) newPwd=null;
|
|
Log.e("pwd",newPwd);
|
|
// 发送请求更新用户信息
|
|
updateUserInfo(newSex, newTel, newAdd, newPwd);
|
|
dialog.dismiss();
|
|
}
|
|
});
|
|
|
|
btnCancel.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
dialog.dismiss();
|
|
}
|
|
});
|
|
|
|
dialog.show();
|
|
}
|
|
|
|
private void showLogoutConfirmationDialog() {
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_logout, null);
|
|
builder.setView(dialogView);
|
|
Button confirmButton = dialogView.findViewById(R.id.btnConfirm);
|
|
Button cancelButton = dialogView.findViewById(R.id.btnCancel);
|
|
final AlertDialog dialog = builder.create();
|
|
//确认按钮监听事件
|
|
confirmButton.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
// 用户点击确认退出
|
|
dialog.dismiss(); // 关闭对话框
|
|
logout(); // 执行退出登录逻辑
|
|
}
|
|
});
|
|
//取消按钮监听事件
|
|
cancelButton.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
// 用户点击取消退出,关闭对话框
|
|
dialog.dismiss();
|
|
}
|
|
});
|
|
dialog.show();
|
|
}
|
|
|
|
private void fetchUserInfo() {
|
|
OkHttpClient client = new OkHttpClient();
|
|
Request request = new Request.Builder()
|
|
.url(Constant.MINE_URL + "?Cac=" + userName)
|
|
.build();
|
|
Log.e("MineFragment",Constant.MINE_URL + "?Cac=" + userName);
|
|
client.newCall(request).enqueue(new Callback() {
|
|
@Override
|
|
public void onFailure(Call call, IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
@Override
|
|
public void onResponse(Call call, Response response) throws IOException {
|
|
if (response.isSuccessful()) {
|
|
String jsonData = response.body().string();
|
|
Gson gson = new Gson();
|
|
Log.e("username",userName);
|
|
Log.w("objectStr",jsonData);
|
|
final UserBean userBean = gson.fromJson(jsonData, UserBean.class);
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
updateUserInfoView(userBean);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
private void updateUserInfo( final String newSex, final String newTel, final String newAdd, final String newPwd) {
|
|
OkHttpClient client = new OkHttpClient();
|
|
Request request = new Request.Builder()
|
|
.url(Constant.CMINE_URL +"?Cac=" + userName + "&Csex=" + newSex + "&Ctel=" + newTel + "&Cadd=" + newAdd + "&Cpw=" + newPwd)
|
|
.build();
|
|
Log.e("updateUserInfo",Constant.CMINE_URL +"?Cac=" + userName + "&Csex=" + newSex + "&Ctel=" + newTel + "&Cadd=" + newAdd + "&Cpw=" + newPwd);
|
|
client.newCall(request).enqueue(new Callback() {
|
|
@Override
|
|
public void onFailure(Call call, IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
@Override
|
|
public void onResponse(Call call, Response response) throws IOException {
|
|
if (response.isSuccessful()) {
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
tvsex.setText(newSex);
|
|
tvtel.setText(newTel);
|
|
tvadd.setText(newAdd);
|
|
tel=newTel;
|
|
add=newAdd;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
private void updateUserInfoView(UserBean userBean) {
|
|
|
|
tvuser.setText(userBean.getUser());
|
|
tvsex.setText(userBean.getSex());
|
|
tvtel.setText(userBean.getTel());
|
|
tvadd.setText(userBean.getAdd());
|
|
tel=userBean.getTel();
|
|
add=userBean.getAdd();
|
|
}
|
|
|
|
private void logout() {
|
|
// 清除用户登录状态或者相关数据,跳转到登录界面或者其他需要登录的界面
|
|
Intent intent = new Intent(getActivity(), MainActivity.class);
|
|
startActivity(intent);
|
|
getActivity().finish();
|
|
}
|
|
|
|
private void refresh() {
|
|
fetchUserInfo();
|
|
}
|
|
|
|
@Override
|
|
public void onHiddenChanged(boolean hidden) {
|
|
super.onHiddenChanged(hidden);
|
|
if (!hidden) {
|
|
refresh();
|
|
}
|
|
}
|
|
|
|
} |