ADD week 5
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user