refactoring code

This commit is contained in:
yb 2022-06-30 21:01:54 +02:00
parent d0594113a1
commit 2ab9d0c59d
9 changed files with 112 additions and 13 deletions

View File

@ -25,6 +25,7 @@ public interface IVehOwnAppClient {
Call<Void> deregisterCustomerVehicle(String vin) throws IOException;
Call<List<CustomerVehicleDto>> getCustomerVehicles() throws IOException;
Call<List<CustomerDeviceDto>> getCustomerDevices() throws IOException;
Call<CustomerDeviceDto> registerCustomerDevice(String serialnumber);

View File

@ -67,12 +67,18 @@ public class ServerVehicleApiClient implements IVehOwnAppClient {
@Override
public Call<Void> deregisterCustomerVehicle(String vin) throws IOException {
throw new UnsupportedEncodingException();
throw new UnsupportedOperationException();
}
@Override
public Call<List<CustomerVehicleDto>> getCustomerVehicles() throws IOException {
return null;
return customerService.listCustomerVehicle();
}
@Override
public Call<List<CustomerDeviceDto>> getCustomerDevices() throws IOException {
return customerService.listCustomerDevices();
}
@Override

View File

@ -130,7 +130,12 @@ public class VehOwnAppClientDummy implements IVehOwnAppClient {
@Override
public Call<List<CustomerVehicleDto>> getCustomerVehicles() throws IOException {
throw new UnsupportedOperationException();
return OfflineDummyCall.getInstance(new ArrayList<>());
}
@Override
public Call<List<CustomerDeviceDto>> getCustomerDevices() throws IOException {
return OfflineDummyCall.getInstance(new ArrayList<>());
}
@Override

View File

@ -5,6 +5,7 @@ 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;
import lombok.var;
/**
* Class that requests authentication and user information from the remote data source and
@ -45,6 +46,8 @@ public class LoginRepository {
private void setLoggedInUser(LoggedInUser user) {
this.user = user;
localStorage.storeCustomer(user);
// If user credentials will be cached in local storage, it is recommended it be encrypted
// @see https://developer.android.com/training/articles/keystore
}
@ -53,9 +56,13 @@ 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());
setLoggedInUser(((Result.Success<LoggedInUser>) result).getData());
}
return result;
}
public void fetchRemoteContent() {
var customerVehicleResult = dataSource.fetchCustomerVehicles(localStorage.loadCustomerOrNull());
var customerDeviceResult = dataSource.fetchCustomerDevices(localStorage.loadCustomerOrNull());
}
}

View File

@ -38,6 +38,9 @@ public class LoginViewModel extends ViewModel {
if (result instanceof Result.Success) {
LoggedInUser data = ((Result.Success<LoggedInUser>) result).getData();
loginResult.setValue(new LoginResult(new LoggedInUserView(data.getDisplayName())));
loginRepository.fetchRemoteContent();
} else {
loginResult.setValue(new LoginResult(R.string.login_failed));
}

View File

@ -2,6 +2,9 @@ package eu.csc.vehown.ui.registration.data;
import androidx.annotation.NonNull;
import eu.csc.ODPAppVehOwnServer.models.regist.CustomerVehicleDto;
import eu.csc.vehown.data.model.CustomerDevice;
import eu.csc.vehown.data.model.CustomerVehicle;
import eu.csc.vehown.data.model.ICustomer;
import eu.csc.vehown.data.model.LoggedInUser;
import eu.csc.vehown.data.model.Vehicle;
@ -11,13 +14,15 @@ import eu.csc.vehown.ui.models.Result;
import lombok.var;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Class that handles authentication w/ login credentials and retrieves user information.
*/
public class CustomerContentDataSource {
public Result<RegisteredVehicle> registerVehicle(@NonNull ICustomer customer, Vehicle vehicle){
public Result<RegisteredVehicle> registerVehicle(@NonNull ICustomer customer, Vehicle vehicle) {
try {
var call = VehOwnApiClientFactory.registerCustomerVehicle(customer, vehicle)
@ -25,7 +30,7 @@ public class CustomerContentDataSource {
var result = new RegisteredVehicle();
if(call.isSuccessful()){
if (call.isSuccessful()) {
result.setVehicle(vehicle);
result.setBrandId(call.body().getBrandId());
result.setModelId(call.body().getModelId());
@ -35,13 +40,11 @@ public class CustomerContentDataSource {
return new Result.Success<>(result);
}else{
} else {
return new Result.Error(new Exception(call.toString()));
}
} catch (IOException e) {
e.printStackTrace();
@ -52,8 +55,6 @@ public class CustomerContentDataSource {
public Result<LoggedInUser> login(String email, String password) {
try {
var profile = VehOwnApiClientFactory.fetchUserProfil(email, password);
LoggedInUser fakeUser =
new LoggedInUser(
@ -68,6 +69,7 @@ public class CustomerContentDataSource {
}
}
public Result<LoggedInUser> registerCustomer(ICustomer customer) {
try {
@ -88,7 +90,7 @@ public class CustomerContentDataSource {
} catch (IOException e) {
e.printStackTrace();
return new Result.Error(new IOException("Error logging in ", e));
return new Result.Error(new IOException("Error logging in ", e));
}
}
@ -96,4 +98,67 @@ public class CustomerContentDataSource {
public void logout() {
// TODO: revoke authentication
}
public Result<List<CustomerVehicle>> fetchCustomerVehicles(@NonNull ICustomer customer) {
var result = new ArrayList<CustomerVehicle>();
try {
var remoteContentCall = VehOwnApiClientFactory.getAuthenticatedDataClient(customer).getCustomerVehicles();
var remoteContent = remoteContentCall.execute();
if (remoteContent.isSuccessful() && remoteContent.body() != null) {
for (var remoteItem : remoteContent.body()) {
var item = new CustomerVehicle();
item.setBrand(remoteItem.getBrandId());
item.setModel(remoteItem.getModelId());
item.setLicenseplate(remoteItem.getLicensePlate());
item.setLicenseplate(remoteItem.getPropulsionType());
item.setVin(remoteItem.getVin());
result.add(item);
}
}
} catch (IOException e) {
e.printStackTrace();
return new Result.Error(e);
}
return new Result.Success<>(result);
}
public Result<List<CustomerDevice>> fetchCustomerDevices(@NonNull ICustomer customer) {
var result = new ArrayList<CustomerDevice>();
try {
var remoteContentCall = VehOwnApiClientFactory.getAuthenticatedDataClient(customer).getCustomerDevices();
var remoteContent = remoteContentCall.execute();
if (remoteContent.isSuccessful() && remoteContent.body() != null) {
for (var remoteItem : remoteContent.body()) {
var item = new CustomerDevice();
item.setDeviceNumber(remoteItem.getDeviceNumber());
item.setSerialNumber(remoteItem.getSerialNumber());
item.setName(remoteItem.getName());
result.add(item);
}
}
} catch (IOException e) {
e.printStackTrace();
return new Result.Error(e);
}
return new Result.Success<>(result);
}
}

View File

@ -26,4 +26,9 @@
<item>German</item>
</string-array>
<string-array name="rest_api_urls">
<item>http://10.0.0.1:8081</item>
</string-array>
</resources>

View File

@ -183,6 +183,7 @@
<string name="title_propulsion_type">Propulsion Type</string>
<string name="hint_choose_propulsion_type">Propulsion Type</string>
<string name="btn_no_text">NO_TEXT</string>
<string name="attachment_api_url">RestApi Url</string>
<!--string-array name="languages">
<item>english</item>

View File

@ -11,5 +11,11 @@
app:summaryOn="@string/attachment_summary_on"
app:summaryOff="@string/attachment_summary_off"
app:dependency="sync"/>
<ListPreference
android:defaultValue="1"
android:entries="@array/rest_api_urls"
android:entryValues="@array/rest_api_urls"
android:key="list_preference_1"
android:title="@string/attachment_api_url" />
</PreferenceScreen>