From 457db5427ef10bb92bffde6a12a7be9c87fe6de0 Mon Sep 17 00:00:00 2001 From: rvyk Date: Mon, 5 Jun 2023 17:44:03 +0200 Subject: [PATCH] Refresh button on solving page if no internet connection --- .../java/pl/rvyk/instapp/SolvingActivity.java | 40 ++++++++++----- .../GenerateQuestionRequestHelper.java | 8 +-- .../solving/SendAnswerRequestHelper.java | 8 +-- .../res/drawable/ic_baseline_refresh_24.xml | 5 ++ app/src/main/res/layout/solving_page.xml | 51 ++++++++++++++++--- app/src/main/res/values/strings.xml | 1 + 6 files changed, 86 insertions(+), 27 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_refresh_24.xml diff --git a/app/src/main/java/pl/rvyk/instapp/SolvingActivity.java b/app/src/main/java/pl/rvyk/instapp/SolvingActivity.java index 8075ed8..e0e3699 100644 --- a/app/src/main/java/pl/rvyk/instapp/SolvingActivity.java +++ b/app/src/main/java/pl/rvyk/instapp/SolvingActivity.java @@ -3,6 +3,8 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -17,6 +19,7 @@ import pl.rvyk.instapp.solving.GenerateQuestionRequestHelper; import pl.rvyk.instapp.solving.SendAnswerRequestHelper; +import pl.rvyk.instapp.utils.SnackbarController; public class SolvingActivity extends AppCompatActivity { @@ -24,9 +27,8 @@ public class SolvingActivity extends AppCompatActivity { TextView translations, translateText; EditText answer; - Button sendAnswerButton; - ProgressBar progressBar; - LinearLayout sendAnswer, summary; + Button sendAnswerButton, refreshButton; + LinearLayout sendAnswer, summary, mainLinearContent, progressBar, noConnection; private Button viewById; private String questionIdentificator; @@ -54,6 +56,23 @@ private void refreshContent() { summary = findViewById(R.id.summary); sendAnswerButton = findViewById(R.id.sendAnswerButton); answer = findViewById(R.id.answer); + mainLinearContent = findViewById(R.id.solving_page); + noConnection = findViewById(R.id.noConnection); + refreshButton = findViewById(R.id.refresh); + + ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); + boolean isNetworkAvailable = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); + + if (!isNetworkAvailable) { + noConnection.setVisibility(View.VISIBLE); + refreshButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + refreshContent(); + } + }); + } Bundle bundle = getIntent().getExtras(); if (bundle != null) { @@ -81,8 +100,8 @@ public void onSuccess(Integer grade, String word, String usageExample) { } @Override - public void onError(String error) { - Toast.makeText(SolvingActivity.this, "Wystąpił błąd" + error, Toast.LENGTH_SHORT).show(); + public void onError(Throwable error) { + SnackbarController.showSnackbar(SolvingActivity.this, mainLinearContent, error, getResources().getString(R.string.unkown_error), true); } }); } @@ -103,20 +122,15 @@ public void run() { } @Override public void onFinish(boolean ended) { - Toast.makeText(SolvingActivity.this, "Sesja zakończona", Toast.LENGTH_SHORT).show(); + SnackbarController.showSnackbar(SolvingActivity.this, mainLinearContent, null, getResources().getString(R.string.solving_finished), false); Intent intent = new Intent(SolvingActivity.this, UserInterface.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); } @Override - public void onError(String error) { - runOnUiThread(new Runnable() { - @Override - public void run() { - // Obsługa błędu - } - }); + public void onError(Throwable error) { + SnackbarController.showSnackbar(SolvingActivity.this, mainLinearContent, error, getResources().getString(R.string.unkown_error), true); } }); } diff --git a/app/src/main/java/pl/rvyk/instapp/solving/GenerateQuestionRequestHelper.java b/app/src/main/java/pl/rvyk/instapp/solving/GenerateQuestionRequestHelper.java index 055abdc..4925a3a 100644 --- a/app/src/main/java/pl/rvyk/instapp/solving/GenerateQuestionRequestHelper.java +++ b/app/src/main/java/pl/rvyk/instapp/solving/GenerateQuestionRequestHelper.java @@ -18,7 +18,7 @@ public class GenerateQuestionRequestHelper { public interface GenerateQuestionResponseListener { void onSuccess(String question, String usageExample, String questionId); void onFinish(boolean ended); - void onError(String error); + void onError(Throwable error); } public static void sendGenerateQuestionRequest(Context context, String phpSessionId, String appId, String studentId, final GenerateQuestionResponseListener listener) { @@ -47,14 +47,16 @@ public void onResponse(JSONObject response) { } else if (ended == true) { listener.onFinish(ended); } else { - listener.onError("Question object is null"); + Throwable throwable = new Throwable("Question object is null"); + listener.onError(throwable); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - listener.onError("VolleyError: " + error.getMessage()); + Throwable throwable = new Throwable("VolleyError: " + error.getMessage()); + listener.onError(throwable); } }); diff --git a/app/src/main/java/pl/rvyk/instapp/solving/SendAnswerRequestHelper.java b/app/src/main/java/pl/rvyk/instapp/solving/SendAnswerRequestHelper.java index dd50683..a26e382 100644 --- a/app/src/main/java/pl/rvyk/instapp/solving/SendAnswerRequestHelper.java +++ b/app/src/main/java/pl/rvyk/instapp/solving/SendAnswerRequestHelper.java @@ -17,7 +17,7 @@ public class SendAnswerRequestHelper { public interface SendAnswerResponseListener { void onSuccess(Integer grade, String word, String usageExample); - void onError(String error); + void onError(Throwable error); } public static void sendAnswerRequest(Context context, String phpSessionId, String appId, String studentId, String questionId, String answer, String login, final SendAnswerResponseListener listener) { @@ -46,14 +46,16 @@ public void onResponse(JSONObject response) { String usageExample = questionObject.optString("usage_example"); listener.onSuccess(grade, word, usageExample); } else { - listener.onError("Output object is null"); + Throwable throwable = new Throwable("Output object is null"); + listener.onError(throwable); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - listener.onError("VolleyError: " + error.getMessage()); + Throwable throwable = new Throwable("VolleyError: " + error.getMessage()); + listener.onError(throwable); } }); diff --git a/app/src/main/res/drawable/ic_baseline_refresh_24.xml b/app/src/main/res/drawable/ic_baseline_refresh_24.xml new file mode 100644 index 0000000..98469ca --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_refresh_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/solving_page.xml b/app/src/main/res/layout/solving_page.xml index 2b7d115..7685ff5 100644 --- a/app/src/main/res/layout/solving_page.xml +++ b/app/src/main/res/layout/solving_page.xml @@ -1,5 +1,6 @@ - + android:layout_gravity="center" + android:gravity="center" + android:id="@+id/progressBar" + android:orientation="vertical" + android:visibility="visible" + android:layout_height="match_parent"> + + + + + + + + Przetłumacz: Twoja odpowiedź Wyślij odpowiedź + Sesja zakończona Odpowiedź