refactoring code

This commit is contained in:
yb 2022-06-30 19:55:06 +02:00
parent 2c2aecbf51
commit a7f4104313
24 changed files with 136 additions and 222 deletions

View File

@ -0,0 +1,43 @@
package eu.csc.vehown.services.rest.vehownserver;
import android.app.Instrumentation;
import androidx.test.espresso.internal.inject.InstrumentationContext;
import androidx.test.platform.app.InstrumentationRegistry;
import junit.framework.TestCase;
import org.junit.Assert;
import java.io.IOException;
import java.net.SocketTimeoutException;
import eu.csc.vehown.data.model.LoggedInUser;
import lombok.var;
public class VehOwnApiClientFactoryTest extends TestCase {
public void setUp() throws Exception {
super.setUp();
VehOwnApiClientFactory.setUrl(InstrumentationRegistry.getInstrumentation().getTargetContext());
}
public void testFetchUserProfil() {
Exception e = null;
try {
LoggedInUser result =VehOwnApiClientFactory.fetchUserProfil("","");
assertNotNull(result);
System.out.println(result);
} catch (IOException ex) {
ex.printStackTrace();
e = ex;
}
if(e != null){
if(e instanceof SocketTimeoutException){
System.out.println(e);
}
else
assertNull(e);
}
}
}

View File

@ -21,10 +21,11 @@ import retrofit2.Call;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
public class VehOwnApiClientFactory {
public static boolean dummyMode = true;
public static boolean dummyMode = false;
public static String URL = null;
@ -138,10 +139,11 @@ public class VehOwnApiClientFactory {
LoggedInUser result = new LoggedInUser();
if(dummyMode)
{
result.setLogin(email);
result.setPassword(password);
return new VehOwnAppClientDummy().getLoggedInUser();
}else{
var call = ClientFactory.createAuthenticationService(getURL(), email, password).fetchCustomerProfile();
call.timeout().deadline(300, TimeUnit.MILLISECONDS);
var response = call.execute();
if(response.isSuccessful())

View File

@ -7,8 +7,10 @@ import eu.csc.ODPAppVehOwnServer.models.data.PropulsionTypeDto;
import eu.csc.ODPAppVehOwnServer.models.regist.CustomerDeviceDto;
import eu.csc.ODPAppVehOwnServer.models.regist.CustomerVehicleDto;
import eu.csc.vehown.data.model.Customer;
import eu.csc.vehown.data.model.LoggedInUser;
import eu.csc.vehown.data.model.Vehicle;
import eu.csc.vehown.services.rest.vehownserver.pub.OfflineDummyCall;
import lombok.Getter;
import lombok.var;
import retrofit2.Call;
@ -22,6 +24,9 @@ public class VehOwnAppClientDummy implements IVehOwnAppClient {
private final List<VehicleModelDto> vehicleModelDtos;
private final List<PropulsionTypeDto> propulsionTypeDtos;
@Getter
private final LoggedInUser loggedInUser;
//region Constructors
public VehOwnAppClientDummy() {
@ -57,6 +62,12 @@ public class VehOwnAppClientDummy implements IVehOwnAppClient {
propulsionTypeDtos.add(new PropulsionTypeDto(4L, "lpg", "Liquid Propane Gas"));
propulsionTypeDtos.add(new PropulsionTypeDto(5L, "eestor", "Electric Energy Storage"));
propulsionTypeDtos.add(new PropulsionTypeDto(6L, "hydro", "Hydrogen"));
loggedInUser = new LoggedInUser();
loggedInUser.setPassword("yannyann1");
loggedInUser.setLogin("test@csc-online.eu");
loggedInUser.setLanguage("en");
}
@Override

View File

@ -1,6 +1,8 @@
package eu.csc.vehown.ui.fragments.data;
import eu.csc.vehown.data.model.Customer;
import eu.csc.vehown.data.model.LoggedInUser;
import eu.csc.vehown.persist.sharedPreferences.LocalStorageClient;
import eu.csc.vehown.ui.models.Result;
import eu.csc.vehown.ui.registration.data.CustomerContentDataSource;
@ -12,20 +14,22 @@ public class LoginRepository {
private static volatile LoginRepository instance;
private CustomerContentDataSource dataSource;
private final LocalStorageClient localStorage;
private final CustomerContentDataSource 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(CustomerContentDataSource dataSource) {
private LoginRepository(CustomerContentDataSource dataSource, LocalStorageClient localStorage) {
this.dataSource = dataSource;
this.localStorage = localStorage;
}
public static LoginRepository getInstance(CustomerContentDataSource dataSource) {
public static LoginRepository getInstance(CustomerContentDataSource dataSource, LocalStorageClient localStorage) {
if (instance == null) {
instance = new LoginRepository(dataSource);
instance = new LoginRepository(dataSource, localStorage);
}
return instance;
}
@ -49,6 +53,7 @@ public class LoginRepository {
// handle login
Result<LoggedInUser> result = dataSource.login(username, password);
if (result instanceof Result.Success) {
localStorage.storeCustomer(((Result.Success<LoggedInUser>) result).getData());
// setLoggedInUser(((Result.Success<LoggedInUser>) result).getData());
}
return result;

View File

@ -1,40 +0,0 @@
package eu.csc.vehown.ui.fragments.ui.login;
import androidx.annotation.Nullable;
/**
* Data validation state of the login form.
*/
class LoginFormState {
@Nullable
private Integer usernameError;
@Nullable
private Integer passwordError;
private boolean isDataValid;
LoginFormState(@Nullable Integer usernameError, @Nullable Integer passwordError) {
this.usernameError = usernameError;
this.passwordError = passwordError;
this.isDataValid = false;
}
LoginFormState(boolean isDataValid) {
this.usernameError = null;
this.passwordError = null;
this.isDataValid = isDataValid;
}
@Nullable
Integer getUsernameError() {
return usernameError;
}
@Nullable
Integer getPasswordError() {
return passwordError;
}
boolean isDataValid() {
return isDataValid;
}
}

View File

@ -26,6 +26,7 @@ import eu.csc.vehown.databinding.FragmentLoginBinding;
import eu.csc.vehown.persist.sharedPreferences.LocalStorageClient;
import eu.csc.vehown.persist.sharedPreferences.SharedPreferencesFactory;
import eu.csc.vehown.ui.models.LoggedInUserView;
import eu.csc.vehown.ui.models.LoginFormState;
import eu.csc.vehown.ui.models.LoginResult;
import eu.csc.vehown.ui.registration.customer.FragmentCustomerRegistrationViewModel;
import eu.csc.vehown.ui.viewmodels.AppViewModelFactory;
@ -57,6 +58,7 @@ public class LoginFragment extends Fragment {
loginViewModel = new ViewModelProvider(this, new AppViewModelFactory(getContext()))
.get(LoginViewModel.class);
final EditText usernameEditText = binding.edEmail;
final EditText passwordEditText = binding.edPassword;
final Button loginButton = binding.btnLogin;
@ -155,6 +157,10 @@ public class LoginFragment extends Fragment {
@Override
public void onDestroyView() {
if(binding.cbRemember.isChecked()){
}
super.onDestroyView();
binding = null;
}

View File

@ -9,6 +9,7 @@ import eu.csc.vehown.R;
import eu.csc.vehown.data.model.LoggedInUser;
import eu.csc.vehown.ui.fragments.data.LoginRepository;
import eu.csc.vehown.ui.models.LoggedInUserView;
import eu.csc.vehown.ui.models.LoginFormState;
import eu.csc.vehown.ui.models.LoginResult;
import eu.csc.vehown.ui.models.Result;

View File

@ -161,8 +161,7 @@ public class LoginActivity extends AppCompatActivity {
}
private void switchView() {
if (
loginVisible) {
if (loginVisible) {
showRegistration();
} else
showLogin();

View File

@ -1,78 +0,0 @@
package eu.csc.vehown.ui.register;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import eu.csc.vehown.data.model.Customer;
import eu.csc.vehown.data.model.Language;
import eu.csc.vehown.data.model.VehicleBrand;
import eu.csc.vehown.data.model.VehiclePropulsionType;
import eu.csc.vehown.databinding.ActivityRegisterCustomerBinding;
import lombok.Getter;
import lombok.var;
import java.util.ArrayList;
import java.util.List;
public class RegisterCustomerViewModel extends ViewModel {
private ActivityRegisterCustomerBinding binding;
//region Observable
private final MutableLiveData<List<Language>> languages;
public RegisterCustomerViewModel() {
languages = new MutableLiveData<>();
var languageList = new ArrayList<Language>();
languageList.add(new Language("en", "English"));
this.languages.setValue(languageList);
}
//endregion
public void setBinding(ActivityRegisterCustomerBinding binding) {
this.binding = binding;
this.initBinding();
}
private void initBinding() {
if(this.binding == null)
return;
}
public void setCustomer(Customer customer){
binding.editEmail.setText(customer.getEmail());
binding.editPassword.setText(customer.getPassword());
binding.editFirstName.setText(customer.getFirstname());
binding.editLastName.setText(customer.getLastname());
binding.editCity.setText(customer.getCity());
binding.editStreet.setText(customer.getStreet());
binding.editPhone.setText(customer.getPhone());
binding.editPassword2.setText(customer.getPassword());
}
public Customer getCustomer(){
var result = new Customer();
result.setCity(binding.editCity.getText().toString());
result.setEmail(binding.editEmail.getText().toString());
result.setPassword(binding.editPassword.getText().toString());
result.setFirstname(binding.editFirstName.getText().toString());
result.setLastname(binding.editLastName.getText().toString());
result.setCity(binding.editCity.getText().toString());
result.setStreet(binding.editStreet.getText().toString());
result.setPhone(binding.editPhone.getText().toString());
result.setLanguageName(binding.spLanguage.getSelectedItem().toString());
return result;
}
}

View File

@ -31,6 +31,7 @@ public class FragmentCustomerRegistrationViewModel extends AbstractBaseViewModel
last_customer = localStorage.loadCustomer().orElse(null);
}
@Getter
private Customer last_customer;
@ -39,8 +40,9 @@ public class FragmentCustomerRegistrationViewModel extends AbstractBaseViewModel
@Getter
private MutableLiveData<Boolean> mIsEnabled = new MutableLiveData<>();
@Getter
@Getter
private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
public void loginDataChanged(ICustomer customer, String password, String password2) {
if (!isUserNameValid(customer.getEmail())) {
loginFormState.setValue(new LoginFormState(R.string.invalid_username, null));
@ -52,7 +54,7 @@ public class FragmentCustomerRegistrationViewModel extends AbstractBaseViewModel
}
public void registerCustomer(ICustomer customer) {
// localStorageClient.serializeContent("REG_C", customer);
// localStorageClient.serializeContent("REG_C", customer);
mIsEnabled.postValue(false);
executorService.execute(new Runnable() {

View File

@ -68,7 +68,7 @@ public class CustomerContentDataSource {
}
}
public Result<LoggedInUser> login(ICustomer customer) {
public Result<LoggedInUser> registerCustomer(ICustomer customer) {
try {
var result = VehOwnApiClientFactory

View File

@ -66,7 +66,7 @@ public class CustomerRegistrationRepository {
public Result<LoggedInUser> registerCustomer(ICustomer customer) {
// handle login
Result<LoggedInUser> result = dataSource.login(customer);
Result<LoggedInUser> result = dataSource.registerCustomer(customer);
if (result instanceof Result.Success) {

View File

@ -59,7 +59,7 @@ public class CustomerVehicleRegistrationRepository {
public Result<LoggedInUser> registerCustomer(ICustomer customer) {
// handle login
Result<LoggedInUser> result = dataSource.login(customer);
Result<LoggedInUser> result = dataSource.registerCustomer(customer);
if (result instanceof Result.Success) {
setLoggedInUser(((Result.Success<LoggedInUser>) result).getData());
}

View File

@ -41,7 +41,6 @@ public class FragmentVehicleRegistrationViewModel extends AbstractBaseViewModel
@Getter
private final MutableLiveData<Vehicle> currentVehicle;
public FragmentVehicleRegistrationViewModel(CustomerRegistrationRepository repository, Context context, LocalStorageClient localStorageClient) {
super(localStorageClient);
vehicleBrands = new MutableLiveData<>();

View File

@ -43,7 +43,7 @@ public class AppViewModelFactory implements ViewModelProvider.Factory {
if (modelClass.isAssignableFrom(LoginViewModel.class)) {
return (T) new LoginViewModel(LoginRepository.getInstance(new CustomerContentDataSource()));
return (T) new LoginViewModel(LoginRepository.getInstance(new CustomerContentDataSource(), localStorage));
}
if (modelClass.isAssignableFrom(CustomerRegistrationViewModel.class)) {

View File

@ -12,89 +12,6 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.login.LoginActivity">
<!-- <EditText-->
<!-- android:id="@+id/username"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_marginStart="24dp"-->
<!-- android:layout_marginTop="96dp"-->
<!-- android:layout_marginEnd="24dp"-->
<!-- android:hint="@string/email"-->
<!-- android:inputType="textEmailAddress"-->
<!-- android:selectAllOnFocus="true"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent"/>-->
<!-- <EditText-->
<!-- android:id="@+id/password"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_marginStart="24dp"-->
<!-- android:layout_marginTop="8dp"-->
<!-- android:layout_marginEnd="24dp"-->
<!-- android:hint="@string/password"-->
<!-- android:imeActionLabel="@string/action_sign_in_short"-->
<!-- android:imeOptions="actionDone"-->
<!-- android:inputType="textPassword"-->
<!-- android:selectAllOnFocus="true"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toBottomOf="@+id/username"-->
<!-- />-->
<!-- <Button-->
<!-- android:id="@+id/login"-->
<!-- android:enabled="false"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_gravity="start"-->
<!-- android:layout_marginStart="48dp"-->
<!-- android:layout_marginTop="16dp"-->
<!-- android:layout_marginEnd="48dp"-->
<!-- android:layout_marginBottom="64dp"-->
<!-- android:text="@string/action_sign_in"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toBottomOf="@+id/password"-->
<!-- app:layout_constraintVertical_bias="0.2"/>-->
<!-- <Button-->
<!-- android:id="@+id/btnRegisterCustomer"-->
<!-- android:enabled="true"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_gravity="start"-->
<!-- android:layout_marginStart="48dp"-->
<!-- android:layout_marginTop="16dp"-->
<!-- android:layout_marginEnd="48dp"-->
<!-- android:layout_marginBottom="64dp"-->
<!-- 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/login"-->
<!-- app:layout_constraintVertical_bias="0.2"/>-->
<!-- <ProgressBar-->
<!-- android:id="@+id/loading"-->
<!-- android:visibility="gone"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_gravity="center"-->
<!-- android:layout_marginStart="32dp"-->
<!-- android:layout_marginTop="64dp"-->
<!-- android:layout_marginEnd="32dp"-->
<!-- android:layout_marginBottom="64dp"-->
<!-- 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"/>-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"

View File

@ -16,7 +16,6 @@
android:id="@+id/edEmail"
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"

View File

@ -3,6 +3,7 @@
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.10.101.40</domain>
<domain includeSubdomains="true">10.10.101.30</domain>
<domain includeSubdomains="true">10.0.0.1</domain>
<domain includeSubdomains="true">192.168.188.54</domain>
</domain-config>
</network-security-config>

View File

@ -21,8 +21,24 @@ public class Customer implements Serializable, ICustomer {
private String phone;
private String languageName;
public Customer(ICustomer customer) {
this.email = customer.getEmail();
this.password = customer.getPassword();
this.firstname = customer.getFirstname();
this.lastname = customer.getLastname();
this.city = customer.getCity();
this.street = customer.getStreet();
this.phone = customer.getPhone();
this.languageName = customer.getLanguage();
}
@Override
public String getLogin() {
return this.getEmail();
}
@Override
public String getLanguage() {
return getLanguageName();
}
}

View File

@ -10,4 +10,5 @@ public interface ICustomer {
String getCity();
String getPassword();
String getStreet();
String getLanguage();
}

View File

@ -22,6 +22,7 @@ public class LoggedInUser implements Serializable, ICustomer {
private String city;
private String street;
private String password;
private String language;
public LoggedInUser(String userId, String displayName) {

View File

@ -0,0 +1,16 @@
package eu.csc.vehown.mapper;
import eu.csc.vehown.data.model.LoggedInUser;
public class ModelMapper {
private static ModelMapper getInstance(){
return new ModelMapper();
}
public LoggedInUser mapFromDto(){
throw new UnsupportedOperationException();
}
}

View File

@ -20,9 +20,10 @@ public interface LocalStorageClient extends IVehOwnData {
String getString(String id, String defaultValue);
void setString(String id, String value);
void storeCustomer(Customer customer);
void storeCustomer(ICustomer customer);
Optional<Customer> loadCustomer();
Customer loadCustomerOrNull();

View File

@ -2,9 +2,11 @@ package eu.csc.vehown.persist.sharedPreferences;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import eu.csc.vehown.data.model.*;
import eu.csc.vehown.persist.localstorage.R;
import lombok.var;
@ -33,7 +35,7 @@ public class LocalStorageClientImpl implements LocalStorageClient {
private File tokenDir;
private File eventsDir;
public static LocalStorageClientImpl getInstance(Context context){
public static LocalStorageClientImpl getInstance(Context context) {
return new LocalStorageClientImpl(context);
}
@ -88,12 +90,17 @@ public class LocalStorageClientImpl implements LocalStorageClient {
}
@Override
public void storeCustomer(Customer customer) {
public void storeCustomer(ICustomer customer) {
String value = null;
if (customer == null) {
} else {
value = gson.toJson(customer);
if(customer instanceof Customer){
value = gson.toJson((Customer)customer);
}else{
value = gson.toJson(new Customer(customer));
}
}
@ -112,6 +119,11 @@ public class LocalStorageClientImpl implements LocalStorageClient {
return Optional.of(gson.fromJson(json, Customer.class));
}
@Override
public Customer loadCustomerOrNull() {
return loadCustomer().orElse(null);
}
@Override
public List<Vehicle> loadVehicles() {
String json = sharedPreferences.getString(PREF_VEHICLES, null);