diff --git a/app/src/main/java/core/notevault/MainActivity.java b/app/src/main/java/core/notevault/MainActivity.java index 2d90c9f..8f7076d 100644 --- a/app/src/main/java/core/notevault/MainActivity.java +++ b/app/src/main/java/core/notevault/MainActivity.java @@ -31,7 +31,9 @@ import core.notevault.ui.gallery.GalleryFragment; import core.notevault.ui.gallery.detail.ConcertSongSelector; import core.notevault.ui.gallery.editor.ConcertEditorDialog; import core.notevault.ui.home.HomeFragment; +import core.notevault.ui.login.LoginCallBackImpl; import core.notevault.ui.login.LoginDialogFragment; +import core.notevault.ui.login.RegisterCallback; import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.util.NoteSheetsUtil; @@ -129,7 +131,7 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On } private void showLoginDialog() { - LoginDialogFragment loginDialogFragment = new LoginDialogFragment(); + LoginDialogFragment loginDialogFragment = LoginDialogFragment.newInstance(new LoginCallBackImpl(this), new RegisterCallback(this)); loginDialogFragment.show(getSupportFragmentManager(), "login"); } diff --git a/app/src/main/java/core/notevault/sync/APICallback.java b/app/src/main/java/core/notevault/sync/APICallback.java new file mode 100644 index 0000000..683079e --- /dev/null +++ b/app/src/main/java/core/notevault/sync/APICallback.java @@ -0,0 +1,7 @@ +package core.notevault.sync; + +public interface APICallback { + + void onSuccess(); + void onError(String error); +} diff --git a/app/src/main/java/core/notevault/sync/ApiClient.java b/app/src/main/java/core/notevault/sync/ApiClient.java index f3ec62f..ed8191e 100644 --- a/app/src/main/java/core/notevault/sync/ApiClient.java +++ b/app/src/main/java/core/notevault/sync/ApiClient.java @@ -24,7 +24,7 @@ public class ApiClient { .build(); retrofit = new Retrofit.Builder() - .baseUrl("http://192.168.178.30:8080/") + .baseUrl("http://192.168.178.30:8000/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); diff --git a/app/src/main/java/core/notevault/sync/StatusResponse.java b/app/src/main/java/core/notevault/sync/StatusResponse.java new file mode 100644 index 0000000..b5a3a7e --- /dev/null +++ b/app/src/main/java/core/notevault/sync/StatusResponse.java @@ -0,0 +1,14 @@ +package core.notevault.sync; + +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/core/notevault/sync/auth/AuthRepository.java b/app/src/main/java/core/notevault/sync/auth/AuthRepository.java index 0f9861e..7c8587d 100644 --- a/app/src/main/java/core/notevault/sync/auth/AuthRepository.java +++ b/app/src/main/java/core/notevault/sync/auth/AuthRepository.java @@ -1,8 +1,13 @@ package core.notevault.sync.auth; import android.content.Context; -import android.content.SharedPreferences; +import core.notevault.sync.APICallback; import core.notevault.sync.ApiClient; +import core.notevault.sync.StatusResponse; +import core.notevault.sync.auth.apimodel.LoginRequest; +import core.notevault.sync.auth.apimodel.LoginResponse; +import core.notevault.sync.auth.apimodel.RegisterRequest; +import core.notevault.sync.auth.apimodel.RegisterResponse; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -16,8 +21,8 @@ public class AuthRepository { this.tokenManager = new TokenManager(context); } - public void performLogin(String username, String password, LoginCallback callback) { - LoginRequest loginRequest = new LoginRequest(username, password); + public void performLogin(String email, String password, APICallback callback) { + LoginRequest loginRequest = new LoginRequest(email, password); authService.login(loginRequest).enqueue(new Callback() { @Override @@ -41,6 +46,26 @@ public class AuthRepository { }); } + public void performRegistration(String email, String username, String password, APICallback callback) { + RegisterRequest registerRequest = new RegisterRequest(username, password, email); + authService.registration(registerRequest).enqueue(new Callback() { + + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful() && response.body() != null) { + callback.onSuccess(); + } else { + callback.onError("Registration fehlgeschlagen. Überprüfe Benutzername und Passwort."); + } + } + + @Override + public void onFailure(Call call, Throwable throwable) { + callback.onError("Netzwerkfehler: " + throwable.getMessage()); + } + }); + } + private void saveToken(String token) { tokenManager.saveToken(token); } diff --git a/app/src/main/java/core/notevault/sync/auth/AuthService.java b/app/src/main/java/core/notevault/sync/auth/AuthService.java index 8790231..abe2d19 100644 --- a/app/src/main/java/core/notevault/sync/auth/AuthService.java +++ b/app/src/main/java/core/notevault/sync/auth/AuthService.java @@ -1,11 +1,20 @@ package core.notevault.sync.auth; +import core.notevault.sync.StatusResponse; +import core.notevault.sync.auth.apimodel.LoginRequest; +import core.notevault.sync.auth.apimodel.LoginResponse; +import core.notevault.sync.auth.apimodel.RegisterRequest; import retrofit2.Call; import retrofit2.http.Body; +import retrofit2.http.Headers; import retrofit2.http.POST; public interface AuthService { - @POST("api/v1/auth/login") + @POST("/login/") Call login(@Body LoginRequest loginRequest); + + @POST("/register/") + @Headers("Content-Type: application/json") + Call registration(@Body RegisterRequest registerRequest); } diff --git a/app/src/main/java/core/notevault/sync/auth/LoginRequest.java b/app/src/main/java/core/notevault/sync/auth/LoginRequest.java deleted file mode 100644 index 6465d72..0000000 --- a/app/src/main/java/core/notevault/sync/auth/LoginRequest.java +++ /dev/null @@ -1,13 +0,0 @@ -package core.notevault.sync.auth; - -public class LoginRequest { - private String username; - private String password; - - public LoginRequest(String username, String password) { - this.username = username; - this.password = password; - } - - -} diff --git a/app/src/main/java/core/notevault/sync/auth/apimodel/LoginRequest.java b/app/src/main/java/core/notevault/sync/auth/apimodel/LoginRequest.java new file mode 100644 index 0000000..cb4417c --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/apimodel/LoginRequest.java @@ -0,0 +1,13 @@ +package core.notevault.sync.auth.apimodel; + +public class LoginRequest { + private String email; + private String password; + + public LoginRequest(String email, String password) { + this.email = email; + this.password = password; + } + + +} diff --git a/app/src/main/java/core/notevault/sync/auth/LoginResponse.java b/app/src/main/java/core/notevault/sync/auth/apimodel/LoginResponse.java similarity index 89% rename from app/src/main/java/core/notevault/sync/auth/LoginResponse.java rename to app/src/main/java/core/notevault/sync/auth/apimodel/LoginResponse.java index e9a1330..2f10347 100644 --- a/app/src/main/java/core/notevault/sync/auth/LoginResponse.java +++ b/app/src/main/java/core/notevault/sync/auth/apimodel/LoginResponse.java @@ -1,4 +1,4 @@ -package core.notevault.sync.auth; +package core.notevault.sync.auth.apimodel; public class LoginResponse { private String token; diff --git a/app/src/main/java/core/notevault/sync/auth/apimodel/RegisterRequest.java b/app/src/main/java/core/notevault/sync/auth/apimodel/RegisterRequest.java new file mode 100644 index 0000000..56adf17 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/apimodel/RegisterRequest.java @@ -0,0 +1,14 @@ +package core.notevault.sync.auth.apimodel; + +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/core/notevault/sync/auth/apimodel/RegisterResponse.java b/app/src/main/java/core/notevault/sync/auth/apimodel/RegisterResponse.java new file mode 100644 index 0000000..db52523 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/apimodel/RegisterResponse.java @@ -0,0 +1,31 @@ +package core.notevault.sync.auth.apimodel; + +public class RegisterResponse { + + private String username; + private String email; + + public RegisterResponse(String username, String email) { + this.username = username; + this.email = email; + } + + public RegisterResponse() { + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java b/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java index c777682..b71777d 100644 --- a/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java +++ b/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java @@ -3,9 +3,10 @@ package core.notevault.ui.login; import android.content.Context; import android.util.Log; import android.widget.Toast; +import core.notevault.sync.APICallback; import core.notevault.sync.auth.LoginCallback; -public class LoginCallBackImpl implements LoginCallback { +public class LoginCallBackImpl implements APICallback { private Context context; public LoginCallBackImpl(Context context) { diff --git a/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java b/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java index 0fcda36..5723e68 100644 --- a/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java +++ b/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java @@ -6,41 +6,44 @@ import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; +import android.widget.Button; import android.widget.EditText; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.DialogFragment; import core.notevault.R; +import core.notevault.sync.APICallback; import core.notevault.sync.auth.AuthRepository; import core.notevault.sync.auth.LoginCallback; import retrofit2.Call; import retrofit2.Callback; public class LoginDialogFragment extends DialogFragment { - private LoginCallback loginCallback; + private APICallback loginCallback; + private APICallback registerCallback; + + private boolean isLoginMode = true; // Default mode @Override public void onAttach(Context context) { super.onAttach(context); - if (context instanceof LoginCallback) { - loginCallback = (LoginCallback) context; - } else { - throw new RuntimeException(context.toString() + " must implement LoginDialogListener"); + if (loginCallback == null || registerCallback == null) { + throw new IllegalStateException("LoginDialogFragment must be created using newInstance()"); } } + public static LoginDialogFragment newInstance(APICallback loginCallback, APICallback registerCallback) { + LoginDialogFragment fragment = new LoginDialogFragment(); + fragment.loginCallback = loginCallback; + fragment.registerCallback = registerCallback; + return fragment; + } + @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { - LayoutInflater inflater = requireActivity().getLayoutInflater(); - View loginView = inflater.inflate(R.layout.fragment_login_dialog, null); // Ersetze 'your_dialog_layout' durch deinen Dateinamen - - EditText usernameInput = loginView.findViewById(R.id.editTextUsername); - EditText passwordInput = loginView.findViewById(R.id.editTextPassword); - - AuthRepository authRepository = new AuthRepository(this.getContext()); - - return new AlertDialog.Builder(requireContext()) + /*return new AlertDialog.Builder(requireContext()) .setView(loginView) .setPositiveButton("Speichern", (dialog, which) -> { String username = usernameInput.getText().toString(); @@ -49,7 +52,48 @@ public class LoginDialogFragment extends DialogFragment { authRepository.performLogin(username, password, loginCallback); } ) .setNegativeButton("Abbrechen", (dialog, which) -> {} ) - .create(); + .create();*/ + + AlertDialog.Builder builder = new AlertDialog.Builder(requireContext()); + LayoutInflater inflater = requireActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.fragment_login_dialog, null); + + TextView textViewTitle = view.findViewById(R.id.textViewTitle); + EditText editTextUsername = view.findViewById(R.id.editTextUsername); + EditText editTextEmail = view.findViewById(R.id.editTextEmail); + EditText editTextPassword = view.findViewById(R.id.editTextPassword); + Button buttonAction = view.findViewById(R.id.buttonAction); + TextView textViewSwitch = view.findViewById(R.id.textViewSwitch); + + // Handle action button click + buttonAction.setOnClickListener(v -> { + if (isLoginMode) { + // Handle login logic + String email = editTextEmail.getText().toString(); + String password = editTextPassword.getText().toString(); + AuthRepository authRepository = new AuthRepository(this.getContext()); + authRepository.performLogin(email, password, loginCallback); + } else { + // Handle registration logic + String email = editTextEmail.getText().toString(); + String password = editTextPassword.getText().toString(); + String username = editTextUsername.getText().toString(); + AuthRepository authRepository = new AuthRepository(this.getContext()); + authRepository.performRegistration(email, username, password, registerCallback); + } + }); + + // Toggle between Login and Registration + textViewSwitch.setOnClickListener(v -> { + isLoginMode = !isLoginMode; + textViewTitle.setText(isLoginMode ? "Login" : "Register"); + editTextUsername.setVisibility(isLoginMode ? View.GONE : View.VISIBLE); + buttonAction.setText(isLoginMode ? "Login" : "Register"); + textViewSwitch.setText(isLoginMode ? "Don't have an account? Register" : "Already have an account? Login"); + }); + + builder.setView(view); + return builder.create(); } diff --git a/app/src/main/java/core/notevault/ui/login/RegisterCallback.java b/app/src/main/java/core/notevault/ui/login/RegisterCallback.java new file mode 100644 index 0000000..c9c905b --- /dev/null +++ b/app/src/main/java/core/notevault/ui/login/RegisterCallback.java @@ -0,0 +1,25 @@ +package core.notevault.ui.login; + +import android.content.Context; +import android.util.Log; +import android.widget.Toast; +import core.notevault.sync.APICallback; + +public class RegisterCallback implements APICallback { + private Context context; + + public RegisterCallback(Context context) { + this.context = context; + } + + @Override + public void onSuccess() { + Toast.makeText(context, "Registration successfull", Toast.LENGTH_SHORT).show(); + } + + @Override + public void onError(String error) { + Log.d("Login", error); + Toast.makeText(context, "Login not successfull: " + error, Toast.LENGTH_LONG).show(); + } +} diff --git a/app/src/main/res/drawable/dialog_background.xml b/app/src/main/res/drawable/dialog_background.xml new file mode 100644 index 0000000..5b12544 --- /dev/null +++ b/app/src/main/res/drawable/dialog_background.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_login_dialog.xml b/app/src/main/res/layout/fragment_login_dialog.xml index cd95cea..da64177 100644 --- a/app/src/main/res/layout/fragment_login_dialog.xml +++ b/app/src/main/res/layout/fragment_login_dialog.xml @@ -1,21 +1,68 @@ - + android:padding="16dp" + android:background="@drawable/dialog_background" + android:layout_gravity="center_horizontal"> + + + + + android:hint="Username" + android:visibility="gone" /> + + + + + + +