ADD: Registration

This commit is contained in:
Fawkes100 2025-01-19 08:18:18 +01:00
parent 83a6c27c56
commit 272e1f4242
6 changed files with 66 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -1,5 +1,6 @@
package com.stormtales.notevault.network.auth; package com.stormtales.notevault.network.auth;
import com.stormtales.notevault.network.StatusResponse;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.Body; import retrofit2.http.Body;
import retrofit2.http.Headers; import retrofit2.http.Headers;
@ -10,7 +11,7 @@ public interface AuthAPI {
@POST("/login/") @POST("/login/")
Call<LoginResponse> login(@Body LoginRequest loginRequest); Call<LoginResponse> login(@Body LoginRequest loginRequest);
/*@POST("/register/") @POST("/register/")
@Headers("Content-Type: application/json") @Headers("Content-Type: application/json")
Call<StatusResponse> registration(@Body RegisterRequest registerRequest);*/ Call<StatusResponse> registration(@Body RegisterRequest registerRequest);
} }

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import com.stormtales.notevault.network.APICallback; import com.stormtales.notevault.network.APICallback;
import com.stormtales.notevault.network.NetworkModule; import com.stormtales.notevault.network.NetworkModule;
import com.stormtales.notevault.network.StatusResponse;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
@ -44,8 +45,8 @@ public class AuthService {
} }
public void performRegistration(String email, String username, String password, APICallback callback) { public void performRegistration(String email, String username, String password, APICallback callback) {
/*RegisterRequest registerRequest = new RegisterRequest(username, password, email); RegisterRequest registerRequest = new RegisterRequest(username, password, email);
authService.registration(registerRequest).enqueue(new Callback<StatusResponse>() { authAPI.registration(registerRequest).enqueue(new Callback<StatusResponse>() {
@Override @Override
public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) { public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {
@ -60,7 +61,7 @@ public class AuthService {
public void onFailure(Call<StatusResponse> call, Throwable throwable) { public void onFailure(Call<StatusResponse> call, Throwable throwable) {
callback.onError("Netzwerkfehler: " + throwable.getMessage()); callback.onError("Netzwerkfehler: " + throwable.getMessage());
} }
});*/ });
} }
private void saveToken(String token) { private void saveToken(String token) {

View File

@ -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;
}
}

View File

@ -33,6 +33,8 @@ public class LoginDialog extends DialogFragment {
private EditText editTextUsername; private EditText editTextUsername;
private EditText editTextPassword; private EditText editTextPassword;
private EditText editTextEmail; private EditText editTextEmail;
private TextView textViewTitle;
private TextView textViewSwitch;
@Override @Override
public void onAttach(Context context) { public void onAttach(Context context) {
@ -51,12 +53,12 @@ public class LoginDialog extends DialogFragment {
LayoutInflater inflater = requireActivity().getLayoutInflater(); LayoutInflater inflater = requireActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_login_dialog, null); 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); editTextUsername = view.findViewById(R.id.editTextUsername);
editTextEmail = view.findViewById(R.id.editTextEmail); editTextEmail = view.findViewById(R.id.editTextEmail);
editTextPassword = view.findViewById(R.id.editTextPassword); editTextPassword = view.findViewById(R.id.editTextPassword);
loginButton = view.findViewById(R.id.buttonAction); 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); loadingProgressBar = view.findViewById(R.id.loading);
// Handle action button click // Handle action button click
@ -114,13 +116,21 @@ public class LoginDialog extends DialogFragment {
} else { } else {
// Handle registration logic // Handle registration logic
String username = editTextUsername.getText().toString(); String username = editTextUsername.getText().toString();
AuthService authRepository = new AuthService(this.getContext()); this.loginViewModel.performRegistration(email, password, username, this::onSuccessFullRegistration);
//authRepository.performRegistration(email, username, password, registerCallback);
} }
}); });
return dialog; 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 @Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);

View File

@ -6,6 +6,7 @@ import android.widget.Toast;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import com.stormtales.notevault.R; import com.stormtales.notevault.R;
import com.stormtales.notevault.network.APICallback;
import com.stormtales.notevault.network.auth.AuthService; import com.stormtales.notevault.network.auth.AuthService;
import com.stormtales.notevault.network.auth.LoginResponse; 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) { public void setAuthService(AuthService authService) {
this.authService = authService; this.authService = authService;
} }