diff --git a/app/src/main/java/com/stormtales/notevault/network/StatusResponse.java b/app/src/main/java/com/stormtales/notevault/network/StatusResponse.java new file mode 100644 index 0000000..215c689 --- /dev/null +++ b/app/src/main/java/com/stormtales/notevault/network/StatusResponse.java @@ -0,0 +1,14 @@ +package com.stormtales.notevault.network; + +public class StatusResponse { + + private String status; + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} diff --git a/app/src/main/java/com/stormtales/notevault/network/auth/AuthAPI.java b/app/src/main/java/com/stormtales/notevault/network/auth/AuthAPI.java index c50a55d..d6922e5 100644 --- a/app/src/main/java/com/stormtales/notevault/network/auth/AuthAPI.java +++ b/app/src/main/java/com/stormtales/notevault/network/auth/AuthAPI.java @@ -1,5 +1,6 @@ package com.stormtales.notevault.network.auth; +import com.stormtales.notevault.network.StatusResponse; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.Headers; @@ -10,7 +11,7 @@ public interface AuthAPI { @POST("/login/") Call login(@Body LoginRequest loginRequest); - /*@POST("/register/") + @POST("/register/") @Headers("Content-Type: application/json") - Call registration(@Body RegisterRequest registerRequest);*/ + Call registration(@Body RegisterRequest registerRequest); } diff --git a/app/src/main/java/com/stormtales/notevault/network/auth/AuthService.java b/app/src/main/java/com/stormtales/notevault/network/auth/AuthService.java index de9d6ca..ad13e87 100644 --- a/app/src/main/java/com/stormtales/notevault/network/auth/AuthService.java +++ b/app/src/main/java/com/stormtales/notevault/network/auth/AuthService.java @@ -4,6 +4,7 @@ import android.content.Context; import android.text.TextUtils; import com.stormtales.notevault.network.APICallback; import com.stormtales.notevault.network.NetworkModule; +import com.stormtales.notevault.network.StatusResponse; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -44,8 +45,8 @@ public class AuthService { } public void performRegistration(String email, String username, String password, APICallback callback) { - /*RegisterRequest registerRequest = new RegisterRequest(username, password, email); - authService.registration(registerRequest).enqueue(new Callback() { + RegisterRequest registerRequest = new RegisterRequest(username, password, email); + authAPI.registration(registerRequest).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { @@ -60,7 +61,7 @@ public class AuthService { public void onFailure(Call call, Throwable throwable) { callback.onError("Netzwerkfehler: " + throwable.getMessage()); } - });*/ + }); } private void saveToken(String token) { diff --git a/app/src/main/java/com/stormtales/notevault/network/auth/RegisterRequest.java b/app/src/main/java/com/stormtales/notevault/network/auth/RegisterRequest.java new file mode 100644 index 0000000..9ad5f0d --- /dev/null +++ b/app/src/main/java/com/stormtales/notevault/network/auth/RegisterRequest.java @@ -0,0 +1,14 @@ +package com.stormtales.notevault.network.auth; + +public class RegisterRequest { + private String username; + private String email; + private String password; + + public RegisterRequest(String username, String password, String email) { + this.username = username; + this.password = password; + this.email = email; + } + +} diff --git a/app/src/main/java/com/stormtales/notevault/ui/login/LoginDialog.java b/app/src/main/java/com/stormtales/notevault/ui/login/LoginDialog.java index b7b6995..57f3518 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/login/LoginDialog.java +++ b/app/src/main/java/com/stormtales/notevault/ui/login/LoginDialog.java @@ -33,6 +33,8 @@ public class LoginDialog extends DialogFragment { private EditText editTextUsername; private EditText editTextPassword; private EditText editTextEmail; + private TextView textViewTitle; + private TextView textViewSwitch; @Override public void onAttach(Context context) { @@ -51,12 +53,12 @@ public class LoginDialog extends DialogFragment { LayoutInflater inflater = requireActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.fragment_login_dialog, null); - TextView textViewTitle = view.findViewById(R.id.textViewTitle); + textViewTitle = view.findViewById(R.id.textViewTitle); editTextUsername = view.findViewById(R.id.editTextUsername); editTextEmail = view.findViewById(R.id.editTextEmail); editTextPassword = view.findViewById(R.id.editTextPassword); loginButton = view.findViewById(R.id.buttonAction); - TextView textViewSwitch = view.findViewById(R.id.textViewSwitch); + textViewSwitch = view.findViewById(R.id.textViewSwitch); loadingProgressBar = view.findViewById(R.id.loading); // Handle action button click @@ -114,13 +116,21 @@ public class LoginDialog extends DialogFragment { } else { // Handle registration logic String username = editTextUsername.getText().toString(); - AuthService authRepository = new AuthService(this.getContext()); - //authRepository.performRegistration(email, username, password, registerCallback); + this.loginViewModel.performRegistration(email, password, username, this::onSuccessFullRegistration); } }); return dialog; } + private void onSuccessFullRegistration() { + Toast.makeText(getContext(), "Successfully registered", Toast.LENGTH_LONG).show(); + this.isLoginMode = true; + textViewTitle.setText(isLoginMode ? "Login" : "Register"); + editTextUsername.setVisibility(isLoginMode ? View.GONE : View.VISIBLE); + loginButton.setText(isLoginMode ? "Login" : "Register"); + textViewSwitch.setText(isLoginMode ? "Don't have an account? Register" : "Already have an account? Login"); + } + @Override public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); diff --git a/app/src/main/java/com/stormtales/notevault/ui/login/LoginViewModel.java b/app/src/main/java/com/stormtales/notevault/ui/login/LoginViewModel.java index 7d20ca7..72a66b7 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/login/LoginViewModel.java +++ b/app/src/main/java/com/stormtales/notevault/ui/login/LoginViewModel.java @@ -6,6 +6,7 @@ import android.widget.Toast; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import com.stormtales.notevault.R; +import com.stormtales.notevault.network.APICallback; import com.stormtales.notevault.network.auth.AuthService; import com.stormtales.notevault.network.auth.LoginResponse; @@ -29,6 +30,22 @@ public class LoginViewModel extends ViewModel { } } + public void performRegistration(String email, String password, String username, SuccessFullLoginCallback successFullLoginCallback) { + if(authService != null) { + authService.performRegistration(email, username, password, new APICallback() { + @Override + public void onSuccess() { + successFullLoginCallback.onSuccess(); + } + + @Override + public void onError(String error) { + Log.d("LoginService", error); + } + }); + } + } + public void setAuthService(AuthService authService) { this.authService = authService; }