ADD week 5
This commit is contained in:
15317
02-Easy5/E5/sources/ch/mod_p/sre24/e5/R.java
Normal file
15317
02-Easy5/E5/sources/ch/mod_p/sre24/e5/R.java
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,28 @@
|
||||
package ch.mod_p.sre24.e5.data;
|
||||
|
||||
import android.content.Context;
|
||||
import ch.mod_p.sre24.e5.R;
|
||||
import ch.mod_p.sre24.e5.data.Result;
|
||||
import ch.mod_p.sre24.e5.data.model.LoggedInUser;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class LoginDataSource {
|
||||
public void logout() {
|
||||
}
|
||||
|
||||
public Result<LoggedInUser> login(Context context, String str, String str2) {
|
||||
try {
|
||||
if (str.compareTo(context.getString(R.string.username)) != 0) {
|
||||
return new Result.Error(new Exception("Unknown username"));
|
||||
}
|
||||
if (new StringBuffer(str2).reverse().toString().compareTo(context.getString(R.string.password)) != 0) {
|
||||
return new Result.Error(new Exception("Wrong password"));
|
||||
}
|
||||
return new Result.Success(new LoggedInUser(UUID.randomUUID().toString(), str));
|
||||
} catch (Exception e) {
|
||||
return new Result.Error(new IOException("Error logging in", e));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package ch.mod_p.sre24.e5.data;
|
||||
|
||||
import android.content.Context;
|
||||
import ch.mod_p.sre24.e5.data.Result;
|
||||
import ch.mod_p.sre24.e5.data.model.LoggedInUser;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class LoginRepository {
|
||||
private static volatile LoginRepository instance;
|
||||
private final LoginDataSource dataSource;
|
||||
private LoggedInUser user = null;
|
||||
|
||||
private void setLoggedInUser(LoggedInUser loggedInUser) {
|
||||
this.user = loggedInUser;
|
||||
}
|
||||
|
||||
public boolean isLoggedIn() {
|
||||
return this.user != null;
|
||||
}
|
||||
|
||||
private LoginRepository(LoginDataSource loginDataSource) {
|
||||
this.dataSource = loginDataSource;
|
||||
}
|
||||
|
||||
public static LoginRepository getInstance(LoginDataSource loginDataSource) {
|
||||
if (instance == null) {
|
||||
instance = new LoginRepository(loginDataSource);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
this.user = null;
|
||||
this.dataSource.logout();
|
||||
}
|
||||
|
||||
public Result<LoggedInUser> login(Context context, String str, String str2) {
|
||||
Result<LoggedInUser> login = this.dataSource.login(context, str, str2);
|
||||
if (login instanceof Result.Success) {
|
||||
setLoggedInUser((LoggedInUser) ((Result.Success) login).getData());
|
||||
}
|
||||
return login;
|
||||
}
|
||||
}
|
43
02-Easy5/E5/sources/ch/mod_p/sre24/e5/data/Result.java
Normal file
43
02-Easy5/E5/sources/ch/mod_p/sre24/e5/data/Result.java
Normal file
@ -0,0 +1,43 @@
|
||||
package ch.mod_p.sre24.e5.data;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class Result<T> {
|
||||
private Result() {
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if (this instanceof Success) {
|
||||
return "Success[data=" + ((Success) this).getData().toString() + "]";
|
||||
}
|
||||
if (!(this instanceof Error)) {
|
||||
return "";
|
||||
}
|
||||
return "Error[exception=" + ((Error) this).getError().toString() + "]";
|
||||
}
|
||||
|
||||
public static final class Success<T> extends Result {
|
||||
private T data;
|
||||
|
||||
public T getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public Success(T t) {
|
||||
super();
|
||||
this.data = t;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Error extends Result {
|
||||
private final Exception error;
|
||||
|
||||
public Exception getError() {
|
||||
return this.error;
|
||||
}
|
||||
|
||||
public Error(Exception exc) {
|
||||
super();
|
||||
this.error = exc;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package ch.mod_p.sre24.e5.data.model;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class LoggedInUser {
|
||||
private final String displayName;
|
||||
private final String userId;
|
||||
|
||||
public String getDisplayName() {
|
||||
return this.displayName;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return this.userId;
|
||||
}
|
||||
|
||||
public LoggedInUser(String str, String str2) {
|
||||
this.userId = str;
|
||||
this.displayName = str2;
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package ch.mod_p.sre24.e5.databinding;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ProgressBar;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.viewbinding.ViewBinding;
|
||||
import androidx.viewbinding.ViewBindings;
|
||||
import ch.mod_p.sre24.e5.R;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public final class ActivityLoginBinding implements ViewBinding {
|
||||
public final ConstraintLayout container;
|
||||
public final ProgressBar loading;
|
||||
public final Button login;
|
||||
public final EditText password;
|
||||
private final ConstraintLayout rootView;
|
||||
public final EditText username;
|
||||
|
||||
@Override // androidx.viewbinding.ViewBinding
|
||||
public ConstraintLayout getRoot() {
|
||||
return this.rootView;
|
||||
}
|
||||
|
||||
private ActivityLoginBinding(ConstraintLayout constraintLayout, ConstraintLayout constraintLayout2, ProgressBar progressBar, Button button, EditText editText, EditText editText2) {
|
||||
this.rootView = constraintLayout;
|
||||
this.container = constraintLayout2;
|
||||
this.loading = progressBar;
|
||||
this.login = button;
|
||||
this.password = editText;
|
||||
this.username = editText2;
|
||||
}
|
||||
|
||||
public static ActivityLoginBinding inflate(LayoutInflater layoutInflater) {
|
||||
return inflate(layoutInflater, null, false);
|
||||
}
|
||||
|
||||
public static ActivityLoginBinding inflate(LayoutInflater layoutInflater, ViewGroup viewGroup, boolean z) {
|
||||
View inflate = layoutInflater.inflate(R.layout.activity_login, viewGroup, false);
|
||||
if (z) {
|
||||
viewGroup.addView(inflate);
|
||||
}
|
||||
return bind(inflate);
|
||||
}
|
||||
|
||||
public static ActivityLoginBinding bind(View view) {
|
||||
ConstraintLayout constraintLayout = (ConstraintLayout) view;
|
||||
int i = R.id.loading;
|
||||
ProgressBar progressBar = (ProgressBar) ViewBindings.findChildViewById(view, R.id.loading);
|
||||
if (progressBar != null) {
|
||||
i = R.id.login;
|
||||
Button button = (Button) ViewBindings.findChildViewById(view, R.id.login);
|
||||
if (button != null) {
|
||||
i = R.id.password;
|
||||
EditText editText = (EditText) ViewBindings.findChildViewById(view, R.id.password);
|
||||
if (editText != null) {
|
||||
i = R.id.username;
|
||||
EditText editText2 = (EditText) ViewBindings.findChildViewById(view, R.id.username);
|
||||
if (editText2 != null) {
|
||||
return new ActivityLoginBinding(constraintLayout, constraintLayout, progressBar, button, editText, editText2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new NullPointerException("Missing required view with ID: ".concat(view.getResources().getResourceName(i)));
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package ch.mod_p.sre24.e5.ui.login;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
class LoggedInUserView {
|
||||
private final String displayName;
|
||||
|
||||
String getDisplayName() {
|
||||
return this.displayName;
|
||||
}
|
||||
|
||||
LoggedInUserView(String str) {
|
||||
this.displayName = str;
|
||||
}
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
package ch.mod_p.sre24.e5.ui.login;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import ch.mod_p.sre24.e5.R;
|
||||
import ch.mod_p.sre24.e5.databinding.ActivityLoginBinding;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
private LoginViewModel loginViewModel;
|
||||
|
||||
@Override // androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity
|
||||
public void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
ActivityLoginBinding inflate = ActivityLoginBinding.inflate(getLayoutInflater());
|
||||
setContentView(inflate.getRoot());
|
||||
this.loginViewModel = (LoginViewModel) new ViewModelProvider(this, new LoginViewModelFactory()).get(LoginViewModel.class);
|
||||
final EditText editText = inflate.username;
|
||||
final EditText editText2 = inflate.password;
|
||||
final Button button = inflate.login;
|
||||
final ProgressBar progressBar = inflate.loading;
|
||||
this.loginViewModel.getLoginFormState().observe(this, new Observer() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda0
|
||||
@Override // androidx.lifecycle.Observer
|
||||
public final void onChanged(Object obj) {
|
||||
LoginActivity.this.m186lambda$onCreate$0$chmod_psre24e5uiloginLoginActivity(button, editText, editText2, (LoginFormState) obj);
|
||||
}
|
||||
});
|
||||
this.loginViewModel.getLoginResult().observe(this, new Observer() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda1
|
||||
@Override // androidx.lifecycle.Observer
|
||||
public final void onChanged(Object obj) {
|
||||
LoginActivity.this.m187lambda$onCreate$1$chmod_psre24e5uiloginLoginActivity(progressBar, (LoginResult) obj);
|
||||
}
|
||||
});
|
||||
TextWatcher textWatcher = new TextWatcher(this) { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity.1
|
||||
final /* synthetic */ LoginActivity this$0;
|
||||
|
||||
@Override // android.text.TextWatcher
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
}
|
||||
|
||||
@Override // android.text.TextWatcher
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
}
|
||||
|
||||
{
|
||||
this.this$0 = this;
|
||||
}
|
||||
|
||||
@Override // android.text.TextWatcher
|
||||
public void afterTextChanged(Editable editable) {
|
||||
this.this$0.loginViewModel.loginDataChanged(editText.getText().toString(), editText2.getText().toString());
|
||||
}
|
||||
};
|
||||
editText.addTextChangedListener(textWatcher);
|
||||
editText2.addTextChangedListener(textWatcher);
|
||||
editText2.setOnEditorActionListener(new TextView.OnEditorActionListener() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda2
|
||||
@Override // android.widget.TextView.OnEditorActionListener
|
||||
public final boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
|
||||
return LoginActivity.this.m188lambda$onCreate$2$chmod_psre24e5uiloginLoginActivity(editText, editText2, textView, i, keyEvent);
|
||||
}
|
||||
});
|
||||
button.setOnClickListener(new View.OnClickListener() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda3
|
||||
@Override // android.view.View.OnClickListener
|
||||
public final void onClick(View view) {
|
||||
LoginActivity.this.m189lambda$onCreate$3$chmod_psre24e5uiloginLoginActivity(progressBar, editText, editText2, view);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* renamed from: lambda$onCreate$0$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
|
||||
/* synthetic */ void m186lambda$onCreate$0$chmod_psre24e5uiloginLoginActivity(Button button, EditText editText, EditText editText2, LoginFormState loginFormState) {
|
||||
if (loginFormState == null) {
|
||||
return;
|
||||
}
|
||||
button.setEnabled(loginFormState.isDataValid());
|
||||
if (loginFormState.getUsernameError() != null) {
|
||||
editText.setError(getString(loginFormState.getUsernameError().intValue()));
|
||||
}
|
||||
if (loginFormState.getPasswordError() != null) {
|
||||
editText2.setError(getString(loginFormState.getPasswordError().intValue()));
|
||||
}
|
||||
}
|
||||
|
||||
/* renamed from: lambda$onCreate$1$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
|
||||
/* synthetic */ void m187lambda$onCreate$1$chmod_psre24e5uiloginLoginActivity(ProgressBar progressBar, LoginResult loginResult) {
|
||||
if (loginResult == null) {
|
||||
return;
|
||||
}
|
||||
progressBar.setVisibility(8);
|
||||
if (loginResult.getError() != null) {
|
||||
showLoginFailed(loginResult.getError());
|
||||
}
|
||||
if (loginResult.getSuccess() != null) {
|
||||
updateUiWithUser(loginResult.getSuccess());
|
||||
}
|
||||
setResult(-1);
|
||||
finish();
|
||||
}
|
||||
|
||||
/* renamed from: lambda$onCreate$2$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
|
||||
/* synthetic */ boolean m188lambda$onCreate$2$chmod_psre24e5uiloginLoginActivity(EditText editText, EditText editText2, TextView textView, int i, KeyEvent keyEvent) {
|
||||
if (i != 6) {
|
||||
return false;
|
||||
}
|
||||
this.loginViewModel.login(getBaseContext(), editText.getText().toString(), editText2.getText().toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
/* renamed from: lambda$onCreate$3$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
|
||||
/* synthetic */ void m189lambda$onCreate$3$chmod_psre24e5uiloginLoginActivity(ProgressBar progressBar, EditText editText, EditText editText2, View view) {
|
||||
progressBar.setVisibility(0);
|
||||
this.loginViewModel.login(getBaseContext(), editText.getText().toString(), editText2.getText().toString());
|
||||
}
|
||||
|
||||
private void updateUiWithUser(LoggedInUserView loggedInUserView) {
|
||||
Toast.makeText(getApplicationContext(), getString(R.string.correct_password) + loggedInUserView.getDisplayName(), 1).show();
|
||||
}
|
||||
|
||||
private void showLoginFailed(Integer num) {
|
||||
Toast.makeText(getApplicationContext(), num.intValue(), 0).show();
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package ch.mod_p.sre24.e5.ui.login;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
class LoginFormState {
|
||||
private final boolean isDataValid;
|
||||
private final Integer passwordError;
|
||||
private final Integer usernameError;
|
||||
|
||||
Integer getPasswordError() {
|
||||
return this.passwordError;
|
||||
}
|
||||
|
||||
Integer getUsernameError() {
|
||||
return this.usernameError;
|
||||
}
|
||||
|
||||
boolean isDataValid() {
|
||||
return this.isDataValid;
|
||||
}
|
||||
|
||||
LoginFormState(Integer num, Integer num2) {
|
||||
this.usernameError = num;
|
||||
this.passwordError = num2;
|
||||
this.isDataValid = false;
|
||||
}
|
||||
|
||||
LoginFormState(boolean z) {
|
||||
this.usernameError = null;
|
||||
this.passwordError = null;
|
||||
this.isDataValid = z;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ch.mod_p.sre24.e5.ui.login;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
class LoginResult {
|
||||
private Integer error;
|
||||
private LoggedInUserView success;
|
||||
|
||||
Integer getError() {
|
||||
return this.error;
|
||||
}
|
||||
|
||||
LoggedInUserView getSuccess() {
|
||||
return this.success;
|
||||
}
|
||||
|
||||
LoginResult(Integer num) {
|
||||
this.error = num;
|
||||
}
|
||||
|
||||
LoginResult(LoggedInUserView loggedInUserView) {
|
||||
this.success = loggedInUserView;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package ch.mod_p.sre24.e5.ui.login;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Patterns;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import ch.mod_p.sre24.e5.R;
|
||||
import ch.mod_p.sre24.e5.data.LoginRepository;
|
||||
import ch.mod_p.sre24.e5.data.Result;
|
||||
import ch.mod_p.sre24.e5.data.model.LoggedInUser;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class LoginViewModel extends ViewModel {
|
||||
private final LoginRepository loginRepository;
|
||||
private final MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
|
||||
private final MutableLiveData<LoginResult> loginResult = new MutableLiveData<>();
|
||||
|
||||
LiveData<LoginFormState> getLoginFormState() {
|
||||
return this.loginFormState;
|
||||
}
|
||||
|
||||
LiveData<LoginResult> getLoginResult() {
|
||||
return this.loginResult;
|
||||
}
|
||||
|
||||
LoginViewModel(LoginRepository loginRepository) {
|
||||
this.loginRepository = loginRepository;
|
||||
}
|
||||
|
||||
public void login(Context context, String str, String str2) {
|
||||
Result<LoggedInUser> login = this.loginRepository.login(context, str, str2);
|
||||
if (login instanceof Result.Success) {
|
||||
this.loginResult.setValue(new LoginResult(new LoggedInUserView(((LoggedInUser) ((Result.Success) login).getData()).getDisplayName())));
|
||||
} else {
|
||||
this.loginResult.setValue(new LoginResult(Integer.valueOf(R.string.login_failed)));
|
||||
}
|
||||
}
|
||||
|
||||
public void loginDataChanged(String str, String str2) {
|
||||
if (!isUserNameValid(str)) {
|
||||
this.loginFormState.setValue(new LoginFormState(Integer.valueOf(R.string.invalid_username), null));
|
||||
} else if (!isPasswordValid(str2)) {
|
||||
this.loginFormState.setValue(new LoginFormState(null, Integer.valueOf(R.string.invalid_password)));
|
||||
} else {
|
||||
this.loginFormState.setValue(new LoginFormState(true));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isUserNameValid(String str) {
|
||||
if (str == null) {
|
||||
return false;
|
||||
}
|
||||
if (str.contains("@")) {
|
||||
return Patterns.EMAIL_ADDRESS.matcher(str).matches();
|
||||
}
|
||||
return !str.trim().isEmpty();
|
||||
}
|
||||
|
||||
private boolean isPasswordValid(String str) {
|
||||
return str != null && str.trim().length() > 5;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ch.mod_p.sre24.e5.ui.login;
|
||||
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.lifecycle.viewmodel.CreationExtras;
|
||||
import ch.mod_p.sre24.e5.data.LoginDataSource;
|
||||
import ch.mod_p.sre24.e5.data.LoginRepository;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class LoginViewModelFactory implements ViewModelProvider.Factory {
|
||||
@Override // androidx.lifecycle.ViewModelProvider.Factory
|
||||
public /* synthetic */ ViewModel create(Class cls, CreationExtras creationExtras) {
|
||||
return ViewModelProvider.Factory.CC.$default$create(this, cls, creationExtras);
|
||||
}
|
||||
|
||||
@Override // androidx.lifecycle.ViewModelProvider.Factory
|
||||
public <T extends ViewModel> T create(Class<T> cls) {
|
||||
if (cls.isAssignableFrom(LoginViewModel.class)) {
|
||||
return new LoginViewModel(LoginRepository.getInstance(new LoginDataSource()));
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown ViewModel class");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user