This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Homework managment (summary, save, send), translations, colored appbar
- Loading branch information
Showing
14 changed files
with
645 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
272 changes: 272 additions & 0 deletions
272
app/src/main/java/pl/rvyk/instapp/fragments/HomeworkSummaryFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<JSONObject>() { | ||
@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<JSONObject>() { | ||
@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); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/main/res/drawable-night/ic_baseline_arrow_back_24.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:autoMirrored="true" android:height="24dp" | ||
android:tint="#fefefe" android:viewportHeight="24" | ||
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:autoMirrored="true" android:height="24dp" | ||
android:tint="#0a0a0a" android:viewportHeight="24" | ||
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M17,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,7l-4,-4zM12,19c-1.66,0 -3,-1.34 -3,-3s1.34,-3 3,-3 3,1.34 3,3 -1.34,3 -3,3zM15,9L5,9L5,5h10v4z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:autoMirrored="true" android:height="24dp" | ||
android:tint="#000000" android:viewportHeight="24" | ||
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.