refactoring code
This commit is contained in:
parent
97c4102c47
commit
2c2aecbf51
|
|
@ -1,28 +0,0 @@
|
|||
package eu.csc.vehown.data;
|
||||
|
||||
import eu.csc.vehown.data.model.LoggedInUser;
|
||||
import eu.csc.vehown.ui.models.Result;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
public class LoginDataSource {
|
||||
|
||||
public Result<LoggedInUser> login(String username, String password) {
|
||||
|
||||
try {
|
||||
// TODO: handle loggedInUser authentication
|
||||
LoggedInUser fakeUser =
|
||||
new LoggedInUser(
|
||||
java.util.UUID.randomUUID().toString(),
|
||||
"John Doe");
|
||||
return new Result.Success<>(fakeUser);
|
||||
} catch (Exception e) {
|
||||
return new Result.Error(new IOException("Error logging in", e));
|
||||
}
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
// TODO: revoke authentication
|
||||
}
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
package eu.csc.vehown.data;
|
||||
|
||||
import eu.csc.vehown.data.model.LoggedInUser;
|
||||
import eu.csc.vehown.ui.models.Result;
|
||||
|
||||
/**
|
||||
* Class that requests authentication and user information from the remote data source and
|
||||
* maintains an in-memory cache of login status and user credentials information.
|
||||
*/
|
||||
public class LoginRepository {
|
||||
|
||||
private static volatile LoginRepository instance;
|
||||
|
||||
private LoginDataSource dataSource;
|
||||
|
||||
// If user credentials will be cached in local storage, it is recommended it be encrypted
|
||||
// @see https://developer.android.com/training/articles/keystore
|
||||
private LoggedInUser user = null;
|
||||
|
||||
// private constructor : singleton access
|
||||
private LoginRepository(LoginDataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public static LoginRepository getInstance(LoginDataSource dataSource) {
|
||||
if (instance == null) {
|
||||
instance = new LoginRepository(dataSource);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public boolean isLoggedIn() {
|
||||
return user != null;
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
user = null;
|
||||
dataSource.logout();
|
||||
}
|
||||
|
||||
private void setLoggedInUser(LoggedInUser user) {
|
||||
this.user = user;
|
||||
// If user credentials will be cached in local storage, it is recommended it be encrypted
|
||||
// @see https://developer.android.com/training/articles/keystore
|
||||
}
|
||||
|
||||
public Result<LoggedInUser> login(String username, String password) {
|
||||
// handle login
|
||||
Result<LoggedInUser> result = dataSource.login(username, password);
|
||||
if (result instanceof Result.Success) {
|
||||
setLoggedInUser(((Result.Success<LoggedInUser>) result).getData());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package eu.csc.vehown.ui.base;
|
||||
|
||||
import android.app.Fragment;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
public abstract class AbstractFragment extends Fragment {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
package eu.csc.vehown.ui.fragments.data;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import eu.csc.vehown.data.model.LoggedInUser;
|
||||
import eu.csc.vehown.ui.models.Result;
|
||||
|
||||
/**
|
||||
* Class that handles authentication w/ login credentials and retrieves user information.
|
||||
*/
|
||||
public class LoginDataSource {
|
||||
|
||||
public Result<LoggedInUser> login(String username, String password) {
|
||||
|
||||
try {
|
||||
// TODO: handle loggedInUser authentication
|
||||
LoggedInUser fakeUser =
|
||||
new LoggedInUser(
|
||||
java.util.UUID.randomUUID().toString(),
|
||||
"Jane Doe");
|
||||
return new Result.Success<>(fakeUser);
|
||||
} catch (Exception e) {
|
||||
return new Result.Error(new IOException("Error logging in", e));
|
||||
}
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
// TODO: revoke authentication
|
||||
}
|
||||
}
|
||||
|
|
@ -8,16 +8,17 @@ import android.os.Bundle;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.Toast;
|
||||
|
||||
import eu.csc.vehown.R;
|
||||
|
|
@ -25,13 +26,17 @@ import eu.csc.vehown.data.model.Customer;
|
|||
import eu.csc.vehown.data.model.ICustomer;
|
||||
import eu.csc.vehown.databinding.DeviceRegistrationFragmentBinding;
|
||||
import eu.csc.vehown.databinding.FragmentCustomerRegistrationFragmentBinding;
|
||||
import eu.csc.vehown.ui.base.AbstractFragment;
|
||||
import eu.csc.vehown.ui.dialogs.DialogInfoFragment;
|
||||
import eu.csc.vehown.ui.models.LoginFormState;
|
||||
import eu.csc.vehown.ui.models.LoginResult;
|
||||
import eu.csc.vehown.ui.registration.devices.DeviceRegistrationViewModel;
|
||||
import eu.csc.vehown.ui.viewmodels.AppViewModelFactory;
|
||||
|
||||
public class FragmentCustomerRegistration extends Fragment implements DialogInfoFragment.IDialogInfoFragment {
|
||||
/**
|
||||
* @see eu.csc.vehown.ui.login.LoginActivity
|
||||
*/
|
||||
public class FragmentCustomerRegistration extends AbstractFragment implements DialogInfoFragment.IDialogInfoFragment {
|
||||
|
||||
private FragmentCustomerRegistrationViewModel mViewModel;
|
||||
|
||||
|
|
@ -48,6 +53,7 @@ public class FragmentCustomerRegistration extends Fragment implements DialogInfo
|
|||
EditText edZip = null;
|
||||
EditText edLastname = null;
|
||||
EditText edStreet = null;
|
||||
Spinner spLanguages = null;
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
|
|
@ -65,6 +71,16 @@ public class FragmentCustomerRegistration extends Fragment implements DialogInfo
|
|||
edLastname = binding.edLastname;
|
||||
edStreet = binding.edStreet;
|
||||
|
||||
spLanguages = binding.spLanguage;
|
||||
|
||||
|
||||
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
|
||||
R.array.supported_languages, android.R.layout.simple_spinner_item);
|
||||
// Specify the layout to use when the list of choices appears
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
|
||||
spLanguages.setAdapter(adapter);
|
||||
|
||||
ProgressBar pbLoading = binding.pbLoading;
|
||||
|
||||
mViewModel = new ViewModelProvider(this, new AppViewModelFactory(getContext()))
|
||||
|
|
@ -94,9 +110,9 @@ public class FragmentCustomerRegistration extends Fragment implements DialogInfo
|
|||
mViewModel.getMIsEnabled().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
|
||||
@Override
|
||||
public void onChanged(Boolean value) {
|
||||
if(value){
|
||||
if (value) {
|
||||
pbLoading.setVisibility(View.VISIBLE);
|
||||
}else {
|
||||
} else {
|
||||
|
||||
pbLoading.setVisibility(View.GONE);
|
||||
}
|
||||
|
|
@ -128,10 +144,10 @@ public class FragmentCustomerRegistration extends Fragment implements DialogInfo
|
|||
mViewModel.getLoginFormState().observe(getViewLifecycleOwner(), new Observer<LoginFormState>() {
|
||||
@Override
|
||||
public void onChanged(LoginFormState loginFormState) {
|
||||
if(loginFormState.getPasswordError() != null){
|
||||
if (loginFormState.getPasswordError() != null) {
|
||||
passwordEditText.setError("NOT SAME");
|
||||
password2EditText.setError("NOT SAME");
|
||||
}else{
|
||||
} else {
|
||||
passwordEditText.setError(null);
|
||||
password2EditText.setError(null);
|
||||
}
|
||||
|
|
@ -146,12 +162,12 @@ public class FragmentCustomerRegistration extends Fragment implements DialogInfo
|
|||
}
|
||||
|
||||
private void setCustomer(ICustomer customer) {
|
||||
if(customer == null)
|
||||
if (customer == null)
|
||||
return;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private ICustomer getCustomer() {
|
||||
Customer result = new Customer();
|
||||
result.setEmail(usernameEditText.getText().toString());
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import eu.csc.vehown.ui.viewmodels.AbstractBaseViewModel;
|
|||
import lombok.Getter;
|
||||
|
||||
public class FragmentCustomerRegistrationViewModel extends AbstractBaseViewModel {
|
||||
|
||||
private final CustomerContentDataSource customerContentDataSource;
|
||||
private final CustomerRegistrationRepository repository;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,44 +1,44 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
tools:context="ui.registration.customer.FragmentCustomerRegistration">
|
||||
|
||||
|
||||
<EditText
|
||||
android:id="@+id/edEmail"
|
||||
android:nextFocusDown="@id/edFirstname"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="96dp"
|
||||
android:autofillHints="@string/prompt_email"
|
||||
android:hint="@string/prompt_email"
|
||||
android:inputType="textEmailAddress"
|
||||
android:nextFocusDown="@id/edFirstname"
|
||||
android:selectAllOnFocus="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"/>
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/edFirstname"
|
||||
android:nextFocusDown="@id/edLastname"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:autofillHints="@string/prompt_firstname"
|
||||
android:hint="@string/prompt_firstname"
|
||||
android:inputType="text"
|
||||
android:nextFocusDown="@id/edLastname"
|
||||
android:selectAllOnFocus="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/edEmail"/>
|
||||
app:layout_constraintTop_toBottomOf="@+id/edEmail" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/edLastname"
|
||||
|
|
@ -50,7 +50,7 @@
|
|||
android:selectAllOnFocus="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/edFirstname"/>
|
||||
app:layout_constraintTop_toBottomOf="@id/edFirstname" />
|
||||
|
||||
<EditText
|
||||
|
||||
|
|
@ -66,7 +66,9 @@
|
|||
android:selectAllOnFocus="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/edLastname"/>
|
||||
app:layout_constraintTop_toBottomOf="@+id/edLastname" />
|
||||
|
||||
|
||||
<EditText
|
||||
|
||||
android:id="@+id/edStreet"
|
||||
|
|
@ -81,7 +83,28 @@
|
|||
android:selectAllOnFocus="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/edZip"/>
|
||||
app:layout_constraintTop_toBottomOf="@+id/edZip" />
|
||||
|
||||
<TextView
|
||||
android:layout_marginTop="8dp"
|
||||
android:id="@+id/tvLanguage"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:labelFor="@id/spLanguage"
|
||||
android:text="@string/language"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/edStreet" />
|
||||
|
||||
<Spinner
|
||||
android:layout_marginTop="8dp"
|
||||
android:id="@+id/spLanguage"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/tvLanguage"
|
||||
/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/password"
|
||||
|
|
@ -96,7 +119,7 @@
|
|||
android:selectAllOnFocus="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/edStreet"/>
|
||||
app:layout_constraintTop_toBottomOf="@+id/spLanguage" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/edPassword2"
|
||||
|
|
@ -111,35 +134,35 @@
|
|||
android:selectAllOnFocus="true"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/password"/>
|
||||
app:layout_constraintTop_toBottomOf="@+id/password" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnRegister"
|
||||
android:enabled="false"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="start"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginBottom="64dp"
|
||||
android:enabled="false"
|
||||
android:text="@string/action_register_customer"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/edPassword2"
|
||||
app:layout_constraintVertical_bias="0.2"/>
|
||||
app:layout_constraintVertical_bias="0.2" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/pbLoading"
|
||||
android:visibility="gone"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginTop="64dp"
|
||||
android:layout_marginBottom="64dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="@+id/password"
|
||||
app:layout_constraintStart_toStartOf="@+id/password"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_bias="0.3"/>
|
||||
app:layout_constraintVertical_bias="0.3" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
@ -21,4 +21,9 @@
|
|||
<item>de</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="supported_languages">
|
||||
<item>English</item>
|
||||
<item>German</item>
|
||||
</string-array>
|
||||
|
||||
</resources>
|
||||
Loading…
Reference in New Issue