From 13ebb9088d9141aa63f8e719fe46b64c7a0ce7a6 Mon Sep 17 00:00:00 2001 From: rvyk Date: Sun, 11 Jun 2023 23:42:01 +0200 Subject: [PATCH] Homework managment (summary, save, send), translations, colored appbar --- .../java/pl/rvyk/instapp/UserInterface.java | 4 +- .../instapp/fragments/HomeworkFragment.java | 23 +- .../fragments/HomeworkSummaryFragment.java | 272 ++++++++++++++++ .../ic_baseline_arrow_back_24.xml | 5 + .../drawable/ic_baseline_arrow_back_24.xml | 5 + .../main/res/drawable/ic_baseline_save_24.xml | 5 + .../main/res/drawable/ic_baseline_send_24.xml | 5 + app/src/main/res/layout/fragment_homework.xml | 3 +- .../res/layout/fragment_homework_summary.xml | 292 ++++++++++++++++++ app/src/main/res/layout/fragment_settings.xml | 3 +- app/src/main/res/layout/fragment_start.xml | 1 + app/src/main/res/values-pl/strings.xml | 11 + app/src/main/res/values-tr-rTR/strings.xml | 11 + app/src/main/res/values/strings.xml | 11 + 14 files changed, 645 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/pl/rvyk/instapp/fragments/HomeworkSummaryFragment.java create mode 100644 app/src/main/res/drawable-night/ic_baseline_arrow_back_24.xml create mode 100644 app/src/main/res/drawable/ic_baseline_arrow_back_24.xml create mode 100644 app/src/main/res/drawable/ic_baseline_save_24.xml create mode 100644 app/src/main/res/drawable/ic_baseline_send_24.xml create mode 100644 app/src/main/res/layout/fragment_homework_summary.xml diff --git a/app/src/main/java/pl/rvyk/instapp/UserInterface.java b/app/src/main/java/pl/rvyk/instapp/UserInterface.java index a4ba4fe..6f97269 100644 --- a/app/src/main/java/pl/rvyk/instapp/UserInterface.java +++ b/app/src/main/java/pl/rvyk/instapp/UserInterface.java @@ -95,9 +95,9 @@ public void onClick(View v) { } } - private Fragment currentFragment = null; + public Fragment currentFragment = null; - private void replaceFragment(Fragment fragment) { + public void replaceFragment(Fragment fragment) { if (currentFragment != null && fragment.getClass().equals(currentFragment.getClass())) { return; } diff --git a/app/src/main/java/pl/rvyk/instapp/fragments/HomeworkFragment.java b/app/src/main/java/pl/rvyk/instapp/fragments/HomeworkFragment.java index 721473d..e297659 100644 --- a/app/src/main/java/pl/rvyk/instapp/fragments/HomeworkFragment.java +++ b/app/src/main/java/pl/rvyk/instapp/fragments/HomeworkFragment.java @@ -16,6 +16,8 @@ import androidx.appcompat.view.menu.ActionMenuItemView; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -228,7 +230,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.statusBar.setBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.colorDone)); } - final String link = homework.getString("homeworkLink"); // Pobierz link do zadania + final String link = homework.getString("homeworkLink"); String title = homework.getString("title"); String deadline = homework.getString("deadline"); @@ -253,7 +255,24 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Toast.makeText(view.getContext(), link, Toast.LENGTH_LONG).show(); + try { + + HomeworkSummaryFragment homeworkSummaryFragment = new HomeworkSummaryFragment(); + + Bundle bundle = new Bundle(); + bundle.putString("homeworkLink", link); + bundle.putString("homeworkExercise", homework.getString("title")); + homeworkSummaryFragment.setArguments(bundle); + + FragmentManager fragmentManager = requireActivity().getSupportFragmentManager(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + fragmentTransaction.replace(R.id.frameLayout, homeworkSummaryFragment); + fragmentTransaction.addToBackStack(null); + fragmentTransaction.commit(); + } catch (Exception e) { + SnackbarController.showSnackbar(getActivity(), content, e, getResources().getString(R.string.unkown_error), true); + + } } }); } catch (JSONException e) { diff --git a/app/src/main/java/pl/rvyk/instapp/fragments/HomeworkSummaryFragment.java b/app/src/main/java/pl/rvyk/instapp/fragments/HomeworkSummaryFragment.java new file mode 100644 index 0000000..868c124 --- /dev/null +++ b/app/src/main/java/pl/rvyk/instapp/fragments/HomeworkSummaryFragment.java @@ -0,0 +1,272 @@ +package pl.rvyk.instapp.fragments; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; + +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.JsonObjectRequest; +import com.android.volley.toolbox.Volley; +import com.google.android.material.appbar.MaterialToolbar; +import com.google.android.material.button.MaterialButton; +import com.google.android.material.card.MaterialCardView; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.textfield.TextInputEditText; +import com.google.android.material.textfield.TextInputLayout; + +import org.json.JSONException; +import org.json.JSONObject; + +import pl.rvyk.instapp.R; +import pl.rvyk.instapp.utils.SnackbarController; + +public class HomeworkSummaryFragment extends Fragment { + + private static final String API_URL = "https://api.ezinstaling.lol/api/v1/instaling/gethomeworksummary"; + private static final String API_URL_SAVE_HOMEWORK = "https://api.ezinstaling.lol/api/v1/instaling/savehomework"; + + private TextView title, term, exercise; + private TextInputLayout answerLayout, teacherNoteLayout, gradeLayout; + private TextInputEditText answer, teacherNote, grade; + private SharedPreferences sharedPreferences; + private RequestQueue requestQueue; + private MaterialButton saveButton, sendButton; + private MaterialCardView information, note, gradeView; + private LinearLayout loaderLayout, content; + + private String homeworkLink, homeworkExercise; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, + @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_homework_summary, container, false); + + sharedPreferences = requireActivity().getSharedPreferences("Account1", Context.MODE_PRIVATE); + String phpsessid = sharedPreferences.getString("phpsessid", ""); + + MaterialToolbar toolbar = view.findViewById(R.id.materialToolbar); + + title = view.findViewById(R.id.titleTextView); + term = view.findViewById(R.id.deadlineTextView); + answerLayout = view.findViewById(R.id.homeworkAnswer); + answer = answerLayout.findViewById(R.id.homeworkAnswerInput); + exercise = view.findViewById(R.id.exercise); + saveButton = view.findViewById(R.id.saveButton); + sendButton = view.findViewById(R.id.sendButton); + information = view.findViewById(R.id.information); + note = view.findViewById(R.id.teacherNote); + teacherNoteLayout = view.findViewById(R.id.homeworkTeacherNote); + teacherNote = teacherNoteLayout.findViewById(R.id.homeworkTeacherNoteText); + gradeLayout = view.findViewById(R.id.homeworkGradeLayout); + gradeView = view.findViewById(R.id.homeworkGradeCard); + grade = gradeLayout.findViewById(R.id.homeworkGrade); + loaderLayout = view.findViewById(R.id.progressBar); + content = view.findViewById(R.id.fragmentHomeworkSummary); + + requestQueue = Volley.newRequestQueue(requireContext()); + + Bundle bundle = getArguments(); + if (bundle != null) { + homeworkLink = bundle.getString("homeworkLink"); + homeworkExercise = bundle.getString("homeworkExercise"); + getHomeworks(homeworkLink, phpsessid); + } + + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + FragmentManager fragmentManager = requireActivity().getSupportFragmentManager(); + fragmentManager.popBackStack(); + } + }); + + saveButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String userResponse = answer.getText().toString(); + sendHomeworkRequest(phpsessid, userResponse, "save"); + } + }); + + sendButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity()); + builder.setTitle(getString(R.string.homework_dialog_title)); + builder.setMessage(getString(R.string.homework_dialog_message)); + builder.setNegativeButton(getActivity().getResources().getString(R.string.reportDialogCancel), null); + builder.setPositiveButton("Potwierdzam", (dialog, which) -> { + String userResponse = answer.getText().toString(); + sendHomeworkRequest(phpsessid, userResponse, "save_and_send"); + }); + builder.show(); + } + }); + + return view; + } + + private void sendHomeworkRequest(String phpsessid, String userResponse, String action) { + JSONObject jsonParams = new JSONObject(); + try { + jsonParams.put("phpsessid", phpsessid); + jsonParams.put("link", homeworkLink); + jsonParams.put("exercise", homeworkExercise); + jsonParams.put("response", userResponse); + jsonParams.put("action", action); + } catch (JSONException e) { + e.printStackTrace(); + SnackbarController.showSnackbar(getActivity(), content, e, getResources().getString(R.string.unkown_error), true); + } + + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, API_URL_SAVE_HOMEWORK, jsonParams, + new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + + Log.d("test", String.valueOf(response)); + if (!isAdded()) { + return; + } + + try { + boolean success = response.getBoolean("success"); + if (success) { + if (action.equals("save")) { + Toast.makeText(getActivity(), getString(R.string.homework_saved), Toast.LENGTH_SHORT).show(); + } else if (action.equals("save_and_send")) { + Toast.makeText(getActivity(), getString(R.string.homework_completed), Toast.LENGTH_SHORT).show(); + FragmentManager fragmentManager = requireActivity().getSupportFragmentManager(); + fragmentManager.popBackStack(); + } + } else { + Throwable error = new Throwable(getString(R.string.unkown_error)); + SnackbarController.showSnackbar(getActivity(), content, error, getResources().getString(R.string.unkown_error), true); + hideLoader(); + } + } catch (JSONException e) { + SnackbarController.showSnackbar(getActivity(), content, e, getResources().getString(R.string.unkown_error), true); + hideLoader(); + } + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + if (!isAdded()) { + return; + } + SnackbarController.showSnackbar(getActivity(), content, error, getResources().getString(R.string.unkown_error), true); + } + }); + + requestQueue.add(jsonObjectRequest); + } + + private void getHomeworks(String link, String phpsessid) { + showLoader(); + + JSONObject jsonParams = new JSONObject(); + try { + jsonParams.put("phpsessid", phpsessid); + jsonParams.put("link", link); + } catch (JSONException e) { + e.printStackTrace(); + SnackbarController.showSnackbar(getActivity(), content, e, getResources().getString(R.string.unkown_error), true); + } + + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, API_URL, jsonParams, + new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + if (!isAdded()) { + return; + } + + try { + boolean success = response.getBoolean("success"); + JSONObject details = response.optJSONObject("details"); + if (success) { + if (response.getBoolean("done")) { + answer.setEnabled(false); + sendButton.setVisibility(View.GONE); + saveButton.setVisibility(View.GONE); + information.setVisibility(View.GONE); + if (details.has("grade")) { + if (details.getString("grade").equals("0")) { + grade.setText(R.string.homework_no_grade); + } else { + grade.setText(details.getString("grade")); + } + gradeView.setVisibility(View.VISIBLE); + } + } + + if (details.has("note")) { + teacherNote.setText(details.getString("note")); + note.setVisibility(View.VISIBLE); + } + + title.setText(response.getString("title")); + term.setText(response.getString("deadline")); + answer.setText(details.getString("answer")); + exercise.setText(details.getString("exercise")); + + hideLoader(); + } else { + Throwable error = new Throwable(getString(R.string.unkown_error)); + SnackbarController.showSnackbar(getActivity(), content, error, getResources().getString(R.string.unkown_error), true); + hideLoader(); + } + } catch (JSONException e) { + SnackbarController.showSnackbar(getActivity(), content, e, getResources().getString(R.string.unkown_error), true); + hideLoader(); + } + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + if (!isAdded()) { + return; + } + SnackbarController.showSnackbar(getActivity(), content, error, getResources().getString(R.string.unkown_error), true); + } + }); + + requestQueue.add(jsonObjectRequest); + } + + private void showLoader() { + if (!isAdded()) { + return; + } + + loaderLayout.setVisibility(View.VISIBLE); + } + + private void hideLoader() { + if (!isAdded()) { + return; + } + + loaderLayout.setVisibility(View.GONE); + } +} diff --git a/app/src/main/res/drawable-night/ic_baseline_arrow_back_24.xml b/app/src/main/res/drawable-night/ic_baseline_arrow_back_24.xml new file mode 100644 index 0000000..e2b2245 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_baseline_arrow_back_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_baseline_arrow_back_24.xml b/app/src/main/res/drawable/ic_baseline_arrow_back_24.xml new file mode 100644 index 0000000..b5aef98 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_arrow_back_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_baseline_save_24.xml b/app/src/main/res/drawable/ic_baseline_save_24.xml new file mode 100644 index 0000000..82070aa --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_save_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_baseline_send_24.xml b/app/src/main/res/drawable/ic_baseline_send_24.xml new file mode 100644 index 0000000..3abc6cb --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_send_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/fragment_homework.xml b/app/src/main/res/layout/fragment_homework.xml index e28d062..036e75f 100644 --- a/app/src/main/res/layout/fragment_homework.xml +++ b/app/src/main/res/layout/fragment_homework.xml @@ -13,7 +13,8 @@ android:layout_height="wrap_content"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 571c595..d098aca 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -18,7 +18,8 @@ app:layout_constraintTop_toTopOf="parent"> Termin: Ocena: Brak oceny + Podsumowanie zadania + Zadanie ukończone! + Odpowiedź zapisana! + Czy na pewno chcesz przesłać ukończoną pracę? Nie będzie już można jej edytować + Potwierdź przesłanie + Możesz edytować swoją pracę do ostatecznego terminu wyznaczonego przez nauczyciela (zostanie ona wysłany automatycznie po jego upływie) lub wysłać swoją pracę, jeśli jest już ukończona. + Otrzymana ocena: + Notatka od nauczyciela: + Wyślij + Zapisz + Twoja odpowiedź: \ No newline at end of file diff --git a/app/src/main/res/values-tr-rTR/strings.xml b/app/src/main/res/values-tr-rTR/strings.xml index 2b9fff9..7fe62d2 100644 --- a/app/src/main/res/values-tr-rTR/strings.xml +++ b/app/src/main/res/values-tr-rTR/strings.xml @@ -74,4 +74,15 @@ Son tarih: Görev: Derecelendirme yok + Ev ödevi özeti + Görev tamamlandı! + Cevap kaydedildi! + Bitmiş çalışmanızı yüklemek istediğinizden emin misiniz? Artık düzenleyemeyeceksiniz + İletimi onaylayın + Çalışmanızı öğretmenin son tarihine kadar düzenleyebilir (son tarih geçtikten sonra otomatik olarak gönderilecektir) veya zaten tamamlanmışsa çalışmanızı gönderebilirsiniz. + Değerlendirme alındı: + Öğretmenin notu: + Gönder + Kaydet + Senin cevabın: \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d24d7d..bb93795 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -77,4 +77,15 @@ Term: Grade: No grade + Homework summary + Task completed! + Answer saved! + Are you sure you want to submit your finished work? You will no longer be able to edit it + Confirm task + You can edit your work up to the final term set by your teacher (it will be sent automatically when it has passed) or send your work if it is already finished. + Grade received: + Note from teacher: + Send + Save + Your answer: \ No newline at end of file