From 15367bf285ecea099b6f0086ba6cde6b7505c28b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 31 Aug 2021 13:57:27 +0200 Subject: [PATCH] Better error handling for the user interface --- .../java/com/mylloon/mobidl/MainActivity.kt | 20 +++++++++-- .../main/java/com/mylloon/mobidl/Scraper.kt | 36 +++++++++++-------- app/src/main/res/values-fr-rFR/strings.xml | 3 ++ app/src/main/res/values/strings.xml | 3 ++ 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt index 2cdf07f..88a06f8 100644 --- a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt +++ b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt @@ -26,6 +26,10 @@ import com.google.gson.Gson import com.google.gson.reflect.TypeToken import kotlinx.coroutines.runBlocking import java.util.* +import android.widget.Toast + + + class MainActivity : AppCompatActivity() { @@ -158,6 +162,12 @@ class MainActivity : AppCompatActivity() { button?.text.toString() ) } + val checkedItems = booleanArrayOf( + false // get this info from the app files somewhere + ) + builder.setMultiChoiceItems(arrayOf(getString(R.string.updateCheck)), checkedItems) { _, _, isChecked -> + println("Update for ${button?.text.toString()}: " + if (isChecked) "enabled" else "disabled" + "") + } builder.setNeutralButton(R.string.cancel) { dialog, _ -> dialog.cancel() } builder.show() true @@ -240,7 +250,7 @@ class MainActivity : AppCompatActivity() { if (returns[0] == "loginAttemptsExceeded") { if (returns[1] == "null") callScrapper(user, password, app) val registeredAnswered: Map = Gson().fromJson( - applicationContext.assets.open("captcha.json").bufferedReader() + assets.open("captcha.json").bufferedReader() .use { it.readText() }, object : TypeToken>() {}.type ) @@ -258,9 +268,15 @@ class MainActivity : AppCompatActivity() { builder.setNeutralButton(R.string.cancel) { dialog, _ -> dialog.cancel() } builder.show() - Toast.makeText(applicationContext(), "${getString(R.string.connectionFailed)}\n${getString(R.string.credentialsDeleted)}.", + Toast.makeText(instance, "${getString(R.string.connectionFailed)}\n${getString(R.string.credentialsDeleted)}.", Toast.LENGTH_LONG).show() } + if (returns[0] == "noSID") + Toast.makeText(instance, R.string.noSID, Toast.LENGTH_SHORT).show() + if (returns[0] == "badSID") + Toast.makeText(instance, "${getString(R.string.badSID)}...", Toast.LENGTH_SHORT).show() + if (returns[0] == "noResults") + Toast.makeText(instance, "${getString(R.string.badSID)}...", Toast.LENGTH_SHORT).show() } } } diff --git a/app/src/main/java/com/mylloon/mobidl/Scraper.kt b/app/src/main/java/com/mylloon/mobidl/Scraper.kt index b401a4c..5b97720 100644 --- a/app/src/main/java/com/mylloon/mobidl/Scraper.kt +++ b/app/src/main/java/com/mylloon/mobidl/Scraper.kt @@ -5,10 +5,11 @@ import com.github.kittinunf.fuel.core.FuelManager import com.github.kittinunf.fuel.httpGet import com.github.kittinunf.fuel.httpPost import com.mylloon.mobidl.MainActivity.Companion.applicationContext +import java.lang.System.currentTimeMillis import com.github.kittinunf.result.Result.Failure as FuelFailure import com.github.kittinunf.result.Result.Success as FuelSuccess - +var timeOfLastToast: Long = currentTimeMillis() - 2000 class Scraper( private var pseudo: String, private var password: String, @@ -26,14 +27,14 @@ class Scraper( private var loginAttemptsExceeded = "You exceeded the maximum allowed number of login attempts." private var searchNotLogged = "Sorry but you are not permitted to use the search system. If you're not logged in please" - private fun errorFormat( + private fun errorFormat( // Pretty error message. code: Int? = null, message: String = "" - ): String { // Pretty error message. + ): String { return "${if (code != null) "[$code]" else ""}${if ((message.isNotEmpty()) and (code is Int)) " " else ""}$message." } - private fun connect(): MutableList? { + private fun connect(): MutableList? { // Login to the forum using credentials. if (debug) println("Connection attempt...") retry++ FuelManager.instance.basePath = url @@ -63,35 +64,39 @@ class Scraper( return search() } - private fun error(htmlPage: String): MutableList? { + private fun error(htmlPage: String): MutableList? { // Handle connection errors var message = "" var array: MutableList? = null var lengthTime = Toast.LENGTH_LONG if (errorNotConnected in htmlPage) { if (loginAttemptsExceeded in htmlPage) { message = "${applicationContext().getString(R.string.knownCaptcha)}..." - lengthTime = Toast.LENGTH_LONG + lengthTime = Toast.LENGTH_SHORT val qaConfirmID = "(?<=qa_confirm_id\" value=\")(.{32})".toRegex().find(htmlPage)?.value - var answer = "(?<=answer\">)(.*))(.*)= 2000) sendToast() return array } fun search(): MutableList? { // Do the research. if (sid == null) { println(errorFormat(message = "SID not found")) - return if (retry < 3) connect() else null + return if (retry < 3) connect() else mutableListOf("noSID") } else { if (debug) println("SID recovered") retry = 0 @@ -115,15 +120,16 @@ class Scraper( val data = session.join().data.decodeToString() return if (searchNotLogged in data) { println(errorFormat(message = "The SID doesn't work, new attempt...")) - if (retry < 3) connect() else null + if (retry < 3) connect() else mutableListOf("badSID") } else parse(data) } - private fun parse(htmlPage: String): MutableList? { + private fun parse(htmlPage: String): MutableList? { // Parse HTML response to a clean list. if (debug) println("Fetching results for $app...") Toast.makeText(applicationContext(), "Fetching results for $app...", Toast.LENGTH_SHORT) .show() // println(htmlPage) + if ("No suitable matches were found." in htmlPage) return mutableListOf("noResults") return null } diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 4fee382..cc97248 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -23,4 +23,7 @@ Vous avez dépassé le nombre maximum de tentatives de connexion, veuillez compléter les captchas Vous n\'êtes pas connecté, redirection vers la page de connexion Résolution des captchas + Aucun SID n\'a été trouvé + Le SID ne fonctionne pas, nouvel essaie + Vérification des mises à jour \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bf560ac..c437ec5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,7 @@ Validate Remove Cancel + Update check You are not logged in, redirection to the login page drop bro @@ -30,4 +31,6 @@ Your credentials have been deleted You have exceeded the maximum number of connection attempts, please complete captchas Resolving captchas + No SID was found + The SID does not work, new attempt \ No newline at end of file