@@ -67,35 +67,41 @@ public class LoginActivity extends AppCompatActivity { | |||||
private void login(String username, String password) { | private void login(String username, String password) { | ||||
loginViewModel.login(username,password,this).observe(this, new Observer<StatusResource<User>>() { | loginViewModel.login(username,password,this).observe(this, new Observer<StatusResource<User>>() { | ||||
@Override public void onChanged(final StatusResource<User> userStatusResource) { | @Override public void onChanged(final StatusResource<User> userStatusResource) { | ||||
switch (userStatusResource.status){ | |||||
switch (userStatusResource.status) { | |||||
case SUCCESS: | case SUCCESS: | ||||
loginViewModel.addDevice(LoginActivity.this).observe(LoginActivity.this, new Observer<StatusResource<Device>>() { | |||||
@Override public void onChanged(final StatusResource<Device> deviceStatusResource) { | |||||
switch (deviceStatusResource.status){ | |||||
case SUCCESS: | |||||
closeLoadingDialog(); | |||||
Toast.makeText(LoginActivity.this,"Success",Toast.LENGTH_SHORT).show(); | |||||
Log.d("TAG","Success"); | |||||
break; | |||||
case LOADING: | |||||
Log.d("TAG","Loading"); | |||||
break; | |||||
case ERROR: | |||||
Toast.makeText(LoginActivity.this,"Login Failed",Toast.LENGTH_SHORT).show(); | |||||
Log.d("TAG","Error"); | |||||
break; | |||||
if (loginViewModel.isDeviceLoggedIn(LoginActivity.this)) { | |||||
Toast.makeText(LoginActivity.this, "Success", Toast.LENGTH_SHORT).show(); | |||||
Log.d("TAG", "Success"); | |||||
closeLoadingDialog(); | |||||
} else { | |||||
loginViewModel.addDevice(LoginActivity.this).observe(LoginActivity.this, new Observer<StatusResource<Device>>() { | |||||
@Override public void onChanged(final StatusResource<Device> deviceStatusResource) { | |||||
switch (deviceStatusResource.status) { | |||||
case SUCCESS: | |||||
closeLoadingDialog(); | |||||
Toast.makeText(LoginActivity.this, "Success", Toast.LENGTH_SHORT).show(); | |||||
Log.d("TAG", "Success"); | |||||
break; | |||||
case LOADING: | |||||
Log.d("TAG", "Loading"); | |||||
break; | |||||
case ERROR: | |||||
Toast.makeText(LoginActivity.this, "Login Failed", Toast.LENGTH_SHORT).show(); | |||||
Log.d("TAG", "Error"); | |||||
break; | |||||
} | |||||
} | } | ||||
} | |||||
}); | |||||
Log.d("TAG","Success"); | |||||
break; | |||||
case LOADING: | |||||
Log.d("TAG","Loading"); | |||||
break; | |||||
case ERROR: | |||||
Toast.makeText(LoginActivity.this,"Login Failed",Toast.LENGTH_SHORT).show(); | |||||
Log.d("TAG","Error"); | |||||
break; | |||||
}); | |||||
} | |||||
Log.d("TAG", "Success"); | |||||
break; | |||||
case LOADING: | |||||
Log.d("TAG", "Loading"); | |||||
break; | |||||
case ERROR: | |||||
Toast.makeText(LoginActivity.this, "Login Failed", Toast.LENGTH_SHORT).show(); | |||||
Log.d("TAG", "Error"); | |||||
break; | |||||
} | } | ||||
} | } | ||||
}); | }); | ||||
@@ -125,6 +125,7 @@ public class DataRepository { | |||||
if(deviceNameItem.length>1){ | if(deviceNameItem.length>1){ | ||||
if(deviceNameItem[0].contains(myDeviceName[0]) && deviceNameItem[1].contains(myDeviceName[1])){ | if(deviceNameItem[0].contains(myDeviceName[0]) && deviceNameItem[1].contains(myDeviceName[1])){ | ||||
setMutableLiveData(StatusResource.success()); | setMutableLiveData(StatusResource.success()); | ||||
UserStore.getInstance(context).setDevice(device.getName()); | |||||
flag = false; | flag = false; | ||||
break; | break; | ||||
} | } | ||||
@@ -148,6 +149,7 @@ public class DataRepository { | |||||
clientApi.addDevice(header, body).enqueue(new Callback<Device>() { | clientApi.addDevice(header, body).enqueue(new Callback<Device>() { | ||||
@Override public void onResponse(final Call<Device> call, final Response<Device> response) { | @Override public void onResponse(final Call<Device> call, final Response<Device> response) { | ||||
if (response.isSuccessful()) { | if (response.isSuccessful()) { | ||||
UserStore.getInstance(context).setDevice(response.body().getName()); | |||||
setMutableLiveData(StatusResource.success()); | setMutableLiveData(StatusResource.success()); | ||||
} else { | } else { | ||||
final String errorMessage = createErrorMessage(call, response); | final String errorMessage = createErrorMessage(call, response); | ||||
@@ -192,6 +194,7 @@ public class DataRepository { | |||||
clientApi.addDevice(header, body).enqueue(new Callback<Device>() { | clientApi.addDevice(header, body).enqueue(new Callback<Device>() { | ||||
@Override public void onResponse(final Call<Device> call, final Response<Device> response) { | @Override public void onResponse(final Call<Device> call, final Response<Device> response) { | ||||
if (response.isSuccessful()) { | if (response.isSuccessful()) { | ||||
UserStore.getInstance(context).setDevice(response.body().getName()); | |||||
setMutableLiveData(StatusResource.success()); | setMutableLiveData(StatusResource.success()); | ||||
} else { | } else { | ||||
final String errorMessage = createErrorMessage(call, response); | final String errorMessage = createErrorMessage(call, response); | ||||
@@ -253,4 +256,8 @@ public class DataRepository { | |||||
private String getDeviceModel(){ | private String getDeviceModel(){ | ||||
return Build.MODEL; | return Build.MODEL; | ||||
} | } | ||||
public boolean isDeviceLoggedIn(Context context){ | |||||
return !UserStore.DEVICE_DEFAULT_VALUE.equals(UserStore.getInstance(context).getDevice()); | |||||
} | |||||
} | } |
@@ -9,7 +9,9 @@ public class UserStore { | |||||
private static final String USER_SHARED_PREF = "com.wireguard.android.util.bubbleUserSharedPref"; | private static final String USER_SHARED_PREF = "com.wireguard.android.util.bubbleUserSharedPref"; | ||||
private static final String USER_DATA_KEY = "com.wireguard.android.util.bubbleUserResponse"; | private static final String USER_DATA_KEY = "com.wireguard.android.util.bubbleUserResponse"; | ||||
private static final String DEVICE_DATA_KEY = "com.wireguard.android.util.bubbleDeviceResponse"; | |||||
public static final String USER_TOKEN_DEFAULT_VALUE = ""; | public static final String USER_TOKEN_DEFAULT_VALUE = ""; | ||||
public static final String DEVICE_DEFAULT_VALUE = ""; | |||||
public static UserStore getInstance(Context context) { | public static UserStore getInstance(Context context) { | ||||
if (instance == null) { | if (instance == null) { | ||||
@@ -35,4 +37,11 @@ public class UserStore { | |||||
return sharedPreferences.getString(USER_DATA_KEY, USER_TOKEN_DEFAULT_VALUE); | return sharedPreferences.getString(USER_DATA_KEY, USER_TOKEN_DEFAULT_VALUE); | ||||
} | } | ||||
public void setDevice(String device){ | |||||
sharedPreferences.edit().putString(DEVICE_DATA_KEY, device).apply(); | |||||
} | |||||
public String getDevice(){ | |||||
return sharedPreferences.getString(DEVICE_DATA_KEY, DEVICE_DEFAULT_VALUE); | |||||
} | |||||
} | } |
@@ -24,4 +24,8 @@ public class LoginViewModel extends ViewModel { | |||||
public LiveData<StatusResource<Device>> addDevice(Context context){ | public LiveData<StatusResource<Device>> addDevice(Context context){ | ||||
return DataRepository.getRepositoryInstance().addDevice(context); | return DataRepository.getRepositoryInstance().addDevice(context); | ||||
} | } | ||||
public boolean isDeviceLoggedIn(Context context){ | |||||
return DataRepository.getRepositoryInstance().isDeviceLoggedIn(context); | |||||
} | |||||
} | } |