nextNoteVault #23
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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<LoginResponse> login(@Body LoginRequest loginRequest);
 | 
			
		||||
 | 
			
		||||
    /*@POST("/register/")
 | 
			
		||||
    @POST("/register/")
 | 
			
		||||
    @Headers("Content-Type: application/json")
 | 
			
		||||
    Call<StatusResponse> registration(@Body RegisterRequest registerRequest);*/
 | 
			
		||||
    Call<StatusResponse> registration(@Body RegisterRequest registerRequest);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<StatusResponse>() {
 | 
			
		||||
        RegisterRequest registerRequest = new RegisterRequest(username, password, email);
 | 
			
		||||
        authAPI.registration(registerRequest).enqueue(new Callback<StatusResponse>() {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {
 | 
			
		||||
@ -60,7 +61,7 @@ public class AuthService {
 | 
			
		||||
            public void onFailure(Call<StatusResponse> call, Throwable throwable) {
 | 
			
		||||
                callback.onError("Netzwerkfehler: " + throwable.getMessage());
 | 
			
		||||
            }
 | 
			
		||||
        });*/
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void saveToken(String token) {
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user