Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
Refresh button on solving page if no internet connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rvyk committed Jun 5, 2023
1 parent fa937a3 commit 457db54
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 27 deletions.
40 changes: 27 additions & 13 deletions app/src/main/java/pl/rvyk/instapp/SolvingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,16 +19,16 @@

import pl.rvyk.instapp.solving.GenerateQuestionRequestHelper;
import pl.rvyk.instapp.solving.SendAnswerRequestHelper;
import pl.rvyk.instapp.utils.SnackbarController;

public class SolvingActivity extends AppCompatActivity {

private TextView questionTextView;

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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
});
}
Expand All @@ -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);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
});

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_refresh_24.xml
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.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>
51 changes: 43 additions & 8 deletions app/src/main/res/layout/solving_page.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/solving_page"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
Expand All @@ -8,15 +9,49 @@
android:orientation="vertical"
android:gravity="center">

<ProgressBar
android:id="@+id/progressBar"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:elevation="20dp"
android:scaleX="-0.3"
android:scaleY="-0.3"
android:visibility="visible" />
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/progressBar"
android:orientation="vertical"
android:visibility="visible"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="100dp"
android:elevation="20dp" />

<LinearLayout
android:id="@+id/noConnection"
android:layout_width="match_parent"
android:orientation="vertical"
android:visibility="gone"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
style="@style/TextAppearance.Material3.HeadlineLarge"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Brak połączenia"
android:textAlignment="center"
android:textStyle="bold"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="206dp" />
<com.google.android.material.button.MaterialButton
style="@style/Widget.Material3.Button.TonalButton"
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:layout_marginHorizontal="30dp"
android:text="Odśwież"
app:iconGravity="textEnd"
app:icon="@drawable/ic_baseline_refresh_24"
android:textSize="18dp" />
</LinearLayout>

</LinearLayout>

<LinearLayout
android:id="@+id/sendAnswer"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="translate_text">Przetłumacz:</string>
<string name="your_answer">Twoja odpowiedź</string>
<string name="send_answer">Wyślij odpowiedź</string>
<string name="solving_finished">Sesja zakończona</string>

<!-- Summary page-->
<string name="summary_answer">Odpowiedź</string>
Expand Down

0 comments on commit 457db54

Please sign in to comment.