Skip to content

Commit 544dfbf

Browse files
committed
error prevention
1 parent 0c67aaf commit 544dfbf

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

app/src/main/java/com/example/ncmonitor/LoginActivity.kt

+30-33
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,19 @@ class LoginActivity : WearableActivity()
7070
// hiding the password using dots
7171
nc_password_input.transformationMethod = PasswordTransformationMethod.getInstance()
7272

73-
if (isNetworkAvailable()) {
74-
if (userInstance != null) {
75-
//There is an instance of a user on the watch, retrieve data
76-
val userJson = JSONObject(userInstance)
77-
requestNcStatus(
78-
userJson.get("serverURL").toString(),
79-
userJson.get("username").toString(),
80-
userJson.get("password").toString()
81-
)
82-
}
83-
} else {
84-
Toast.makeText(this,
85-
"Internet connection not available", Toast.LENGTH_SHORT).show()
73+
if (userInstance != null) {
74+
//There is an instance of a user on the watch, retrieve data
75+
val userJson = JSONObject(userInstance)
76+
requestNcStatus(
77+
userJson.get("serverURL").toString(),
78+
userJson.get("username").toString(),
79+
userJson.get("password").toString(),
80+
this
81+
)
8682
}
83+
8784
checkAndRequestPermissions()
88-
btn_next.setOnClickListener {
89-
if (isNetworkAvailable())
90-
onNextClicked()
91-
else
92-
Toast.makeText(this,
93-
"Internet connection not available", Toast.LENGTH_SHORT).show()
94-
}
85+
btn_next.setOnClickListener { onNextClicked() }
9586
}
9687

9788
/* This method is used to request permissions
@@ -148,7 +139,7 @@ class LoginActivity : WearableActivity()
148139
* NC instance. Then, if the result code is 200_OK, results
149140
* are passed to the next activity.
150141
*/
151-
private fun requestNcStatus(serverURL :String, username :String, password :String)
142+
private fun requestNcStatus(serverURL: String, username: String, password: String, context: Context)
152143
{
153144
showProgressBar()
154145
val credentials = basic(username, password)
@@ -186,14 +177,29 @@ class LoginActivity : WearableActivity()
186177
navigateToResults(responseData)
187178
}
188179
} catch (e: JSONException) {
189-
Log.d("requestNcStatus","JsonException!!")
180+
Log.e("requestNcStatus","JsonException!!")
190181
e.printStackTrace()
182+
runOnUiThread {
183+
Toast.makeText(
184+
context,
185+
"Check the URL, maybe it's not a NC instance", Toast.LENGTH_SHORT
186+
).show()
187+
}
188+
showLoginFields()
191189
}
192190
}
193191

194192
override fun onFailure(call: Call?, e: IOException?) {
195193
//todo: handle it showing info to the user
196194
showLoginFields()
195+
if (e != null) {
196+
runOnUiThread {
197+
Toast.makeText(
198+
context,
199+
e.message + "\nCheck your internet connectivity", Toast.LENGTH_SHORT
200+
).show()
201+
}
202+
}
197203
}
198204
})
199205
}
@@ -220,7 +226,8 @@ class LoginActivity : WearableActivity()
220226
serverURL += SERVERINFO_API
221227
requestNcStatus(serverURL,
222228
nc_username_input.text.toString(),
223-
nc_password_input.text.toString())
229+
nc_password_input.text.toString(),
230+
this)
224231
} else {
225232
Toast.makeText(this,
226233
"All fields are mandatory", Toast.LENGTH_SHORT).show()
@@ -239,14 +246,4 @@ class LoginActivity : WearableActivity()
239246
}
240247
}
241248

242-
/* This method is used to test the connectivity
243-
* https://developer.android.com/training/monitoring-device-state/connectivity-status-type
244-
*/
245-
private fun isNetworkAvailable(): Boolean {
246-
val connectivityMgr =
247-
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
248-
val networkInfo: NetworkInfo? = connectivityMgr.activeNetworkInfo
249-
// if no network is available networkInfo will be null
250-
return networkInfo?.isConnectedOrConnecting == true
251-
}
252249
}

0 commit comments

Comments
 (0)