Skip to content

Commit d12f84e

Browse files
committed
Improve messages to user when connection fails
1 parent a98ed6a commit d12f84e

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

app/src/main/java/uk/co/digitme/machinemonitoring/MainActivity.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class MainActivity extends AppCompatActivity {
4949
Button mRetryButton;
5050
Button mSetAddressButton;
5151
Button mFindServerButton;
52+
TextView mAddressText;
5253

5354
DbHelper dbHelper;
5455
SharedPreferences prefs = null;
@@ -68,12 +69,16 @@ protected void onCreate(Bundle savedInstanceState) {
6869
mStatusText = findViewById(R.id.main_activity_status);
6970
mRetryButton = findViewById(R.id.retry_button);
7071
mFindServerButton = findViewById(R.id.find_server_button);
72+
mAddressText = findViewById(R.id.main_activity_address_tv);
73+
74+
mAddressText.setText(dbHelper.getServerAddress());
7175

7276
// The retry button attempts to contact the server again.
7377
mRetryButton.setOnClickListener(new OnOneOffClickListener() {
7478
@Override
7579
public void onSingleClick(View v) {
7680
try {
81+
mStatusText.setText("Connecting...");
7782
checkState();
7883
} catch (Exception e) {
7984
showError(e.getMessage());
@@ -107,13 +112,15 @@ protected void onResume() {
107112
mRetryButton.setVisibility(View.INVISIBLE);
108113
mFindServerButton.setVisibility(View.VISIBLE);
109114
mSetAddressButton.setVisibility(View.VISIBLE);
115+
mAddressText.setVisibility(View.VISIBLE);
110116
prefs.edit().putBoolean("firstrun", false).apply();
111117
} else {
112118
// Hide the buttons by default. This stops them showing during transitions between activities
113119
mStatusText.setVisibility(View.INVISIBLE);
114120
mRetryButton.setVisibility(View.INVISIBLE);
115121
mSetAddressButton.setVisibility(View.INVISIBLE);
116122
mFindServerButton.setVisibility(View.INVISIBLE);
123+
mAddressText.setVisibility(View.INVISIBLE);
117124
// When arriving at this page, immediately contact the server to see which screen the app
118125
// should be on, and start that activity
119126
try {
@@ -151,6 +158,7 @@ private void showError(String errorText) {
151158
mRetryButton.setVisibility(View.VISIBLE);
152159
mFindServerButton.setVisibility(View.VISIBLE);
153160
mSetAddressButton.setVisibility(View.VISIBLE);
161+
mAddressText.setVisibility(View.VISIBLE);
154162
}
155163

156164
/**
@@ -172,10 +180,9 @@ private void checkState() {
172180
}, error -> {
173181
// Show a different error message depending on the error
174182
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
175-
showError("Could not connect to network");
183+
showError("Could not connect");
176184
} else if (error instanceof ServerError) {
177185
showError("Could not connect to server");
178-
showError(error.getMessage());
179186
}
180187
Log.v("ErrorListener", String.valueOf(error));
181188
});

app/src/main/res/layout/activity_main.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,13 @@
4343
app:layout_constraintTop_toBottomOf="@+id/find_server_button"
4444
app:layout_constraintLeft_toLeftOf="parent"
4545
app:layout_constraintRight_toRightOf="parent"/>
46+
<TextView
47+
android:id="@+id/main_activity_address_tv"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
android:text=""
51+
app:layout_constraintTop_toBottomOf="@+id/set_address_button"
52+
app:layout_constraintLeft_toLeftOf="parent"
53+
app:layout_constraintRight_toRightOf="parent"/>
4654

4755
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
<string name="main_activity_status_text">Connecting to server</string>
4444
<string name="find_server_button_text">Find Server</string>
4545
<string name="retry_button_text">Retry</string>
46-
<string name="set_address_button_text">Enter Server Address</string>
46+
<string name="set_address_button_text">Enter Manually</string>
47+
4748

4849
<string name="server_address_textview">Enter the full address to your server e.g. http://192.168.0.1:8000</string>
4950
<string name="server_address_hint"> Address e.g. http://192.168.0.1:8000</string>

0 commit comments

Comments
 (0)