|
|
@ -12,10 +12,9 @@ import android.widget.Button; |
|
|
|
import android.widget.EditText; |
|
|
|
import android.widget.EditText; |
|
|
|
import android.widget.ProgressBar; |
|
|
|
import android.widget.ProgressBar; |
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
|
|
import androidx.annotation.RequiresApi; |
|
|
|
import androidx.annotation.RequiresApi; |
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout; |
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout; |
|
|
|
import androidx.lifecycle.Observer; |
|
|
|
import androidx.lifecycle.MutableLiveData; |
|
|
|
import androidx.lifecycle.ViewModelProvider; |
|
|
|
import androidx.lifecycle.ViewModelProvider; |
|
|
|
|
|
|
|
|
|
|
|
import com.community.pocket.R; |
|
|
|
import com.community.pocket.R; |
|
|
@ -28,7 +27,6 @@ import com.community.pocket.ui.main.ui.share.Response; |
|
|
|
import com.community.pocket.ui.register.RegisterActivity; |
|
|
|
import com.community.pocket.ui.register.RegisterActivity; |
|
|
|
import com.community.pocket.ui.resetpwd.ResetPwdActivity; |
|
|
|
import com.community.pocket.ui.resetpwd.ResetPwdActivity; |
|
|
|
import com.community.pocket.util.AppDatabase; |
|
|
|
import com.community.pocket.util.AppDatabase; |
|
|
|
import com.community.pocket.util.HttpParse; |
|
|
|
|
|
|
|
import com.community.pocket.util.HttpRequest; |
|
|
|
import com.community.pocket.util.HttpRequest; |
|
|
|
import com.community.pocket.util.HttpResponse; |
|
|
|
import com.community.pocket.util.HttpResponse; |
|
|
|
import com.community.pocket.util.HttpUtil; |
|
|
|
import com.community.pocket.util.HttpUtil; |
|
|
@ -41,7 +39,6 @@ import org.xutils.view.annotation.ViewInject; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import okhttp3.Call; |
|
|
|
|
|
|
|
import okhttp3.FormBody; |
|
|
|
import okhttp3.FormBody; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -74,17 +71,16 @@ public class LoginActivity extends BaseActivity { |
|
|
|
@ViewInject(R.id.login_layout) |
|
|
|
@ViewInject(R.id.login_layout) |
|
|
|
private ConstraintLayout layout; |
|
|
|
private ConstraintLayout layout; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private MutableLiveData<Boolean> bool = new MutableLiveData<>(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 保存令牌到数据库并跳转到主界面 |
|
|
|
* 保存令牌到数据库并跳转到主界面 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void saveTokenToDB(@NotNull final LoginResponse response) { |
|
|
|
private void saveTokenToDB(@NotNull final LoginResponse response) { |
|
|
|
new Thread(new Runnable() { |
|
|
|
new Thread(() -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
Token token = response.getToken(); |
|
|
|
Token token = response.getToken(); |
|
|
|
sInstance.tokenDao().save(token); |
|
|
|
sInstance.tokenDao().save(token); |
|
|
|
saveToken(response); |
|
|
|
saveToken(response); |
|
|
|
} |
|
|
|
|
|
|
|
}).start(); |
|
|
|
}).start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -107,29 +103,35 @@ public class LoginActivity extends BaseActivity { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
HttpUtil.checkServer(this); |
|
|
|
HttpUtil.checkServer(this, bool); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool.observe(this, aBoolean -> { |
|
|
|
|
|
|
|
if (aBoolean) { |
|
|
|
|
|
|
|
init(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void init() { |
|
|
|
loginViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(LoginViewModel.class); |
|
|
|
loginViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(LoginViewModel.class); |
|
|
|
|
|
|
|
|
|
|
|
//后台检查token
|
|
|
|
//后台检查token
|
|
|
|
hasOnlyUserToken(); |
|
|
|
hasOnlyUserToken(); |
|
|
|
|
|
|
|
|
|
|
|
//监听token校验状态
|
|
|
|
//监听token校验状态
|
|
|
|
loginViewModel.getCheckToken().observe(this, new Observer<LoginResponse>() { |
|
|
|
loginViewModel.getCheckToken().observe(this, loginResponse -> { |
|
|
|
@Override |
|
|
|
if (loginResponse.getResult() == Response.Result.OK) { |
|
|
|
public void onChanged(LoginResponse loginResponse) { |
|
|
|
|
|
|
|
if (loginResponse.getResult() == com.community.pocket.ui.main.ui.share.Response.Result.OK) { |
|
|
|
|
|
|
|
saveToken(loginResponse); |
|
|
|
saveToken(loginResponse); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
layout.setVisibility(View.VISIBLE); |
|
|
|
layout.setVisibility(View.VISIBLE); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
//监听数据校验状态
|
|
|
|
//监听数据校验状态
|
|
|
|
loginViewModel.getLoginFormState().observe(this, new Observer<LoginFormState>() { |
|
|
|
loginViewModel.getLoginFormState().observe(this, loginFormState -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
public void onChanged(@Nullable LoginFormState loginFormState) { |
|
|
|
|
|
|
|
if (loginFormState == null) { |
|
|
|
if (loginFormState == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -140,13 +142,10 @@ public class LoginActivity extends BaseActivity { |
|
|
|
if (loginFormState.getPasswordError() != null) { |
|
|
|
if (loginFormState.getPasswordError() != null) { |
|
|
|
passwordEditText.setError(getString(loginFormState.getPasswordError(), PropertiesUtil.getIntValue("password.length"))); |
|
|
|
passwordEditText.setError(getString(loginFormState.getPasswordError(), PropertiesUtil.getIntValue("password.length"))); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
//监听登陆请求结果
|
|
|
|
//监听登陆请求结果
|
|
|
|
loginViewModel.getLoginResult().observe(this, new Observer<LoginResponse>() { |
|
|
|
loginViewModel.getLoginResult().observe(this, loginResponse -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
public void onChanged(@Nullable LoginResponse loginResponse) { |
|
|
|
|
|
|
|
if (loginResponse == null) { |
|
|
|
if (loginResponse == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -158,7 +157,6 @@ public class LoginActivity extends BaseActivity { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
loginResponse.toast(getApplicationContext()); |
|
|
|
loginResponse.toast(getApplicationContext()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -176,7 +174,6 @@ public class LoginActivity extends BaseActivity { |
|
|
|
passwordEditText.addTextChangedListener(afterTextChangedListener); |
|
|
|
passwordEditText.addTextChangedListener(afterTextChangedListener); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 查询本地是否有且只有一个用户token,如果有则自动登录 |
|
|
|
* 查询本地是否有且只有一个用户token,如果有则自动登录 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -191,13 +188,7 @@ public class LoginActivity extends BaseActivity { |
|
|
|
List<Token> tokenList = sInstance.tokenDao().queryAll(); |
|
|
|
List<Token> tokenList = sInstance.tokenDao().queryAll(); |
|
|
|
if (tokenList != null && tokenList.size() == 1) { |
|
|
|
if (tokenList != null && tokenList.size() == 1) { |
|
|
|
Token currentToken = tokenList.get(0); |
|
|
|
Token currentToken = tokenList.get(0); |
|
|
|
HttpUtil.getRequest(HttpUtil.Method.POST, new HttpResponse<>(LoginResponse.class, new HttpParse<LoginResponse>() { |
|
|
|
HttpUtil.getRequest(HttpUtil.Method.POST, new HttpResponse<>(LoginResponse.class, (call, response, loginResponse) -> loginViewModel.getCheckToken().postValue(loginResponse)), |
|
|
|
@Override |
|
|
|
|
|
|
|
public void onParseOk(@NotNull Call call, @NotNull okhttp3.Response response, LoginResponse loginResponse) { |
|
|
|
|
|
|
|
loginViewModel.getCheckToken().postValue(loginResponse); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
new FormBody.Builder() |
|
|
|
new FormBody.Builder() |
|
|
|
.add("token", currentToken.getToken()) |
|
|
|
.add("token", currentToken.getToken()) |
|
|
|
.build() |
|
|
|
.build() |
|
|
|