diff --git a/app/src/main/java/com/community/pocket/data/login/LoginRequest.java b/app/src/main/java/com/community/pocket/data/login/LoginRequest.java index e4b5dc3..7edb16c 100644 --- a/app/src/main/java/com/community/pocket/data/login/LoginRequest.java +++ b/app/src/main/java/com/community/pocket/data/login/LoginRequest.java @@ -1,13 +1,18 @@ package com.community.pocket.data.login; +import androidx.lifecycle.MutableLiveData; + import com.community.pocket.R; -import com.community.pocket.data.model.Token; import com.community.pocket.ui.login.LoginResponse; +import com.community.pocket.util.HttpRequest; +import com.community.pocket.util.HttpUtil; +import com.community.pocket.util.SimpleHttpParse; + +import okhttp3.FormBody; /** * 登陆请求接口 - * TODO 完善逻辑 */ public class LoginRequest { private static volatile LoginRequest instance; @@ -22,14 +27,13 @@ public class LoginRequest { return instance; } - public LoginResponse login(String username, String password) { - Token token = new Token(); - token.setTime(System.currentTimeMillis()); - token.setToken("123"); + @HttpRequest("/login") + public void login(MutableLiveData liveData, String username, String password) { - LoginResponse loginResponse = new LoginResponse(); - loginResponse.setSuccess(R.string.login_ok, username); - loginResponse.setBody(token); - return loginResponse; + HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData, R.string.register_ok, null).getInterface(LoginResponse.class), + new FormBody.Builder() + .add("username", username) + .add("password", password) + .build()); } } diff --git a/app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java b/app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java index 348a558..ecca190 100644 --- a/app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java @@ -31,7 +31,7 @@ public class GarbageSortingRequest { @HttpRequest("/garbage/sorting") public void search(MutableLiveData liveData, String name) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.garbage_search_ok, R.string.garbage_search_ok).getInterface(GarbageSortingResponse.class), + new SimpleHttpParse(liveData, R.string.garbage_search_ok, null).getInterface(GarbageSortingResponse.class), new FormBody.Builder().add("name", name).build()); } } diff --git a/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java b/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java index e7f8fe4..77aa554 100644 --- a/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java +++ b/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java @@ -1,6 +1,13 @@ package com.community.pocket.data.register; -import com.community.pocket.util.Valid; +import androidx.lifecycle.MutableLiveData; + +import com.community.pocket.ui.register.RegisterResponse; +import com.community.pocket.util.HttpRequest; +import com.community.pocket.util.HttpUtil; +import com.community.pocket.util.SimpleHttpParse; + +import okhttp3.FormBody; /** * 该类请求注册接口完成注册操作 @@ -18,8 +25,14 @@ public class RegisterRequest { return instance; } - //TODO 接口请求逻辑 - public Valid register(String username, String password, String confirmPassword, String mobilePhone, String email) { - return Valid.ok; + @HttpRequest("/register") + public void register(MutableLiveData liveData, String username, String password, String mobilePhone, String email) { + HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(RegisterResponse.class), + new FormBody.Builder() + .add("username", username) + .add("password", password) + .add("mobilePhone", mobilePhone) + .add("email", email) + .build()); } } diff --git a/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java b/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java index 5548612..9753963 100644 --- a/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java +++ b/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java @@ -1,10 +1,16 @@ package com.community.pocket.data.resetpwd; +import androidx.lifecycle.MutableLiveData; + import com.community.pocket.ui.resetpwd.ResetPwdResponse; +import com.community.pocket.util.HttpRequest; +import com.community.pocket.util.HttpUtil; +import com.community.pocket.util.SimpleHttpParse; + +import okhttp3.FormBody; /** * 重置密码请求 - * TODO 接口请求逻辑完善 */ public class ResetPwdRequest { private static volatile ResetPwdRequest instance; @@ -21,34 +27,50 @@ public class ResetPwdRequest { /** * 检查用户邮箱 - * TODO R.string.resetpwd_step1_ok */ - public ResetPwdResponse checkUser(String username, String email) { - return new ResetPwdResponse(); + @HttpRequest("/resetpwd/checkUser") + public void checkUser(MutableLiveData liveData, String username, String email) { + HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(ResetPwdResponse.class), + new FormBody.Builder() + .add("username", username) + .add("email", email) + .build()); } /** * 发送验证码 - * TODO R.string.resetpwd_step2_ok */ - public ResetPwdResponse sendCode(String username, String email) { - return new ResetPwdResponse(); + @HttpRequest("/resetpwd/sendCode") + public void sendCode(MutableLiveData liveData, String username, String email) { + HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(ResetPwdResponse.class), + new FormBody.Builder() + .add("username", username) + .add("email", email) + .build()); } /** * 检查验证码 - * TODO R.string.resetpwd_step2_valid_ok */ - public ResetPwdResponse checkCode(String username, String email, String code) { - return new ResetPwdResponse(); + @HttpRequest("/resetpwd/checkCode") + public void checkCode(MutableLiveData liveData, String username, String code) { + HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(ResetPwdResponse.class), + new FormBody.Builder() + .add("username", username) + .add("code", code) + .build()); } /** * 重置密码 - * TODO R.string.resetpwd_step3_ok */ - public ResetPwdResponse resetPwd(String username, String password) { - return new ResetPwdResponse(); + @HttpRequest("/resetpwd/resetPwd") + public void resetPwd(MutableLiveData liveData, String username, String password) { + HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(ResetPwdResponse.class), + new FormBody.Builder() + .add("username", username) + .add("password", password) + .build()); } } diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java index 3a711cb..3e7652e 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java @@ -10,7 +10,6 @@ import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; -import android.widget.Toast; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; @@ -21,6 +20,7 @@ import com.community.pocket.R; import com.community.pocket.ui.BaseActivity; import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.main.MainMenu; +import com.community.pocket.ui.main.ui.share.Response; import com.community.pocket.ui.register.RegisterActivity; import com.community.pocket.ui.resetpwd.ResetPwdActivity; import com.community.pocket.util.PropertiesUtil; @@ -85,18 +85,14 @@ public class LoginActivity extends BaseActivity { return; } loadingProgressBar.setVisibility(View.GONE); - - if (loginResponse.getError() != null) { - Toast.makeText(getApplicationContext(), loginResponse.fail(getApplicationContext()), Toast.LENGTH_SHORT).show(); - } - if (loginResponse.getSuccess() != null) { - Toast.makeText(getApplicationContext(), loginResponse.ok(getApplicationContext()), Toast.LENGTH_SHORT).show(); - } setResult(Activity.RESULT_OK); - startActivity(new Intent(getApplicationContext(), MainMenu.class)); - //Complete and destroy login activity once successful - finish(); + loginResponse.toast(getBaseContext()); + if (loginResponse.getResult() == Response.Result.OK) { + startActivity(new Intent(getBaseContext(), MainMenu.class)); + //Complete and destroy login activity once successful + finish(); + } } }); diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java b/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java index b7f200d..500f3ae 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java @@ -1,11 +1,47 @@ package com.community.pocket.ui.login; +import androidx.annotation.StringRes; + +import com.community.pocket.R; import com.community.pocket.data.model.Token; -import com.community.pocket.ui.main.ui.share.Response; +import com.community.pocket.ui.main.ui.share.ToastResponse; +import com.community.pocket.util.CustomMessage; + +import org.jetbrains.annotations.NotNull; /** * 登陆响应结果 */ -public class LoginResponse extends Response { +public class LoginResponse extends ToastResponse { + private Token token; + + public Token getToken() { + return token; + } + + public void setToken(Token token) { + this.token = token; + } + + + enum Msg implements CustomMessage { + ok(R.string.login_ok), + fail(R.string.login_fail); + + private Integer msg; + + Msg(@StringRes Integer msg) { + this.msg = msg; + } + + @NotNull + @Override + public Integer getMsg() { + return msg; + } + public void setMsg(Integer msg) { + this.msg = msg; + } + } } diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java b/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java index 2ea0dda..6aae713 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java @@ -28,8 +28,7 @@ public class LoginViewModel extends BaseViewModel { //登陆 public void login(String username, String password) { - LoginResponse response = getRequest().login(username, password); - loginResult.setValue(response); + getRequest().login(loginResult, username, password); } //登陆表单数据变化触发数据校验 diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/sorting/GarbageSortingFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/sorting/GarbageSortingFragment.java index 2bc1d67..6211ccc 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/sorting/GarbageSortingFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/sorting/GarbageSortingFragment.java @@ -6,7 +6,6 @@ import android.view.View; import android.widget.AutoCompleteTextView; import android.widget.ImageView; import android.widget.TextView; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -68,7 +67,7 @@ public class GarbageSortingFragment extends BaseFragment { } if (garbageSortingResponse.getError() != null) { - Toast.makeText(getContext(), garbageSortingResponse.ok(getContext()), Toast.LENGTH_SHORT).show(); + garbageSortingResponse.fail(getContext()); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/waste/GarbageWasteFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/waste/GarbageWasteFragment.java index 2485b65..994b3e1 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/waste/GarbageWasteFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/waste/GarbageWasteFragment.java @@ -7,7 +7,6 @@ import android.view.Gravity; import android.view.View; import android.widget.AutoCompleteTextView; import android.widget.TextView; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -23,6 +22,7 @@ import com.community.pocket.data.model.GarbageWastePrice; import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView; +import com.community.pocket.ui.main.ui.share.Response; import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ViewInject; @@ -73,7 +73,7 @@ public class GarbageWasteFragment extends BaseFragment { } if (garbageWasteResponse.getError() != null) { - Toast.makeText(getContext(), garbageWasteResponse.fail(getContext()), Toast.LENGTH_SHORT).show(); + garbageWasteResponse.fail(getContext()); } } }); @@ -86,12 +86,11 @@ public class GarbageWasteFragment extends BaseFragment { return; } - if (garbageWasteResponse.getSuccess() != null) { - initSearch(garbageWasteResponse.getBody()); - } - if (garbageWasteResponse.getError() != null) { - Toast.makeText(getContext(), garbageWasteResponse.fail(getContext()), Toast.LENGTH_SHORT).show(); + if (garbageWasteResponse.getResult() == Response.Result.OK) { + initSearch(garbageWasteResponse.getBody()); + } else { + garbageWasteResponse.fail(getContext()); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java index a4e8866..b11fd88 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java @@ -1,10 +1,13 @@ package com.community.pocket.ui.main.ui.share; import android.content.Context; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import com.community.pocket.R; import com.google.gson.Gson; /** @@ -15,11 +18,13 @@ import com.google.gson.Gson; public abstract class Response { //成功描述 @Nullable + @Deprecated private Integer success; //失败描述 @Nullable + @Deprecated private Integer error; - + //描述参数 private Object[] args; //响应体 private T body; @@ -46,23 +51,29 @@ public abstract class Response { } @Nullable + @Deprecated + // TODO 废弃 public Integer getSuccess() { return success; } - public void setSuccess(@Nullable Integer success, Object... args) { + @Deprecated + // TODO 废弃 + public void setSuccess(@Nullable @StringRes Integer success) { this.success = success; - this.args = args; } @Nullable + @Deprecated + // TODO 废弃 public Integer getError() { return error; } - public void setError(@Nullable Integer error, Object... args) { + @Deprecated + // TODO 废弃 + public void setError(@Nullable @StringRes Integer error) { this.error = error; - this.args = args; } public T getBody() { @@ -73,21 +84,7 @@ public abstract class Response { this.body = body; } - public String ok(Context context) { - if (success != null) { - return context.getString(success, args); - } - return null; - } - - public String fail(Context context) { - if (error != null) { - return context.getString(error, args); - } - return null; - } - - public Object[] getArgs() { + Object[] getArgs() { return args; } @@ -95,6 +92,19 @@ public abstract class Response { this.args = args; } + public void ok(Context context) { + if (success != null) { + Toast.makeText(context, context.getString(success, args), Toast.LENGTH_SHORT).show(); + } + } + + public void fail(Context context) { + if (error != null) { + Toast.makeText(context, context.getString(error, args), Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(context, context.getString(R.string.unknow_error), Toast.LENGTH_LONG).show(); + } + } public enum Result { OK, diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java new file mode 100644 index 0000000..bb93b77 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java @@ -0,0 +1,24 @@ +package com.community.pocket.ui.main.ui.share; + +import android.content.Context; +import android.widget.Toast; + +import com.community.pocket.R; +import com.community.pocket.util.CustomMessage; + +/** + * 弹窗响应 + * + * @param + */ +public abstract class ToastResponse extends Response { + + public void toast(Context context) { + if (getBody() != null) { + Toast.makeText(context, context.getString(getBody().getMsg(), getArgs()), Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(context, R.string.unknow_error, Toast.LENGTH_LONG).show(); + } + } + +} diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java index a13e85d..4e6ebaf 100644 --- a/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java +++ b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java @@ -8,7 +8,6 @@ import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; -import android.widget.Toast; import androidx.annotation.Nullable; import androidx.lifecycle.Observer; @@ -114,16 +113,14 @@ public class RegisterActivity extends BaseActivity { return; } - if (registerResponse.getError() != null) { - Toast.makeText(getApplicationContext(), R.string.register_fail, Toast.LENGTH_SHORT).show(); - } - if (registerResponse.getSuccess() != null) { - Toast.makeText(getApplicationContext(), R.string.register_ok, Toast.LENGTH_SHORT).show(); + registerResponse.toast(getBaseContext()); + if (registerResponse.getBody() == RegisterResponse.Msg.ok) { startActivity(new Intent(getApplicationContext(), LoginActivity.class)); //Complete and destroy login activity once successful finish(); - } + + } setResult(Activity.RESULT_OK); } @@ -133,7 +130,7 @@ public class RegisterActivity extends BaseActivity { @Event(value = R.id.register) private void register(View v) { - registerViewModel.register(username.getText().toString(), password.getText().toString(), confirmPassword.getText().toString(), mobiePhone.getText().toString(), email.getText().toString()); + registerViewModel.register(username.getText().toString(), password.getText().toString(), mobiePhone.getText().toString(), email.getText().toString()); } @Event(value = R.id.back) diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterResponse.java b/app/src/main/java/com/community/pocket/ui/register/RegisterResponse.java index 62a4dd5..0accf5a 100644 --- a/app/src/main/java/com/community/pocket/ui/register/RegisterResponse.java +++ b/app/src/main/java/com/community/pocket/ui/register/RegisterResponse.java @@ -1,33 +1,41 @@ package com.community.pocket.ui.register; -import androidx.annotation.Nullable; +import androidx.annotation.StringRes; + +import com.community.pocket.R; +import com.community.pocket.ui.main.ui.share.ToastResponse; +import com.community.pocket.util.CustomMessage; + +import org.jetbrains.annotations.NotNull; /** * 注册结果 */ -class RegisterResponse { - @Nullable - private Integer success; - @Nullable - private Integer error; - - @Nullable - Integer getSuccess() { - return success; - } +public class RegisterResponse extends ToastResponse { - RegisterResponse setSuccess(@Nullable Integer success) { - this.success = success; - return this; - } + /** + * 响应类型 + */ + enum Msg implements CustomMessage { + ok(R.string.register_ok), + name(R.string.register_fail_name_repeat), + mobie(R.string.register_fail_mobie_repeat), + email(R.string.register_fail_email_repeat); - @Nullable - Integer getError() { - return error; - } + private Integer msg; + + Msg(@StringRes Integer msg) { + this.msg = msg; + } + + @NotNull + public @StringRes + Integer getMsg() { + return msg; + } - RegisterResponse setError(@Nullable Integer error) { - this.error = error; - return this; + public void setMsg(Integer msg) { + this.msg = msg; + } } } diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java b/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java index 2bc63ec..68af8cd 100644 --- a/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java @@ -5,7 +5,6 @@ import androidx.lifecycle.ViewModel; import com.community.pocket.R; import com.community.pocket.data.register.RegisterRequest; -import com.community.pocket.util.Valid; import com.community.pocket.util.ValidUtil; /** @@ -34,13 +33,8 @@ class RegisterViewModel extends ViewModel { } //注册 - void register(String username, String password, String confirmPassword, String mobilePhone, String email) { - Valid valid = registerRequest.register(username, password, confirmPassword, mobilePhone, email); - if (Valid.ok == valid) { - registerResult.setValue(new RegisterResponse().setSuccess(R.string.register_ok)); - } else { - registerResult.setValue(new RegisterResponse().setError(R.string.register_fail)); - } + void register(String username, String password, String mobilePhone, String email) { + registerRequest.register(registerResult, username, password, mobilePhone, email); } //监听注册表单数据变化触发数据校验 diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdResponse.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdResponse.java index 7f10b7c..0bb1e5e 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdResponse.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdResponse.java @@ -1,33 +1,42 @@ package com.community.pocket.ui.resetpwd; -import androidx.annotation.Nullable; +import androidx.annotation.StringRes; + +import com.community.pocket.R; +import com.community.pocket.ui.main.ui.share.ToastResponse; +import com.community.pocket.util.CustomMessage; + +import org.jetbrains.annotations.NotNull; /** * 重置密码结果 */ -public class ResetPwdResponse { - @Nullable - private Integer success; - @Nullable - private Integer error; - - @Nullable - Integer getSuccess() { - return success; - } +public class ResetPwdResponse extends ToastResponse { - ResetPwdResponse setSuccess(@Nullable Integer success) { - this.success = success; - return this; - } + enum Msg implements CustomMessage { + step1_ok(R.string.resetpwd_step1_ok), + step1_fail(R.string.resetpwd_step1_fail), + step2_ok(R.string.resetpwd_step2_ok), + step2_fail(R.string.resetpwd_step2_fail), + step2_valid_ok(R.string.resetpwd_step2_valid_ok), + step2_valid_fail(R.string.resetpwd_step2_valid_fail), + step3_ok(R.string.resetpwd_step3_ok), + step3_fail(R.string.resetpwd_step3_fail); - @Nullable - Integer getError() { - return error; - } + private Integer msg; + + Msg(@StringRes Integer msg) { + this.msg = msg; + } + + public void setMsg(Integer msg) { + this.msg = msg; + } - ResetPwdResponse setError(@Nullable Integer error) { - this.error = error; - return this; + @NotNull + @Override + public Integer getMsg() { + return msg; + } } } diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java index 0733fc7..5b389ca 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java @@ -9,7 +9,6 @@ import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -22,6 +21,7 @@ import com.community.pocket.R; 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.Param; import com.community.pocket.util.PropertiesUtil; @@ -92,13 +92,9 @@ public class ResetPwdStep1 extends BaseFragment { return; } - if (step1.getError() != null) { - Toast.makeText(getContext(), step1.getError(), Toast.LENGTH_LONG).show(); - } - - if (step1.getSuccess() != null) { - Toast.makeText(getContext(), step1.getSuccess(), Toast.LENGTH_LONG).show(); + step1.toast(getContext()); + if (step1.getResult() == Response.Result.OK) { Bundle bundle = new Bundle(); bundle.putString(Param.username.name(), username.getText().toString()); bundle.putString(Param.email.name(), email.getText().toString()); diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java index aa2fb2d..b1bbe72 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java @@ -7,7 +7,6 @@ import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -19,6 +18,7 @@ import androidx.navigation.fragment.NavHostFragment; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.listener.MyTextChange; +import com.community.pocket.ui.main.ui.share.Response; import com.community.pocket.util.Param; import org.xutils.view.annotation.ContentView; @@ -98,31 +98,35 @@ public class ResetPwdStep2 extends BaseFragment { }; code.addTextChangedListener(afterTextChangedListener); - resetPwdViewModel.getResetPwdResultStep2().observe(getViewLifecycleOwner(), new Observer() { + //监听发送验证码状态 + resetPwdViewModel.getSendCodeResponse().observe(getViewLifecycleOwner(), new Observer() { @Override public void onChanged(ResetPwdResponse resetPwdResponse) { if (resetPwdResponse == null) { return; } - if (resetPwdResponse.getSuccess() != null) { - Toast.makeText(getContext(), resetPwdResponse.getSuccess(), Toast.LENGTH_LONG).show(); - if (resetPwdResponse.getSuccess() == R.string.resetpwd_step2_ok) { - code.setEnabled(true); - } else { - Bundle bundle = new Bundle(); + code.setEnabled(resetPwdResponse.getResult() == Response.Result.OK); + resetPwdResponse.toast(getContext()); + } + }); + + //监听校验验证码状态 + resetPwdViewModel.getCheckCodeResponse().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(ResetPwdResponse resetPwdResponse) { + if (resetPwdResponse == null) { + return; + } + + resetPwdResponse.toast(getContext()); + if (resetPwdResponse.getResult() == Response.Result.OK) { + Bundle bundle = new Bundle(); bundle.putString(Param.username.name(), username.getText().toString()); NavHostFragment.findNavController(ResetPwdStep2.this) .navigate(R.id.action_Second2Fragment_to_threeFragment, bundle); - } } - if (resetPwdResponse.getError() != null) { - Toast.makeText(getContext(), resetPwdResponse.getError(), Toast.LENGTH_LONG).show(); - if (resetPwdResponse.getError() == R.string.resetpwd_step2_fail) { - code.setEnabled(false); - } - } } }); } @@ -132,7 +136,7 @@ public class ResetPwdStep2 extends BaseFragment { */ @Event(value = R.id.next) private void next(View view) { - resetPwdViewModel.checkCode(username.getText().toString(), email.getText().toString(), code.getText().toString()); + resetPwdViewModel.checkCode(username.getText().toString(), code.getText().toString()); } /** diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java index 8a9b68d..e8d94ac 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java @@ -8,7 +8,6 @@ import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -20,6 +19,7 @@ import com.community.pocket.R; 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.Param; import com.community.pocket.util.PropertiesUtil; @@ -94,13 +94,10 @@ public class ResetPwdStep3 extends BaseFragment { return; } - if (resetPwdResponse.getSuccess() != null) { - Toast.makeText(getContext(), getString(R.string.resetpwd_step3_ok), Toast.LENGTH_LONG).show(); - startActivity(new Intent(getContext(), LoginActivity.class)); + resetPwdResponse.toast(getContext()); - } - if (resetPwdResponse.getError() != null) { - Toast.makeText(getContext(), getString(R.string.resetpwd_step3_fail), Toast.LENGTH_LONG).show(); + if (resetPwdResponse.getResult() == Response.Result.OK) { + startActivity(new Intent(getContext(), LoginActivity.class)); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java index 70f94a8..f0573b0 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java @@ -19,7 +19,10 @@ class ResetPwdViewModel extends ViewModel { //重置密码第二步校验状态 private MutableLiveData resetPwdFormStep2 = new MutableLiveData<>(); //重置密码第二步发送验证码结果 - private MutableLiveData resetPwdResultStep2 = new MutableLiveData<>(); + private MutableLiveData sendCodeResponse = new MutableLiveData<>(); + //重置密码验证码校验结果 + private MutableLiveData checkCodeResponse = new MutableLiveData<>(); + //重置密码第三步校验状态 private MutableLiveData resetPwdFormStep3 = new MutableLiveData<>(); //重置密码第三部重置结果 @@ -44,8 +47,12 @@ class ResetPwdViewModel extends ViewModel { return resetPwdFormStep2; } - MutableLiveData getResetPwdResultStep2() { - return resetPwdResultStep2; + MutableLiveData getSendCodeResponse() { + return sendCodeResponse; + } + + MutableLiveData getCheckCodeResponse() { + return checkCodeResponse; } MutableLiveData getResetPwdFormStep3() { @@ -58,26 +65,22 @@ class ResetPwdViewModel extends ViewModel { //重置密码第一步 void checkUser(String username, String email) { - ResetPwdResponse response = resetPwdRequest.checkUser(username, email); - resetPwdResultStep1.setValue(response); + resetPwdRequest.checkUser(resetPwdResultStep1, username, email); } //重置密码第二步 void sendCode(String username, String email) { - ResetPwdResponse response = resetPwdRequest.sendCode(username, email); - resetPwdResultStep2.setValue(response); + resetPwdRequest.sendCode(sendCodeResponse, username, email); } //重置密码第二步 - void checkCode(String username, String email, String code) { - ResetPwdResponse response = resetPwdRequest.checkCode(username, email, code); - resetPwdResultStep2.setValue(response); + void checkCode(String username, String code) { + resetPwdRequest.checkCode(checkCodeResponse, username, code); } //重置密码第三步 void resetPwd(String username, String password) { - ResetPwdResponse response = resetPwdRequest.resetPwd(username, password); - resetPwdResultStep3.setValue(response); + resetPwdRequest.resetPwd(resetPwdResultStep3, username, password); } //监听重置密码第一步表单触发校验 diff --git a/app/src/main/java/com/community/pocket/util/CustomMessage.java b/app/src/main/java/com/community/pocket/util/CustomMessage.java new file mode 100644 index 0000000..4643311 --- /dev/null +++ b/app/src/main/java/com/community/pocket/util/CustomMessage.java @@ -0,0 +1,14 @@ +package com.community.pocket.util; + +import androidx.annotation.StringRes; + +import org.jetbrains.annotations.NotNull; + +/** + * 自定义响应信息 + */ +public interface CustomMessage { + @StringRes + @NotNull + Integer getMsg(); +} diff --git a/app/src/main/java/com/community/pocket/util/SimpleHttpParse.java b/app/src/main/java/com/community/pocket/util/SimpleHttpParse.java index 1158e35..9c3fc6c 100644 --- a/app/src/main/java/com/community/pocket/util/SimpleHttpParse.java +++ b/app/src/main/java/com/community/pocket/util/SimpleHttpParse.java @@ -8,20 +8,36 @@ import org.jetbrains.annotations.NotNull; import okhttp3.Call; import okhttp3.Response; +/** + * HTTP响应实体 + * + * @param + */ public class SimpleHttpParse implements HttpParse { private MutableLiveData liveData; + //TODO 废弃 + @Deprecated private Integer success; + //TODO 废弃 + @Deprecated private Integer error; + + //TODO 废弃 + @Deprecated public SimpleHttpParse(MutableLiveData liveData, @StringRes Integer success, @StringRes Integer error) { this.liveData = liveData; this.success = success; this.error = error; } + public SimpleHttpParse(MutableLiveData liveData) { + this.liveData = liveData; + } + public HttpResponse getInterface(Class tClass) { return new HttpResponse<>(tClass, this); } diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 08d6f05..327cd06 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -28,7 +28,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_user" android:inputType="text" android:selectAllOnFocus="true" @@ -43,7 +43,7 @@ android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:autofillHints="@string/AUTOFILL_HINT_PASSWORD" + android:importantForAutofill="no" android:hint="@string/prompt_password" android:imeActionLabel="@string/action_sign_in_short" android:inputType="textPassword" diff --git a/app/src/main/res/layout/activity_register.xml b/app/src/main/res/layout/activity_register.xml index afdd7f6..eb9d973 100644 --- a/app/src/main/res/layout/activity_register.xml +++ b/app/src/main/res/layout/activity_register.xml @@ -27,7 +27,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_user" android:inputType="text" android:selectAllOnFocus="true" @@ -41,7 +41,7 @@ android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_password" android:inputType="text|textPassword" android:selectAllOnFocus="true" @@ -55,7 +55,7 @@ android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_confirm_password" android:inputType="text|textPassword" android:selectAllOnFocus="true" @@ -69,7 +69,7 @@ android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_phone" android:inputType="text|phone" android:selectAllOnFocus="true" @@ -83,7 +83,7 @@ android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_email" android:inputType="none|textEmailAddress" android:selectAllOnFocus="true" diff --git a/app/src/main/res/layout/resetpwd/layout/step1.xml b/app/src/main/res/layout/resetpwd/layout/step1.xml index ed44798..188345d 100644 --- a/app/src/main/res/layout/resetpwd/layout/step1.xml +++ b/app/src/main/res/layout/resetpwd/layout/step1.xml @@ -11,7 +11,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_user" android:inputType="text" android:selectAllOnFocus="true" @@ -25,7 +25,7 @@ android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" + android:importantForAutofill="no" android:hint="@string/prompt_email" android:inputType="text|textEmailAddress" android:selectAllOnFocus="true" diff --git a/app/src/main/res/layout/resetpwd/layout/step2.xml b/app/src/main/res/layout/resetpwd/layout/step2.xml index 09ea476..3296d56 100644 --- a/app/src/main/res/layout/resetpwd/layout/step2.xml +++ b/app/src/main/res/layout/resetpwd/layout/step2.xml @@ -11,14 +11,14 @@ android:layout_width="0dp" android:layout_height="wrap_content" - android:autofillHints="@string/AUTOFILL_HINT_USERNAME" android:enabled="false" android:hint="@string/prompt_user" android:selectAllOnFocus="true" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - tools:ignore="TextFields" /> + tools:ignore="TextFields" + android:importantForAutofill="no" /> + tools:ignore="TextFields" + android:importantForAutofill="no" /> "Welcome !" Username must be >%1$s characters Password must be >%1$s characters - "Login failed" system language chs en @@ -95,7 +94,6 @@ Cell phone Numbers are illegal Invalid mailbox register OK - register fail username or email error Check success please input code @@ -163,4 +161,9 @@ search success name price + username repeat + mobie repeat + email repeat + unknow error + login fail \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 0fa6636..06b0cf2 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -7,7 +7,6 @@ 登陆 "欢迎 !" 密码必须大于%1$s个字符 - "登陆失败" 系统语言 简体中文 英文 @@ -94,7 +93,6 @@ 手机号码不合法 邮箱不合法 注册成功 - 注册失败 用户名必须大于%1$s个字符 用户名或邮箱错误 校验成功 @@ -163,4 +161,9 @@ 检索废品成功 废品 价格 + 用户名%1s已注册 + 手机号%1s已绑定 + 邮箱%1s已绑定 + 未知错误,请联系管理员 + 登陆失败,账号或密码错误 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f089793..9537cf9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,7 +8,6 @@ "Welcome !" Username must be >%1$s characters Password must be >%1$s characters - "Login failed" system language chs en @@ -96,7 +95,6 @@ Cell phone Numbers are illegal Invalid mailbox register OK - register fail username or email error Check success please input code @@ -164,6 +162,11 @@ search success name price + username repeat + mobie repeat + email repeat + unknow error + login fail