diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 69284fd4..94645a0b 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,11 +1,4 @@ - - @@ -17,10 +10,13 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:usesCleartextTraffic="true" android:theme="@style/Theme.Aether" + android:usesCleartextTraffic="true" tools:ignore="AllowBackup"> - + + { -// Log.d("refreshResponse", refreshResponse.getMessage() +" " + refreshResponse.isSuccess() + " " + refreshResponse.getAccessToken()); - viewModel.switchActivity(refreshResponse); + if(!refreshResponse.isSuccess()){ + viewModel.switchActivity(refreshResponse); + }else{ + viewModel.getProfileValidityResponseMutableLiveData().observe(this, profileValidityResponse -> { + viewModel.switchActivity(profileValidityResponse); + }); + } }); } @Override protected void onDestroy() { super.onDestroy(); - - if(viewModel.getRefreshResponseMutableLiveData().hasActiveObservers()) { - viewModel.getRefreshResponseMutableLiveData().removeObservers(this); - } } } \ No newline at end of file diff --git a/android/app/src/main/java/life/nsu/aether/models/Student.java b/android/app/src/main/java/life/nsu/aether/models/Student.java new file mode 100644 index 00000000..898a8d55 --- /dev/null +++ b/android/app/src/main/java/life/nsu/aether/models/Student.java @@ -0,0 +1,84 @@ +/* + * Student Created by Samiur Prapon + * Last modified 24/8/21, 12:51 am + * Copyright (c) 2021. All rights reserved. + * + */ + +package life.nsu.aether.models; +public class Student { + + private String id; + private String name; + private String studentID; + private String sex; + private String uid; + private String status; + private String createdAt; + private String updatedAt; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getStudentID() { + return studentID; + } + + public void setStudentID(String studentID) { + this.studentID = studentID; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public String getUid() { + return uid; + } + + public void setUid(String uid) { + this.uid = uid; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + public String getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(String updatedAt) { + this.updatedAt = updatedAt; + } + +} \ No newline at end of file diff --git a/android/app/src/main/java/life/nsu/aether/repositories/ProfileRepository.java b/android/app/src/main/java/life/nsu/aether/repositories/ProfileRepository.java new file mode 100644 index 00000000..0cf4fd1c --- /dev/null +++ b/android/app/src/main/java/life/nsu/aether/repositories/ProfileRepository.java @@ -0,0 +1,142 @@ +/* + * ProfileRepository Created by Samiur Prapon + * Last modified 24/8/21, 12:51 am + * Copyright (c) 2021. All rights reserved. + * + */ + +package life.nsu.aether.repositories; + +import android.app.Application; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; + +import java.io.IOException; +import java.lang.annotation.Annotation; + +import life.nsu.aether.models.Student; +import life.nsu.aether.utils.networking.NetworkingService; +import life.nsu.aether.utils.networking.requests.ProfileUpdateRequest; +import life.nsu.aether.utils.networking.responses.ProfileValidityResponse; +import life.nsu.aether.utils.networking.responses.StudentProfileDetailsResponse; +import okhttp3.ResponseBody; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Converter; +import retrofit2.Response; + +public class ProfileRepository { + Application application; + MutableLiveData profileValidityResponseMutableLiveData; + MutableLiveData studentProfileDetailsResponseMutableLiveData; + MutableLiveData studentProfileUpdateDetailsResponseMutableLiveData; + + + private static ProfileRepository profileRepository = null; + + public synchronized static ProfileRepository getInstance(Application application) { + if (profileRepository == null) { + profileRepository = new ProfileRepository(application); + } + + return profileRepository; + } + + private ProfileRepository(Application application) { + this.application = application; + + profileValidityResponseMutableLiveData = new MutableLiveData<>(); + studentProfileDetailsResponseMutableLiveData = new MutableLiveData<>(); + studentProfileUpdateDetailsResponseMutableLiveData = new MutableLiveData<>(); + + } + + public MutableLiveData getProfileValidityResponseMutableLiveData(String refreshToken) { + Call call = NetworkingService.getInstance() + .getRoute() + .validateStudentProfile(refreshToken); + + call.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.body() != null) { + profileValidityResponseMutableLiveData.postValue(response.body()); + } + + if (response.errorBody() != null) { + profileValidityResponseMutableLiveData.postValue(new ProfileValidityResponse(false, response.message(), false)); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + profileValidityResponseMutableLiveData.postValue(new ProfileValidityResponse(false, t.getMessage(), false)); + } + }); + + return profileValidityResponseMutableLiveData; + } + + public MutableLiveData getStudentProfileDetailsResponseMutableLiveData(String refreshToken) { + Call call = NetworkingService.getInstance() + .getRoute() + .getStudentProfile(refreshToken); + + call.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.body() != null) { + studentProfileDetailsResponseMutableLiveData.postValue(response.body()); + } + + if (response.errorBody() != null) { + studentProfileDetailsResponseMutableLiveData.postValue(new StudentProfileDetailsResponse(false, response.message(), new Student())); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + studentProfileDetailsResponseMutableLiveData.postValue(new StudentProfileDetailsResponse(false, t.getMessage(), new Student())); + } + }); + + return studentProfileDetailsResponseMutableLiveData; + } + + public LiveData getStudentProfileUpdateResponseLiveData(String refreshToken, String name, String gender, String studentId) { + Call call = NetworkingService.getInstance() + .getRoute() + .updateProfile(refreshToken, new ProfileUpdateRequest(Integer.parseInt(studentId), name, gender)); + + call.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + + if (response.body() != null) { + studentProfileUpdateDetailsResponseMutableLiveData.postValue(response.body()); + } + + if (response.errorBody() != null) { + Log.e("Error", String.valueOf(response.code())); + try { + Log.e("Error", response.errorBody().string()); + } catch (IOException e) { + e.printStackTrace(); + } + + studentProfileUpdateDetailsResponseMutableLiveData.postValue(new StudentProfileDetailsResponse(false, response.message(), new Student())); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + studentProfileUpdateDetailsResponseMutableLiveData.postValue(new StudentProfileDetailsResponse(false, t.getMessage(), new Student())); + } + }); + + return studentProfileUpdateDetailsResponseMutableLiveData; + } +} diff --git a/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java b/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java index 78f82923..409bea04 100644 --- a/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java +++ b/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java @@ -69,7 +69,6 @@ public void onResponse(@NonNull Call call, @NonNull Response call, @NonNull Response deAuthentication(@Header("Authorization") String accessToken); + @GET("student/valid") + Call validateStudentProfile(@Header("Authorization") String accessToken); + + @POST("student") + Call updateProfile(@Header("Authorization") String accessToken, @Body ProfileUpdateRequest request); + + @GET("student") + Call getStudentProfile(@Header("Authorization") String accessToken); + } \ No newline at end of file diff --git a/android/app/src/main/java/life/nsu/aether/utils/networking/requests/ProfileUpdateRequest.java b/android/app/src/main/java/life/nsu/aether/utils/networking/requests/ProfileUpdateRequest.java new file mode 100644 index 00000000..d09ff0eb --- /dev/null +++ b/android/app/src/main/java/life/nsu/aether/utils/networking/requests/ProfileUpdateRequest.java @@ -0,0 +1,48 @@ +/* + * ProfileUpdateRequest Created by Samiur Prapon + * Last modified 24/8/21, 12:51 am + * Copyright (c) 2021. All rights reserved. + * + */ + +package life.nsu.aether.utils.networking.requests; + +public class ProfileUpdateRequest { + private int studentID; + private String name; + private String sex; + + public ProfileUpdateRequest(){ + // Default Constructor + } + + public ProfileUpdateRequest(int studentID, String name, String sex) { + this.studentID = studentID; + this.name = name; + this.sex = sex; + } + + public int getStudentID() { + return studentID; + } + + public void setStudentID(int studentID) { + this.studentID = studentID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } +} diff --git a/android/app/src/main/java/life/nsu/aether/utils/networking/responses/ProfileValidityResponse.java b/android/app/src/main/java/life/nsu/aether/utils/networking/responses/ProfileValidityResponse.java new file mode 100644 index 00000000..242d7dec --- /dev/null +++ b/android/app/src/main/java/life/nsu/aether/utils/networking/responses/ProfileValidityResponse.java @@ -0,0 +1,32 @@ +/* + * ProfileValidityResponse Created by Samiur Prapon + * Last modified 24/8/21, 12:51 am + * Copyright (c) 2021. All rights reserved. + * + */ + +package life.nsu.aether.utils.networking.responses; + +public class ProfileValidityResponse { + private boolean success; + private String message; + private boolean isCompleted; + + public ProfileValidityResponse(boolean success, String message, boolean isCompleted) { + this.success = success; + this.message = message; + this.isCompleted = isCompleted; + } + + public String getMessage() { + return message; + } + + public boolean isCompleted() { + return isCompleted; + } + + public boolean isSuccess() { + return success; + } +} diff --git a/android/app/src/main/java/life/nsu/aether/utils/networking/responses/StudentProfileDetailsResponse.java b/android/app/src/main/java/life/nsu/aether/utils/networking/responses/StudentProfileDetailsResponse.java new file mode 100644 index 00000000..d1b6f8eb --- /dev/null +++ b/android/app/src/main/java/life/nsu/aether/utils/networking/responses/StudentProfileDetailsResponse.java @@ -0,0 +1,48 @@ +/* + * StudentProfileDetailsResponse Created by Samiur Prapon + * Last modified 24/8/21, 12:51 am + * Copyright (c) 2021. All rights reserved. + * + */ + +package life.nsu.aether.utils.networking.responses; + +import life.nsu.aether.models.Student; + +public class StudentProfileDetailsResponse { + + public StudentProfileDetailsResponse(boolean success, String message, Student student) { + this.success = success; + this.message = message; + this.student = student; + } + + private Boolean success; + private String message; + private Student student; + + public Boolean getSuccess() { + return success; + } + + public void setSuccess(Boolean success) { + this.success = success; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Student getStudent() { + return student; + } + + public void setStudent(Student student) { + this.student = student; + } + +} diff --git a/android/app/src/main/java/life/nsu/aether/viewModels/SplashViewModel.java b/android/app/src/main/java/life/nsu/aether/viewModels/SplashViewModel.java index b3e88841..e76f0715 100644 --- a/android/app/src/main/java/life/nsu/aether/viewModels/SplashViewModel.java +++ b/android/app/src/main/java/life/nsu/aether/viewModels/SplashViewModel.java @@ -18,41 +18,61 @@ import java.util.Objects; +import life.nsu.aether.repositories.ProfileRepository; import life.nsu.aether.repositories.RefreshRepository; import life.nsu.aether.utils.Preference; +import life.nsu.aether.utils.networking.responses.ProfileValidityResponse; import life.nsu.aether.utils.networking.responses.RefreshResponse; import life.nsu.aether.views.authentication.LoginActivity; import life.nsu.aether.views.student.StudentHomeActivity; +import life.nsu.aether.views.student.profile.EditProfileActivity; public class SplashViewModel extends AndroidViewModel { RefreshRepository refreshRepository; + ProfileRepository profileRepository; Preference preference; public SplashViewModel(@NonNull Application application) { super(application); - preference = new Preference(application); + refreshRepository = RefreshRepository.getInstance(application); + profileRepository = ProfileRepository.getInstance(application); } public LiveData getRefreshResponseMutableLiveData() { return refreshRepository.getRefreshResponseMutableLiveData(preference.getRefreshToken()); } + public LiveData getProfileValidityResponseMutableLiveData() { + return profileRepository.getProfileValidityResponseMutableLiveData(preference.getAccessToken()); + } + public void switchActivity(RefreshResponse refreshResponse) { new Handler(Objects.requireNonNull(Looper.myLooper())).postDelayed(() -> { Intent intent; - if (!refreshResponse.isSuccess()) { intent = new Intent(getApplication().getApplicationContext(), LoginActivity.class); } else { preference.setAccessToken(refreshResponse.getAccessToken()); - intent = new Intent(getApplication().getApplicationContext(), StudentHomeActivity.class); } + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + getApplication().getApplicationContext().startActivity(intent); + }, 250); + } + public void switchActivity(ProfileValidityResponse profileValidityResponse) { + new Handler(Objects.requireNonNull(Looper.myLooper())).postDelayed(() -> { + Intent intent; + if (!profileValidityResponse.isSuccess() || !profileValidityResponse.isCompleted()) { + intent = new Intent(getApplication().getApplicationContext(), EditProfileActivity.class); + intent.putExtra("fetch_profile_data", false); + } else{ + intent = new Intent(getApplication().getApplicationContext(), StudentHomeActivity.class); + } intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); getApplication().getApplicationContext().startActivity(intent); }, 250); diff --git a/android/app/src/main/java/life/nsu/aether/viewModels/authentication/LoginViewModel.java b/android/app/src/main/java/life/nsu/aether/viewModels/authentication/LoginViewModel.java index 00faeaa0..3b1c50f9 100644 --- a/android/app/src/main/java/life/nsu/aether/viewModels/authentication/LoginViewModel.java +++ b/android/app/src/main/java/life/nsu/aether/viewModels/authentication/LoginViewModel.java @@ -21,12 +21,16 @@ import java.util.Objects; import life.nsu.aether.repositories.LoginRepository; +import life.nsu.aether.repositories.ProfileRepository; import life.nsu.aether.utils.Preference; import life.nsu.aether.utils.networking.responses.LoginResponse; +import life.nsu.aether.utils.networking.responses.ProfileValidityResponse; import life.nsu.aether.views.student.StudentHomeActivity; +import life.nsu.aether.views.student.profile.EditProfileActivity; public class LoginViewModel extends AndroidViewModel { LoginRepository loginRepository; + ProfileRepository profileRepository; Preference preference; public LoginViewModel(@NonNull Application application) { @@ -34,15 +38,19 @@ public LoginViewModel(@NonNull Application application) { preference = new Preference(application); loginRepository = LoginRepository.getInstance(application); + profileRepository = ProfileRepository.getInstance(application); } public LiveData getMessageResponseLiveData(String email, String password) { return loginRepository.getMutableLoginResponse(email, password); } + public LiveData getProfileValidityResponseMutableLiveData() { + return profileRepository.getProfileValidityResponseMutableLiveData(preference.getAccessToken()); + } + public void switchActivity(LoginResponse loginResponse) { new Handler(Objects.requireNonNull(Looper.myLooper())).postDelayed(() -> { - if (loginResponse.isSuccess()) { if(loginResponse.getType().equals("student")) { preference.setType(loginResponse.getType()); @@ -60,4 +68,23 @@ public void switchActivity(LoginResponse loginResponse) { }, 250); } + + public void switchActivity(LoginResponse loginResponse, ProfileValidityResponse profileValidityResponse) { + new Handler(Objects.requireNonNull(Looper.myLooper())).postDelayed(() -> { + Intent intent; + if(loginResponse.getType().equals("student")) { + preference.setType(loginResponse.getType()); + preference.setAccessToken(loginResponse.getAccessToken()); + preference.setRefreshToken(loginResponse.getRefreshToken()); + } + if (!profileValidityResponse.isSuccess() || !profileValidityResponse.isCompleted()) { + intent = new Intent(getApplication().getApplicationContext(), EditProfileActivity.class); + intent.putExtra("fetch_profile_data", false); + } else{ + intent = new Intent(getApplication().getApplicationContext(), StudentHomeActivity.class); + } + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + getApplication().getApplicationContext().startActivity(intent); + }, 250); + } } diff --git a/android/app/src/main/java/life/nsu/aether/viewModels/student/StudentMoreViewModel.java b/android/app/src/main/java/life/nsu/aether/viewModels/student/StudentMoreViewModel.java index 84e7d165..75509e61 100644 --- a/android/app/src/main/java/life/nsu/aether/viewModels/student/StudentMoreViewModel.java +++ b/android/app/src/main/java/life/nsu/aether/viewModels/student/StudentMoreViewModel.java @@ -44,7 +44,6 @@ public LiveData getDeAuthResponse() { public void switchActivity(MessageResponse messageResponse) { new Handler(Objects.requireNonNull(Looper.myLooper())).postDelayed(() -> { - if (messageResponse.isSuccess()) { preference.clearAuth(); Intent intent = new Intent(getApplication().getApplicationContext(), LoginActivity.class); diff --git a/android/app/src/main/java/life/nsu/aether/viewModels/student/StudentProfileViewModel.java b/android/app/src/main/java/life/nsu/aether/viewModels/student/StudentProfileViewModel.java new file mode 100644 index 00000000..ae0c9e15 --- /dev/null +++ b/android/app/src/main/java/life/nsu/aether/viewModels/student/StudentProfileViewModel.java @@ -0,0 +1,58 @@ +/* + * StudentProfileViewModel Created by Samiur Prapon + * Last modified 24/8/21, 12:51 am + * Copyright (c) 2021. All rights reserved. + * + */ + +package life.nsu.aether.viewModels.student; + +import android.app.Application; +import android.content.Intent; +import android.os.Handler; +import android.os.Looper; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; + +import java.util.Objects; + +import life.nsu.aether.repositories.ProfileRepository; +import life.nsu.aether.utils.Preference; +import life.nsu.aether.utils.networking.responses.StudentProfileDetailsResponse; +import life.nsu.aether.views.student.StudentHomeActivity; + +public class StudentProfileViewModel extends AndroidViewModel { + + ProfileRepository profileRepository; + Preference preference; + + public StudentProfileViewModel(@NonNull Application application) { + super(application); + + preference = new Preference(application); + + profileRepository = ProfileRepository.getInstance(application); + } + + public void switchActivity(StudentProfileDetailsResponse profileDetailsResponse) { + new Handler(Objects.requireNonNull(Looper.myLooper())).postDelayed(() -> { + Intent intent; + if(profileDetailsResponse.getSuccess()){ + intent = new Intent(getApplication().getApplicationContext(), StudentHomeActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + getApplication().getApplicationContext().startActivity(intent); + } + }, 250); + } + + + public LiveData getStudentProfileDetailsResponseMutableLiveData() { + return profileRepository.getStudentProfileDetailsResponseMutableLiveData(preference.getAccessToken()); + } + + public LiveData getProfileUpdateResponseLiveData(String name, String gender, String studentId) { + return profileRepository.getStudentProfileUpdateResponseLiveData(preference.getAccessToken(), name, gender, studentId); + } +} diff --git a/android/app/src/main/java/life/nsu/aether/views/authentication/LoginActivity.java b/android/app/src/main/java/life/nsu/aether/views/authentication/LoginActivity.java index 1cab5fca..50a51369 100644 --- a/android/app/src/main/java/life/nsu/aether/views/authentication/LoginActivity.java +++ b/android/app/src/main/java/life/nsu/aether/views/authentication/LoginActivity.java @@ -67,12 +67,17 @@ protected void onCreate(Bundle savedInstanceState) { progressBar.hide(); return; } - - new Handler(Looper.myLooper()).postDelayed(() -> viewModel.getMessageResponseLiveData(email, password).observe(this, loginResponse -> { - progressBar.hide(); - - viewModel.switchActivity(loginResponse); - }), 250); + viewModel.getMessageResponseLiveData(email, password).observe(this, loginResponse -> { + if(loginResponse.isSuccess()){ + viewModel.getProfileValidityResponseMutableLiveData().observe(this, profileValidityResponse -> { + progressBar.hide(); + viewModel.switchActivity(loginResponse, profileValidityResponse); + }); + }else{ + progressBar.hide(); + viewModel.switchActivity(loginResponse); + } + }); }); mRegister.setOnClickListener(v -> { diff --git a/android/app/src/main/java/life/nsu/aether/views/authentication/RegistrationActivity.java b/android/app/src/main/java/life/nsu/aether/views/authentication/RegistrationActivity.java index ddbc99cb..14aa1420 100644 --- a/android/app/src/main/java/life/nsu/aether/views/authentication/RegistrationActivity.java +++ b/android/app/src/main/java/life/nsu/aether/views/authentication/RegistrationActivity.java @@ -103,4 +103,4 @@ public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right); } -} \ No newline at end of file +} diff --git a/android/app/src/main/java/life/nsu/aether/views/student/profile/EditProfileActivity.java b/android/app/src/main/java/life/nsu/aether/views/student/profile/EditProfileActivity.java new file mode 100644 index 00000000..a6d541dc --- /dev/null +++ b/android/app/src/main/java/life/nsu/aether/views/student/profile/EditProfileActivity.java @@ -0,0 +1,120 @@ +/* + * EditProfileActivity Created by Samiur Prapon + * Last modified 24/8/21, 12:51 am + * Copyright (c) 2021. All rights reserved. + * + */ + +package life.nsu.aether.views.student.profile; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.ViewModelProvider; + +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.widget.RadioGroup; + +import com.google.android.material.button.MaterialButton; +import com.google.android.material.textfield.TextInputEditText; + +import java.util.Objects; + +import life.nsu.aether.R; +import life.nsu.aether.utils.CustomProgressBar; +import life.nsu.aether.utils.networking.responses.StudentProfileDetailsResponse; +import life.nsu.aether.viewModels.student.StudentProfileViewModel; + +public class EditProfileActivity extends AppCompatActivity { + + StudentProfileViewModel studentProfileViewModel; + CustomProgressBar progressBar; + MaterialButton saveDetailsButton; + TextInputEditText textInputEditTextName, textInputEditTextStudentId; + RadioGroup genderSelectionRadioGroup; + boolean fetchProfileData = false; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_edit_profile); + studentProfileViewModel = new ViewModelProvider(this).get(StudentProfileViewModel.class); + saveDetailsButton = findViewById(R.id.mb_save); + textInputEditTextName = findViewById(R.id.et_name); + textInputEditTextStudentId = findViewById(R.id.et_student_id); + genderSelectionRadioGroup = findViewById(R.id.radioGroup); + fetchProfileData = getIntent().getBooleanExtra("fetch_profile_data", false); + progressBar = new CustomProgressBar(this); + if(fetchProfileData){ + // Fetch students previous data and place them on ui for editing profile + studentProfileViewModel.getStudentProfileDetailsResponseMutableLiveData().observe(this, this::changeUiAccordingToStudentsProfileData); + }else{ + // Let student complete their profile for the first time in app + initSaveButtonListener(); + } + } + + private void changeUiAccordingToStudentsProfileData(StudentProfileDetailsResponse studentProfileDetailsResponse) { + textInputEditTextName.setText(studentProfileDetailsResponse.getStudent().getName()); + if(studentProfileDetailsResponse.getStudent().getSex().equals("male")){ + genderSelectionRadioGroup.check(R.id.rb_male); + }else{ + genderSelectionRadioGroup.check(R.id.rb_female); + } + } + + private void initSaveButtonListener() { + textInputEditTextName.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + @Override + public void afterTextChanged(Editable s) { + saveDetailsButton.setEnabled(!s.toString().isEmpty() && !Objects.requireNonNull(textInputEditTextStudentId.getText()).toString().isEmpty()); + } + }); + textInputEditTextStudentId.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + @Override + public void afterTextChanged(Editable s) { + saveDetailsButton.setEnabled(!s.toString().isEmpty() && !Objects.requireNonNull(textInputEditTextName.getText()).toString().isEmpty()); + } + }); + saveDetailsButton.setOnClickListener(v -> { + String name = Objects.requireNonNull(textInputEditTextName.getText()).toString(); + String studentId = Objects.requireNonNull(textInputEditTextStudentId.getText()).toString(); + String gender; + if(genderSelectionRadioGroup.getCheckedRadioButtonId()==R.id.rb_male){ + gender = "male"; + }else{ + gender = "female"; + } + progressBar.show("Updating Profile"); + studentProfileViewModel.getProfileUpdateResponseLiveData(name, gender, studentId).observe(EditProfileActivity.this, studentProfileDetailsResponse -> { + progressBar.hide(); + if(studentProfileDetailsResponse.getSuccess()){ + if(fetchProfileData){ + // As this means the user was updating his profile so we can simply destroy this activity + finish(); + }else{ + // Otherwise the user was adding his profile for the first time so let's redirect him to home activity + studentProfileViewModel.switchActivity(studentProfileDetailsResponse); + } + } + }); + }); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + } +} \ No newline at end of file diff --git a/android/app/src/main/res/drawable/ic_baseline_person_24.xml b/android/app/src/main/res/drawable/ic_baseline_person_24.xml new file mode 100644 index 00000000..19ddb42b --- /dev/null +++ b/android/app/src/main/res/drawable/ic_baseline_person_24.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/android/app/src/main/res/drawable/ic_baseline_school_24.xml b/android/app/src/main/res/drawable/ic_baseline_school_24.xml new file mode 100644 index 00000000..b70eda04 --- /dev/null +++ b/android/app/src/main/res/drawable/ic_baseline_school_24.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/android/app/src/main/res/drawable/shape_bg_top_curved_corners.xml b/android/app/src/main/res/drawable/shape_bg_top_curved_corners.xml new file mode 100644 index 00000000..5349334a --- /dev/null +++ b/android/app/src/main/res/drawable/shape_bg_top_curved_corners.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/activity_edit_profile.xml b/android/app/src/main/res/layout/activity_edit_profile.xml new file mode 100644 index 00000000..7440bd36 --- /dev/null +++ b/android/app/src/main/res/layout/activity_edit_profile.xml @@ -0,0 +1,184 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/app/src/main/res/layout/activity_registration.xml b/android/app/src/main/res/layout/activity_registration.xml index 13f2b6eb..2313752c 100644 --- a/android/app/src/main/res/layout/activity_registration.xml +++ b/android/app/src/main/res/layout/activity_registration.xml @@ -188,7 +188,7 @@ - \ No newline at end of file + diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index d1aa0dbf..7fd2655a 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -71,6 +71,13 @@ Privacy Policy Logout + User Profile ImageView + Save + Female + Male + Gender + Name + Student Id \ No newline at end of file